优化:优化device模板、plugin等模块
This commit is contained in:
parent
43e769a587
commit
e1a417270b
|
@ -80,58 +80,58 @@ func CommunicationManageDel(){
|
|||
//log.Printf("funName %s\n", cmd.funName)
|
||||
|
||||
|
||||
for k,v := range DeviceInterfaceMap[cmd.interfaceID].DeviceNodeAddrMap{
|
||||
if v == cmd.deviceAddr{
|
||||
for k,v := range DeviceInterfaceMap[cmd.interfaceID].DeviceNodeMap{
|
||||
if v.Addr == 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].GenerateGetRealVariables(v.Addr)
|
||||
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].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:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -158,15 +158,15 @@ func CommunicationManageAddEmergencyTest(){
|
|||
|
||||
func CommunicationManagePoll(){
|
||||
|
||||
//cmd := CommunicationCmd{}
|
||||
//
|
||||
//for i:=0;i<DeviceInterfaceMap[InterFaceID0].DeviceNodeCnt;i++{
|
||||
//
|
||||
// cmd.interfaceID = InterFaceID0
|
||||
// cmd.deviceAddr = DeviceInterfaceMap[InterFaceID0].DeviceNodeAddrMap[i]
|
||||
// cmd.deviceType = DeviceInterfaceMap[InterFaceID0].DeviceNodeTypeMap[i]
|
||||
// cmd.funName = "GetDeviceRealVariables"
|
||||
//
|
||||
// CommunicationManageAdd(cmd)
|
||||
//}
|
||||
cmd := CommunicationCmd{}
|
||||
|
||||
for i:=0;i<DeviceInterfaceMap[InterFaceID0].DeviceNodeCnt;i++{
|
||||
|
||||
cmd.interfaceID = InterFaceID0
|
||||
cmd.deviceAddr = DeviceInterfaceMap[InterFaceID0].DeviceNodeMap[i].Addr
|
||||
cmd.deviceType = DeviceInterfaceMap[InterFaceID0].DeviceNodeMap[i].Type
|
||||
cmd.funName = "GetDeviceRealVariables"
|
||||
|
||||
CommunicationManageAdd(cmd)
|
||||
}
|
||||
}
|
|
@ -21,8 +21,6 @@ const (
|
|||
InterFaceID6 int = 6
|
||||
InterFaceID7 int = 7
|
||||
|
||||
|
||||
|
||||
)
|
||||
|
||||
//通信接口模板
|
||||
|
@ -73,9 +71,11 @@ func WriteDeviceInterfaceManageToJson() {
|
|||
DeviceInterfaceParamMap.DeviceInterfaceParam[k].OfflinePeriod = v.OfflinePeriod
|
||||
DeviceInterfaceParamMap.DeviceInterfaceParam[k].DeviceNodeCnt = v.DeviceNodeCnt
|
||||
|
||||
DeviceInterfaceParamMap.DeviceInterfaceParam[k].DeviceNodeAddrMap = DeviceInterfaceParamMap.DeviceInterfaceParam[k].DeviceNodeAddrMap[0:0]
|
||||
DeviceInterfaceParamMap.DeviceInterfaceParam[k].DeviceNodeTypeMap = DeviceInterfaceParamMap.DeviceInterfaceParam[k].DeviceNodeTypeMap[0:0]
|
||||
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
|
||||
DeviceInterfaceParamMap.DeviceInterfaceParam[k].DeviceNodeAddrMap = append(DeviceInterfaceParamMap.DeviceInterfaceParam[k].DeviceNodeAddrMap,v.DeviceNodeMap[i].Addr)
|
||||
DeviceInterfaceParamMap.DeviceInterfaceParam[k].DeviceNodeTypeMap = append(DeviceInterfaceParamMap.DeviceInterfaceParam[k].DeviceNodeTypeMap,v.DeviceNodeMap[i].Type)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -109,6 +109,12 @@ func ReadDeviceInterfaceManageFromJson() bool {
|
|||
data := make([]byte, 20480)
|
||||
dataCnt, err := fp.Read(data)
|
||||
|
||||
for _,v := range DeviceInterfaceParamMap.DeviceInterfaceParam{
|
||||
|
||||
v.DeviceNodeAddrMap = make([]string,0)
|
||||
v.DeviceNodeTypeMap = make([]string,0)
|
||||
}
|
||||
|
||||
err = json.Unmarshal(data[:dataCnt], &DeviceInterfaceParamMap)
|
||||
if err != nil {
|
||||
log.Println("deviceNodeManage unmarshal err", err)
|
||||
|
@ -126,6 +132,9 @@ func ReadDeviceInterfaceManageFromJson() bool {
|
|||
|
||||
func DeviceNodeManageInit() {
|
||||
|
||||
ReadDeviceNodeTypeMapFromJson()
|
||||
|
||||
|
||||
if ReadDeviceInterfaceManageFromJson() == true {
|
||||
log.Println("read interface json ok")
|
||||
|
||||
|
@ -238,7 +247,7 @@ func (d *DeviceInterfaceTemplate) AddDeviceNode(dType string, dAddr string) (boo
|
|||
node.Index = len(d.DeviceNodeMap)
|
||||
node.VariableMap = make([]api.VariableTemplate, 0)
|
||||
variables := node.NewVariables()
|
||||
log.Printf("variables %+v\n", variables)
|
||||
node.VariableMap = append(node.VariableMap,variables...)
|
||||
|
||||
d.DeviceNodeMap = append(d.DeviceNodeMap, node)
|
||||
d.DeviceNodeCnt++
|
||||
|
|
|
@ -20,23 +20,38 @@ type DeviceNodeTemplate struct {
|
|||
|
||||
func (d *DeviceNodeTemplate) NewVariables() []api.VariableTemplate {
|
||||
|
||||
newVariablesFun, _ := DeviceTemplateMap[0].Lookup("NewVariables")
|
||||
variables := newVariablesFun.(func() []api.VariableTemplate)()
|
||||
|
||||
return variables
|
||||
for k,v := range DeviceNodeTypeMap.DeviceNodeType{
|
||||
if d.Type == v.TemplateType{
|
||||
newVariablesFun, _ := DeviceTypePluginMap[k].Lookup("NewVariables")
|
||||
variables := newVariablesFun.(func() []api.VariableTemplate)()
|
||||
return variables
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *DeviceNodeTemplate) GenerateGetRealVariables(sAddr string) []byte {
|
||||
|
||||
generateGetRealVariablesFun, _ := DeviceTemplateMap[0].Lookup("GenerateGetRealVariables")
|
||||
nBytes := generateGetRealVariablesFun.(func(string) []byte)(sAddr)
|
||||
|
||||
return nBytes
|
||||
for k,v := range DeviceNodeTypeMap.DeviceNodeType {
|
||||
if d.Type == v.TemplateType {
|
||||
generateGetRealVariablesFun, _ := DeviceTypePluginMap[k].Lookup("GenerateGetRealVariables")
|
||||
nBytes := generateGetRealVariablesFun.(func(string) []byte)(sAddr)
|
||||
return nBytes
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *DeviceNodeTemplate)AnalysisRx(sAddr string,rxBuf []byte,rxBufCnt int) bool{
|
||||
func (d *DeviceNodeTemplate) AnalysisRx(sAddr string,variables []api.VariableTemplate,rxBuf []byte,rxBufCnt int) chan bool{
|
||||
|
||||
analysisRxFun, _ := DeviceTemplateMap[0].Lookup("AnalysisRx")
|
||||
status := analysisRxFun.(func(string,[]byte,int) bool)(sAddr,rxBuf,rxBufCnt)
|
||||
status := make(chan bool,1)
|
||||
|
||||
for k,v := range DeviceNodeTypeMap.DeviceNodeType {
|
||||
if d.Type == v.TemplateType {
|
||||
analysisRxFun, _ := DeviceTypePluginMap[k].Lookup("AnalysisRx")
|
||||
status = analysisRxFun.(func(string,[]api.VariableTemplate,[]byte, int) chan bool)(sAddr, variables, rxBuf, rxBufCnt)
|
||||
return status
|
||||
}
|
||||
}
|
||||
return status
|
||||
}
|
|
@ -74,12 +74,19 @@ func ReadDeviceNodeTypeMapFromJson() bool {
|
|||
}
|
||||
//创建设备模版
|
||||
DeviceTypePluginMap = make(map[int]*plugin.Plugin)
|
||||
//log.Printf("plugin %+v\n",DeviceNodeTypeMap)
|
||||
for k,v := range DeviceNodeTypeMap.DeviceNodeType{
|
||||
|
||||
str := "plugin/" + v.TemplateType + ".so"
|
||||
template,_ := plugin.Open(str)
|
||||
log.Printf("pluginStr %s\n",str)
|
||||
log.Printf("plugin %+v\n",DeviceNodeTypeMap)
|
||||
template,pluginerr := plugin.Open(str)
|
||||
if pluginerr!=nil{
|
||||
log.Printf("open %s, %\n",pluginerr)
|
||||
}
|
||||
DeviceTypePluginMap[k] = template
|
||||
}
|
||||
//log.Printf("plugin %+v\n",DeviceTypePluginMap)
|
||||
|
||||
return true
|
||||
} else {
|
||||
|
@ -90,3 +97,4 @@ func ReadDeviceNodeTypeMapFromJson() bool {
|
|||
return false
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -6,7 +6,6 @@ import (
|
|||
"github.com/gin-gonic/gin"
|
||||
"goAdapter/api"
|
||||
"goAdapter/device"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
@ -255,8 +254,8 @@ func apiGetNode(context *gin.Context){
|
|||
}{}
|
||||
|
||||
iID,_ := strconv.Atoi(sID)
|
||||
for k,v := range device.DeviceInterfaceMap[iID].DeviceNodeAddrMap{
|
||||
if v == sAddr{
|
||||
for k,v := range device.DeviceInterfaceMap[iID].DeviceNodeMap{
|
||||
if v.Addr == sAddr{
|
||||
aParam.Code = "0"
|
||||
aParam.Message = ""
|
||||
aParam.Data = device.DeviceInterfaceMap[iID].DeviceNodeMap[k].VariableMap
|
||||
|
@ -332,13 +331,13 @@ func apiAddTemplate(context *gin.Context){
|
|||
n,_ := context.Request.Body.Read(bodyBuf)
|
||||
fmt.Println(string(bodyBuf[:n]))
|
||||
|
||||
interfaceInfo := &struct{
|
||||
typeInfo := &struct{
|
||||
TemplateName string `json:"templateName"` //模板名称
|
||||
TemplateType string `json:"templateType"` //模板型号
|
||||
TemplateMessage string `json:"templateMessage"` //备注信息
|
||||
}{}
|
||||
|
||||
err := json.Unmarshal(bodyBuf[:n],interfaceInfo)
|
||||
err := json.Unmarshal(bodyBuf[:n],typeInfo)
|
||||
if err != nil {
|
||||
fmt.Println("interfaceInfo json unMarshall err,",err)
|
||||
|
||||
|
@ -350,17 +349,17 @@ func apiAddTemplate(context *gin.Context){
|
|||
return
|
||||
}
|
||||
|
||||
index := len(device.DeviceInterfaceParamMap.DeviceNodeTypeMap)
|
||||
index := len(device.DeviceNodeTypeMap.DeviceNodeType)
|
||||
template := device.DeviceNodeTypeTemplate{
|
||||
TemplateName:interfaceInfo.TemplateName,
|
||||
TemplateType:interfaceInfo.TemplateType,
|
||||
TemplateName:typeInfo.TemplateName,
|
||||
TemplateType:typeInfo.TemplateType,
|
||||
TemplateID: index,
|
||||
TemplateMessage:interfaceInfo.TemplateMessage,
|
||||
TemplateMessage:typeInfo.TemplateMessage,
|
||||
}
|
||||
|
||||
device.DeviceInterfaceParamMap.DeviceNodeTypeMap = append(device.DeviceInterfaceParamMap.DeviceNodeTypeMap,template)
|
||||
device.DeviceNodeTypeMap.DeviceNodeType = append(device.DeviceNodeTypeMap.DeviceNodeType,template)
|
||||
|
||||
device.WriteDeviceInterfaceManageToJson()
|
||||
device.WriteDeviceNodeTypeMapToJson()
|
||||
|
||||
aParam.Code = "0"
|
||||
aParam.Data = ""
|
||||
|
@ -377,11 +376,17 @@ func apiGetTemplate(context *gin.Context){
|
|||
Data []device.DeviceNodeTypeTemplate
|
||||
}{}
|
||||
|
||||
log.Printf("%+v\n",device.DeviceInterfaceParamMap.DeviceNodeTypeMap)
|
||||
if len(device.DeviceNodeTypeMap.DeviceNodeType) > 0{
|
||||
aParam.Code = "0"
|
||||
aParam.Message = ""
|
||||
aParam.Data = device.DeviceNodeTypeMap.DeviceNodeType
|
||||
}else{
|
||||
aParam.Code = "1"
|
||||
aParam.Message = "nodeTypeCnt is 0"
|
||||
aParam.Data = device.DeviceNodeTypeMap.DeviceNodeType
|
||||
}
|
||||
|
||||
|
||||
aParam.Code = "0"
|
||||
aParam.Message = ""
|
||||
aParam.Data = device.DeviceInterfaceParamMap.DeviceNodeTypeMap
|
||||
|
||||
sJson, _ := json.Marshal(aParam)
|
||||
|
||||
|
|
|
@ -11,8 +11,6 @@ func RouterWeb() http.Handler {
|
|||
router := gin.Default()
|
||||
//router := gin.New()
|
||||
|
||||
//router.Use(gin.Logger())
|
||||
|
||||
loginRouter := router.Group("/api/v1/system/")
|
||||
{
|
||||
loginRouter.POST("/login",apiLogin)
|
||||
|
@ -125,3 +123,5 @@ func RouterWeb() http.Handler {
|
|||
|
||||
return router
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@ import (
|
|||
"log"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type DeviceNodeTemplate struct{
|
||||
|
@ -111,7 +112,9 @@ func GenerateGetRealVariables(sAddr string) []byte{
|
|||
return requestAdu
|
||||
}
|
||||
|
||||
func AnalysisRx(sAddr string,rxBuf []byte,rxBufCnt int) bool{
|
||||
func AnalysisRx(sAddr string,variables []api.VariableTemplate,rxBuf []byte,rxBufCnt int) chan bool{
|
||||
|
||||
status := make(chan bool,1)
|
||||
|
||||
addr,_ := strconv.Atoi(sAddr)
|
||||
|
||||
|
@ -119,32 +122,33 @@ func AnalysisRx(sAddr string,rxBuf []byte,rxBufCnt int) bool{
|
|||
for {
|
||||
if index < rxBufCnt{
|
||||
if rxBufCnt < 4{
|
||||
return false
|
||||
return status
|
||||
}
|
||||
crc := crc16(rxBuf[0:len(rxBuf)-2])
|
||||
expect := binary.LittleEndian.Uint16(rxBuf[len(rxBuf)-2:])
|
||||
if crc != expect{
|
||||
return false
|
||||
return status
|
||||
}
|
||||
|
||||
if rxBuf[0] != byte(addr){
|
||||
return false
|
||||
return status
|
||||
}
|
||||
if rxBuf[1] != 0x03{
|
||||
return false
|
||||
return status
|
||||
}
|
||||
if rxBuf[2] != 4{
|
||||
return false
|
||||
return status
|
||||
}
|
||||
log.Println("processRx ok")
|
||||
|
||||
//timeNowStr := time.Now().Format("2006-01-02 15:04:05")
|
||||
timeNowStr := time.Now().Format("2006-01-02 15:04:05")
|
||||
|
||||
return true
|
||||
variables[0].TimeStamp = timeNowStr
|
||||
status<-true
|
||||
return status
|
||||
}else{
|
||||
break
|
||||
}
|
||||
index++
|
||||
}
|
||||
return false
|
||||
return status
|
||||
}
|
BIN
plugin/wdt200.so
BIN
plugin/wdt200.so
Binary file not shown.
|
@ -1 +1 @@
|
|||
{"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":"风机盘管控制器"}]}
|
||||
{"DeviceInterfaceParam":[{"InterfaceID":0,"PollPeriod":60,"OfflinePeriod":180,"DeviceNodeCnt":2,"DeviceAddrMap":["1","2"],"DeviceTypeMap":["wdt200","wdt200"]},{"InterfaceID":1,"PollPeriod":60,"OfflinePeriod":180,"DeviceNodeCnt":0,"DeviceAddrMap":null,"DeviceTypeMap":null}]}
|
|
@ -0,0 +1 @@
|
|||
{"DeviceNodeType":[{"templateID":0,"templateName":"风机盘管控制器","templateType":"wdt200","templateMessage":""}]}
|
Loading…
Reference in New Issue