From cba2d4a4d260fcf908c6def794ec8cc703f614f8 Mon Sep 17 00:00:00 2001 From: pengwang <969753572@qq.com> Date: Fri, 21 May 2021 12:01:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=EF=BC=9A1=E3=80=81comm?= =?UTF-8?q?=E9=80=9A=E4=BF=A1=E6=8E=A5=E5=8F=A3=E5=A2=9E=E5=8A=A0IoOut=202?= =?UTF-8?q?=E3=80=81httpApi=E5=A2=9E=E5=8A=A0IoOut=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=20=E4=BF=AE=E6=94=B9=EF=BC=9A1=E3=80=81commM?= =?UTF-8?q?anage=E4=B8=AD=E5=AF=B9=E9=80=9A=E4=BF=A1=E6=8A=A5=E6=96=87?= =?UTF-8?q?=E4=B8=AD=E5=AF=B9=E8=BF=9E=E7=BB=AD=E5=B8=A7=E7=9A=84=E5=88=A4?= =?UTF-8?q?=E6=96=AD=E5=81=9A=E4=BA=86=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- device/commInterface.go | 8 +++ device/commIoOut.go | 140 +++++++++++++++++++++++++++++++++++++ device/commManage.go | 25 +++---- device/deviceNode.go | 9 +-- httpServer/routerDevice.go | 72 ++++++++++++++++++- 5 files changed, 234 insertions(+), 20 deletions(-) create mode 100644 device/commIoOut.go diff --git a/device/commInterface.go b/device/commInterface.go index b717b83..4ec8695 100644 --- a/device/commInterface.go +++ b/device/commInterface.go @@ -30,6 +30,14 @@ func CommInterfaceInit() { CommunicationInterfaceMap = append(CommunicationInterfaceMap, v) } } + + //获取开关量输出通信接口参数 + if ReadCommIoOutInterfaceListFromJson() == true { + for _, v := range CommunicationIoOutMap { + CommunicationInterfaceMap = append(CommunicationInterfaceMap, v) + } + } + for _, v := range CommunicationInterfaceMap { setting.Logger.Debugf("commName %v,", v.GetName()) v.Open() diff --git a/device/commIoOut.go b/device/commIoOut.go new file mode 100644 index 0000000..dddf1b6 --- /dev/null +++ b/device/commIoOut.go @@ -0,0 +1,140 @@ +package device + +import ( + "encoding/json" + "goAdapter/setting" + "log" + "os" + "path/filepath" +) + +type IoOutInterfaceParam struct { + Name string `json:"Name"` + FD *os.File `json:"-"` +} + +type CommunicationIoOutTemplate struct { + Name string `json:"Name"` //接口名称 + Type string `json:"Type"` //接口类型,比如serial,IoOut,udp,http + Param IoOutInterfaceParam `json:"Param"` //接口参数 +} + +var CommunicationIoOutMap = make([]*CommunicationIoOutTemplate, 0) + +func (c *CommunicationIoOutTemplate) Open() bool { + + fd, err := os.OpenFile(c.Param.Name, os.O_RDWR, 0666) + if err != nil { + setting.Logger.Errorf("IoOut open err,%v", err) + return false + } + c.Param.FD = fd + + return true +} + +func (c *CommunicationIoOutTemplate) Close() bool { + + if c.Param.FD != nil { + err := c.Param.FD.Close() + if err != nil { + setting.Logger.Errorf("IoOut close err,%v", err) + return false + } + } + + return true +} + +func (c *CommunicationIoOutTemplate) WriteData(data []byte) int { + + if c.Param.FD != nil { + //setting.Logger.Debugf("IoOut write %v", data) + if len(data) > 0 { + _, err := c.Param.FD.Write(data) + if err != nil { + setting.Logger.Errorf("IoOut write err,%v", err) + } + } + return 0 + } + return 0 +} + +func (c *CommunicationIoOutTemplate) ReadData(data []byte) int { + + return 0 +} + +func (c *CommunicationIoOutTemplate) GetName() string { + return c.Name +} + +func (c *CommunicationIoOutTemplate) GetTimeOut() string { + return "" +} + +func (c *CommunicationIoOutTemplate) GetInterval() string { + return "" +} + +func ReadCommIoOutInterfaceListFromJson() bool { + + exeCurDir, _ := filepath.Abs(filepath.Dir(os.Args[0])) + fileDir := exeCurDir + "/selfpara/commIoOutInterface.json" + + if fileExist(fileDir) == true { + fp, err := os.OpenFile(fileDir, os.O_RDONLY, 0777) + if err != nil { + log.Println("open commIoOutInterface.json err", err) + return false + } + defer func(fp *os.File) { + err = fp.Close() + if err != nil { + + } + }(fp) + + data := make([]byte, 20480) + dataCnt, err := fp.Read(data) + + err = json.Unmarshal(data[:dataCnt], &CommunicationIoOutMap) + if err != nil { + log.Println("commIoOutInterface unmarshal err", err) + return false + } + return true + } else { + log.Println("commIoOutInterface.json is not exist") + + return false + } +} + +func WriteCommIoOutInterfaceListToJson() { + + exeCurDir, _ := filepath.Abs(filepath.Dir(os.Args[0])) + + fileDir := exeCurDir + "/selfpara/commIoOutInterface.json" + + fp, err := os.OpenFile(fileDir, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0777) + if err != nil { + log.Println("open commIoOutInterface.json err", err) + return + } + defer func(fp *os.File) { + err = fp.Close() + if err != nil { + + } + }(fp) + + sJson, _ := json.Marshal(CommunicationIoOutMap) + + _, err = fp.Write(sJson) + if err != nil { + log.Println("write commIoOutInterface.json err", err) + } + setting.Logger.Infof("write commIoOutInterface.json sucess") +} diff --git a/device/commManage.go b/device/commManage.go index 5ead83e..a16a98b 100644 --- a/device/commManage.go +++ b/device/commManage.go @@ -128,13 +128,13 @@ func (c *CommunicationManageTemplate) CommunicationStateMachine(cmd Communicatio if cmd.FunName == "GetDeviceRealVariables" { txBuf, ok, con = v.GenerateGetRealVariables(v.Addr, step) if ok == false { - setting.Logger.Errorf("%v:GetRealVariables complete", c.CollInterface.CollInterfaceName) + setting.Logger.Errorf("%v:GetRealVariables fail", c.CollInterface.CollInterfaceName) goto LoopCommon } } else { txBuf, ok, con = v.DeviceCustomCmd(v.Addr, cmd.FunName, cmd.FunPara, step) if ok == false { - setting.Logger.Errorf("%v:DeviceCustomCmd complete", c.CollInterface.CollInterfaceName) + setting.Logger.Errorf("%v:DeviceCustomCmd fail", c.CollInterface.CollInterfaceName) goto LoopCommon } } @@ -224,16 +224,6 @@ func (c *CommunicationManageTemplate) CommunicationStateMachine(cmd Communicatio c.CollInterface.CommMessage = c.CollInterface.CommMessage[1:] c.CollInterface.CommMessage = append(c.CollInterface.CommMessage, CommunicationMessage) } - if len(c.CommonRequestChan) > 0 { - //通信帧延时 - time.Sleep(time.Duration(interval) * time.Millisecond) - } else { - //是否后续有通信帧 - if con == true { - //通信帧延时 - time.Sleep(time.Duration(interval) * time.Millisecond) - } - } //防止Chan阻塞 if len(c.CollInterface.PropertyReportChan) >= 100 { @@ -268,8 +258,15 @@ func (c *CommunicationManageTemplate) CommunicationStateMachine(cmd Communicatio } } } - //} LoopCommonStep: + //是否后续有通信帧 + if con == false { + setting.Logger.Errorf("%v:comm complete", c.CollInterface.CollInterfaceName) + goto LoopCommon + } else { + //通信帧延时 + time.Sleep(time.Duration(interval) * time.Millisecond) + } } LoopCommon: } @@ -309,7 +306,7 @@ func (c *CommunicationManageTemplate) CommunicationManageDel() { select { case cmd := <-c.CommonRequestChan: { - setting.Logger.Debugf("%v:,commChanLen %v\n", c.CollInterface.CollInterfaceName, len(c.CommonRequestChan)) + setting.Logger.Debugf("%v:commChanLen %v\n", c.CollInterface.CollInterfaceName, len(c.CommonRequestChan)) c.CommunicationStateMachine(cmd) GetDeviceOnline() diff --git a/device/deviceNode.go b/device/deviceNode.go index 938140a..2c4c47e 100644 --- a/device/deviceNode.go +++ b/device/deviceNode.go @@ -2,14 +2,15 @@ package device import ( "bytes" - "github.com/yuin/gluamapper" - lua "github.com/yuin/gopher-lua" "goAdapter/setting" - "layeh.com/gopher-luar" "runtime" "strconv" "sync" "time" + + "github.com/yuin/gluamapper" + lua "github.com/yuin/gopher-lua" + luar "layeh.com/gopher-luar" ) var MaxDeviceNodeCnt int = 50 @@ -145,7 +146,7 @@ func (d *DeviceNodeTemplate) GenerateGetRealVariables(sAddr string, step int) ([ nBytes = append(nBytes, *v) } } else { - ok = false + ok = true } lock.Unlock() return nBytes, ok, con diff --git a/httpServer/routerDevice.go b/httpServer/routerDevice.go index 311f36e..841f1eb 100644 --- a/httpServer/routerDevice.go +++ b/httpServer/routerDevice.go @@ -789,7 +789,7 @@ func apiAddCommInterface(context *gin.Context) { switch interfaceInfo.Type { case "LocalSerial": serial := device.SerialInterfaceParam{} - err := json.Unmarshal(Param, &serial) + err = json.Unmarshal(Param, &serial) if err != nil { setting.Logger.Errorf("CommunicationSerialInterface json unMarshall err,", err) break @@ -804,7 +804,7 @@ func apiAddCommInterface(context *gin.Context) { device.WriteCommSerialInterfaceListToJson() case "TcpClient": TcpClient := device.TcpClientInterfaceParam{} - err := json.Unmarshal(Param, &TcpClient) + err = json.Unmarshal(Param, &TcpClient) if err != nil { setting.Logger.Errorf("CommunicationTcpClientInterface json unMarshall err,%v", err) break @@ -818,6 +818,22 @@ func apiAddCommInterface(context *gin.Context) { device.CommunicationTcpClientMap = append(device.CommunicationTcpClientMap, TcpClientInterface) device.WriteCommTcpClientInterfaceListToJson() + case "IoOut": + IoOut := device.IoOutInterfaceParam{} + err = json.Unmarshal(Param, &IoOut) + if err != nil { + setting.Logger.Errorf("CommunicationIoOutInterface json unMarshall err,", err) + break + } + setting.Logger.Debugf("type %+v\n", IoOut) + IoOutInterface := &device.CommunicationIoOutTemplate{ + Param: IoOut, + Name: interfaceInfo.Name, + Type: interfaceInfo.Type, + } + device.CommunicationIoOutMap = append(device.CommunicationIoOutMap, IoOutInterface) + device.WriteCommIoOutInterfaceListToJson() + } aParam.Code = "0" @@ -909,6 +925,33 @@ func apiModifyCommInterface(context *gin.Context) { device.CommunicationTcpClientMap[k] = TcpClientInterface device.WriteCommTcpClientInterfaceListToJson() + aParam.Code = "0" + aParam.Message = "" + aParam.Data = "" + sJson, _ := json.Marshal(aParam) + context.String(http.StatusOK, string(sJson)) + return + } + } + case "IoOut": + IoOut := device.IoOutInterfaceParam{} + err := json.Unmarshal(Param, &IoOut) + if err != nil { + setting.Logger.Errorf("CommunicationIoOutInterface json unMarshall err,%v", err) + break + } + setting.Logger.Debugf("type %+v\n", IoOut) + IoOutInterface := &device.CommunicationIoOutTemplate{ + Param: IoOut, + Name: interfaceInfo.Name, + Type: interfaceInfo.Type, + } + + for k, v := range device.CommunicationIoOutMap { + if v.Name == IoOutInterface.Name { + device.CommunicationIoOutMap[k] = IoOutInterface + device.WriteCommIoOutInterfaceListToJson() + aParam.Code = "0" aParam.Message = "" aParam.Data = "" @@ -969,6 +1012,20 @@ func apiDeleteCommInterface(context *gin.Context) { } } + for k, v := range device.CommunicationIoOutMap { + if v.Name == cName { + device.CommunicationIoOutMap = append(device.CommunicationIoOutMap[:k], device.CommunicationIoOutMap[k+1:]...) + device.WriteCommIoOutInterfaceListToJson() + + aParam.Code = "0" + aParam.Message = "" + aParam.Data = "" + sJson, _ := json.Marshal(aParam) + context.String(http.StatusOK, string(sJson)) + return + } + } + aParam.Code = "1" aParam.Message = "commInterface is not exist" aParam.Data = "" @@ -1024,6 +1081,17 @@ func apiGetCommInterface(context *gin.Context) { CommunicationInterfaceManage.InterfaceMap = append(CommunicationInterfaceManage.InterfaceMap, CommunicationInterface) } + + for _, v := range device.CommunicationIoOutMap { + 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)