增加:1、comm通信接口增加IoOut 2、httpApi增加IoOut相关接口

修改:1、commManage中对通信报文中对连续帧的判断做了修改
This commit is contained in:
pengwang 2021-05-21 12:01:45 +08:00
parent 12a94dfbb0
commit cba2d4a4d2
5 changed files with 234 additions and 20 deletions

View File

@ -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()

140
device/commIoOut.go Normal file
View File

@ -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")
}

View File

@ -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()

View File

@ -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

View File

@ -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)