package contorl
import (
"encoding/csv"
"encoding/json"
"fmt"
"goAdapter/device"
"goAdapter/httpServer/model"
"goAdapter/setting"
"goAdapter/utils"
"net/http"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"github.com/gin-gonic/gin"
)
func ApiAddDeviceTSL(context *gin.Context) {
aParam := struct {
Code string `json:"Code"`
Message string `json:"Message"`
Data string `json:"Data"`
}{
Code: "1",
Message: "",
Data: "",
}
bodyBuf := make([]byte, 1024)
n, _ := context.Request.Body.Read(bodyBuf)
fmt.Println(string(bodyBuf[:n]))
tslInfo := &struct {
TSLName string `json:"TSLName"` // 名称
TSLExplain string `json:"TSLExplain"` // 描述
}{}
err := json.Unmarshal(bodyBuf[:n], tslInfo)
if err != nil {
setting.ZAPS.Error("tslInfo格式化错误")
context.JSON(http.StatusOK, model.Response{
Code: "1",
Message: "tslInfo格式化错误",
Data: "",
})
return
}
setting.ZAPS.Debugf("tslInfo %v", tslInfo)
tslParam := device.NewDeviceTSL(tslInfo.TSLName, tslInfo.TSLExplain)
_, err = device.DeviceTSLAdd(tslParam)
if err != nil {
setting.ZAPS.Errorf("DeviceTSLAdd err %v", err)
aParam.Code = "1"
aParam.Message = "DeviceTSLAdd err"
sJson, _ := json.Marshal(aParam)
context.String(http.StatusOK, string(sJson))
return
}
aParam.Code = "0"
aParam.Message = ""
sJson, _ := json.Marshal(aParam)
context.String(http.StatusOK, string(sJson))
}
func ApiDeleteDeviceTSL(context *gin.Context) {
aParam := struct {
Code string `json:"Code"`
Message string `json:"Message"`
Data string `json:"Data"`
}{
Code: "1",
Message: "",
Data: "",
}
bodyBuf := make([]byte, 1024)
n, _ := context.Request.Body.Read(bodyBuf)
fmt.Println(string(bodyBuf[:n]))
tslInfo := &struct {
TSLName string `json:"TSLName"` // 名称
}{}
err := json.Unmarshal(bodyBuf[:n], tslInfo)
if err != nil {
fmt.Println("tslInfo json unMarshall err,", err)
aParam.Code = "1"
aParam.Message = "json unMarshall err"
sJson, _ := json.Marshal(aParam)
context.String(http.StatusOK, string(sJson))
return
}
setting.ZAPS.Debugf("tslInfo %v", tslInfo)
_, err = device.DeviceTSLDelete(tslInfo.TSLName)
if err != nil {
setting.ZAPS.Errorf("DeviceTSLDelete err %v", err)
aParam.Code = "1"
aParam.Message = err.Error()
sJson, _ := json.Marshal(aParam)
context.String(http.StatusOK, string(sJson))
return
}
aParam.Code = "0"
aParam.Message = ""
sJson, _ := json.Marshal(aParam)
context.String(http.StatusOK, string(sJson))
}
func ApiModifyDeviceTSL(context *gin.Context) {
aParam := struct {
Code string `json:"Code"`
Message string `json:"Message"`
Data string `json:"Data"`
}{
Code: "1",
Message: "",
Data: "",
}
bodyBuf := make([]byte, 1024)
n, _ := context.Request.Body.Read(bodyBuf)
fmt.Println(string(bodyBuf[:n]))
tslInfo := &struct {
TSLName string `json:"TSLName"` // 名称
TSLExplain string `json:"TSLExplain"` // 描述
}{}
err := json.Unmarshal(bodyBuf[:n], tslInfo)
if err != nil {
setting.ZAPS.Errorf("tslInfo json unMarshall err %v", err)
aParam.Code = "1"
aParam.Message = "json unMarshall err"
sJson, _ := json.Marshal(aParam)
context.String(http.StatusOK, string(sJson))
return
}
setting.ZAPS.Debugf("tslInfo %v", tslInfo)
for _, v := range device.DeviceTSLMap {
if v.Name == tslInfo.TSLName {
_, err := device.DeviceTSLModifyExplain(tslInfo.TSLName, tslInfo.TSLExplain)
if err != nil {
setting.ZAPS.Errorf("DeviceTSLModify err %v", err)
aParam.Code = "1"
aParam.Message = "DeviceTSLModify err"
sJson, _ := json.Marshal(aParam)
context.String(http.StatusOK, string(sJson))
return
}
aParam.Code = "0"
aParam.Message = ""
sJson, _ := json.Marshal(aParam)
context.String(http.StatusOK, string(sJson))
return
}
}
aParam.Code = "1"
aParam.Message = "tSLName is not exist"
sJson, _ := json.Marshal(aParam)
context.String(http.StatusOK, string(sJson))
}
func ApiGetDeviceTSL(context *gin.Context) {
type TSLInfoTemplate struct {
Name string
Explain string
Plugin string
}
aParam := struct {
Code string `json:"Code"`
Message string `json:"Message"`
Data []TSLInfoTemplate `json:"Data"`
}{
Code: "1",
Message: "",
Data: make([]TSLInfoTemplate, 0),
}
for _, v := range device.DeviceTSLMap {
tslInfo := TSLInfoTemplate{
Name: v.Name,
Explain: v.Explain,
Plugin: v.Plugin,
}
aParam.Data = append(aParam.Data, tslInfo)
}
aParam.Code = "0"
aParam.Message = ""
sJson, _ := json.Marshal(aParam)
context.String(http.StatusOK, string(sJson))
}
func ApiGetDeviceTSLContents(context *gin.Context) {
type DeviceTSLPropertyIntUintParamTempate struct {
Min string `json:"Min"` //最小
Max string `json:"Max"` //最大
MinMaxAlarm bool `json:"MinMaxAlarm"` //范围报警
Step string `json:"Step"` //步长
StepAlarm bool `json:"StepAlarm"` //阶跃报警
Unit string `json:"Unit"` //单位
}
type DeviceTSLPropertyDoubleParamTempate struct {
Min string `json:"Min"` //最小
Max string `json:"Max"` //最大
MinMaxAlarm bool `json:"MinMaxAlarm"` //范围报警
Step string `json:"Step"` //步长
StepAlarm bool `json:"StepAlarm"` //阶跃报警
Decimals string `json:"Decimals"` //小数位数
Unit string `json:"Unit"` //单位
}
type DeviceTSLPropertyStringParamTempate struct {
DataLength string `json:"DataLength,omitempty"` //字符串长度
DataLengthAlarm bool `json:"DataLengthAlarm,omitempty"` //字符长度报警
}
type DeviceTSLPropertyTemplate struct {
Name string `json:"Name"` //属性名称,只可以是字母和数字的组合
Explain string `json:"Explain"` //属性解释
AccessMode int `json:"AccessMode"` //读写属性
Type int `json:"Type"` //类型 uint32 int32 double string
Params interface{} `json:"Params"`
}
type DeviceTSLTemplate struct {
Properties []DeviceTSLPropertyTemplate `json:"Properties"` //属性
Services []device.DeviceTSLServiceTempalte `json:"Services"` //服务
}
tslInfo := DeviceTSLTemplate{}
aParam := struct {
Code string `json:"Code"`
Message string `json:"Message"`
Data DeviceTSLTemplate `json:"Data"`
}{
Code: "1",
Message: "",
}
tslName := context.Query("TSLName")
for _, v := range device.DeviceTSLMap {
if v.Name == tslName {
tslInfo.Services = v.Services
tslInfo.Properties = make([]DeviceTSLPropertyTemplate, 0)
property := DeviceTSLPropertyTemplate{}
for _, p := range v.Properties {
property.Name = p.Name
property.Explain = p.Explain
property.AccessMode = p.AccessMode
property.Type = p.Type
switch p.Type {
case device.PropertyTypeUInt32:
fallthrough
case device.PropertyTypeInt32:
{
intUintPropertyParam := DeviceTSLPropertyIntUintParamTempate{
Min: p.Params.Min,
Max: p.Params.Max,
MinMaxAlarm: p.Params.MinMaxAlarm,
Step: p.Params.Step,
StepAlarm: p.Params.StepAlarm,
Unit: p.Params.Unit,
}
property.Params = intUintPropertyParam
}
case device.PropertyTypeDouble:
{
doublePropertyParam := DeviceTSLPropertyDoubleParamTempate{
Min: p.Params.Min,
Max: p.Params.Max,
MinMaxAlarm: p.Params.MinMaxAlarm,
Step: p.Params.Step,
StepAlarm: p.Params.StepAlarm,
Unit: p.Params.Unit,
Decimals: p.Params.Decimals,
}
property.Params = doublePropertyParam
}
case device.PropertyTypeString:
{
stringPropertyParam := DeviceTSLPropertyStringParamTempate{
DataLength: p.Params.Dat