修改:1、采集接口中nodeMap由slice改成map

This commit is contained in:
pengwang 2021-09-23 11:37:16 +08:00
parent 8fd3a8f4fe
commit 46d25af10c
6 changed files with 63 additions and 224 deletions

View File

@ -33,7 +33,7 @@ type CollectInterfaceTemplate struct {
OfflinePeriod int `json:"OfflinePeriod"` //离线超时周期
DeviceNodeCnt int `json:"DeviceNodeCnt"` //设备数量
DeviceNodeOnlineCnt int `json:"DeviceNodeOnlineCnt"` //设备在线数量
DeviceNodeMap []*DeviceNodeTemplate `json:"DeviceNodeMap"` //节点表
DeviceNodeMap map[string]*DeviceNodeTemplate `json:"DeviceNodeMap"` //节点表
CollEventBus eventBus.Bus `json:"-"` //事件总线
Cron *cron.Cron `json:"-"` //定时管理
ContextCancelFun context.CancelFunc `json:"-"`
@ -105,11 +105,12 @@ func WriteCollectInterfaceManageToJson() {
ParamTemplate.DeviceNodeAddrMap = make([]string, 0)
ParamTemplate.DeviceNodeTypeMap = make([]string, 0)
for i := 0; i < v.DeviceNodeCnt; i++ {
ParamTemplate.DeviceNodeNameMap = append(ParamTemplate.DeviceNodeNameMap, v.DeviceNodeMap[i].Name)
ParamTemplate.DeviceNodeAddrMap = append(ParamTemplate.DeviceNodeAddrMap, v.DeviceNodeMap[i].Addr)
ParamTemplate.DeviceNodeTypeMap = append(ParamTemplate.DeviceNodeTypeMap, v.DeviceNodeMap[i].Type)
for _, d := range v.DeviceNodeMap {
ParamTemplate.DeviceNodeNameMap = append(ParamTemplate.DeviceNodeNameMap, d.Name)
ParamTemplate.DeviceNodeAddrMap = append(ParamTemplate.DeviceNodeAddrMap, d.Addr)
ParamTemplate.DeviceNodeTypeMap = append(ParamTemplate.DeviceNodeTypeMap, d.Type)
}
CollectInterfaceParamMap.CollectInterfaceParam = append(CollectInterfaceParamMap.CollectInterfaceParam,
ParamTemplate)
}
@ -224,7 +225,7 @@ func NewCollectInterface(collInterfaceName, commInterfaceName string,
PollPeriod: pollPeriod,
OfflinePeriod: offlinePeriod,
DeviceNodeCnt: deviceNodeCnt,
DeviceNodeMap: make([]*DeviceNodeTemplate, 0),
DeviceNodeMap: make(map[string]*DeviceNodeTemplate),
CollEventBus: eventBus.NewBus(),
Cron: cron.New(),
}
@ -347,7 +348,7 @@ func (d *CollectInterfaceTemplate) NewDeviceNode(dName string, dType string, dAd
services := node.NewServices()
node.Services = append(node.Services, services...)
d.DeviceNodeMap = append(d.DeviceNodeMap, node)
d.DeviceNodeMap[dName] = node
}
func (d *CollectInterfaceTemplate) AddDeviceNode(dName string, dType string, dAddr string) (bool, string) {
@ -368,8 +369,10 @@ func (d *CollectInterfaceTemplate) AddDeviceNode(dName string, dType string, dAd
properties := node.NewVariables()
node.Properties = append(node.Properties, properties...)
services := node.NewServices()
node.Services = append(node.Services, services...)
d.DeviceNodeMap = append(d.DeviceNodeMap, node)
d.DeviceNodeMap[dName] = node
d.DeviceNodeCnt++
@ -378,12 +381,10 @@ func (d *CollectInterfaceTemplate) AddDeviceNode(dName string, dType string, dAd
func (d *CollectInterfaceTemplate) DeleteDeviceNode(dName string) {
for k, v := range d.DeviceNodeMap {
if v.Name == dName {
//d.DeviceNodeMap = d.DeviceNodeMap[k : k+1]
d.DeviceNodeMap = append(d.DeviceNodeMap[:k], d.DeviceNodeMap[k+1:]...)
d.DeviceNodeCnt--
}
_, ok := d.DeviceNodeMap[dName]
if ok {
d.DeviceNodeCnt--
delete(d.DeviceNodeMap, dName)
}
}
@ -407,19 +408,15 @@ func (d *CollectInterfaceTemplate) CommunicationManageDel(ctx context.Context) {
case cmd := <-d.CommQueueManage.EmergencyRequestChan:
{
setting.Logger.Infof("emergency chan collName %v nodeName %v funName %v", d.CollInterfaceName, cmd.DeviceName, cmd.FunName)
index := -1
for k, v := range d.DeviceNodeMap {
if cmd.DeviceName == v.Name {
index = k
}
}
if index == -1 {
node, ok := d.DeviceNodeMap[cmd.DeviceName]
if !ok {
continue
}
rxData := d.CommQueueManage.CommunicationStateMachine(cmd,
d.CollInterfaceName,
d.CommInterface,
d.DeviceNodeMap[index],
node,
&d.CollEventBus,
d.OfflinePeriod)
//更新设备在线数量
@ -438,13 +435,8 @@ func (d *CollectInterfaceTemplate) CommunicationManageDel(ctx context.Context) {
select {
case cmd := <-d.CommQueueManage.CommonRequestChan:
{
index := -1
for k, v := range d.DeviceNodeMap {
if cmd.DeviceName == v.Name {
index = k
}
}
if index == -1 {
node, ok := d.DeviceNodeMap[cmd.DeviceName]
if !ok {
continue
}
@ -452,7 +444,7 @@ func (d *CollectInterfaceTemplate) CommunicationManageDel(ctx context.Context) {
d.CommQueueManage.CommunicationStateMachine(cmd,
d.CollInterfaceName,
d.CommInterface,
d.DeviceNodeMap[index],
node,
&d.CollEventBus,
d.OfflinePeriod)
//更新设备在线数量,当本次采集最后一个设备时进行更新

View File

@ -11,18 +11,14 @@ import (
"os"
"path"
"path/filepath"
"sort"
"strconv"
"strings"
"sync"
"goAdapter/device"
"github.com/gin-gonic/gin"
)
var lock sync.Mutex
func apiAddInterface(context *gin.Context) {
interfaceInfo := &struct {
CollectInterfaceName string `json:"CollInterfaceName"` // 采集接口名字
@ -43,7 +39,7 @@ func apiAddInterface(context *gin.Context) {
}
err = device.AddCollectInterface(interfaceInfo.CollectInterfaceName,
interfaceInfo.CollectInterfaceName,
interfaceInfo.CommInterfaceName,
interfaceInfo.PollPeriod,
interfaceInfo.OfflinePeriod)
if err != nil {
@ -212,12 +208,12 @@ func apiGetAllInterfaceInfo(context *gin.Context) {
aParam.Data = append(aParam.Data, Param)
}
//排序,方便前端页面显示
sort.Slice(aParam.Data, func(i, j int) bool {
iName, _ := strconv.Atoi(aParam.Data[i].CollInterfaceName)
jName, _ := strconv.Atoi(aParam.Data[j].CollInterfaceName)
return iName > jName
})
////排序,方便前端页面显示
//sort.Slice(aParam.Data, func(i, j int) bool {
// iName, _ := strconv.Atoi(aParam.Data[i].CollInterfaceName)
// jName, _ := strconv.Atoi(aParam.Data[j].CollInterfaceName)
// return iName > jName
//})
context.JSON(http.StatusOK, aParam)
}
@ -667,7 +663,6 @@ func apiGetNodeReadVariable(context *gin.Context) {
}{}
//查找设备是否存在
nodeIndex := -1
coll, ok := device.CollectInterfaceMap[sName]
if !ok {
aParam.Code = "1"
@ -677,12 +672,13 @@ func apiGetNodeReadVariable(context *gin.Context) {
return
}
for k, v := range coll.DeviceNodeMap {
nodeName := ""
for _, v := range coll.DeviceNodeMap {
if v.Addr == sAddr {
nodeIndex = k
nodeName = v.Name
}
}
if nodeIndex == -1 {
if nodeName == "" {
aParam.Code = "1"
aParam.Message = "node is no exist"
sJson, _ := json.Marshal(aParam)
@ -691,10 +687,9 @@ func apiGetNodeReadVariable(context *gin.Context) {
}
//发送命令到响应的采集接口
cmd := device.CommunicationCmdTemplate{}
cmd.CollInterfaceName = sName
cmd.DeviceName = coll.DeviceNodeMap[nodeIndex].Name
cmd.DeviceName = coll.DeviceNodeMap[nodeName].Name
cmd.FunName = "GetRealVariables"
cmd.FunPara = ""
cmdRX := coll.CommQueueManage.CommunicationManageAddEmergency(cmd)
@ -704,7 +699,7 @@ func apiGetNodeReadVariable(context *gin.Context) {
aParam.Data = make([]VariableTemplate, 0)
index := 0
variable := VariableTemplate{}
for _, v := range coll.DeviceNodeMap[nodeIndex].Properties {
for _, v := range coll.DeviceNodeMap[nodeName].Properties {
//variable.Index = v.Index
variable.Name = v.Name
variable.Label = v.Explain
@ -735,133 +730,6 @@ func apiGetNodeReadVariable(context *gin.Context) {
}
}
/**
从设备中获取设备变量
*/
func apiGetNodeVariableFromDevice(context *gin.Context) {
// sName := context.Query("interfaceName")
// sAddr := context.Query("addr")
//
// aParam := &struct {
// Code string
// Message string
// Data []api.VariableTemplate
// }{}
//
//
// for _,v := range device.CollectInterfaceMap {
// if v.CollInterfaceName == nodeInfo.InterfaceName {
//
// }
// }
//
// iID, _ := strconv.Atoi(sID)
// for k, v := range device.CollectInterfaceMap[iID].DeviceNodeMap {
// if v.Addr == sAddr {
//
// cmd := device.CommunicationCmd{}
// cmd.InterfaceID = device.InterFaceID0
// cmd.DeviceAddr = v.Addr
// cmd.FunName = "GenerateGetRealVariables"
// if device.CommunicationManageAddEmergency(cmd) == true {
// aParam.Code = "0"
// aParam.Message = ""
// aParam.Data = device.CollectInterfaceMap[iID].DeviceNodeMap[k].VariableMap
// } else {
// aParam.Code = "1"
// aParam.Message = ""
// aParam.Data = device.CollectInterfaceMap[iID].DeviceNodeMap[k].VariableMap
//
// }
// sJson, _ := json.Marshal(aParam)
// context.String(http.StatusOK, string(sJson))
// return
// }
// }
// aParam.Code = "1"
// aParam.Message = "node is noexist"
// sJson, _ := json.Marshal(aParam)
// context.String(http.StatusOK, string(sJson))
}
//func apiAddTemplate(context *gin.Context) {
//
// aParam := struct {
// Code string `json:"Code"`
// Message string `json:"Message"`
// Data string `json:"Data"`
// }{
// Code: "1",
// Message: "",
// Data: "",
// }
//
// bodyBuf := make([]byte, 1024)
// n, _ := context.Request.Body.Read(bodyBuf)
// fmt.Println(string(bodyBuf[:n]))
//
// typeInfo := &struct {
// TemplateName string `json:"TemplateName"` // 模板名称
// TemplateType string `json:"TemplateType"` // 模板型号
// TemplateMessage string `json:"TemplateMessage"` // 备注信息
// }{}
//
// err := json.Unmarshal(bodyBuf[:n], typeInfo)
// if err != nil {
// fmt.Println("interfaceInfo json unMarshall err,", err)
//
// aParam.Code = "1"
// aParam.Message = "json unMarshall err"
//
// sJson, _ := json.Marshal(aParam)
// context.String(http.StatusOK, string(sJson))
// return
// }
//
// setting.Logger.Debugf("typeInfo %v", typeInfo)
//
// index := len(device.DeviceNodeTypeMap.DeviceNodeType)
// template := device.DeviceNodeTypeTemplate{
// TemplateName: typeInfo.TemplateName,
// TemplateType: typeInfo.TemplateType,
// TemplateID: index,
// TemplateMessage: typeInfo.TemplateMessage,
// }
//
// device.DeviceNodeTypeMap.DeviceNodeType = append(device.DeviceNodeTypeMap.DeviceNodeType, template)
//
// aParam.Code = "0"
// aParam.Data = ""
//
// sJson, _ := json.Marshal(aParam)
// context.String(http.StatusOK, string(sJson))
//}
//
//func apiGetTemplate(context *gin.Context) {
//
// aParam := &struct {
// Code string
// Message string
// Data []device.DeviceNodeTypeTemplate
// }{}
//
// lock.Lock()
// //清空设备模版缓存
// device.DeviceNodeTypeMap.DeviceNodeType = device.DeviceNodeTypeMap.DeviceNodeType[0:0]
// //获取最新的模版
// device.ReadDeviceNodeTypeMap()
// lock.Unlock()
//
// aParam.Code = "0"
// aParam.Message = ""
// aParam.Data = device.DeviceNodeTypeMap.DeviceNodeType
//
// sJson, _ := json.Marshal(aParam)
//
// context.String(http.StatusOK, string(sJson))
//}
func apiAddDeviceTSL(context *gin.Context) {
aParam := struct {

View File

@ -318,18 +318,13 @@ func (r *ReportServiceParamAliyunTemplate) ProcessCollEvent(sub eventBus.Sub) {
if !ok {
continue
}
nodeIndex := -1
for k, v := range coll.DeviceNodeMap {
if v.Name == msg.NodeName {
nodeIndex = k
}
}
if nodeIndex == -1 {
continue
node, ok := coll.DeviceNodeMap[msg.NodeName]
if !ok {
return
}
reportStatus := false
for _, v := range coll.DeviceNodeMap[nodeIndex].Properties {
for _, v := range node.Properties {
if v.Params.StepAlarm == true {
valueCnt := len(v.Value)
if valueCnt >= 2 { //阶跃报警必须是2个值
@ -339,7 +334,7 @@ func (r *ReportServiceParamAliyunTemplate) ProcessCollEvent(sub eventBus.Sub) {
step, _ := strconv.Atoi(v.Params.Step)
if math.Abs(float64(pValueCur-pValuePre)) > float64(step) {
reportStatus = true //满足报警条件,上报
nodeName = append(nodeName, coll.DeviceNodeMap[nodeIndex].Name)
nodeName = append(nodeName, node.Name)
}
} else if v.Type == device.PropertyTypeUInt32 {
pValueCur := v.Value[valueCnt-1].Value.(uint32)
@ -347,7 +342,7 @@ func (r *ReportServiceParamAliyunTemplate) ProcessCollEvent(sub eventBus.Sub) {
step, _ := strconv.Atoi(v.Params.Step)
if math.Abs(float64(pValueCur-pValuePre)) > float64(step) {
reportStatus = true //满足报警条件,上报
nodeName = append(nodeName, coll.DeviceNodeMap[nodeIndex].Name)
nodeName = append(nodeName, node.Name)
}
} else if v.Type == device.PropertyTypeDouble {
pValueCur := v.Value[valueCnt-1].Value.(float64)
@ -355,7 +350,7 @@ func (r *ReportServiceParamAliyunTemplate) ProcessCollEvent(sub eventBus.Sub) {
step, _ := strconv.Atoi(v.Params.Step)
if math.Abs(pValueCur-pValuePre) > float64(step) {
reportStatus = true //满足报警条件,上报
nodeName = append(nodeName, coll.DeviceNodeMap[nodeIndex].Name)
nodeName = append(nodeName, node.Name)
}
}
}
@ -367,7 +362,7 @@ func (r *ReportServiceParamAliyunTemplate) ProcessCollEvent(sub eventBus.Sub) {
max, _ := strconv.Atoi(v.Params.Max)
if pValueCur < int32(min) || pValueCur > int32(max) {
reportStatus = true //满足报警条件,上报
nodeName = append(nodeName, coll.DeviceNodeMap[nodeIndex].Name)
nodeName = append(nodeName, node.Name)
}
} else if v.Type == device.PropertyTypeUInt32 {
pValueCur := v.Value[valueCnt-1].Value.(uint32)
@ -375,7 +370,7 @@ func (r *ReportServiceParamAliyunTemplate) ProcessCollEvent(sub eventBus.Sub) {
max, _ := strconv.Atoi(v.Params.Max)
if pValueCur < uint32(min) || pValueCur > uint32(max) {
reportStatus = true //满足报警条件,上报
nodeName = append(nodeName, coll.DeviceNodeMap[nodeIndex].Name)
nodeName = append(nodeName, node.Name)
}
} else if v.Type == device.PropertyTypeDouble {
pValueCur := v.Value[valueCnt-1].Value.(float64)
@ -383,7 +378,7 @@ func (r *ReportServiceParamAliyunTemplate) ProcessCollEvent(sub eventBus.Sub) {
max, _ := strconv.Atoi(v.Params.Max)
if pValueCur < float64(min) || pValueCur > float64(max) {
reportStatus = true //满足报警条件,上报
nodeName = append(nodeName, coll.DeviceNodeMap[nodeIndex].Name)
nodeName = append(nodeName, node.Name)
}
}
}

View File

@ -347,18 +347,13 @@ func (r *ReportServiceParamEmqxTemplate) ProcessCollEvent(sub eventBus.Sub) {
if !ok {
continue
}
nodeIndex := -1
for k, v := range coll.DeviceNodeMap {
if v.Name == msg.NodeName {
nodeIndex = k
}
}
if nodeIndex == -1 {
continue
node, ok := coll.DeviceNodeMap[msg.NodeName]
if !ok {
return
}
reportStatus := false
for _, v := range coll.DeviceNodeMap[nodeIndex].Properties {
for _, v := range node.Properties {
if v.Params.StepAlarm == true {
valueCnt := len(v.Value)
if valueCnt >= 2 { //阶跃报警必须是2个值
@ -368,7 +363,7 @@ func (r *ReportServiceParamEmqxTemplate) ProcessCollEvent(sub eventBus.Sub) {
step, _ := strconv.Atoi(v.Params.Step)
if math.Abs(float64(pValueCur-pValuePre)) > float64(step) {
reportStatus = true //满足报警条件,上报
nodeName = append(nodeName, coll.DeviceNodeMap[nodeIndex].Name)
nodeName = append(nodeName, node.Name)
}
} else if v.Type == device.PropertyTypeUInt32 {
pValueCur := v.Value[valueCnt-1].Value.(uint32)
@ -376,7 +371,7 @@ func (r *ReportServiceParamEmqxTemplate) ProcessCollEvent(sub eventBus.Sub) {
step, _ := strconv.Atoi(v.Params.Step)
if math.Abs(float64(pValueCur-pValuePre)) > float64(step) {
reportStatus = true //满足报警条件,上报
nodeName = append(nodeName, coll.DeviceNodeMap[nodeIndex].Name)
nodeName = append(nodeName, node.Name)
}
} else if v.Type == device.PropertyTypeDouble {
pValueCur := v.Value[valueCnt-1].Value.(float64)
@ -384,7 +379,7 @@ func (r *ReportServiceParamEmqxTemplate) ProcessCollEvent(sub eventBus.Sub) {
step, _ := strconv.Atoi(v.Params.Step)
if math.Abs(pValueCur-pValuePre) > float64(step) {
reportStatus = true //满足报警条件,上报
nodeName = append(nodeName, coll.DeviceNodeMap[nodeIndex].Name)
nodeName = append(nodeName, node.Name)
}
}
}
@ -396,7 +391,7 @@ func (r *ReportServiceParamEmqxTemplate) ProcessCollEvent(sub eventBus.Sub) {
max, _ := strconv.Atoi(v.Params.Max)
if pValueCur < int32(min) || pValueCur > int32(max) {
reportStatus = true //满足报警条件,上报
nodeName = append(nodeName, coll.DeviceNodeMap[nodeIndex].Name)
nodeName = append(nodeName, node.Name)
}
} else if v.Type == device.PropertyTypeUInt32 {
pValueCur := v.Value[valueCnt-1].Value.(uint32)
@ -404,7 +399,7 @@ func (r *ReportServiceParamEmqxTemplate) ProcessCollEvent(sub eventBus.Sub) {
max, _ := strconv.Atoi(v.Params.Max)
if pValueCur < uint32(min) || pValueCur > uint32(max) {
reportStatus = true //满足报警条件,上报
nodeName = append(nodeName, coll.DeviceNodeMap[nodeIndex].Name)
nodeName = append(nodeName, node.Name)
}
} else if v.Type == device.PropertyTypeDouble {
pValueCur := v.Value[valueCnt-1].Value.(float64)
@ -412,7 +407,7 @@ func (r *ReportServiceParamEmqxTemplate) ProcessCollEvent(sub eventBus.Sub) {
max, _ := strconv.Atoi(v.Params.Max)
if pValueCur < float64(min) || pValueCur > float64(max) {
reportStatus = true //满足报警条件,上报
nodeName = append(nodeName, coll.DeviceNodeMap[nodeIndex].Name)
nodeName = append(nodeName, node.Name)
}
}
}

View File

@ -48,20 +48,15 @@ func ReportServiceHuaweiProcessWriteCmd(r *ReportServiceParamHuaweiTemplate, req
if !ok {
return
}
i := -1
for k, v := range coll.DeviceNodeMap {
if v.Name == r.NodeList[x].Name {
i = k
break
}
}
if i == -1 {
node, ok := coll.DeviceNodeMap[r.NodeList[x].Name]
if !ok {
return
}
cmd := device.CommunicationCmdTemplate{}
cmd.CollInterfaceName = coll.CollInterfaceName
cmd.DeviceName = coll.DeviceNodeMap[i].Name
cmd.DeviceName = node.Name
cmd.FunName = request.CommandName
paramStr, _ := json.Marshal(request.Paras)
cmd.FunPara = string(paramStr)

View File

@ -48,20 +48,14 @@ func ReportServiceHuaweiProcessGetProperties(r *ReportServiceParamHuaweiTemplate
return
}
i := -1
for k, v := range coll.DeviceNodeMap {
if v.Name == r.NodeList[x].Name {
i = k
break
}
}
if i == -1 {
node, ok := coll.DeviceNodeMap[r.NodeList[x].Name]
if !ok {
return
}
cmd := device.CommunicationCmdTemplate{}
cmd.CollInterfaceName = coll.CollInterfaceName
cmd.DeviceName = coll.DeviceNodeMap[i].Name
cmd.DeviceName = node.Name
cmd.FunName = "GetRealVariables"
cmd.FunPara = ""
@ -69,7 +63,7 @@ func ReportServiceHuaweiProcessGetProperties(r *ReportServiceParamHuaweiTemplate
if cmdRX.Status == true {
setting.Logger.Debugf("GetRealVariables ok")
service := MQTTHuaweiServiceTemplate{}
for _, v := range coll.DeviceNodeMap[i].Properties {
for _, v := range node.Properties {
if v.Name == request.ServiceID {
if len(v.Value) >= 1 {
index := len(v.Value) - 1