add response and response data

fix http server parse and response
This commit is contained in:
thinkgo 2021-02-02 21:02:11 +08:00
parent f4bd009c42
commit 93d026da22
8 changed files with 384 additions and 607 deletions

13
httpServer/message.go Normal file
View File

@ -0,0 +1,13 @@
package httpServer
type Response struct {
Code string `json:"Code"`
Message string `json:"Message"`
Data string `json:"Data"`
}
type ResponseData struct {
Code string `json:"Code"`
Message string `json:"Message"`
Data interface{} `json:"Data"`
}

View File

@ -3,68 +3,48 @@ package httpServer
import (
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"goAdapter/device"
"log"
"net/http"
"goAdapter/device"
"github.com/gin-gonic/gin"
)
func apiAddInterface(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]))
interfaceInfo := &struct {
CollectInterfaceName string `json:"CollInterfaceName"` //采集接口名字
CommInterfaceName string `json:"CommInterfaceName"` //通信接口名字
PollPeriod int `json:"PollPeriod"`
OfflinePeriod int `json:"OfflinePeriod"`
CollectInterfaceName string `json:"CollInterfaceName"` // 采集接口名字
CommInterfaceName string `json:"CommInterfaceName"` // 通信接口名字
PollPeriod int `json:"PollPeriod"`
OfflinePeriod int `json:"OfflinePeriod"`
}{}
err := json.Unmarshal(bodyBuf[:n], interfaceInfo)
err := context.ShouldBindJSON(interfaceInfo)
if err != nil {
fmt.Println("interfaceInfo json unMarshall err,", err)
aParam.Code = "1"
aParam.Message = "json unMarshall err"
sJson, _ := json.Marshal(aParam)
context.String(http.StatusOK, string(sJson))
context.JSON(http.StatusOK, Response{
Code: "1",
Message: "json unMarshall err",
})
return
}
device.CollectInterfaceMap = append(device.CollectInterfaceMap,device.NewCollectInterface(interfaceInfo.CollectInterfaceName,
device.CollectInterfaceMap = append(device.CollectInterfaceMap, device.NewCollectInterface(interfaceInfo.CollectInterfaceName,
interfaceInfo.CommInterfaceName,
interfaceInfo.PollPeriod,
interfaceInfo.OfflinePeriod,0))
interfaceInfo.OfflinePeriod, 0))
device.WriteCollectInterfaceManageToJson()
aParam.Code = "0"
aParam.Data = ""
sJson, _ := json.Marshal(aParam)
context.String(http.StatusOK, string(sJson))
context.JSON(http.StatusOK, Response{
Code: "0",
})
}
func apiModifyInterface(context *gin.Context) {
aParam := struct {
Code string `json:"Code"`
Message string `json:"Message"`
Data string `json:"Data"`
}{
aParam := Response{
Code: "1",
Message: "",
Data: "",
@ -75,10 +55,10 @@ func apiModifyInterface(context *gin.Context) {
fmt.Println(string(bodyBuf[:n]))
interfaceInfo := &struct {
CollectInterfaceName string `json:"CollInterfaceName"` //采集接口名字
CommInterfaceName string `json:"CommInterfaceName"` //通信接口名字
PollPeriod int
OfflinePeriod int
CollectInterfaceName string `json:"CollInterfaceName"` // 采集接口名字
CommInterfaceName string `json:"CommInterfaceName"` // 通信接口名字
PollPeriod int
OfflinePeriod int
}{}
err := json.Unmarshal(bodyBuf[:n], interfaceInfo)
@ -88,13 +68,12 @@ func apiModifyInterface(context *gin.Context) {
aParam.Code = "1"
aParam.Message = "json unMarshall err"
sJson, _ := json.Marshal(aParam)
context.String(http.StatusOK, string(sJson))
context.JSON(http.StatusOK, aParam)
return
}
for _,v := range device.CollectInterfaceMap{
if v.CollInterfaceName == interfaceInfo.CollectInterfaceName{
for _, v := range device.CollectInterfaceMap {
if v.CollInterfaceName == interfaceInfo.CollectInterfaceName {
v.CommInterfaceName = interfaceInfo.CommInterfaceName
v.PollPeriod = interfaceInfo.PollPeriod
v.OfflinePeriod = interfaceInfo.OfflinePeriod
@ -103,9 +82,7 @@ func apiModifyInterface(context *gin.Context) {
aParam.Code = "0"
aParam.Data = ""
sJson, _ := json.Marshal(aParam)
context.String(http.StatusOK, string(sJson))
context.JSON(http.StatusOK, aParam)
return
}
}
@ -113,18 +90,12 @@ func apiModifyInterface(context *gin.Context) {
aParam.Code = "1"
aParam.Message = "collInterface is not exist"
aParam.Data = ""
sJson, _ := json.Marshal(aParam)
context.String(http.StatusOK, string(sJson))
context.JSON(http.StatusOK, aParam)
}
func apiDeleteInterface(context *gin.Context) {
aParam := struct {
Code string `json:"Code"`
Message string `json:"Message"`
Data string `json:"Data"`
}{
aParam := Response{
Code: "1",
Message: "",
Data: "",
@ -135,7 +106,7 @@ func apiDeleteInterface(context *gin.Context) {
fmt.Println(string(bodyBuf[:n]))
interfaceInfo := &struct {
CollectInterfaceName string `json:"CollInterfaceName"` //采集接口名字
CollectInterfaceName string `json:"CollInterfaceName"` // 采集接口名字
}{}
err := json.Unmarshal(bodyBuf[:n], interfaceInfo)
@ -145,15 +116,14 @@ func apiDeleteInterface(context *gin.Context) {
aParam.Code = "1"
aParam.Message = "json unMarshall err"
sJson, _ := json.Marshal(aParam)
context.String(http.StatusOK, string(sJson))
context.JSON(http.StatusOK, aParam)
return
}
for k,v := range device.CollectInterfaceMap{
if v.CollInterfaceName == interfaceInfo.CollectInterfaceName{
for k, v := range device.CollectInterfaceMap {
if v.CollInterfaceName == interfaceInfo.CollectInterfaceName {
device.CollectInterfaceMap = append(device.CollectInterfaceMap[:k],device.CollectInterfaceMap[k+1:]...)
device.CollectInterfaceMap = append(device.CollectInterfaceMap[:k], device.CollectInterfaceMap[k+1:]...)
device.WriteCollectInterfaceManageToJson()
@ -169,9 +139,7 @@ func apiDeleteInterface(context *gin.Context) {
aParam.Code = "1"
aParam.Message = "collInterface is not exist"
aParam.Data = ""
sJson, _ := json.Marshal(aParam)
context.String(http.StatusOK, string(sJson))
context.JSON(http.StatusOK, aParam)
}
func apiGetInterfaceInfo(context *gin.Context) {
@ -184,16 +152,15 @@ func apiGetInterfaceInfo(context *gin.Context) {
Data device.CollectInterfaceTemplate
}{}
for k,v := range device.CollectInterfaceMap{
if v.CollInterfaceName == sName{
for k, v := range device.CollectInterfaceMap {
if v.CollInterfaceName == sName {
aParam.Code = "0"
aParam.Message = ""
aParam.Data = *device.CollectInterfaceMap[k]
sJson, _ := json.Marshal(aParam)
context.String(http.StatusOK, string(sJson))
context.JSON(http.StatusOK, aParam)
return
}
}
@ -201,19 +168,18 @@ func apiGetInterfaceInfo(context *gin.Context) {
aParam.Code = "1"
aParam.Message = "interface is not exist"
sJson, _ := json.Marshal(aParam)
context.String(http.StatusOK, string(sJson))
context.JSON(http.StatusOK, aParam)
}
func apiGetAllInterfaceInfo(context *gin.Context) {
type InterfaceParamTemplate struct{
CollInterfaceName string `json:"CollInterfaceName"` //采集接口
CommInterfaceName string `json:"CommInterfaceName"` //通信接口
PollPeriod int `json:"PollPeriod"` //采集周期
OfflinePeriod int `json:"OfflinePeriod"` //离线超时周期
DeviceNodeCnt int `json:"DeviceNodeCnt"` //设备数量
DeviceNodeOnlineCnt int `json:"DeviceNodeOnlineCnt"` //设备在线数量
type InterfaceParamTemplate struct {
CollInterfaceName string `json:"CollInterfaceName"` // 采集接口
CommInterfaceName string `json:"CommInterfaceName"` // 通信接口
PollPeriod int `json:"PollPeriod"` // 采集周期
OfflinePeriod int `json:"OfflinePeriod"` // 离线超时周期
DeviceNodeCnt int `json:"DeviceNodeCnt"` // 设备数量
DeviceNodeOnlineCnt int `json:"DeviceNodeOnlineCnt"` // 设备在线数量
}
aParam := &struct {
@ -222,35 +188,29 @@ func apiGetAllInterfaceInfo(context *gin.Context) {
Data []InterfaceParamTemplate
}{}
aParam.Data = make([]InterfaceParamTemplate,0)
aParam.Data = make([]InterfaceParamTemplate, 0)
aParam.Code = "0"
aParam.Message = ""
for _,v := range device.CollectInterfaceMap{
for _, v := range device.CollectInterfaceMap {
Param := InterfaceParamTemplate{
CollInterfaceName:v.CollInterfaceName,
CommInterfaceName:v.CommInterfaceName,
PollPeriod: v.PollPeriod,
OfflinePeriod: v.OfflinePeriod,
DeviceNodeCnt: v.DeviceNodeCnt,
CollInterfaceName: v.CollInterfaceName,
CommInterfaceName: v.CommInterfaceName,
PollPeriod: v.PollPeriod,
OfflinePeriod: v.OfflinePeriod,
DeviceNodeCnt: v.DeviceNodeCnt,
DeviceNodeOnlineCnt: v.DeviceNodeOnlineCnt,
}
aParam.Data = append(aParam.Data,Param)
aParam.Data = append(aParam.Data, Param)
}
sJson, _ := json.Marshal(aParam)
context.String(http.StatusOK, string(sJson))
context.JSON(http.StatusOK, aParam)
}
func apiAddNode(context *gin.Context) {
aParam := struct {
Code string `json:"Code"`
Message string `json:"Message"`
Data string `json:"Data"`
}{
aParam := Response{
Code: "1",
Message: "",
Data: "",
@ -274,16 +234,14 @@ func apiAddNode(context *gin.Context) {
aParam.Code = "1"
aParam.Message = "json unMarshall err"
sJson, _ := json.Marshal(aParam)
context.String(http.StatusOK, string(sJson))
context.JSON(http.StatusOK, aParam)
return
}
for _,v := range device.CollectInterfaceMap{
if v.CollInterfaceName == nodeInfo.InterfaceName{
for _,v := range v.DeviceNodeMap{
if v.Name == nodeInfo.DName{
for _, v := range device.CollectInterfaceMap {
if v.CollInterfaceName == nodeInfo.InterfaceName {
for _, v := range v.DeviceNodeMap {
if v.Name == nodeInfo.DName {
aParam.Code = "1"
aParam.Data = ""
aParam.Message = "name is exist"
@ -293,14 +251,13 @@ func apiAddNode(context *gin.Context) {
return
}
}
_,aParam.Message = v.AddDeviceNode(nodeInfo.DName,nodeInfo.DType, nodeInfo.DAddr)
_, aParam.Message = v.AddDeviceNode(nodeInfo.DName, nodeInfo.DType, nodeInfo.DAddr)
device.WriteCollectInterfaceManageToJson()
aParam.Code = "0"
aParam.Data = ""
sJson, _ := json.Marshal(aParam)
context.String(http.StatusOK, string(sJson))
context.JSON(http.StatusOK, aParam)
return
}
}
@ -308,26 +265,24 @@ func apiAddNode(context *gin.Context) {
aParam.Code = "1"
aParam.Data = ""
aParam.Message = "interfaceName is not exist"
sJson, _ := json.Marshal(aParam)
context.String(http.StatusOK, string(sJson))
context.JSON(http.StatusOK, aParam)
}
func apiModifyNode(context *gin.Context) {
type DeleteAck struct{
type DeleteAck struct {
Name string
Status bool
}
aParam := struct {
Code string `json:"Code"`
Message string `json:"Message"`
Code string `json:"Code"`
Message string `json:"Message"`
Data []DeleteAck `json:"Data"`
}{
Code: "1",
Message: "",
Data: make([]DeleteAck,0),
Data: make([]DeleteAck, 0),
}
bodyBuf := make([]byte, 1024)
@ -335,15 +290,15 @@ func apiModifyNode(context *gin.Context) {
log.Println(string(bodyBuf[:n]))
nodeInfo := &struct {
InterfaceName string `json:"CollInterfaceName"`
Name string `json:"Name"`
DType string `json:"Type"`
Addr string `json:"Addr"`
InterfaceName string `json:"CollInterfaceName"`
Name string `json:"Name"`
DType string `json:"Type"`
Addr string `json:"Addr"`
}{
InterfaceName:"",
DType:"",
Name:"",
Addr:"",
InterfaceName: "",
DType: "",
Name: "",
Addr: "",
}
err := json.Unmarshal(bodyBuf[:n], nodeInfo)
@ -353,8 +308,7 @@ func apiModifyNode(context *gin.Context) {
aParam.Code = "1"
aParam.Message = "json unMarshall err"
sJson, _ := json.Marshal(aParam)
context.String(http.StatusOK, string(sJson))
context.JSON(http.StatusOK, aParam)
return
}
@ -378,25 +332,24 @@ func apiModifyNode(context *gin.Context) {
aParam.Code = "1"
aParam.Message = "name is not exist"
sJson, _ := json.Marshal(aParam)
context.String(http.StatusOK, string(sJson))
context.JSON(http.StatusOK, aParam)
}
func apiModifyNodes(context *gin.Context) {
type DeleteAck struct{
type DeleteAck struct {
Name string
Status bool
}
aParam := struct {
Code string `json:"Code"`
Message string `json:"Message"`
Code string `json:"Code"`
Message string `json:"Message"`
Data []DeleteAck `json:"Data"`
}{
Code: "1",
Message: "",
Data: make([]DeleteAck,0),
Data: make([]DeleteAck, 0),
}
bodyBuf := make([]byte, 1024)
@ -404,13 +357,13 @@ func apiModifyNodes(context *gin.Context) {
log.Println(string(bodyBuf[:n]))
nodeInfo := &struct {
InterfaceName string `json:"CollInterfaceName"`
DType string `json:"Type"`
Name []string `json:"Name"`
InterfaceName string `json:"CollInterfaceName"`
DType string `json:"Type"`
Name []string `json:"Name"`
}{
InterfaceName:"",
DType:"",
Name:make([]string,0),
InterfaceName: "",
DType: "",
Name: make([]string, 0),
}
err := json.Unmarshal(bodyBuf[:n], nodeInfo)
@ -425,15 +378,15 @@ func apiModifyNodes(context *gin.Context) {
return
}
for _,v := range nodeInfo.Name{
for _, v := range nodeInfo.Name {
ack := DeleteAck{
Name: v,
Name: v,
Status: false,
}
aParam.Data = append(aParam.Data,ack)
aParam.Data = append(aParam.Data, ack)
}
for k,n := range nodeInfo.Name{
for k, n := range nodeInfo.Name {
for _, v := range device.CollectInterfaceMap {
if v.CollInterfaceName == nodeInfo.InterfaceName {
for _, v := range v.DeviceNodeMap {
@ -487,7 +440,7 @@ func apiGetNode(context *gin.Context) {
func apiDeleteNode(context *gin.Context) {
type DeleteAck struct{
type DeleteAck struct {
Name string
Status bool
}
@ -499,7 +452,7 @@ func apiDeleteNode(context *gin.Context) {
}{
Code: "1",
Message: "",
Data: make([]DeleteAck,0),
Data: make([]DeleteAck, 0),
}
bodyBuf := make([]byte, 1024)
@ -507,14 +460,13 @@ func apiDeleteNode(context *gin.Context) {
fmt.Println(string(bodyBuf[:n]))
nodeInfo := &struct {
InterfaceName string `json:"CollInterfaceName"`
DName []string `json:"Name"`
InterfaceName string `json:"CollInterfaceName"`
DName []string `json:"Name"`
}{
InterfaceName:"",
DName :make([]string,0),
InterfaceName: "",
DName: make([]string, 0),
}
err := json.Unmarshal(bodyBuf[:n], nodeInfo)
if err != nil {
fmt.Println("nodeInfo json unMarshall err,", err)
@ -527,16 +479,16 @@ func apiDeleteNode(context *gin.Context) {
return
}
for _,v := range nodeInfo.DName{
for _, v := range nodeInfo.DName {
ack := DeleteAck{
Name: v,
Name: v,
Status: false,
}
aParam.Data = append(aParam.Data,ack)
aParam.Data = append(aParam.Data, ack)
}
for k,DName := range nodeInfo.DName {
for k, DName := range nodeInfo.DName {
for _, v := range device.CollectInterfaceMap {
if v.CollInterfaceName == nodeInfo.InterfaceName {
for _, n := range v.DeviceNodeMap {
@ -566,14 +518,14 @@ func apiDeleteNode(context *gin.Context) {
*/
func apiGetNodeVariableFromCache(context *gin.Context) {
type VariableTemplate struct{
Index int `json:"index"` //变量偏移量
Name string `json:"name"` //变量名
Label string `json:"lable"` //变量标签
Value interface{} `json:"value"` //变量值
Explain interface{} `json:"explain"`
TimeStamp string `json:"timestamp"` //变量时间戳
Type string `json:"type"` //变量类型
type VariableTemplate struct {
Index int `json:"index"` // 变量偏移量
Name string `json:"name"` // 变量名
Label string `json:"lable"` // 变量标签
Value interface{} `json:"value"` // 变量值
Explain interface{} `json:"explain"`
TimeStamp string `json:"timestamp"` // 变量时间戳
Type string `json:"type"` // 变量类型
}
sName := context.Query("CollInterfaceName")
@ -592,26 +544,26 @@ func apiGetNodeVariableFromCache(context *gin.Context) {
aParam.Code = "0"
aParam.Message = ""
aParam.Data = make([]VariableTemplate,0)
aParam.Data = make([]VariableTemplate, 0)
index := 0
variable := VariableTemplate{}
for _,v := range v.VariableMap{
for _, v := range v.VariableMap {
variable.Index = v.Index
variable.Name = v.Name
variable.Label = v.Label
//取出切片中最后一个值
if len(v.Value) > 0{
index = len(v.Value)-1
// 取出切片中最后一个值
if len(v.Value) > 0 {
index = len(v.Value) - 1
variable.Value = v.Value[index].Value
variable.Explain = v.Value[index].Explain
variable.TimeStamp = v.Value[index].TimeStamp
}else{
} else {
variable.Value = ""
variable.Explain = ""
variable.TimeStamp = ""
}
variable.Type = v.Type
aParam.Data = append(aParam.Data,variable)
aParam.Data = append(aParam.Data, variable)
}
sJson, _ := json.Marshal(aParam)
@ -628,7 +580,6 @@ func apiGetNodeVariableFromCache(context *gin.Context) {
context.String(http.StatusOK, string(sJson))
}
func apiGetNodeHistoryVariableFromCache(context *gin.Context) {
sName := context.Query("CollInterfaceName")
@ -648,8 +599,8 @@ func apiGetNodeHistoryVariableFromCache(context *gin.Context) {
aParam.Code = "0"
aParam.Message = ""
for _,v := range v.VariableMap{
if v.Name == sVariable{
for _, v := range v.VariableMap {
if v.Name == sVariable {
aParam.Data = v.Value
}
}
@ -673,21 +624,21 @@ func apiGetNodeHistoryVariableFromCache(context *gin.Context) {
*/
func apiGetNodeVariableFromDevice(context *gin.Context) {
//sName := context.Query("interfaceName")
//sAddr := context.Query("addr")
// sName := context.Query("interfaceName")
// sAddr := context.Query("addr")
//
//aParam := &struct {
// aParam := &struct {
// Code string
// Message string
// Data []api.VariableTemplate
//}{}
// }{}
//
//
//for _,v := range device.CollectInterfaceMap {
// for _,v := range device.CollectInterfaceMap {
// if v.CollInterfaceName == nodeInfo.InterfaceName {
//
// }
//}
// }
//
// iID, _ := strconv.Atoi(sID)
// for k, v := range device.CollectInterfaceMap[iID].DeviceNodeMap {
@ -735,9 +686,9 @@ func apiAddTemplate(context *gin.Context) {
fmt.Println(string(bodyBuf[:n]))
typeInfo := &struct {
TemplateName string `json:"TemplateName"` //模板名称
TemplateType string `json:"TemplateType"` //模板型号
TemplateMessage string `json:"TemplateMessage"` //备注信息
TemplateName string `json:"TemplateName"` // 模板名称
TemplateType string `json:"TemplateType"` // 模板型号
TemplateMessage string `json:"TemplateMessage"` // 备注信息
}{}
err := json.Unmarshal(bodyBuf[:n], typeInfo)
@ -779,7 +730,7 @@ func apiGetTemplate(context *gin.Context) {
Data []device.DeviceNodeTypeTemplate
}{}
//device.ReadDeviceNodeTypeMapFromJson()
// device.ReadDeviceNodeTypeMapFromJson()
aParam.Code = "0"
aParam.Message = ""
@ -804,11 +755,11 @@ func apiAddCommInterface(context *gin.Context) {
bodyBuf := make([]byte, 1024)
n, _ := context.Request.Body.Read(bodyBuf)
//fmt.Println(string(bodyBuf[:n]))
// fmt.Println(string(bodyBuf[:n]))
interfaceInfo := struct {
Name string `json:"Name"` //接口名称
Type string `json:"Type"` //接口类型,比如serial,tcp,udp,http
Name string `json:"Name"` // 接口名称
Type string `json:"Type"` // 接口类型,比如serial,tcp,udp,http
Param *json.RawMessage `json:"Param"`
}{}
@ -823,19 +774,19 @@ func apiAddCommInterface(context *gin.Context) {
return
}
//log.Printf("info %+v\n",interfaceInfo)
//log.Printf("type %+v\n",reflect.TypeOf(interfaceInfo.Param))
//switch t:= interfaceInfo.Param.(type){
//case device.SerialInterfaceParam:
// log.Printf("info %+v\n",interfaceInfo)
// log.Printf("type %+v\n",reflect.TypeOf(interfaceInfo.Param))
// switch t:= interfaceInfo.Param.(type){
// case device.SerialInterfaceParam:
// log.Printf("param %+v\n",t)
// device.CommInterfaceList.AddCommInterface(t.Name,t.Type,t.Param)
//default:
// default:
// aParam.Code = "1"
// aParam.Message = "param is noexist"
// aParam.Data = ""
// sJson,_ := json.Marshal(aParam)
// context.String(http.StatusOK,string(sJson))
//}
// }
var msg json.RawMessage
switch interfaceInfo.Type {
@ -847,11 +798,11 @@ func apiAddCommInterface(context *gin.Context) {
break
}
log.Printf("type %+v\n", serial)
//device.CommInterfaceList.AddCommInterface(serial.Name,serial.Type,serial.Param)
// device.CommInterfaceList.AddCommInterface(serial.Name,serial.Type,serial.Param)
case "tcp":
}
//device.WriteCommInterfaceListToJson()
// device.WriteCommInterfaceListToJson()
aParam.Code = "0"
aParam.Message = ""
@ -864,9 +815,9 @@ func apiAddCommInterface(context *gin.Context) {
func apiGetCommInterface(context *gin.Context) {
type CommunicationInterfaceTemplate struct {
Name string `json:"Name"` //接口名称
Type string `json:"Type"` //接口类型,比如serial,tcp,udp,http
Param interface{} `json:"Param"` //接口参数
Name string `json:"Name"` // 接口名称
Type string `json:"Type"` // 接口类型,比如serial,tcp,udp,http
Param interface{} `json:"Param"` // 接口参数
}
type CommunicationInterfaceManageTemplate struct {
@ -921,8 +872,8 @@ func apiAddCommSerialInterface(context *gin.Context) {
n, _ := context.Request.Body.Read(bodyBuf)
interfaceInfo := struct {
Name string `json:"Name"` //接口名称
Type string `json:"Type"` //接口类型,比如serial,tcp,udp,http
Name string `json:"Name"` // 接口名称
Type string `json:"Type"` // 接口类型,比如serial,tcp,udp,http
Param device.SerialInterfaceParam `json:"Param"`
}{}
@ -938,8 +889,8 @@ func apiAddCommSerialInterface(context *gin.Context) {
}
for _, v := range device.CommunicationSerialMap {
//判断通信接口名称是否一致
if (v.Name==interfaceInfo.Name) || (v.Param.Name == interfaceInfo.Param.Name){
// 判断通信接口名称是否一致
if (v.Name == interfaceInfo.Name) || (v.Param.Name == interfaceInfo.Param.Name) {
aParam.Code = "1"
aParam.Message = "name is exist"
aParam.Data = ""
@ -958,7 +909,7 @@ func apiAddCommSerialInterface(context *gin.Context) {
},
}
device.CommunicationSerialMap = append(device.CommunicationSerialMap,SerialInterface)
device.CommunicationSerialMap = append(device.CommunicationSerialMap, SerialInterface)
device.WriteCommSerialInterfaceListToJson()
aParam.Code = "0"
@ -984,8 +935,8 @@ func apiModifyCommSerialInterface(context *gin.Context) {
n, _ := context.Request.Body.Read(bodyBuf)
interfaceInfo := struct {
Name string `json:"Name"` //接口名称
Type string `json:"Type"` //接口类型,比如serial,tcp,udp,http
Name string `json:"Name"` // 接口名称
Type string `json:"Type"` // 接口类型,比如serial,tcp,udp,http
Param device.SerialInterfaceParam `json:"Param"`
}{}
@ -1001,7 +952,7 @@ func apiModifyCommSerialInterface(context *gin.Context) {
}
for k, v := range device.CommunicationSerialMap {
//判断名称是否一致
// 判断名称是否一致
if v.Name == interfaceInfo.Name {
device.CommunicationSerialMap[k].Type = interfaceInfo.Type
device.CommunicationSerialMap[k].Param = interfaceInfo.Param
@ -1040,8 +991,8 @@ func apiDeleteCommSerialInterface(context *gin.Context) {
n, _ := context.Request.Body.Read(bodyBuf)
interfaceInfo := struct {
Name string `json:"Name"` //接口名称
Type string `json:"Type"` //接口类型,比如serial,tcp,udp,http
Name string `json:"Name"` // 接口名称
Type string `json:"Type"` // 接口类型,比如serial,tcp,udp,http
}{}
err := json.Unmarshal(bodyBuf[:n], &interfaceInfo)
@ -1056,7 +1007,7 @@ func apiDeleteCommSerialInterface(context *gin.Context) {
}
for k, v := range device.CommunicationSerialMap {
//判断名称是否一致
// 判断名称是否一致
if v.Name == interfaceInfo.Name {
device.CommunicationSerialMap = append(device.CommunicationSerialMap[:k],

View File

@ -2,7 +2,6 @@ package httpServer
import (
"encoding/binary"
"encoding/json"
"fmt"
"net/http"
"strconv"
@ -33,8 +32,6 @@ func NewMbParam(addr byte, regAddr, regCnt uint16) *MbParam {
}
func mbParamOpenPort(port string) bool {
status := false
//调用RTUClientProvider的构造函数,返回结构体指针
p := modbus.NewRTUClientProvider(modbus.WithSerialConfig(serial.Config{
Address: port,
@ -50,11 +47,10 @@ func mbParamOpenPort(port string) bool {
err := client.Connect()
if err != nil {
fmt.Println("start err,", err)
return status
return false
}
status = true
return status
return true
}
func mbParamReadHoldReg(slaveAddr byte, regAddr uint16, regCnt uint16) []uint16 {
@ -78,34 +74,16 @@ func mbParamWriteMutilReg(slaveAddr byte, regAddr uint16, regCnt uint16, data []
}
func apiReadHoldReg(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]))
//获取读寄存器的参数
rMbParam := &MbParam{}
err := json.Unmarshal(bodyBuf[:n], rMbParam)
err := context.ShouldBindJSON(rMbParam)
if err != nil {
fmt.Println("rMbParam json unMarshall err,", err)
aParam.Code = "1"
aParam.Message = "json unMarshall err"
sJson, _ := json.Marshal(aParam)
context.String(http.StatusOK, string(sJson))
context.JSON(http.StatusOK, Response{
Code: "1",
Message: "json unMarshall err",
})
return
}
@ -124,43 +102,25 @@ func apiReadHoldReg(context *gin.Context) {
}
fmt.Println(sRegValue)
aParam.Code = "0"
aParam.Message = ""
aParam.Data = sRegValue
sJson, _ := json.Marshal(aParam)
context.String(http.StatusOK, string(sJson))
context.JSON(http.StatusOK, Response{
Code: "0",
Message: "",
Data: sRegValue,
})
}
func apiWriteMultiReg(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]))
//获取写寄存器的参数
rMbParam := &MbParam{}
err := json.Unmarshal(bodyBuf[:n], rMbParam)
err := context.ShouldBindJSON(rMbParam)
if err != nil {
fmt.Println("rMbParam json unMarshall err,", err)
aParam.Code = "1"
aParam.Message = "json unMarshall err"
sJson, _ := json.Marshal(aParam)
context.String(http.StatusOK, string(sJson))
context.JSON(http.StatusOK, Response{
Code: "1",
Message: "json unMarshall err",
Data: "",
})
return
}
@ -181,16 +141,16 @@ func apiWriteMultiReg(context *gin.Context) {
err = mbParamWriteMutilReg(rMbParam.Addr, rMbParam.RegAddr,
rMbParam.RegCnt, bData)
if err != nil {
aParam.Code = "1"
aParam.Message = "write reg timeout"
sJson, _ := json.Marshal(aParam)
context.String(http.StatusOK, string(sJson))
context.JSON(http.StatusOK, Response{
Code: "1",
Message: "write reg timeout",
})
return
}
aParam.Code = "0"
aParam.Message = ""
sJson, _ := json.Marshal(aParam)
context.String(http.StatusOK, string(sJson))
context.JSON(http.StatusOK, Response{
Code: "0",
Message: "",
Data: "",
})
}

View File

@ -1,84 +1,46 @@
package httpServer
import (
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"goAdapter/setting"
"net/http"
"goAdapter/setting"
"github.com/gin-gonic/gin"
)
func apiSetNetwork(context *gin.Context){
bodyBuf := make([]byte,1024)
n,_ := context.Request.Body.Read(bodyBuf)
fmt.Println(string(bodyBuf[:n]))
func apiSetNetwork(context *gin.Context) {
networkParam := setting.NetworkParamTemplate{}
err := json.Unmarshal(bodyBuf[:n],&networkParam)
err := context.ShouldBindJSON(&networkParam)
if err != nil {
fmt.Println("rNetworkParam json unMarshall err,",err)
aParam := struct{
Code string `json:"Code"`
Message string `json:"Message"`
Data string `json:"Data"`
}{
Code:"1",
Message:"",
Data:"",
}
sJson,_ := json.Marshal(aParam)
context.String(http.StatusOK,string(sJson))
fmt.Println("rNetworkParam json unMarshall err,", err)
context.JSON(http.StatusOK, Response{
Code: "1",
Message: "",
Data: "",
})
return
}
setting.NetworkParamList.SetNetworkParam(networkParam)
aParam := struct{
Code string `json:"Code"`
Message string `json:"Message"`
Data string `json:"Data"`
}{
Code:"0",
Message:"",
Data:"",
}
sJson,_ := json.Marshal(aParam)
context.String(http.StatusOK,string(sJson))
context.JSON(http.StatusOK, Response{
Code: "0",
Message: "",
Data: "",
})
}
func apiGetNetwork(context *gin.Context){
aParam := &struct{
Code string
Message string
Data setting.NetworkParamListTemplate
}{}
aParam.Code = "0"
aParam.Data = *setting.NetworkParamList
sJson,_ := json.Marshal(aParam)
context.String(http.StatusOK,string(sJson))
func apiGetNetwork(context *gin.Context) {
context.JSON(http.StatusOK, ResponseData{
Code: "0",
Data: *setting.NetworkParamList,
})
}
func apiGetNetworkLinkState(context *gin.Context){
//aParam := struct{
// Code string
// Message string
// Data setting.NetworkLinkStateTemplate
//}{Code:"0"}
//
//aParam.Data = setting.NetworkLinkState
//
//sJson,_ := json.Marshal(aParam)
//
//context.String(http.StatusOK,string(sJson))
func apiGetNetworkLinkState(context *gin.Context) {
// context.JSON(http.StatusOK, ResponseData{
// Code: "0",
// Data: setting.NetworkLinkState,
// })
}

View File

@ -1,35 +1,29 @@
package httpServer
import (
"encoding/json"
"github.com/gin-gonic/gin"
"goAdapter/setting"
"net/http"
"goAdapter/setting"
"github.com/gin-gonic/gin"
)
func apiGetSerial(context *gin.Context) {
type SerialPortNameTemplate struct{
Name string `json:"Name"`
type SerialPortNameTemplate struct {
Name string `json:"Name"`
}
aParam := struct {
Code string `json:"Code"`
Message string `json:"Message"`
Data []SerialPortNameTemplate `json:"Data"`
}{
Code: "0",
Message: "",
Data: make([]SerialPortNameTemplate,0),
}
data := make([]SerialPortNameTemplate, 0)
SerialPortName := SerialPortNameTemplate{}
for _,v := range setting.SerialPortNameTemplateMap.Name{
for _, v := range setting.SerialPortNameTemplateMap.Name {
SerialPortName.Name = v
aParam.Data = append(aParam.Data,SerialPortName)
data = append(data, SerialPortName)
}
sJson, _ := json.Marshal(aParam)
context.String(http.StatusOK, string(sJson))
context.JSON(http.StatusOK, ResponseData{
Code: "0",
Message: "",
Data: data,
})
}

View File

@ -1,61 +1,42 @@
package httpServer
import (
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"goAdapter/setting"
"net/http"
"goAdapter/setting"
"github.com/gin-gonic/gin"
)
func apiSystemReboot(context *gin.Context){
aParam := struct{
Code string `json:"Code"`
Message string `json:"Message"`
Data string `json:"Data"`
}{
Code:"1",
Message:"",
Data:"",
}
aParam.Code = "0"
sJson,_ := json.Marshal(aParam)
context.String(http.StatusOK,string(sJson))
func apiSystemReboot(context *gin.Context) {
context.JSON(http.StatusOK, Response{
Code: "0",
Message: "",
Data: "",
})
setting.SystemReboot()
}
func apiGetSystemStatus(context *gin.Context){
func apiGetSystemStatus(context *gin.Context) {
setting.GetMemState()
setting.GetDiskState()
setting.GetRunTime()
aParam := struct{
Code string
Message string
Data setting.SystemStateTemplate
}{"0","",setting.SystemState}
sJson,_ := json.Marshal(aParam)
context.String(http.StatusOK,string(sJson))
context.JSON(http.StatusOK, ResponseData{
"0",
"",
setting.SystemState,
})
}
func apiSystemLoginParam(context *gin.Context){
aParam := struct{
Code string
Message string
Data LoginResult `json:"Data"`
}{"0","",loginResult}
sJson,_ := json.Marshal(aParam)
context.String(http.StatusOK,string(sJson))
func apiSystemLoginParam(context *gin.Context) {
context.JSON(http.StatusOK, ResponseData{
"0",
"",
loginResult,
})
}
// 定义登陆逻辑
@ -70,165 +51,107 @@ func apiLogin(c *gin.Context) {
generateToken(c, user)
} else {
c.JSON(http.StatusOK, gin.H{
"Code" : "-1",
"Message" : "验证失败" + err.Error(),
"Data" : "",
"Code": "-1",
"Message": "验证失败" + err.Error(),
"Data": "",
})
return
}
}else {
} else {
c.JSON(http.StatusOK, gin.H{
"Code" : "-1",
"Message" : "用户数据解析失败",
"Data" : "",
"Code": "-1",
"Message": "用户数据解析失败",
"Data": "",
})
return
}
}
func apiSystemMemoryUseList(context *gin.Context){
aParam := struct{
Code string
Message string
Data setting.DataStreamTemplate
}{"0","",*setting.MemoryDataStream}
sJson,_ := json.Marshal(aParam)
context.String(http.StatusOK,string(sJson))
func apiSystemMemoryUseList(context *gin.Context) {
context.JSON(http.StatusOK, ResponseData{
"0",
"",
*setting.MemoryDataStream,
})
}
func apiSystemDiskUseList(context *gin.Context){
aParam := struct{
Code string
Message string
Data setting.DataStreamTemplate
}{"0","",*setting.DiskDataStream}
sJson,_ := json.Marshal(aParam)
context.String(http.StatusOK,string(sJson))
func apiSystemDiskUseList(context *gin.Context) {
context.JSON(http.StatusOK, ResponseData{
"0",
"",
*setting.DiskDataStream,
})
}
func apiSystemDeviceOnlineList(context *gin.Context){
aParam := struct{
Code string
Message string
Data setting.DataStreamTemplate
}{"0","",*setting.DeviceOnlineDataStream}
sJson,_ := json.Marshal(aParam)
context.String(http.StatusOK,string(sJson))
func apiSystemDeviceOnlineList(context *gin.Context) {
context.JSON(http.StatusOK, ResponseData{
"0",
"",
*setting.DeviceOnlineDataStream,
})
}
func apiSystemDevicePacketLossList(context *gin.Context){
aParam := struct{
Code string
Message string
Data setting.DataStreamTemplate
}{"0","",*setting.DevicePacketLossDataStream}
sJson,_ := json.Marshal(aParam)
context.String(http.StatusOK,string(sJson))
func apiSystemDevicePacketLossList(context *gin.Context) {
context.JSON(http.StatusOK, ResponseData{
"0",
"",
*setting.DevicePacketLossDataStream,
})
}
func apiSystemSetSystemRTC(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]))
rRTC := &struct{
systemRTC string
func apiSystemSetSystemRTC(context *gin.Context) {
rRTC := &struct {
systemRTC string
}{}
err := json.Unmarshal(bodyBuf[:n],rRTC)
err := context.ShouldBindJSON(rRTC)
if err != nil {
fmt.Println("rRTC json unMarshall err,",err)
aParam.Code = "1"
aParam.Message = "json unMarshall err"
sJson,_ := json.Marshal(aParam)
context.String(http.StatusOK,string(sJson))
fmt.Println("rRTC json unMarshall err,", err)
context.JSON(http.StatusOK, Response{
Code: "1",
Message: "json unMarshall err",
Data: "",
})
return
}
setting.SystemSetRTC(rRTC.systemRTC)
aParam.Code = "0"
aParam.Message = ""
aParam.Data = ""
sJson,_ := json.Marshal(aParam)
context.String(http.StatusOK,string(sJson))
context.JSON(http.StatusOK, Response{
Code: "0",
Message: "",
Data: "",
})
}
func apiSystemSetNTPHost(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]))
rNTPHostAddr := setting.NTPHostAddrTemplate{}
err := json.Unmarshal(bodyBuf[:n], &rNTPHostAddr)
err := context.ShouldBindJSON(&rNTPHostAddr)
if err != nil {
fmt.Println("rNTPHostAddr json unMarshall err,", err)
aParam.Code = "1"
aParam.Message = "json unMarshall err"
sJson, _ := json.Marshal(aParam)
context.String(http.StatusOK, string(sJson))
context.JSON(http.StatusOK, Response{
Code: "1",
Message: "json unMarshall err",
Data: "",
})
return
}
setting.NTPHostAddr = rNTPHostAddr
setting.WriteNTPHostAddrToJson()
aParam.Code = "0"
aParam.Data = ""
aParam.Message = ""
sJson, _ := json.Marshal(aParam)
context.String(http.StatusOK, string(sJson))
context.JSON(http.StatusOK, Response{
Code: "0",
Message: "",
Data: "",
})
}
func apiSystemGetNTPHost(context *gin.Context){
aParam := struct{
Code string
Message string
Data setting.NTPHostAddrTemplate
}{
Code:"0",
Message:"",
Data:setting.NTPHostAddr,
}
sJson,_ := json.Marshal(aParam)
context.String(http.StatusOK,string(sJson))
}
func apiSystemGetNTPHost(context *gin.Context) {
context.JSON(http.StatusOK, ResponseData{
Code: "0",
Message: "",
Data: setting.NTPHostAddr,
})
}

View File

@ -1,55 +1,41 @@
package httpServer
import (
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"goAdapter/device"
"net/http"
"github.com/gin-gonic/gin"
)
func apiGetCommMessage(context *gin.Context) {
aParam := &struct {
Code string
Message string
Data []device.CommunicationMessageTemplate
}{}
bodyBuf := make([]byte, 1024)
n, _ := context.Request.Body.Read(bodyBuf)
interfaceName := struct {
CollInterfaceNames []string `json:"CollInterfaceNames"` //接口名称
CollInterfaceNames []string `json:"CollInterfaceNames"` //接口名称
}{}
commMessageMap := make([]device.CommunicationMessageTemplate,0)
commMessageMap := make([]device.CommunicationMessageTemplate, 0)
err := json.Unmarshal(bodyBuf[:n], &interfaceName)
err := context.ShouldBindJSON(&interfaceName)
if err != nil {
fmt.Println("interfaceInfo json unMarshall err,", err)
aParam.Code = "1"
aParam.Message = "json unMarshall err"
sJson, _ := json.Marshal(aParam)
context.String(http.StatusOK, string(sJson))
context.JSON(http.StatusOK, Response{
Code: "1",
Message: "json unMarshall err",
})
return
}else {
for _,name := range interfaceName.CollInterfaceNames{
for _,v := range device.CollectInterfaceMap{
if name == v.CollInterfaceName{
commMessageMap = append(commMessageMap,v.CommMessage...)
// 清空map
v.CommMessage = v.CommMessage[0:0]
}
}
for _, name := range interfaceName.CollInterfaceNames {
for _, v := range device.CollectInterfaceMap {
if name == v.CollInterfaceName {
commMessageMap = append(commMessageMap, v.CommMessage...)
// 清空map
v.CommMessage = v.CommMessage[0:0]
}
}
}
aParam.Code = "0"
aParam.Message = ""
aParam.Data = commMessageMap
sJson, _ := json.Marshal(aParam)
context.String(http.StatusOK, string(sJson))
}
context.JSON(http.StatusOK, ResponseData{
Code: "1",
Data: commMessageMap,
})
}

View File

@ -2,13 +2,13 @@ package httpServer
import (
"archive/zip"
"encoding/json"
"github.com/gin-gonic/gin"
"io"
"log"
"net/http"
"os"
"path/filepath"
"github.com/gin-gonic/gin"
)
func fileExist(path string) bool {
@ -16,42 +16,42 @@ func fileExist(path string) bool {
return !os.IsNotExist(err)
}
func unZip(zipFile string,destDir string) error{
func unZip(zipFile string, destDir string) error {
zipReader,err := zip.OpenReader(zipFile)
if err != nil{
log.Printf("OpenReader err,",err)
zipReader, err := zip.OpenReader(zipFile)
if err != nil {
log.Printf("OpenReader err,", err)
return err
}
defer zipReader.Close()
for _,f := range zipReader.File{
fpath := filepath.Join(destDir,f.Name)
log.Println("fpath ",fpath)
if f.FileInfo().IsDir(){
os.MkdirAll(fpath,os.ModePerm)
}else{
if err = os.MkdirAll(filepath.Dir(fpath),os.ModePerm);err != nil{
log.Println("mkdir err",err)
for _, f := range zipReader.File {
fpath := filepath.Join(destDir, f.Name)
log.Println("fpath ", fpath)
if f.FileInfo().IsDir() {
os.MkdirAll(fpath, os.ModePerm)
} else {
if err = os.MkdirAll(filepath.Dir(fpath), os.ModePerm); err != nil {
log.Println("mkdir err", err)
return err
}
inFile,err := f.Open()
if err != nil{
log.Println("open err,",err)
inFile, err := f.Open()
if err != nil {
log.Println("open err,", err)
return err
}
defer inFile.Close()
outFile,err := os.OpenFile(fpath,os.O_WRONLY|os.O_CREATE|os.O_TRUNC,f.Mode())
if err != nil{
log.Println("openFile err,",err)
outFile, err := os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
log.Println("openFile err,", err)
return err
}
defer outFile.Close()
_,err = io.Copy(outFile,inFile)
if err != nil{
log.Println("copy err,",err)
_, err = io.Copy(outFile, inFile)
if err != nil {
log.Println("copy err,", err)
return err
}
}
@ -60,24 +60,16 @@ func unZip(zipFile string,destDir string) error{
return nil
}
func apiUpdatePlugin(context *gin.Context) {
aParam := struct {
Code string `json:"Code"`
Message string `json:"Message"`
Data string `json:"Data"`
}{
Code: "1",
Message: "",
Data: "",
}
// 获取文件头
file, err := context.FormFile("file")
if err != nil {
sJson, _ := json.Marshal(aParam)
context.String(http.StatusOK, string(sJson))
context.JSON(http.StatusOK, Response{
Code: "1",
Message: "",
Data: "",
})
return
}
@ -87,32 +79,28 @@ func apiUpdatePlugin(context *gin.Context) {
fileName := fileDir + "/" + file.Filename
log.Println(fileName)
if fileExist(fileDir)==false{
if fileExist(fileDir) == false {
os.MkdirAll(fileDir, os.ModePerm)
}
//保存文件到服务器本地
if err := context.SaveUploadedFile(file, fileName); err != nil {
log.Println(err)
aParam.Code = "1"
aParam.Message = "save error"
sJson, _ := json.Marshal(aParam)
context.String(http.StatusOK, string(sJson))
context.JSON(http.StatusOK, Response{
Code: "1",
Message: "save error",
})
return
}
unZip(fileName,fileDir)
unZip(fileName, fileDir)
err = os.Remove(fileName)
if err != nil{
log.Printf("removeFile err,%s\n",fileName)
if err != nil {
log.Printf("removeFile err,%s\n", fileName)
}
aParam.Code = "0"
aParam.Message = "save sucess"
sJson, _ := json.Marshal(aParam)
context.String(http.StatusOK, string(sJson))
}
context.JSON(http.StatusOK, Response{
Code: "0",
Message: "save sucess",
})
}