diff --git a/.DS_Store b/.DS_Store index 6ea8959..2933f31 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/device/collectInterface.go b/device/collectInterface.go index 23fb797..3f51c8e 100644 --- a/device/collectInterface.go +++ b/device/collectInterface.go @@ -39,11 +39,11 @@ func WriteCollectInterfaceManageToJson() { exeCurDir, _ := filepath.Abs(filepath.Dir(os.Args[0])) - fileDir := exeCurDir + "/selfpara/deviceNodeManage.json" + fileDir := exeCurDir + "/selfpara/collInterface.json" fp, err := os.OpenFile(fileDir, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0777) if err != nil { - log.Println("open deviceNodeManage.json err", err) + log.Println("open collInterface.json err", err) return } defer fp.Close() @@ -94,9 +94,9 @@ func WriteCollectInterfaceManageToJson() { _, err = fp.Write(sJson) if err != nil { - log.Println("write deviceNodeManage.json err", err) + log.Println("write collInterface.json err", err) } - log.Println("write deviceNodeManage.json sucess") + log.Println("write collInterface.json sucess") } func fileExist(path string) bool { @@ -107,12 +107,12 @@ func fileExist(path string) bool { func ReadCollectInterfaceManageFromJson() bool { exeCurDir, _ := filepath.Abs(filepath.Dir(os.Args[0])) - fileDir := exeCurDir + "/selfpara/deviceNodeManage.json" + fileDir := exeCurDir + "/selfpara/collInterface.json" if fileExist(fileDir) == true { fp, err := os.OpenFile(fileDir, os.O_RDONLY, 0777) if err != nil { - log.Println("open deviceNodeManage.json err", err) + log.Println("open collInterface.json err", err) return false } defer fp.Close() @@ -144,7 +144,7 @@ func ReadCollectInterfaceManageFromJson() bool { err = json.Unmarshal(data[:dataCnt], &CollectInterfaceParamMap) if err != nil { - log.Println("deviceNodeManage unmarshal err", err) + log.Println("collInterface unmarshal err", err) return false } @@ -170,7 +170,7 @@ func ReadCollectInterfaceManageFromJson() bool { return true } else { - log.Println("deviceNodeManage.json is not exist") + log.Println("collInterface.json is not exist") return false } @@ -277,7 +277,7 @@ func (d *CollectInterfaceTemplate) AddDeviceNode(dName string,dType string, dAdd node.Index = len(d.DeviceNodeMap) node.CommTotalCnt = 0 node.CommSuccessCnt = 0 - node.CommStatus = "online" + node.CommStatus = "onLine" node.VariableMap = make([]api.VariableTemplate, 0) variables := node.NewVariables() node.VariableMap = append(node.VariableMap, variables...) diff --git a/device/commInterface.go b/device/commInterface.go index 261022c..b10fa11 100644 --- a/device/commInterface.go +++ b/device/commInterface.go @@ -2,39 +2,39 @@ package device import "log" -type CommunicationInterface interface{ - Open() bool - Close() bool - WriteData(data []byte) int - ReadData(data []byte) int +type CommunicationInterface interface { + Open() bool + Close() bool + WriteData(data []byte) int + ReadData(data []byte) int } -type CommunicationTemplate struct{ - Name string `json:"Name"` //接口名称 - Type string `json:"Type"` //接口类型,比如serial,tcp,udp,http - Status bool `json:"-"` //接口状态 +type CommunicationTemplate struct { + Name string `json:"Name"` //接口名称 + Type string `json:"Type"` //接口类型,比如serial,tcp,udp,http + Status bool `json:"-"` //接口状态 } //通信接口Map -var CommunicationInterfaceMap = make([]CommunicationInterface,0) +var CommunicationInterfaceMap = make([]CommunicationInterface, 0) func CommInterfaceInit() { //获取串口通信接口参数 - if ReadCommSerialInterfaceListFromJson() == false{ + if ReadCommSerialInterfaceListFromJson() == false { - }else{ + } else { log.Println("read CommSerialInterfaceList.json ok") - for _,v := range CommunicationSerialMap{ + for _, v := range CommunicationSerialMap { - CommunicationInterfaceMap = append(CommunicationInterfaceMap,&v) + CommunicationInterfaceMap = append(CommunicationInterfaceMap, &v) } } //打开串口通信 - for _,v := range CommunicationInterfaceMap{ + for _, v := range CommunicationInterfaceMap { v.Open() } -} \ No newline at end of file +} diff --git a/device/commManage.go b/device/commManage.go index a57b88d..29bbc19 100644 --- a/device/commManage.go +++ b/device/commManage.go @@ -6,12 +6,12 @@ import ( ) type CommunicationCmd struct { - InterfaceID int //接口ID - DeviceAddr string //接口下设备地址 - DeviceType string - FunName string - FunIndex int - FunPara interface{} + CollInterfaceName string //采集接口名称 + DeviceAddr string //采集接口下设备地址 + DeviceType string + FunName string + FunIndex int + FunPara interface{} } var ( @@ -51,80 +51,20 @@ func CommunicationManageDel() { log.Println("emergency chan") log.Printf("funName %s\n", cmd.FunName) var status bool = false - for k, v := range CollectInterfaceMap[cmd.InterfaceID].DeviceNodeMap { - if v.Addr == cmd.DeviceAddr { - log.Printf("index is %d\n", k) - //--------------组包--------------------------- - txBuf := CollectInterfaceMap[cmd.InterfaceID].DeviceNodeMap[k].GenerateGetRealVariables(v.Addr) - log.Printf("tx buf is %+v\n", txBuf) - //---------------发送------------------------- - //setting.SerialInterface.SerialPort[cmd.InterfaceID].Write(txBuf) - //---------------等待接收---------------------- - //阻塞读 - rxBuf := make([]byte, 256) - rxTotalBuf := make([]byte, 0) - rxBufCnt := 0 - rxTotalBufCnt := 0 - //timeOut,_ := strconv.Atoi(setting.SerialInterface.SerialParam[cmd.InterfaceID].Timeout) - timer := time.NewTimer(time.Duration(100) * time.Millisecond) - for { - select { - //是否正确收到数据包 - case <-CollectInterfaceMap[cmd.InterfaceID].DeviceNodeMap[k].AnalysisRx(v.Addr, v.VariableMap, rxTotalBuf, rxTotalBufCnt): - { - log.Println("rx ok") - //通信帧延时 - //interval,_ := strconv.Atoi(setting.SerialInterface.SerialParam[cmd.InterfaceID].Interval) - //time.Sleep(time.Duration(interval)*time.Millisecond) - status = true - goto LoopEmerg - } - //是否接收超时 - case <-timer.C: - { - log.Println("rx timeout") - //通信帧延时 - //interval,_ := strconv.Atoi(setting.SerialInterface.SerialParam[cmd.InterfaceID].Interval) - //time.Sleep(time.Duration(interval)*time.Millisecond) - status = false - goto LoopEmerg - } - //继续接收数据 - default: - { - //rxBufCnt,_ = setting.SerialInterface.SerialPort[cmd.InterfaceID].Read(rxBuf) - if rxBufCnt > 0 { - rxTotalBufCnt += rxBufCnt - //追加接收的数据到接收缓冲区 - rxTotalBuf = append(rxTotalBuf, rxBuf[:rxBufCnt]...) - //清除本地接收数据 - rxBufCnt = 0 - log.Printf("rxbuf %+v\n", rxTotalBuf) - } - } - } - } - LoopEmerg: - } - } - emergencyAckChan <- status - } - default: - { - select { - case cmd := <-commonChan: - { - log.Println("common chan") - //log.Printf("funName %s\n", cmd.funName) - - for k, v := range CollectInterfaceMap[cmd.InterfaceID].DeviceNodeMap { + for _, c := range CollectInterfaceMap { + if c.CollInterfaceName == cmd.CollInterfaceName { + for k,v := range c.DeviceNodeMap{ if v.Addr == cmd.DeviceAddr { log.Printf("index is %d\n", k) //--------------组包--------------------------- - txBuf := CollectInterfaceMap[cmd.InterfaceID].DeviceNodeMap[k].GenerateGetRealVariables(v.Addr) + txBuf := v.GenerateGetRealVariables(v.Addr) log.Printf("tx buf is %+v\n", txBuf) //---------------发送------------------------- - //setting.SerialInterface.SerialPort[cmd.InterfaceID].Write(txBuf) + for _,v := range CommunicationSerialMap{ + if v.Name == c.CommInterfaceName{ + v.WriteData(txBuf) + } + } //---------------等待接收---------------------- //阻塞读 rxBuf := make([]byte, 256) @@ -136,13 +76,14 @@ func CommunicationManageDel() { for { select { //是否正确收到数据包 - case <-CollectInterfaceMap[cmd.InterfaceID].DeviceNodeMap[k].AnalysisRx(v.Addr, v.VariableMap, rxTotalBuf, rxTotalBufCnt): + case <-v.AnalysisRx(v.Addr, v.VariableMap, rxTotalBuf, rxTotalBufCnt): { log.Println("rx ok") //通信帧延时 //interval,_ := strconv.Atoi(setting.SerialInterface.SerialParam[cmd.InterfaceID].Interval) //time.Sleep(time.Duration(interval)*time.Millisecond) - goto Loop + status = true + goto LoopEmerg } //是否接收超时 case <-timer.C: @@ -151,12 +92,18 @@ func CommunicationManageDel() { //通信帧延时 //interval,_ := strconv.Atoi(setting.SerialInterface.SerialParam[cmd.InterfaceID].Interval) //time.Sleep(time.Duration(interval)*time.Millisecond) - goto Loop + status = false + goto LoopEmerg } //继续接收数据 default: { //rxBufCnt,_ = setting.SerialInterface.SerialPort[cmd.InterfaceID].Read(rxBuf) + for _,v := range CommunicationSerialMap{ + if v.Name == c.CommInterfaceName{ + rxBufCnt = v.ReadData(rxBuf) + } + } if rxBufCnt > 0 { rxTotalBufCnt += rxBufCnt //追加接收的数据到接收缓冲区 @@ -168,6 +115,75 @@ func CommunicationManageDel() { } } } + LoopEmerg: + } + } + } + } + emergencyAckChan <- status + } + default: + { + select { + case cmd := <-commonChan: + { + log.Println("common chan") + log.Printf("funName %s\n", cmd.FunName) + for _, v := range CollectInterfaceMap { + if v.CollInterfaceName == cmd.CollInterfaceName { + for k, v := range v.DeviceNodeMap { + if v.Addr == cmd.DeviceAddr { + log.Printf("index is %d\n", k) + //--------------组包--------------------------- + txBuf := v.GenerateGetRealVariables(v.Addr) + log.Printf("tx buf is %+v\n", txBuf) + //---------------发送------------------------- + //setting.SerialInterface.SerialPort[cmd.InterfaceID].Write(txBuf) + //---------------等待接收---------------------- + //阻塞读 + rxBuf := make([]byte, 256) + rxTotalBuf := make([]byte, 0) + rxBufCnt := 0 + rxTotalBufCnt := 0 + //timeOut,_ := strconv.Atoi(setting.SerialInterface.SerialParam[cmd.InterfaceID].Timeout) + timer := time.NewTimer(time.Duration(100) * time.Millisecond) + for { + select { + //是否正确收到数据包 + case <-v.AnalysisRx(v.Addr, v.VariableMap, rxTotalBuf, rxTotalBufCnt): + { + log.Println("rx ok") + //通信帧延时 + //interval,_ := strconv.Atoi(setting.SerialInterface.SerialParam[cmd.InterfaceID].Interval) + //time.Sleep(time.Duration(interval)*time.Millisecond) + goto Loop + } + //是否接收超时 + case <-timer.C: + { + log.Println("rx timeout") + //通信帧延时 + //interval,_ := strconv.Atoi(setting.SerialInterface.SerialParam[cmd.InterfaceID].Interval) + //time.Sleep(time.Duration(interval)*time.Millisecond) + goto Loop + } + //继续接收数据 + default: + { + //rxBufCnt,_ = setting.SerialInterface.SerialPort[cmd.InterfaceID].Read(rxBuf) + if rxBufCnt > 0 { + rxTotalBufCnt += rxBufCnt + //追加接收的数据到接收缓冲区 + rxTotalBuf = append(rxTotalBuf, rxBuf[:rxBufCnt]...) + //清除本地接收数据 + rxBufCnt = 0 + log.Printf("rxbuf %+v\n", rxTotalBuf) + } + } + } + } + } + } Loop: } } diff --git a/device/deviceType.go b/device/deviceType.go index 1caeac1..7fd53ef 100644 --- a/device/deviceType.go +++ b/device/deviceType.go @@ -82,7 +82,7 @@ func ReadDeviceNodeTypeMapFromJson() bool { log.Printf("plugin %+v\n",DeviceNodeTypeMap) template,pluginerr := plugin.Open(str) if pluginerr!=nil{ - log.Printf("open %s, %\n",pluginerr) + log.Printf("openPlug %s err\n",pluginerr) } DeviceTypePluginMap[k] = template } diff --git a/goAdapter b/goAdapter index 3293392..a9993b0 100755 Binary files a/goAdapter and b/goAdapter differ diff --git a/main.go b/main.go index 8ffe108..f6aa1f2 100644 --- a/main.go +++ b/main.go @@ -22,10 +22,7 @@ func main() { //记录起始时间 setting.GetTimeStart() - log.Println("HRx-WDT300 V0.0.1") - - //pluginTest,_ := p.Lookup("PluginTest") - //pluginTest.(func())() + log.Println("goteway V0.0.1") setting.MemoryDataStream = setting.NewDataStreamTemplate("内存使用率") setting.DiskDataStream = setting.NewDataStreamTemplate("硬盘使用率") diff --git a/selfpara/.DS_Store b/selfpara/.DS_Store index e2476d3..1ad4565 100644 Binary files a/selfpara/.DS_Store and b/selfpara/.DS_Store differ diff --git a/selfpara/collInterface.json b/selfpara/collInterface.json new file mode 100755 index 0000000..68fb5ec --- /dev/null +++ b/selfpara/collInterface.json @@ -0,0 +1 @@ +{"CollectInterfaceParam":[{"CollInterfaceName":"1","CommInterfaceName":"com1","PollPeriod":60,"OfflinePeriod":3,"DeviceNodeCnt":2}],"DeviceNodeNameMap":["1","2"],"DeviceNodeAddrMap":["1","2"],"DeviceNodeTypeMap":["wdt200","wdt200"]} \ No newline at end of file diff --git a/selfpara/commSerialInterface.json b/selfpara/commSerialInterface.json index 2199672..447f4ca 100755 --- a/selfpara/commSerialInterface.json +++ b/selfpara/commSerialInterface.json @@ -1 +1 @@ -[{"Name":"com1","Type":"LocalSerial","Param":{"Name":"/dev/ttyUSB0","BaudRate":"9600","DataBits":"8","StopBits":"1","Parity":"N","Timeout":"1500","Interval":"500"}}] \ No newline at end of file +[{"Name":"com1","Type":"LocalSerial","Param":{"Name":"/dev/ttyUSB0","BaudRate":"9600","DataBits":"8","StopBits":"1","Parity":"N","Timeout":"1000","Interval":"300"}}] \ No newline at end of file diff --git a/selfpara/deviceNodeManage.json b/selfpara/deviceNodeManage.json deleted file mode 100755 index 8566a51..0000000 --- a/selfpara/deviceNodeManage.json +++ /dev/null @@ -1 +0,0 @@ -{"CollectInterfaceParam":[{"CollInterfaceName":"com1","CommInterfaceName":"com1","PollPeriod":60,"OfflinePeriod":5,"DeviceNodeCnt":3},{"CollInterfaceName":"com2","CommInterfaceName":"com2","PollPeriod":60,"OfflinePeriod":5,"DeviceNodeCnt":0}],"DeviceNodeNameMap":["3","2","1"],"DeviceNodeAddrMap":["3","2","1"],"DeviceNodeTypeMap":["wdt200","wdt200","wdt200"]} \ No newline at end of file diff --git a/selfpara/serialpara.json b/selfpara/serialpara.json deleted file mode 100644 index 24dda83..0000000 --- a/selfpara/serialpara.json +++ /dev/null @@ -1 +0,0 @@ -{"SerialParam":[{"ID":"1","Name":"/dev/ttyS1","BaudRate":"9600","DataBits":"8","StopBits":"1","Parity":"N","Timeout":"3000","Interval":"300"},{"ID":"2","Name":"/dev/ttyS2","BaudRate":"9600","DataBits":"8","StopBits":"1","Parity":"N","Timeout":"3000","Interval":"300"}]} diff --git a/setting/system.go b/setting/system.go index 0103b47..e1fff3e 100644 --- a/setting/system.go +++ b/setting/system.go @@ -40,9 +40,9 @@ var SystemState = SystemStateTemplate{ MemUse :"0", DiskTotal :"0", DiskUse :"0", - Name :"WDT600", + Name :"goteway", SN :"22005260001", - HardVer :"WDT600-V.A", + HardVer :"goteway-V.A", SoftVer :"V0.0.1", SystemRTC :"2020-05-26 12:00:00", RunTime :"0",