修改:增加通信接口框架,通信接口管理串口,网络等

This commit is contained in:
pengwang 2020-07-09 17:19:49 +08:00
parent fdd439d3c0
commit fdaec381d2
6 changed files with 236 additions and 0 deletions

166
device/commInterface.go Normal file
View File

@ -0,0 +1,166 @@
package device
import (
"encoding/json"
"goAdapter/setting"
"log"
"os"
"path/filepath"
)
type CommInterfaceTemplate struct{
Name string `json:"Name"` //接口名称
Type string `json:"Type"` //接口类型,比如serial,tcp,udp,http
Param interface{} `json:"Param"`
Status bool `json:"Status"`
}
type CommInterfaceListTemplate struct{
InterfaceCnt int `json:"InterfaceCnt"`
InterfaceMap []CommInterfaceTemplate
}
var CommInterfaceList *CommInterfaceListTemplate
func NewCommInterfaceList() *CommInterfaceListTemplate{
return &CommInterfaceListTemplate{
InterfaceCnt: 0,
InterfaceMap:make([]CommInterfaceTemplate,0),
}
}
func (c *CommInterfaceListTemplate)NewCommInterface(Name string,Type string,Param interface{}){
comm := CommInterfaceTemplate{
Name:Name,
Type:Type,
Param:Param,
}
c.InterfaceMap = append(c.InterfaceMap,comm)
c.InterfaceCnt++
}
func (c *CommInterfaceListTemplate)AddCommInterface(Name string,Type string,Param interface{}){
comm := CommInterfaceTemplate{
Name:Name,
Type:Type,
Param: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() {
CommInterfaceList = NewCommInterfaceList()
if ReadCommInterfaceListFromJson() == true{
log.Println("read commInterface.json ok")
for _,v := range CommInterfaceList.InterfaceMap{
switch v.Type{
case "serial":
{
param,ok := v.Param.(setting.SerialParamTemplate)
if !ok{
v.Status = false
continue
}
}
case "tcp":
case "http":
}
}
}
}

View File

@ -134,6 +134,7 @@ func DeviceNodeManageInit() {
ReadDeviceNodeTypeMapFromJson()
CommInterfaceInit()
if ReadDeviceInterfaceManageFromJson() == true {
log.Println("read interface json ok")

BIN
goAdapter

Binary file not shown.

View File

@ -462,5 +462,67 @@ func apiGetNodeVariableFromDevice(context *gin.Context){
aParam.Code = "1"
aParam.Message = "node is noexist"
sJson, _ := json.Marshal(aParam)
context.String(http.StatusOK, string(sJson))
}
func apiAddCommInterface(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]))
intefaceInfo := &struct{
Name string `json:"Name"` //接口名称
Type string `json:"Type"` //接口类型,比如serial,tcp,udp,http
Param interface{} `json:"Param"`
}{}
err := json.Unmarshal(bodyBuf[:n],intefaceInfo)
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
}
device.CommInterfaceList.AddCommInterface(intefaceInfo.Name,intefaceInfo.Type,intefaceInfo.Param)
device.WriteCommInterfaceListToJson()
aParam.Code = "0"
aParam.Data = ""
sJson,_ := json.Marshal(aParam)
context.String(http.StatusOK,string(sJson))
}
func apiGetCommInterface(context *gin.Context){
aParam := &struct{
Code string
Message string
Data device.CommInterfaceListTemplate
}{}
aParam.Code = "0"
aParam.Message = ""
aParam.Data = *device.CommInterfaceList
sJson, _ := json.Marshal(aParam)
context.String(http.StatusOK, string(sJson))
}

View File

@ -113,6 +113,12 @@ func RouterWeb() http.Handler {
//获取设备模板
deviceRouter.GET("/template",apiGetTemplate)
//获取通信接口
deviceRouter.GET("/commInterface",apiGetCommInterface)
//增加通信接口
deviceRouter.POST("/commInterface",apiAddCommInterface)
}
remoteRouter := router.Group("/api/v1/remote")

1
selfpara/commInterface.json Executable file
View File

@ -0,0 +1 @@
{"InterfaceCnt":3,"InterfaceMap":[{"Name":"1","Type":"serial","Param":{"BaudRate":"9600","DataBits":"8","Name":"/dev/ttyUSB2","Parity":"N","StopBits":"1","Timeout":"1000"}},{"Name":"2","Type":"serial","Param":{"BaudRate":"9600","DataBits":"8","Name":"/dev/ttyUSB1","Parity":"N","StopBits":"1","Timeout":"1000"}},{"Name":"3","Type":"tcp","Param":{"IP":"192.168.1.1","Port":60000}}]}