增加plugin

This commit is contained in:
pengwang 2020-07-07 18:08:01 +08:00
parent 8b710ae852
commit 82164ea2dd
9 changed files with 277 additions and 86 deletions

View File

@ -84,54 +84,54 @@ func CommunicationManageDel(){
if v == cmd.deviceAddr{
log.Printf("index is %d\n",k)
//--------------组包---------------------------
txBuf := DeviceInterfaceMap[cmd.interfaceID].DeviceNodeMap[k].GetDeviceRealVariables()
log.Printf("tx buf is %+v\n",txBuf)
//txBuf := DeviceInterfaceMap[cmd.interfaceID].DeviceNodeMap[k].GetDeviceRealVariables()
//log.Printf("tx buf is %+v\n",txBuf)
//---------------发送-------------------------
setting.SerialInterface.SerialPort[cmd.interfaceID].Write(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(timeOut)*time.Millisecond)
for {
select{
//是否正确收到数据包
case <-DeviceInterfaceMap[cmd.interfaceID].DeviceNodeMap[k].ProcessRx(rxChan,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:
//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(timeOut)*time.Millisecond)
//for {
// select{
// //是否正确收到数据包
// case <-DeviceInterfaceMap[cmd.interfaceID].DeviceNodeMap[k].ProcessRx(rxChan,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:
}
}
}

View File

@ -7,29 +7,8 @@ type DeviceNodeTypeTemplate struct{
TemplateMessage string `json:"templateMessage"` //备注信息
}
//变量标签模版
type VariableTemplate struct{
Index int `json:"index"` //变量偏移量
Name string `json:"name"` //变量名
Lable string `json:"lable"` //变量标签
Value interface{} `json:"value"` //变量值
TimeStamp string `json:"timestamp"` //变量时间戳
Type string `json:"type"` //变量类型
}
//设备模板
type DeviceNodeTemplate struct{
Index int `json:"Index"` //设备偏移量
Addr string `json:"Addr"` //设备地址
Type string `json:"Type"` //设备类型
LastCommRTC string `json:"LastCommRTC"` //最后一次通信时间戳
CommTotalCnt int `json:"CommTotalCnt"` //通信总次数
CommSuccessCnt int `json:"CommSuccessCnt"` //通信成功次数
CommStatus string `json:"CommStatus"` //通信状态
VariableMap []VariableTemplate `json:"-"` //变量列表
}
type DeviceNodeInterface interface {
//解析接收到的数据

View File

@ -6,8 +6,30 @@ import (
"log"
"os"
"path/filepath"
"plugin"
)
//变量标签模版
type VariableTemplate struct{
Index int `json:"index"` //变量偏移量
Name string `json:"name"` //变量名
Lable string `json:"lable"` //变量标签
Value interface{} `json:"value"` //变量值
TimeStamp string `json:"timestamp"` //变量时间戳
Type string `json:"type"` //变量类型
}
//设备模板
type DeviceNodeTemplate struct{
Index int `json:"Index"` //设备偏移量
Addr string `json:"Addr"` //设备地址
Type string `json:"Type"` //设备类型
LastCommRTC string `json:"LastCommRTC"` //最后一次通信时间戳
CommTotalCnt int `json:"CommTotalCnt"` //通信总次数
CommSuccessCnt int `json:"CommSuccessCnt"` //通信成功次数
CommStatus string `json:"CommStatus"` //通信状态
VariableMap []VariableTemplate `json:"-"` //变量列表
}
//接口模板
type DeviceInterfaceTemplate struct{
@ -15,7 +37,7 @@ type DeviceInterfaceTemplate struct{
PollPeriod int `json:"PollPeriod"` //采集周期
OfflinePeriod int `json:"OfflinePeriod"` //离线超时周期
DeviceNodeCnt int `json:"DeviceNodeCnt"` //设备数量
DeviceNodeMap []DeviceNodeInterface `json:"DeviceNodeMap"` //节点表
DeviceNodeMap []*DeviceNodeTemplate `json:"DeviceNodeMap"` //节点表
DeviceNodeAddrMap []string `json:"DeviceAddrMap"` //节点地址
DeviceNodeTypeMap []string `json:"DeviceTypeMap"` //节点类型
}
@ -58,6 +80,7 @@ const (
//var DeviceNodeTypeMap [MaxDeviceNodeTypeCnt]DeviceNodeTypeTemplate
var DeviceInterfaceMap [MaxDeviceInterfaceManage]*DeviceInterfaceTemplate
var DeviceInterfaceParamMap DeviceInterfaceParamMapTemplate
var wdt200Template *plugin.Plugin
func WriteDeviceInterfaceManageToJson(){
@ -98,6 +121,8 @@ func fileExist(path string) bool {
func ReadDeviceInterfaceManageFromJson() bool{
wdt200Template,_ = plugin.Open("plugin/wdt200.so")
exeCurDir,_ := filepath.Abs(filepath.Dir(os.Args[0]))
fileDir := exeCurDir + "/selfpara/deviceNodeManage.json"
@ -185,7 +210,7 @@ func NewDeviceInterface(interfaceID,pollPeriod,offlinePeriod int,deviceNodeCnt i
PollPeriod : pollPeriod,
OfflinePeriod : offlinePeriod,
DeviceNodeCnt : deviceNodeCnt,
DeviceNodeMap : make([]DeviceNodeInterface,0),
DeviceNodeMap : make([]*DeviceNodeTemplate,0),
DeviceNodeAddrMap : make([]string,0),
DeviceNodeTypeMap : make([]string,0),
}
@ -224,29 +249,52 @@ func (d *DeviceInterfaceTemplate)ModifyDeviceInterface(pollPeriod,offlinePeriod
********************************************************/
func (d *DeviceInterfaceTemplate)NewDeviceNode(dType string,dAddr string){
builder,ok := DeviceTemplateMap[dType]
if !ok{
panic("deviceNodeType is not exist")
}
//builder,ok := DeviceTemplateMap[dType]
//if !ok{
// panic("deviceNodeType is not exist")
//}
//
//index := len(d.DeviceNodeMap)
//node := builder.New(index,dType,dAddr)
//d.DeviceNodeMap = append(d.DeviceNodeMap,node)
node := &DeviceNodeTemplate{}
node.Type = dType
node.Addr = dAddr
node.Index = len(d.DeviceNodeMap)
node.VariableMap = make([]VariableTemplate,0)
NewVariables,_ := wdt200Template.Lookup("NewVariables")
variables := NewVariables.(func() []VariableTemplate)()
log.Printf("variables %+v\n",variables)
//node.VariableMap = append(node.VariableMap,variables)
index := len(d.DeviceNodeMap)
node := builder.New(index,dType,dAddr)
d.DeviceNodeMap = append(d.DeviceNodeMap,node)
}
func (d *DeviceInterfaceTemplate)AddDeviceNode(dType string,dAddr string) (bool,string){
builder,ok := DeviceTemplateMap[dType]
if !ok{
panic("deviceNodeType is not exist")
}
//builder,ok := DeviceTemplateMap[dType]
//if !ok{
// panic("deviceNodeType is not exist")
//}
//
//index := len(d.DeviceNodeMap)
//node := builder.New(index,dType,dAddr)
//d.DeviceNodeMap = append(d.DeviceNodeMap,node)
//d.DeviceNodeAddrMap = append(d.DeviceNodeAddrMap,dAddr)
//d.DeviceNodeTypeMap = append(d.DeviceNodeTypeMap,dType)
//d.DeviceNodeCnt++
node := &DeviceNodeTemplate{}
node.Type = dType
node.Addr = dAddr
node.Index = len(d.DeviceNodeMap)
node.VariableMap = make([]VariableTemplate,0)
NewVariables,_ := wdt200Template.Lookup("NewVariables")
variables := NewVariables.(func() []VariableTemplate)()
log.Printf("variables %+v\n",variables)
index := len(d.DeviceNodeMap)
node := builder.New(index,dType,dAddr)
d.DeviceNodeMap = append(d.DeviceNodeMap,node)
d.DeviceNodeAddrMap = append(d.DeviceNodeAddrMap,dAddr)
d.DeviceNodeTypeMap = append(d.DeviceNodeTypeMap,dType)
d.DeviceNodeCnt++
return true,"add success"

BIN
goAdapter

Binary file not shown.

View File

@ -254,11 +254,11 @@ func apiGetNode(context *gin.Context){
}{}
iID,_ := strconv.Atoi(sID)
for k,v := range device.DeviceInterfaceMap[iID].DeviceNodeAddrMap{
for _,v := range device.DeviceInterfaceMap[iID].DeviceNodeAddrMap{
if v == sAddr{
aParam.Code = "0"
aParam.Message = ""
aParam.Data = device.DeviceInterfaceMap[iID].DeviceNodeMap[k].GetDeviceVariablesValue()
//aParam.Data = device.DeviceInterfaceMap[iID].DeviceNodeMap[k].GetDeviceVariablesValue()
sJson, _ := json.Marshal(aParam)
context.String(http.StatusOK, string(sJson))
return

View File

@ -15,6 +15,7 @@ import (
var (
g errgroup.Group
)
@ -26,6 +27,11 @@ func main() {
log.Println("HRx-WDT300 V0.0.1")
//pluginTest,_ := p.Lookup("PluginTest")
//pluginTest.(func())()
setting.MemoryDataStream = setting.NewDataStreamTemplate("内存使用率")
setting.DiskDataStream = setting.NewDataStreamTemplate("硬盘使用率")
setting.DeviceOnlineDataStream = setting.NewDataStreamTemplate("设备在线率")

159
plugin/wdt200.go Normal file
View File

@ -0,0 +1,159 @@
package main
import (
"encoding/binary"
"log"
"strconv"
"sync"
)
//变量标签模版
type VariableTemplate struct{
Index int `json:"index"` //变量偏移量
Name string `json:"name"` //变量名
Lable string `json:"lable"` //变量标签
Value interface{} `json:"value"` //变量值
TimeStamp string `json:"timestamp"` //变量时间戳
Type string `json:"type"` //变量类型
}
type DeviceNodeTemplate struct{
Variables []VariableTemplate
TemplateName string //模板名称
TemplateType string //模板型号
TemplateID string //模板ID
TemplateMessage string //备注信息
}
type crc struct {
once sync.Once
table []uint16
}
var crcTb crc
// initTable 初始化表
func (c *crc) initTable() {
crcPoly16 := uint16(0xa001)
c.table = make([]uint16, 256)
for i := uint16(0); i < 256; i++ {
crc := uint16(0)
b := i
for j := uint16(0); j < 8; j++ {
if ((crc ^ b) & 0x0001) > 0 {
crc = (crc >> 1) ^ crcPoly16
} else {
crc = crc >> 1
}
b = b >> 1
}
c.table[i] = crc
}
}
func crc16(bs []byte) uint16 {
crcTb.once.Do(crcTb.initTable)
val := uint16(0xFFFF)
for _, v := range bs {
val = (val >> 8) ^ crcTb.table[(val^uint16(v))&0x00FF]
}
return val
}
func AddVariable(vindex int,vname string,vlable string,vtype string) VariableTemplate{
variable := VariableTemplate{}
variable.Index = vindex
variable.Name = vname
variable.Lable = vlable
variable.Type = vtype
return variable
}
func NewVariables() []VariableTemplate{
VariableMap := make([]VariableTemplate,0)
VariableMap = append(VariableMap,AddVariable(0,"Addr","通信地址","string"))
VariableMap = append(VariableMap,AddVariable(1,"DeviceType","设备类型","string"))
VariableMap = append(VariableMap,AddVariable(2,"SoftVer","软件版本","string"))
VariableMap = append(VariableMap,AddVariable(3,"SerialNumber","设备编码","string"))
VariableMap = append(VariableMap,AddVariable(4,"RTC","设备时钟","string"))
VariableMap = append(VariableMap,AddVariable(5,"RoomTemp","室内温度","string"))
VariableMap = append(VariableMap,AddVariable(6,"RoomHumi","室内湿度","string"))
VariableMap = append(VariableMap,AddVariable(7,"AirStatus","风机状态","string"))
VariableMap = append(VariableMap,AddVariable(8,"RelayStatus","阀门状态","string"))
VariableMap = append(VariableMap,AddVariable(9,"CurModeSet","运行模式","string"))
VariableMap = append(VariableMap,AddVariable(10,"SeaconStatus","季节","string"))
VariableMap = append(VariableMap,AddVariable(11,"CurTempStep","设定温度","byte"))
VariableMap = append(VariableMap,AddVariable(12,"LockStatus","锁定状态","string"))
VariableMap = append(VariableMap,AddVariable(13,"EnergySavingStatus","节能状态","string"))
VariableMap = append(VariableMap,AddVariable(14,"RSSI","信号强度","byte"))
VariableMap = append(VariableMap,AddVariable(15,"BLKStatus","背光状态","string"))
VariableMap = append(VariableMap,AddVariable(16,"ErrorCode","故障代码","uint16"))
VariableMap = append(VariableMap,AddVariable(17,"TotalTime","累计时间","uint32"))
VariableMap = append(VariableMap,AddVariable(18,"LowTotalTime","低速累计时间","uint32"))
VariableMap = append(VariableMap,AddVariable(19,"MiddleTotalTime","中速累计时间","uint32"))
VariableMap = append(VariableMap,AddVariable(20,"HighTotalTime","高速累计时间","uint32"))
return VariableMap
}
func GenerateGetRealVariables(sAddr string) []byte{
addr,_ := strconv.Atoi(sAddr)
requestAdu := make([]byte,0)
requestAdu = append(requestAdu,byte(addr))
requestAdu = append(requestAdu,0x03)
requestAdu = append(requestAdu,0x00,0x01)
requestAdu = append(requestAdu,0x00,0x02)
checksum := crc16(requestAdu)
requestAdu = append(requestAdu,byte(checksum),byte(checksum>>8))
return requestAdu
}
func AnalysisRx(sAddr string,rxBuf []byte,rxBufCnt int) bool{
addr,_ := strconv.Atoi(sAddr)
index := 0
for {
if index < rxBufCnt{
if rxBufCnt < 4{
return false
}
crc := crc16(rxBuf[0:len(rxBuf)-2])
expect := binary.LittleEndian.Uint16(rxBuf[len(rxBuf)-2:])
if crc != expect{
return false
}
if rxBuf[0] != byte(addr){
return false
}
if rxBuf[1] != 0x03{
return false
}
if rxBuf[2] != 4{
return false
}
log.Println("processRx ok")
//timeNowStr := time.Now().Format("2006-01-02 15:04:05")
return true
}else{
break
}
index++
}
return false
}

BIN
plugin/wdt200.so Normal file

Binary file not shown.

View File

@ -1 +0,0 @@
{"DeviceInterfaceParam":[{"InterfaceID":0,"PollPeriod":60,"OfflinePeriod":180,"DeviceNodeCnt":1,"DeviceAddrMap":["1"],"DeviceTypeMap":["modbus"]},{"InterfaceID":1,"PollPeriod":60,"OfflinePeriod":180,"DeviceNodeCnt":0,"DeviceAddrMap":[],"DeviceTypeMap":[]}],"DeviceNodeTypeMap":[],"DeviceTemplateMap":{"modbus":{"Index":0,"Addr":"","Type":"","LastCommRTC":"","CommTotalCnt":0,"CommSuccessCnt":0,"CommStatus":"","TemplateName":"","TemplateType":"","TemplateID":"","TemplateMessage":""}}}