修改:1、增加串口通信相关接口
This commit is contained in:
parent
719729be6f
commit
ba0a10cf9a
|
@ -1,183 +1,42 @@
|
||||||
package device
|
package device
|
||||||
|
|
||||||
import (
|
import "log"
|
||||||
"encoding/json"
|
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
)
|
|
||||||
|
|
||||||
type CommunicationInterface interface{
|
type CommunicationInterface interface{
|
||||||
Open(interface{}) bool
|
Open() bool
|
||||||
Close() bool
|
Close() bool
|
||||||
WriteData(data []byte) int
|
WriteData(data []byte) int
|
||||||
ReadData(data []byte) int
|
ReadData(data []byte) int
|
||||||
}
|
}
|
||||||
|
|
||||||
type CommInterfaceTemplate struct{
|
type CommunicationTemplate struct{
|
||||||
Name string `json:"Name"` //接口名称
|
Name string `json:"Name"` //接口名称
|
||||||
Type string `json:"Type"` //接口类型,比如serial,tcp,udp,http
|
Type string `json:"Type"` //接口类型,比如serial,tcp,udp,http
|
||||||
Param interface{} `json:"Param"` //接口参数
|
Status bool `json:"-"` //接口状态
|
||||||
Status bool `json:"Status"` //接口状态
|
|
||||||
Port CommunicationInterface `json:"-"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type CommInterfaceListTemplate struct{
|
type CommunicationInterfaceListTemplate struct{
|
||||||
InterfaceCnt int `json:"InterfaceCnt"`
|
CommunicationInterfaceMap []CommunicationInterface
|
||||||
InterfaceMap []CommInterfaceTemplate
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var CommInterfaceList *CommInterfaceListTemplate
|
var CommunicationInterfaceList CommunicationInterfaceListTemplate
|
||||||
|
|
||||||
|
|
||||||
func NewCommInterfaceList() *CommInterfaceListTemplate{
|
|
||||||
|
|
||||||
return &CommInterfaceListTemplate{
|
|
||||||
InterfaceCnt: 0,
|
|
||||||
InterfaceMap: make([]CommInterfaceTemplate,0),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewCommInterface(Name string,Type string,Param interface{}) CommInterfaceTemplate{
|
|
||||||
|
|
||||||
comm := CommInterfaceTemplate{
|
|
||||||
Name:Name,
|
|
||||||
Type:Type,
|
|
||||||
Param:Param,
|
|
||||||
}
|
|
||||||
|
|
||||||
return comm
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *CommInterfaceTemplate)NewCommInterfacePort(){
|
|
||||||
|
|
||||||
switch c.Type{
|
|
||||||
case "serial":
|
|
||||||
c.Port = &CommunicationSerialInterface{}
|
|
||||||
case "tcp":
|
|
||||||
c.Port = &CommunicationTcpInterface{}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//func (c *CommInterfaceListTemplate)NewCommInterfacePort(){
|
|
||||||
//
|
|
||||||
// for _,v := range c.InterfaceMap{
|
|
||||||
// log.Printf("type is %s\n",v.Type)
|
|
||||||
// switch v.Type{
|
|
||||||
// case "serial":
|
|
||||||
// c.InterfacePortMap = append(c.InterfacePortMap,&CommunicationSerialInterface{})
|
|
||||||
// case "tcp":
|
|
||||||
// c.InterfacePortMap = append(c.InterfacePortMap,&CommunicationTcpInterface{})
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
|
|
||||||
func (c *CommInterfaceListTemplate)AddCommInterface(Name string,Type string,Param interface{}){
|
|
||||||
|
|
||||||
comm := NewCommInterface(Name,Type,Param)
|
|
||||||
|
|
||||||
c.InterfaceMap = append(c.InterfaceMap,comm)
|
|
||||||
c.InterfaceCnt++
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *CommInterfaceListTemplate)ModifyCommInterface(Name string,Type string,Param interface{}) bool{
|
|
||||||
|
|
||||||
for k,v := range c.InterfaceMap{
|
|
||||||
if v.Name == Name{
|
|
||||||
c.InterfaceMap[k].Name = Name
|
|
||||||
c.InterfaceMap[k].Type = Type
|
|
||||||
c.InterfaceMap[k].Param = Param
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *CommInterfaceListTemplate)GetCommInterface(Name string) CommInterfaceTemplate{
|
|
||||||
|
|
||||||
for k,v := range c.InterfaceMap{
|
|
||||||
if v.Name == Name{
|
|
||||||
return c.InterfaceMap[k]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return CommInterfaceTemplate{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *CommInterfaceListTemplate)DeleteCommInterface(Name string) bool{
|
|
||||||
|
|
||||||
for k,v := range c.InterfaceMap{
|
|
||||||
if v.Name == Name{
|
|
||||||
c.InterfaceMap = append(c.InterfaceMap[:k],c.InterfaceMap[k+1:]...)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func WriteCommInterfaceListToJson() {
|
|
||||||
|
|
||||||
exeCurDir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
|
|
||||||
|
|
||||||
fileDir := exeCurDir + "/selfpara/commInterface.json"
|
|
||||||
|
|
||||||
fp, err := os.OpenFile(fileDir, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0777)
|
|
||||||
if err != nil {
|
|
||||||
log.Println("open commInterface.json err", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer fp.Close()
|
|
||||||
|
|
||||||
sJson, _ := json.Marshal(CommInterfaceList)
|
|
||||||
|
|
||||||
_, err = fp.Write(sJson)
|
|
||||||
if err != nil {
|
|
||||||
log.Println("write commInterface.json err", err)
|
|
||||||
}
|
|
||||||
log.Println("write commInterface.json sucess")
|
|
||||||
}
|
|
||||||
|
|
||||||
func ReadCommInterfaceListFromJson() bool {
|
|
||||||
|
|
||||||
exeCurDir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
|
|
||||||
fileDir := exeCurDir + "/selfpara/commInterface.json"
|
|
||||||
|
|
||||||
if fileExist(fileDir) == true {
|
|
||||||
fp, err := os.OpenFile(fileDir, os.O_RDONLY, 0777)
|
|
||||||
if err != nil {
|
|
||||||
log.Println("open commInterface.json err", err)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
defer fp.Close()
|
|
||||||
|
|
||||||
data := make([]byte, 20480)
|
|
||||||
dataCnt, err := fp.Read(data)
|
|
||||||
|
|
||||||
err = json.Unmarshal(data[:dataCnt], CommInterfaceList)
|
|
||||||
if err != nil {
|
|
||||||
log.Println("commInterface unmarshal err", err)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
|
||||||
} else {
|
|
||||||
log.Println("commInterface.json is not exist")
|
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func CommInterfaceInit() {
|
func CommInterfaceInit() {
|
||||||
|
|
||||||
CommInterfaceList = NewCommInterfaceList()
|
if ReadCommSerialInterfaceListFromJson() == false{
|
||||||
if ReadCommInterfaceListFromJson() == true{
|
|
||||||
log.Println("read commInterface.json ok")
|
CommunicationSerialInterfaceList.SerialInterfaceMap = make([]CommunicationSerialInterface,0)
|
||||||
log.Printf("%+v\n",CommInterfaceList)
|
}else{
|
||||||
for _,v := range CommInterfaceList.InterfaceMap{
|
log.Println("read CommSerialInterfaceList.json ok")
|
||||||
v.NewCommInterfacePort()
|
|
||||||
v.Status = v.Port.Open(v.Param)
|
//for _,v := range CommunicationSerialInterfaceList.SerialInterfaceMap{
|
||||||
if v.Status != true{
|
//
|
||||||
log.Printf("%s open err\n",v.Param)
|
// CommunicationInterfaceList.CommunicationInterfaceMap = append(CommunicationInterfaceList.CommunicationInterfaceMap,v)
|
||||||
}
|
//}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _,v := range CommunicationInterfaceList.CommunicationInterfaceMap{
|
||||||
|
|
||||||
|
v.Open()
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,9 +1,13 @@
|
||||||
package device
|
package device
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"github.com/tarm/serial"
|
"github.com/tarm/serial"
|
||||||
"log"
|
"log"
|
||||||
"reflect"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SerialInterfaceParam struct{
|
type SerialInterfaceParam struct{
|
||||||
|
@ -17,24 +21,20 @@ type SerialInterfaceParam struct{
|
||||||
}
|
}
|
||||||
|
|
||||||
type CommunicationSerialInterface struct{
|
type CommunicationSerialInterface struct{
|
||||||
Name string `json:"Name"`
|
CommunicationTemplate
|
||||||
BaudRate string `json:"BaudRate"`
|
Param SerialInterfaceParam `json:"Param"` //接口参数
|
||||||
DataBits string `json:"DataBits"` //数据位: 5, 6, 7 or 8 (default 8)
|
Port *serial.Port `json:"-"` //通信句柄
|
||||||
StopBits string `json:"StopBits"` //停止位: 1 or 2 (default 1)
|
|
||||||
Parity string `json:"Parity"` //校验: N - None, E - Even, O - Odd (default E),(The use of no parity requires 2 stop bits.)
|
|
||||||
Timeout string `json:"Timeout"` //通信超时
|
|
||||||
Interval string `json:"Interval"` //通信间隔
|
|
||||||
Port *serial.Port `json:"-"` //通信句柄
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CommunicationSerialInterface)Open(param interface{}) bool{
|
type CommunicationSerialInterfaceListTemplate struct{
|
||||||
|
SerialInterfaceMap []CommunicationSerialInterface
|
||||||
|
}
|
||||||
|
|
||||||
|
var CommunicationSerialInterfaceList CommunicationSerialInterfaceListTemplate
|
||||||
|
|
||||||
log.Println(" ",reflect.TypeOf(param))
|
func (c *CommunicationSerialInterface)Open() bool{
|
||||||
//log.Printf("Name is %s\n",serialParam.FieldByName("Name"))
|
|
||||||
|
|
||||||
/*
|
serialParam := c.Param
|
||||||
serialParam := param.(CommunicationSerialInterface)
|
|
||||||
serialBaud,_ := strconv.Atoi(serialParam.BaudRate)
|
serialBaud,_ := strconv.Atoi(serialParam.BaudRate)
|
||||||
|
|
||||||
var serialParity serial.Parity
|
var serialParity serial.Parity
|
||||||
|
@ -65,13 +65,15 @@ func (c *CommunicationSerialInterface)Open(param interface{}) bool{
|
||||||
ReadTimeout: time.Millisecond*1,
|
ReadTimeout: time.Millisecond*1,
|
||||||
}
|
}
|
||||||
|
|
||||||
serial, err := serial.OpenPort(serialConfig)
|
serialPort, err := serial.OpenPort(serialConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("open serial err,%s",err)
|
log.Printf("open serial err,%s",err)
|
||||||
return false
|
return false
|
||||||
|
}else{
|
||||||
|
log.Printf("open serial %s ok\n",c.Param.Name)
|
||||||
}
|
}
|
||||||
serialParam.Port = serial
|
|
||||||
*/
|
c.Port = serialPort
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -93,3 +95,69 @@ func (c *CommunicationSerialInterface)ReadData(data []byte) int{
|
||||||
|
|
||||||
return cnt
|
return cnt
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewCommunicationSerialInterface(commName,commType string,param SerialInterfaceParam) *CommunicationSerialInterface{
|
||||||
|
|
||||||
|
return &CommunicationSerialInterface{
|
||||||
|
Param:param,
|
||||||
|
CommunicationTemplate:CommunicationTemplate{
|
||||||
|
Name:commName,
|
||||||
|
Type:commType,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReadCommSerialInterfaceListFromJson() bool {
|
||||||
|
|
||||||
|
exeCurDir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
|
||||||
|
fileDir := exeCurDir + "/selfpara/commSerialInterface.json"
|
||||||
|
|
||||||
|
if fileExist(fileDir) == true {
|
||||||
|
fp, err := os.OpenFile(fileDir, os.O_RDONLY, 0777)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("open commSerialInterface.json err", err)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
defer fp.Close()
|
||||||
|
|
||||||
|
data := make([]byte, 20480)
|
||||||
|
dataCnt, err := fp.Read(data)
|
||||||
|
|
||||||
|
CommunicationSerialInterfaceList.SerialInterfaceMap = make([]CommunicationSerialInterface,0)
|
||||||
|
|
||||||
|
err = json.Unmarshal(data[:dataCnt], &CommunicationSerialInterfaceList)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("commSerialInterface unmarshal err", err)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
//log.Printf("SerialInterfaceMap %+v\n",CommunicationSerialInterfaceList.SerialInterfaceMap)
|
||||||
|
return true
|
||||||
|
} else {
|
||||||
|
log.Println("commSerialInterface.json is not exist")
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WriteCommSerialInterfaceListToJson() {
|
||||||
|
|
||||||
|
exeCurDir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
|
||||||
|
|
||||||
|
fileDir := exeCurDir + "/selfpara/commSerialInterface.json"
|
||||||
|
|
||||||
|
fp, err := os.OpenFile(fileDir, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0777)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("open commSerialInterface.json err", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer fp.Close()
|
||||||
|
|
||||||
|
sJson, _ := json.Marshal(CommunicationSerialInterfaceList)
|
||||||
|
|
||||||
|
_, err = fp.Write(sJson)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("write commSerialInterface.json err", err)
|
||||||
|
}
|
||||||
|
log.Println("write commSerialInterface.json sucess")
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
package device
|
package device
|
||||||
|
|
||||||
type CommunicationTcpInterface struct{
|
type TcpInterfaceParam struct{
|
||||||
Name string `json:"Name"`
|
Name string `json:"Name"`
|
||||||
IP string `json:"IP"`
|
IP string `json:"IP"`
|
||||||
Port string `json:"Port"`
|
Port string `json:"Port"`
|
||||||
|
@ -8,7 +8,12 @@ type CommunicationTcpInterface struct{
|
||||||
Interval string `json:"Interval"` //通信间隔
|
Interval string `json:"Interval"` //通信间隔
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CommunicationTcpInterface)Open(param interface{}) bool{
|
type CommunicationTcpInterface struct{
|
||||||
|
CommunicationTemplate
|
||||||
|
Param TcpInterfaceParam `json:"Param"` //接口参数
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *CommunicationTcpInterface)Open() bool{
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
|
@ -480,38 +480,59 @@ func apiAddCommInterface(context *gin.Context){
|
||||||
|
|
||||||
bodyBuf := make([]byte,1024)
|
bodyBuf := make([]byte,1024)
|
||||||
n,_ := context.Request.Body.Read(bodyBuf)
|
n,_ := context.Request.Body.Read(bodyBuf)
|
||||||
fmt.Println(string(bodyBuf[:n]))
|
//fmt.Println(string(bodyBuf[:n]))
|
||||||
|
|
||||||
interfaceInfo := &struct{
|
interfaceInfo := struct{
|
||||||
Name string `json:"Name"` //接口名称
|
Name string `json:"Name"` //接口名称
|
||||||
Type string `json:"Type"` //接口类型,比如serial,tcp,udp,http
|
Type string `json:"Type"` //接口类型,比如serial,tcp,udp,http
|
||||||
|
Param *json.RawMessage `json:"Param"`
|
||||||
}{}
|
}{}
|
||||||
|
|
||||||
err := json.Unmarshal(bodyBuf[:n],interfaceInfo)
|
err := json.Unmarshal(bodyBuf[:n],&interfaceInfo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("interfaceInfo json unMarshall err,",err)
|
fmt.Println("interfaceInfo json unMarshall err,",err)
|
||||||
|
|
||||||
aParam.Code = "1"
|
aParam.Code = "1"
|
||||||
aParam.Message = "json unMarshall err"
|
aParam.Message = "json unMarshall err"
|
||||||
|
|
||||||
sJson,_ := json.Marshal(aParam)
|
sJson,_ := json.Marshal(aParam)
|
||||||
context.String(http.StatusOK,string(sJson))
|
context.String(http.StatusOK,string(sJson))
|
||||||
return
|
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("param %+v\n",t)
|
||||||
|
// device.CommInterfaceList.AddCommInterface(t.Name,t.Type,t.Param)
|
||||||
|
//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{
|
switch interfaceInfo.Type{
|
||||||
case "serial":
|
case "serial":
|
||||||
|
serial := &device.SerialInterfaceParam{}
|
||||||
|
err := json.Unmarshal(msg,serial)
|
||||||
|
if err != nil{
|
||||||
|
log.Println("CommunicationSerialInterface json unMarshall err,",err)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
log.Printf("type %+v\n",serial)
|
||||||
|
//device.CommInterfaceList.AddCommInterface(serial.Name,serial.Type,serial.Param)
|
||||||
case "tcp":
|
case "tcp":
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("type %+v\n",interfaceInfo)
|
//device.WriteCommInterfaceListToJson()
|
||||||
|
|
||||||
//device.CommInterfaceList.AddCommInterface(interfaceInfo.Name,interfaceInfo.Type,interfaceInfo.Param)
|
|
||||||
|
|
||||||
device.WriteCommInterfaceListToJson()
|
|
||||||
|
|
||||||
aParam.Code = "0"
|
aParam.Code = "0"
|
||||||
|
aParam.Message = ""
|
||||||
aParam.Data = ""
|
aParam.Data = ""
|
||||||
|
|
||||||
sJson,_ := json.Marshal(aParam)
|
sJson,_ := json.Marshal(aParam)
|
||||||
|
@ -520,17 +541,209 @@ func apiAddCommInterface(context *gin.Context){
|
||||||
|
|
||||||
func apiGetCommInterface(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"` //接口参数
|
||||||
|
}
|
||||||
|
|
||||||
|
type CommunicationInterfaceManageTemplate struct{
|
||||||
|
InterfaceCnt int
|
||||||
|
InterfaceMap []CommunicationInterfaceTemplate
|
||||||
|
}
|
||||||
|
|
||||||
aParam := &struct{
|
aParam := &struct{
|
||||||
Code string
|
Code string
|
||||||
Message string
|
Message string
|
||||||
Data device.CommInterfaceListTemplate
|
Data CommunicationInterfaceManageTemplate
|
||||||
}{}
|
}{}
|
||||||
|
|
||||||
|
CommunicationInterfaceManage := CommunicationInterfaceManageTemplate{
|
||||||
|
InterfaceCnt: 0,
|
||||||
|
InterfaceMap: make([]CommunicationInterfaceTemplate,0),
|
||||||
|
}
|
||||||
|
|
||||||
aParam.Code = "0"
|
aParam.Code = "0"
|
||||||
aParam.Message = ""
|
aParam.Message = ""
|
||||||
aParam.Data = *device.CommInterfaceList
|
for _,v := range device.CommunicationSerialInterfaceList.SerialInterfaceMap{
|
||||||
|
|
||||||
|
CommunicationInterface := CommunicationInterfaceTemplate{
|
||||||
|
Name: v.Name,
|
||||||
|
Type: v.Type,
|
||||||
|
Param: v.Param,
|
||||||
|
}
|
||||||
|
CommunicationInterfaceManage.InterfaceCnt++
|
||||||
|
CommunicationInterfaceManage.InterfaceMap = append(CommunicationInterfaceManage.InterfaceMap,
|
||||||
|
CommunicationInterface)
|
||||||
|
}
|
||||||
|
aParam.Data = CommunicationInterfaceManage
|
||||||
|
|
||||||
sJson, _ := json.Marshal(aParam)
|
sJson, _ := json.Marshal(aParam)
|
||||||
|
|
||||||
context.String(http.StatusOK, string(sJson))
|
context.String(http.StatusOK, string(sJson))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func apiAddCommSerialInterface(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{
|
||||||
|
Name string `json:"Name"` //接口名称
|
||||||
|
Type string `json:"Type"` //接口类型,比如serial,tcp,udp,http
|
||||||
|
Param device.SerialInterfaceParam `json:"Param"`
|
||||||
|
}{}
|
||||||
|
|
||||||
|
err := json.Unmarshal(bodyBuf[:n],&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))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
SerialInterface := device.CommunicationSerialInterface{
|
||||||
|
Param:interfaceInfo.Param,
|
||||||
|
CommunicationTemplate:device.CommunicationTemplate{
|
||||||
|
Name:interfaceInfo.Name,
|
||||||
|
Type:interfaceInfo.Type,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
device.CommunicationSerialInterfaceList.SerialInterfaceMap = append(device.CommunicationSerialInterfaceList.SerialInterfaceMap,
|
||||||
|
SerialInterface)
|
||||||
|
device.WriteCommSerialInterfaceListToJson()
|
||||||
|
|
||||||
|
aParam.Code = "0"
|
||||||
|
aParam.Message = ""
|
||||||
|
aParam.Data = ""
|
||||||
|
|
||||||
|
sJson,_ := json.Marshal(aParam)
|
||||||
|
context.String(http.StatusOK,string(sJson))
|
||||||
|
}
|
||||||
|
|
||||||
|
func apiModifyCommSerialInterface(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)
|
||||||
|
|
||||||
|
interfaceInfo := struct{
|
||||||
|
Name string `json:"Name"` //接口名称
|
||||||
|
Type string `json:"Type"` //接口类型,比如serial,tcp,udp,http
|
||||||
|
Param device.SerialInterfaceParam `json:"Param"`
|
||||||
|
}{}
|
||||||
|
|
||||||
|
err := json.Unmarshal(bodyBuf[:n],&interfaceInfo)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("CommSerialInterface json unMarshall err,",err)
|
||||||
|
|
||||||
|
aParam.Code = "1"
|
||||||
|
aParam.Message = "json unMarshall err"
|
||||||
|
sJson,_ := json.Marshal(aParam)
|
||||||
|
context.String(http.StatusOK,string(sJson))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for k,v := range device.CommunicationSerialInterfaceList.SerialInterfaceMap{
|
||||||
|
//判断名称是否一致
|
||||||
|
if v.Name == interfaceInfo.Name{
|
||||||
|
device.CommunicationSerialInterfaceList.SerialInterfaceMap[k].Type = interfaceInfo.Type
|
||||||
|
device.CommunicationSerialInterfaceList.SerialInterfaceMap[k].Param = interfaceInfo.Param
|
||||||
|
device.WriteCommSerialInterfaceListToJson()
|
||||||
|
|
||||||
|
aParam.Code = "0"
|
||||||
|
aParam.Message = ""
|
||||||
|
aParam.Data = ""
|
||||||
|
sJson,_ := json.Marshal(aParam)
|
||||||
|
context.String(http.StatusOK,string(sJson))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
aParam.Code = "1"
|
||||||
|
aParam.Message = "addr is not exist"
|
||||||
|
aParam.Data = ""
|
||||||
|
|
||||||
|
sJson,_ := json.Marshal(aParam)
|
||||||
|
context.String(http.StatusOK,string(sJson))
|
||||||
|
}
|
||||||
|
|
||||||
|
func apiDeleteCommSerialInterface(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)
|
||||||
|
|
||||||
|
interfaceInfo := struct{
|
||||||
|
Name string `json:"Name"` //接口名称
|
||||||
|
Type string `json:"Type"` //接口类型,比如serial,tcp,udp,http
|
||||||
|
}{}
|
||||||
|
|
||||||
|
err := json.Unmarshal(bodyBuf[:n],&interfaceInfo)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("CommSerialInterface json unMarshall err,",err)
|
||||||
|
|
||||||
|
aParam.Code = "1"
|
||||||
|
aParam.Message = "json unMarshall err"
|
||||||
|
sJson,_ := json.Marshal(aParam)
|
||||||
|
context.String(http.StatusOK,string(sJson))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for k,v := range device.CommunicationSerialInterfaceList.SerialInterfaceMap{
|
||||||
|
//判断名称是否一致
|
||||||
|
if v.Name == interfaceInfo.Name{
|
||||||
|
|
||||||
|
device.CommunicationSerialInterfaceList.SerialInterfaceMap = append(device.CommunicationSerialInterfaceList.SerialInterfaceMap[:k],
|
||||||
|
device.CommunicationSerialInterfaceList.SerialInterfaceMap[k+1:]...)
|
||||||
|
device.WriteCommSerialInterfaceListToJson()
|
||||||
|
|
||||||
|
aParam.Code = "0"
|
||||||
|
aParam.Message = ""
|
||||||
|
aParam.Data = ""
|
||||||
|
sJson,_ := json.Marshal(aParam)
|
||||||
|
context.String(http.StatusOK,string(sJson))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
aParam.Code = "1"
|
||||||
|
aParam.Message = "addr is not exist"
|
||||||
|
aParam.Data = ""
|
||||||
|
|
||||||
|
sJson,_ := json.Marshal(aParam)
|
||||||
|
context.String(http.StatusOK,string(sJson))
|
||||||
|
}
|
||||||
|
|
|
@ -119,6 +119,15 @@ func RouterWeb() http.Handler {
|
||||||
|
|
||||||
//增加通信接口
|
//增加通信接口
|
||||||
deviceRouter.POST("/commInterface",apiAddCommInterface)
|
deviceRouter.POST("/commInterface",apiAddCommInterface)
|
||||||
|
|
||||||
|
//增加串口通信接口
|
||||||
|
deviceRouter.POST("/commSerialInterface",apiAddCommSerialInterface)
|
||||||
|
|
||||||
|
//修改串口通信接口
|
||||||
|
deviceRouter.PUT("/commSerialInterface",apiModifyCommSerialInterface)
|
||||||
|
|
||||||
|
//删除串口通信接口
|
||||||
|
deviceRouter.DELETE("/commSerialInterface",apiDeleteCommSerialInterface)
|
||||||
}
|
}
|
||||||
|
|
||||||
remoteRouter := router.Group("/api/v1/remote")
|
remoteRouter := router.Group("/api/v1/remote")
|
||||||
|
|
BIN
plugin/wdt200.so
BIN
plugin/wdt200.so
Binary file not shown.
Binary file not shown.
|
@ -1 +0,0 @@
|
||||||
{"InterfaceCnt":0,"InterfaceMap":[]}
|
|
|
@ -0,0 +1 @@
|
||||||
|
{"SerialInterfaceMap":[{"Name":"1","Type":"serial","Param":{"Name":"/dev/ttyUSB2","BaudRate":"9600","DataBits":"8","StopBits":"1","Parity":"N","Timeout":"1000","Interval":"500"}}]}
|
Loading…
Reference in New Issue