Merge branch 'feature' into develop

This commit is contained in:
pengwang 2021-07-13 17:30:08 +08:00
commit e1707f1390
3 changed files with 207 additions and 63 deletions

View File

@ -314,7 +314,37 @@ func (r *ReportServiceParamAliyunTemplate) LogOut(nodeName []string) {
r.NodeLogOut(nodeName)
}
func (r *ReportServiceParamAliyunTemplate) ReportTimeOut() {
//查看上报服务中设备通信状态
func (r *ReportServiceParamAliyunTemplate) ReportCommStatusTimeFun() {
setting.Logger.Infof("service:%s,CheckCommStatus", r.GWParam.ServiceName)
for k, n := range r.NodeList {
name := make([]string, 0)
for _, c := range device.CollectInterfaceMap {
if c.CollInterfaceName == n.CollInterfaceName {
for _, d := range c.DeviceNodeMap {
if n.Name == d.Name {
//通信状态发生了改变
if d.CommStatus != n.CommStatus {
if d.CommStatus == "onLine" {
setting.Logger.Infof("DeviceOnline %v\n", n.Name)
name = append(name, n.Name)
r.LogInRequestFrameChan <- name
} else if d.CommStatus == "offLine" {
setting.Logger.Infof("DeviceOffline %v\n", n.Name)
name = append(name, n.Name)
r.LogOutRequestFrameChan <- name
}
r.NodeList[k].CommStatus = d.CommStatus
}
}
}
}
}
}
}
func (r *ReportServiceParamAliyunTemplate) ReportTimeFun() {
if r.GWParam.ReportStatus == "onLine" {
//网关上报
@ -340,7 +370,7 @@ func (r *ReportServiceParamAliyunTemplate) ReportTimeOut() {
}
//查看上报服务中设备是否离线
func (r *ReportServiceParamAliyunTemplate) ReportOfflineTime() {
func (r *ReportServiceParamAliyunTemplate) ReportOfflineTimeFun() {
setting.Logger.Infof("service:%s,CheckReportOffline", r.GWParam.ServiceName)
if r.GWParam.ReportErrCnt >= 3 {
@ -365,13 +395,19 @@ func ReportServiceAliyunPoll(r *ReportServiceParamAliyunTemplate) {
// 定义一个cron运行器
cronProcess := cron.New()
//每10s查看一下上报节点的通信状态
reportCommStatusTime := fmt.Sprintf("@every %dm%ds", 10/60, 10%60)
setting.Logger.Infof("reportServiceAliyun reportCommStatusTime%v", reportCommStatusTime)
reportTime := fmt.Sprintf("@every %dm%ds", r.GWParam.ReportTime/60, r.GWParam.ReportTime%60)
setting.Logger.Infof("reportServiceAliyun reportTime%v", reportTime)
reportOfflineTime := fmt.Sprintf("@every %dm%ds", (3*r.GWParam.ReportTime)/60, (3*r.GWParam.ReportTime)%60)
setting.Logger.Infof("reportServiceAliyun reportOfflineTime%v", reportOfflineTime)
_ = cronProcess.AddFunc(reportOfflineTime, r.ReportOfflineTime)
_ = cronProcess.AddFunc(reportTime, r.ReportTimeOut)
_ = cronProcess.AddFunc(reportCommStatusTime, r.ReportCommStatusTimeFun)
_ = cronProcess.AddFunc(reportOfflineTime, r.ReportOfflineTimeFun)
_ = cronProcess.AddFunc(reportTime, r.ReportTimeFun)
cronProcess.Start()
defer cronProcess.Stop()
@ -382,7 +418,7 @@ func ReportServiceAliyunPoll(r *ReportServiceParamAliyunTemplate) {
go r.ProcessInvokeThingsService()
name := make([]string, 0)
//name := make([]string, 0)
for {
switch reportState {
case 0:
@ -402,35 +438,35 @@ func ReportServiceAliyunPoll(r *ReportServiceParamAliyunTemplate) {
r.GWParam.ReportErrCnt = 0
}
for k, v := range r.NodeList {
commStatus := "offLine"
for _, d := range device.CollectInterfaceMap {
if v.CollInterfaceName == v.CollInterfaceName {
for _, n := range d.DeviceNodeMap {
if v.Name == n.Name {
commStatus = n.CommStatus
break
}
}
}
}
if commStatus != v.CommStatus {
if commStatus == "onLine" {
//节点发生了上线
setting.Logger.Debugf("service %s,node %s onLine", v.ServiceName, v.Name)
name = append(name, v.Name)
r.LogInRequestFrameChan <- name
name = name[0:0]
} else if commStatus == "offLine" {
//节点发生了离线
setting.Logger.Debugf("service %s,node %s onLine", v.ServiceName, v.Name)
name = append(name, v.Name)
r.LogOutRequestFrameChan <- name
name = name[0:0]
}
r.NodeList[k].CommStatus = commStatus
}
}
//for k, v := range r.NodeList {
// commStatus := "offLine"
// for _, d := range device.CollectInterfaceMap {
// if v.CollInterfaceName == v.CollInterfaceName {
// for _, n := range d.DeviceNodeMap {
// if v.Name == n.Name {
// commStatus = n.CommStatus
// break
// }
// }
// }
// }
// if commStatus != v.CommStatus {
// if commStatus == "onLine" {
// //节点发生了上线
// setting.Logger.Debugf("service %s,node %s onLine", v.ServiceName, v.Name)
// name = append(name, v.Name)
// r.LogInRequestFrameChan <- name
// name = name[0:0]
// } else if commStatus == "offLine" {
// //节点发生了离线
// setting.Logger.Debugf("service %s,node %s onLine", v.ServiceName, v.Name)
// name = append(name, v.Name)
// r.LogOutRequestFrameChan <- name
// name = name[0:0]
// }
// r.NodeList[k].CommStatus = commStatus
// }
//}
//节点有属性变化
//for _, c := range device.CollectInterfaceMap {

View File

@ -0,0 +1,77 @@
package mqttHuawei
import (
"encoding/json"
"goAdapter/device"
"goAdapter/setting"
)
type MQTTHuaweiWriteCmdRequestTemplate struct {
ServiceID string `json:"service_id"`
ObjectDeviceID string `json:"object_device_id"`
CommandName string `json:"command_name"`
Paras map[string]interface{} `json:"paras"`
}
type MQTTHuaweiWriteCmdAckTemplate struct {
ResultCode int `json:"result_code"`
ResponseName string `json:"response_name"`
Paras map[string]interface{} `json:"paras"`
}
func MQTTHuaweiWriteCmdAck(r *ReportServiceParamHuaweiTemplate, requestID string, ack MQTTHuaweiWriteCmdAckTemplate) {
sJson, _ := json.Marshal(ack)
setting.Logger.Debugf("writeCmdAck post msg: %s\n", sJson)
serviceTopic := "$oc/devices/" + r.GWParam.Param.DeviceID + "/sys/commands/response/request_id=" + requestID
setting.Logger.Infof("writeCmdAck post topic: %s\n", serviceTopic)
if r.GWParam.MQTTClient != nil {
token := r.GWParam.MQTTClient.Publish(serviceTopic, 0, false, sJson)
token.Wait()
}
}
func ReportServiceHuaweiProcessWriteCmd(r *ReportServiceParamHuaweiTemplate, requestID string, request MQTTHuaweiWriteCmdRequestTemplate) {
x := 0
for k, v := range r.NodeList {
if v.Param.DeviceID == request.ObjectDeviceID {
x = k
break
}
}
y := 0
for k, v := range device.CollectInterfaceMap {
if v.CollInterfaceName == r.NodeList[x].CollInterfaceName {
y = k
break
}
}
i := 0
for k, v := range device.CollectInterfaceMap[y].DeviceNodeMap {
if v.Name == r.NodeList[x].Name {
i = k
break
}
}
cmd := device.CommunicationCmdTemplate{}
cmd.CollInterfaceName = device.CollectInterfaceMap[y].CollInterfaceName
cmd.DeviceName = device.CollectInterfaceMap[y].DeviceNodeMap[i].Name
cmd.FunName = request.CommandName
paramStr, _ := json.Marshal(request.Paras)
cmd.FunPara = string(paramStr)
cmdAck := MQTTHuaweiWriteCmdAckTemplate{}
if device.CommunicationManage[y].CommunicationManageAddEmergency(cmd) == true {
setting.Logger.Debugf("WriteCmd ok")
cmdAck.ResultCode = 0
cmdAck.ResponseName = request.ServiceID
} else {
cmdAck.ResultCode = 1
cmdAck.ResponseName = request.ServiceID
}
MQTTHuaweiWriteCmdAck(r, requestID, cmdAck)
}

View File

@ -280,6 +280,25 @@ func (r *ReportServiceParamHuaweiTemplate) ProcessDownLinkFrame() {
} else if strings.Contains(frame.Topic, "/thing/service/property/set") { //设置属性请求
} else if strings.Contains(frame.Topic, "/sys/commands/") { //下发命令
writeCmdRequest := MQTTHuaweiWriteCmdRequestTemplate{}
err := json.Unmarshal(frame.Payload, &writeCmdRequest)
if err != nil {
setting.Logger.Errorf("writeCmdRequest json unmarshal err")
return
}
topicPara := strings.Split(frame.Topic, "/")
//setting.Logger.Debugf("topicPara %v", topicPara)
for _, v := range topicPara {
if strings.Contains(v, "request_id") {
idIndex := strings.Index(v, "=") + 1
if idIndex > 0 {
requestID := v[idIndex:]
//setting.Logger.Debugf("requestID %v", requestID)
ReportServiceHuaweiProcessWriteCmd(r, requestID, writeCmdRequest)
}
}
}
}
}
}
@ -306,7 +325,7 @@ func (r *ReportServiceParamHuaweiTemplate) LogOut(nodeName []string) {
r.NodeLogOut(nodeName)
}
func (r *ReportServiceParamHuaweiTemplate) ReportTimeOut() {
func (r *ReportServiceParamHuaweiTemplate) ReportTimeFun() {
if r.GWParam.ReportStatus == "onLine" {
//网关上报
@ -331,8 +350,38 @@ func (r *ReportServiceParamHuaweiTemplate) ReportTimeOut() {
}
}
//查看上报服务中设备通信状态
func (r *ReportServiceParamHuaweiTemplate) ReportCommStatusTimeFun() {
setting.Logger.Infof("service:%s,CheckCommStatus", r.GWParam.ServiceName)
for k, n := range r.NodeList {
name := make([]string, 0)
for _, c := range device.CollectInterfaceMap {
if c.CollInterfaceName == n.CollInterfaceName {
for _, d := range c.DeviceNodeMap {
if n.Name == d.Name {
//通信状态发生了改变
if d.CommStatus != n.CommStatus {
if d.CommStatus == "onLine" {
setting.Logger.Infof("DeviceOnline %v\n", n.Name)
name = append(name, n.Name)
r.LogInRequestFrameChan <- name
} else if d.CommStatus == "offLine" {
setting.Logger.Infof("DeviceOffline %v\n", n.Name)
name = append(name, n.Name)
r.LogOutRequestFrameChan <- name
}
r.NodeList[k].CommStatus = d.CommStatus
}
}
}
}
}
}
}
//查看上报服务中设备是否离线
func (r *ReportServiceParamHuaweiTemplate) ReportOfflineTime() {
func (r *ReportServiceParamHuaweiTemplate) ReportOfflineTimeFun() {
setting.Logger.Infof("service:%s,CheckReportOffline", r.GWParam.ServiceName)
if r.GWParam.ReportErrCnt >= 3 {
@ -357,13 +406,19 @@ func ReportServiceHuaweiPoll(r *ReportServiceParamHuaweiTemplate) {
// 定义一个cron运行器
cronProcess := cron.New()
//每10s查看一下上报节点的通信状态
reportCommStatusTime := fmt.Sprintf("@every %dm%ds", 10/60, 10%60)
setting.Logger.Infof("reportServiceHuawei reportCommStatusTime%v", reportCommStatusTime)
reportTime := fmt.Sprintf("@every %dm%ds", r.GWParam.ReportTime/60, r.GWParam.ReportTime%60)
setting.Logger.Infof("reportServiceHuawei reportTime%v", reportTime)
reportOfflineTime := fmt.Sprintf("@every %dm%ds", (3*r.GWParam.ReportTime)/60, (3*r.GWParam.ReportTime)%60)
setting.Logger.Infof("reportServiceHuawei reportOfflineTime%v", reportOfflineTime)
_ = cronProcess.AddFunc(reportOfflineTime, r.ReportOfflineTime)
_ = cronProcess.AddFunc(reportTime, r.ReportTimeOut)
_ = cronProcess.AddFunc(reportCommStatusTime, r.ReportCommStatusTimeFun)
_ = cronProcess.AddFunc(reportOfflineTime, r.ReportOfflineTimeFun)
_ = cronProcess.AddFunc(reportTime, r.ReportTimeFun)
cronProcess.Start()
defer cronProcess.Stop()
@ -372,7 +427,7 @@ func ReportServiceHuaweiPoll(r *ReportServiceParamHuaweiTemplate) {
go r.ProcessDownLinkFrame()
name := make([]string, 0)
//name := make([]string, 0)
for {
switch reportState {
case 0:
@ -391,30 +446,6 @@ func ReportServiceHuaweiPoll(r *ReportServiceParamHuaweiTemplate) {
r.GWParam.ReportErrCnt = 0
}
//节点发生了上线
for _, c := range device.CollectInterfaceMap {
for i := 0; i < len(c.OnlineReportChan); i++ {
name = append(name, <-c.OnlineReportChan)
}
}
if len(name) > 0 {
setting.Logger.Infof("DeviceOnline %v\n", name)
r.LogInRequestFrameChan <- name
name = name[0:0]
}
//节点发生了离线
for _, c := range device.CollectInterfaceMap {
for i := 0; i < len(c.OfflineReportChan); i++ {
name = append(name, <-c.OfflineReportChan)
}
}
if len(name) > 0 {
setting.Logger.Infof("DeviceOffline %v\n", name)
r.LogOutRequestFrameChan <- name
name = name[0:0]
}
//节点有属性变化
//for _, c := range device.CollectInterfaceMap {
// for i := 0; i < len(c.PropertyReportChan); i++ {