修改:优化device部分的代码,将接口和节点分开
This commit is contained in:
parent
4564c82f38
commit
43e769a587
|
@ -7,27 +7,23 @@ import (
|
|||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"plugin"
|
||||
)
|
||||
|
||||
type DeviceNodeTypeTemplate struct {
|
||||
TemplateID int `json:"templateID"` //模板ID
|
||||
TemplateName string `json:"templateName"` //模板名称
|
||||
TemplateType string `json:"templateType"` //模板型号
|
||||
TemplateMessage string `json:"templateMessage"` //备注信息
|
||||
}
|
||||
const (
|
||||
MaxDeviceInterfaceManage int = 2
|
||||
|
||||
//设备模板
|
||||
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 []api.VariableTemplate `json:"-"` //变量列表
|
||||
}
|
||||
InterFaceID0 int = 0
|
||||
InterFaceID1 int = 1
|
||||
InterFaceID2 int = 2
|
||||
InterFaceID3 int = 3
|
||||
InterFaceID4 int = 4
|
||||
InterFaceID5 int = 5
|
||||
InterFaceID6 int = 6
|
||||
InterFaceID7 int = 7
|
||||
|
||||
|
||||
|
||||
)
|
||||
|
||||
//通信接口模板
|
||||
type DeviceInterfaceTemplate struct {
|
||||
|
@ -36,8 +32,6 @@ type DeviceInterfaceTemplate struct {
|
|||
OfflinePeriod int `json:"OfflinePeriod"` //离线超时周期
|
||||
DeviceNodeCnt int `json:"DeviceNodeCnt"` //设备数量
|
||||
DeviceNodeMap []*DeviceNodeTemplate `json:"DeviceNodeMap"` //节点表
|
||||
DeviceNodeAddrMap []string `json:"DeviceAddrMap"` //节点地址
|
||||
DeviceNodeTypeMap []string `json:"DeviceTypeMap"` //节点类型
|
||||
}
|
||||
|
||||
//配置参数
|
||||
|
@ -53,32 +47,12 @@ type DeviceInterfaceParamTemplate struct {
|
|||
//配置参数
|
||||
type DeviceInterfaceParamMapTemplate struct {
|
||||
DeviceInterfaceParam [MaxDeviceInterfaceManage]DeviceInterfaceParamTemplate
|
||||
DeviceNodeTypeMap []DeviceNodeTypeTemplate
|
||||
}
|
||||
|
||||
const (
|
||||
MaxDeviceInterfaceManage int = 2
|
||||
|
||||
InterFaceID0 int = 0
|
||||
InterFaceID1 int = 1
|
||||
InterFaceID2 int = 2
|
||||
InterFaceID3 int = 3
|
||||
InterFaceID4 int = 4
|
||||
InterFaceID5 int = 5
|
||||
InterFaceID6 int = 6
|
||||
InterFaceID7 int = 7
|
||||
|
||||
MaxDeviceNodeCnt int = 50
|
||||
|
||||
//最大设备模板数
|
||||
MaxDeviceNodeTypeCnt int = 10
|
||||
)
|
||||
|
||||
//var DeviceNodeTypeMap [MaxDeviceNodeTypeCnt]DeviceNodeTypeTemplate
|
||||
var DeviceInterfaceMap [MaxDeviceInterfaceManage]*DeviceInterfaceTemplate
|
||||
var DeviceInterfaceParamMap DeviceInterfaceParamMapTemplate
|
||||
var wdt200Template *plugin.Plugin
|
||||
var DeviceTemplateMap map[int]*plugin.Plugin
|
||||
|
||||
|
||||
func WriteDeviceInterfaceManageToJson() {
|
||||
|
||||
|
@ -98,8 +72,11 @@ func WriteDeviceInterfaceManageToJson() {
|
|||
DeviceInterfaceParamMap.DeviceInterfaceParam[k].PollPeriod = v.PollPeriod
|
||||
DeviceInterfaceParamMap.DeviceInterfaceParam[k].OfflinePeriod = v.OfflinePeriod
|
||||
DeviceInterfaceParamMap.DeviceInterfaceParam[k].DeviceNodeCnt = v.DeviceNodeCnt
|
||||
DeviceInterfaceParamMap.DeviceInterfaceParam[k].DeviceNodeAddrMap = v.DeviceNodeAddrMap
|
||||
DeviceInterfaceParamMap.DeviceInterfaceParam[k].DeviceNodeTypeMap = v.DeviceNodeTypeMap
|
||||
|
||||
for i:=0;i<v.DeviceNodeCnt;i++{
|
||||
DeviceInterfaceParamMap.DeviceInterfaceParam[k].DeviceNodeAddrMap[i] = v.DeviceNodeMap[i].Addr
|
||||
DeviceInterfaceParamMap.DeviceInterfaceParam[k].DeviceNodeTypeMap[i] = v.DeviceNodeMap[i].Type
|
||||
}
|
||||
}
|
||||
|
||||
sJson, _ := json.Marshal(DeviceInterfaceParamMap)
|
||||
|
@ -118,11 +95,6 @@ func fileExist(path string) bool {
|
|||
|
||||
func ReadDeviceInterfaceManageFromJson() bool {
|
||||
|
||||
|
||||
DeviceTemplateMap = make(map[int]*plugin.Plugin)
|
||||
|
||||
//wdt200Template, _ = plugin.Open("plugin/wdt200.so")
|
||||
|
||||
exeCurDir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
|
||||
fileDir := exeCurDir + "/selfpara/deviceNodeManage.json"
|
||||
|
||||
|
@ -137,8 +109,6 @@ func ReadDeviceInterfaceManageFromJson() bool {
|
|||
data := make([]byte, 20480)
|
||||
dataCnt, err := fp.Read(data)
|
||||
|
||||
DeviceInterfaceParamMap.DeviceNodeTypeMap = make([]DeviceNodeTypeTemplate, 0)
|
||||
|
||||
err = json.Unmarshal(data[:dataCnt], &DeviceInterfaceParamMap)
|
||||
if err != nil {
|
||||
log.Println("deviceNodeManage unmarshal err", err)
|
||||
|
@ -146,14 +116,6 @@ func ReadDeviceInterfaceManageFromJson() bool {
|
|||
return false
|
||||
}
|
||||
|
||||
//log.Printf("%+v\n", DeviceInterfaceParamMap)
|
||||
for k,v := range DeviceInterfaceParamMap.DeviceNodeTypeMap{
|
||||
|
||||
str := "plugin/" + v.TemplateType + ".so"
|
||||
template,_ := plugin.Open(str)
|
||||
DeviceTemplateMap[k] = template
|
||||
}
|
||||
|
||||
return true
|
||||
} else {
|
||||
log.Println("deviceNodeManage.json is not exist")
|
||||
|
@ -175,9 +137,6 @@ func DeviceNodeManageInit() {
|
|||
v.OfflinePeriod,
|
||||
v.DeviceNodeCnt)
|
||||
|
||||
DeviceInterfaceMap[k].DeviceNodeAddrMap = v.DeviceNodeAddrMap
|
||||
DeviceInterfaceMap[k].DeviceNodeTypeMap = v.DeviceNodeTypeMap
|
||||
|
||||
//创建设备实例
|
||||
for i := 0; i < v.DeviceNodeCnt; i++ {
|
||||
DeviceInterfaceMap[k].NewDeviceNode(
|
||||
|
@ -186,8 +145,6 @@ func DeviceNodeManageInit() {
|
|||
}
|
||||
}
|
||||
} else {
|
||||
//初始化设备模板
|
||||
DeviceInterfaceParamMap.DeviceNodeTypeMap = make([]DeviceNodeTypeTemplate, 0)
|
||||
|
||||
for i := 0; i < MaxDeviceInterfaceManage; i++ {
|
||||
//创建接口实例
|
||||
|
@ -217,8 +174,6 @@ func NewDeviceInterface(interfaceID, pollPeriod, offlinePeriod int, deviceNodeCn
|
|||
OfflinePeriod: offlinePeriod,
|
||||
DeviceNodeCnt: deviceNodeCnt,
|
||||
DeviceNodeMap: make([]*DeviceNodeTemplate, 0),
|
||||
DeviceNodeAddrMap: make([]string, 0),
|
||||
DeviceNodeTypeMap: make([]string, 0),
|
||||
}
|
||||
|
||||
//打开串口
|
||||
|
@ -273,24 +228,10 @@ func (d *DeviceInterfaceTemplate) NewDeviceNode(dType string, dAddr string) {
|
|||
node.VariableMap = append(node.VariableMap,variables...)
|
||||
|
||||
d.DeviceNodeMap = append(d.DeviceNodeMap, node)
|
||||
d.DeviceNodeAddrMap = append(d.DeviceNodeAddrMap, dAddr)
|
||||
d.DeviceNodeTypeMap = append(d.DeviceNodeTypeMap, dType)
|
||||
}
|
||||
|
||||
func (d *DeviceInterfaceTemplate) AddDeviceNode(dType string, dAddr string) (bool, string) {
|
||||
|
||||
//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
|
||||
|
@ -300,8 +241,6 @@ func (d *DeviceInterfaceTemplate) AddDeviceNode(dType string, dAddr string) (boo
|
|||
log.Printf("variables %+v\n", variables)
|
||||
|
||||
d.DeviceNodeMap = append(d.DeviceNodeMap, node)
|
||||
d.DeviceNodeAddrMap = append(d.DeviceNodeAddrMap, dAddr)
|
||||
d.DeviceNodeTypeMap = append(d.DeviceNodeTypeMap, dType)
|
||||
d.DeviceNodeCnt++
|
||||
|
||||
return true, "add success"
|
||||
|
@ -312,12 +251,10 @@ func (d *DeviceInterfaceTemplate) DeleteDeviceNode(dAddr string, dType string) {
|
|||
log.Printf("addr %s\n", dAddr)
|
||||
log.Printf("type %s\n", dType)
|
||||
|
||||
for k, v := range d.DeviceNodeAddrMap {
|
||||
if v == dAddr {
|
||||
for k, v := range d.DeviceNodeMap {
|
||||
if v.Addr == dAddr {
|
||||
d.DeviceNodeMap = d.DeviceNodeMap[k : k+1]
|
||||
d.DeviceNodeMap = append(d.DeviceNodeMap[:k], d.DeviceNodeMap[k+1:]...)
|
||||
d.DeviceNodeAddrMap = append(d.DeviceNodeAddrMap[:k], d.DeviceNodeAddrMap[k+1:]...)
|
||||
d.DeviceNodeTypeMap = append(d.DeviceNodeTypeMap[:k], d.DeviceNodeTypeMap[k+1:]...)
|
||||
d.DeviceNodeCnt--
|
||||
}
|
||||
}
|
||||
|
@ -325,34 +262,11 @@ func (d *DeviceInterfaceTemplate) DeleteDeviceNode(dAddr string, dType string) {
|
|||
|
||||
func (d *DeviceInterfaceTemplate) GetDeviceNode(dAddr string) interface{} {
|
||||
|
||||
for _, v := range d.DeviceNodeAddrMap {
|
||||
if v == dAddr {
|
||||
for _, v := range d.DeviceNodeMap {
|
||||
if v.Addr == dAddr {
|
||||
return v
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *DeviceNodeTemplate) NewVariables() []api.VariableTemplate {
|
||||
|
||||
newVariablesFun, _ := DeviceTemplateMap[0].Lookup("NewVariables")
|
||||
variables := newVariablesFun.(func() []api.VariableTemplate)()
|
||||
|
||||
return variables
|
||||
}
|
||||
|
||||
func (d *DeviceNodeTemplate) GenerateGetRealVariables(sAddr string) []byte {
|
||||
|
||||
generateGetRealVariablesFun, _ := DeviceTemplateMap[0].Lookup("GenerateGetRealVariables")
|
||||
nBytes := generateGetRealVariablesFun.(func(string) []byte)(sAddr)
|
||||
|
||||
return nBytes
|
||||
}
|
||||
|
||||
func (d *DeviceNodeTemplate)AnalysisRx(sAddr string,rxBuf []byte,rxBufCnt int) bool{
|
||||
|
||||
analysisRxFun, _ := DeviceTemplateMap[0].Lookup("AnalysisRx")
|
||||
status := analysisRxFun.(func(string,[]byte,int) bool)(sAddr,rxBuf,rxBufCnt)
|
||||
return status
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
package device
|
||||
|
||||
import (
|
||||
"goAdapter/api"
|
||||
)
|
||||
|
||||
var MaxDeviceNodeCnt int = 50
|
||||
|
||||
//设备模板
|
||||
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 []api.VariableTemplate `json:"-"` //变量列表
|
||||
}
|
||||
|
||||
func (d *DeviceNodeTemplate) NewVariables() []api.VariableTemplate {
|
||||
|
||||
newVariablesFun, _ := DeviceTemplateMap[0].Lookup("NewVariables")
|
||||
variables := newVariablesFun.(func() []api.VariableTemplate)()
|
||||
|
||||
return variables
|
||||
}
|
||||
|
||||
func (d *DeviceNodeTemplate) GenerateGetRealVariables(sAddr string) []byte {
|
||||
|
||||
generateGetRealVariablesFun, _ := DeviceTemplateMap[0].Lookup("GenerateGetRealVariables")
|
||||
nBytes := generateGetRealVariablesFun.(func(string) []byte)(sAddr)
|
||||
|
||||
return nBytes
|
||||
}
|
||||
|
||||
func (d *DeviceNodeTemplate)AnalysisRx(sAddr string,rxBuf []byte,rxBufCnt int) bool{
|
||||
|
||||
analysisRxFun, _ := DeviceTemplateMap[0].Lookup("AnalysisRx")
|
||||
status := analysisRxFun.(func(string,[]byte,int) bool)(sAddr,rxBuf,rxBufCnt)
|
||||
return status
|
||||
}
|
|
@ -0,0 +1,92 @@
|
|||
package device
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"plugin"
|
||||
)
|
||||
|
||||
//最大设备模板数
|
||||
var MaxDeviceNodeTypeCnt int = 10
|
||||
|
||||
type DeviceNodeTypeTemplate struct {
|
||||
TemplateID int `json:"templateID"` //模板ID
|
||||
TemplateName string `json:"templateName"` //模板名称
|
||||
TemplateType string `json:"templateType"` //模板型号
|
||||
TemplateMessage string `json:"templateMessage"` //备注信息
|
||||
}
|
||||
|
||||
//配置参数
|
||||
type DeviceNodeTypeMapStruct struct {
|
||||
DeviceNodeType []DeviceNodeTypeTemplate
|
||||
}
|
||||
|
||||
var DeviceNodeTypeMap DeviceNodeTypeMapStruct
|
||||
var DeviceTypePluginMap map[int]*plugin.Plugin
|
||||
|
||||
func WriteDeviceNodeTypeMapToJson() {
|
||||
|
||||
exeCurDir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
|
||||
|
||||
fileDir := exeCurDir + "/selfpara/deviceNodeType.json"
|
||||
|
||||
fp, err := os.OpenFile(fileDir, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0777)
|
||||
if err != nil {
|
||||
log.Println("open DeviceNodeType.json err", err)
|
||||
return
|
||||
}
|
||||
defer fp.Close()
|
||||
|
||||
sJson, _ := json.Marshal(DeviceNodeTypeMap)
|
||||
|
||||
_, err = fp.Write(sJson)
|
||||
if err != nil {
|
||||
log.Println("write DeviceNodeType.json err", err)
|
||||
}
|
||||
log.Println("write DeviceNodeType.json sucess")
|
||||
}
|
||||
|
||||
func ReadDeviceNodeTypeMapFromJson() bool {
|
||||
|
||||
exeCurDir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
|
||||
fileDir := exeCurDir + "/selfpara/deviceNodeType.json"
|
||||
|
||||
if fileExist(fileDir) == true {
|
||||
fp, err := os.OpenFile(fileDir, os.O_RDONLY, 0777)
|
||||
if err != nil {
|
||||
log.Println("open DeviceNodeType.json err", err)
|
||||
return false
|
||||
}
|
||||
defer fp.Close()
|
||||
|
||||
data := make([]byte, 20480)
|
||||
dataCnt, err := fp.Read(data)
|
||||
|
||||
DeviceNodeTypeMap.DeviceNodeType = make([]DeviceNodeTypeTemplate, 0)
|
||||
|
||||
err = json.Unmarshal(data[:dataCnt], &DeviceNodeTypeMap)
|
||||
if err != nil {
|
||||
log.Println("DeviceNodeType unmarshal err", err)
|
||||
|
||||
return false
|
||||
}
|
||||
//创建设备模版
|
||||
DeviceTypePluginMap = make(map[int]*plugin.Plugin)
|
||||
for k,v := range DeviceNodeTypeMap.DeviceNodeType{
|
||||
|
||||
str := "plugin/" + v.TemplateType + ".so"
|
||||
template,_ := plugin.Open(str)
|
||||
DeviceTypePluginMap[k] = template
|
||||
}
|
||||
|
||||
return true
|
||||
} else {
|
||||
log.Println("DeviceNodeType.json is not exist")
|
||||
//创建设备模版
|
||||
DeviceTypePluginMap = make(map[int]*plugin.Plugin)
|
||||
|
||||
return false
|
||||
}
|
||||
}
|
|
@ -1 +1 @@
|
|||
{"DeviceInterfaceParam":[{"InterfaceID":0,"PollPeriod":60,"OfflinePeriod":180,"DeviceNodeCnt":1,"DeviceAddrMap":["1"],"DeviceTypeMap":["wdt200"]},{"InterfaceID":1,"PollPeriod":60,"OfflinePeriod":180,"DeviceNodeCnt":0,"DeviceAddrMap":[],"DeviceTypeMap":[]}],"DeviceNodeTypeMap":[{"templateID":0,"templateName":"wdt","templateType":"wdt200","templateMessage":"风机盘管控制器"}]}
|
||||
{"DeviceInterfaceParam":[{"InterfaceID":0,"PollPeriod":60,"OfflinePeriod":180,"DeviceNodeCnt":0,"DeviceNodeMap":[],"DeviceAddrMap":[],"DeviceTypeMap":[]},{"InterfaceID":1,"PollPeriod":60,"OfflinePeriod":180,"DeviceNodeCnt":0,"DeviceNodeMap":[],"DeviceAddrMap":[],"DeviceTypeMap":[]}],"DeviceNodeTypeMap":[{"templateID":0,"templateName":"wdt","templateType":"wdt200","templateMessage":"风机盘管控制器"}]}
|
Binary file not shown.
BIN
update/fcu.bin
BIN
update/fcu.bin
Binary file not shown.
Loading…
Reference in New Issue