修改:1、修改产品名称为gotaway 2、修改采集接口存储参数文件为collInterface.json
This commit is contained in:
parent
3189cf64bb
commit
2b0c506b0f
|
@ -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...)
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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:
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
5
main.go
5
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("硬盘使用率")
|
||||
|
|
Binary file not shown.
|
@ -0,0 +1 @@
|
|||
{"CollectInterfaceParam":[{"CollInterfaceName":"1","CommInterfaceName":"com1","PollPeriod":60,"OfflinePeriod":3,"DeviceNodeCnt":2}],"DeviceNodeNameMap":["1","2"],"DeviceNodeAddrMap":["1","2"],"DeviceNodeTypeMap":["wdt200","wdt200"]}
|
|
@ -1 +1 @@
|
|||
[{"Name":"com1","Type":"LocalSerial","Param":{"Name":"/dev/ttyUSB0","BaudRate":"9600","DataBits":"8","StopBits":"1","Parity":"N","Timeout":"1500","Interval":"500"}}]
|
||||
[{"Name":"com1","Type":"LocalSerial","Param":{"Name":"/dev/ttyUSB0","BaudRate":"9600","DataBits":"8","StopBits":"1","Parity":"N","Timeout":"1000","Interval":"300"}}]
|
|
@ -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"]}
|
|
@ -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"}]}
|
|
@ -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",
|
||||
|
|
Loading…
Reference in New Issue