/// <summary> /// 通用的监听方法 /// 工作内容: /// 1.根据Topic获取设备及属性实例。Topic规则:DeviceLabel/PropertyLabel /// 2.查询是否命中报警策略,如果有则记录报警信息 /// 3.将实时数据保存到默认的TSDB数据库中 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MqttService_OnMqttMessageNotify(object sender, MqttMessageNotifyEventArgs e) { log.InfoFormat("[MQTT]收到消息:客户端:{0},主题:{1},消息:{2},Qos:{3},保留:{4}", e.ClientId, e.MqttApplicationMessage.Topic, Encoding.UTF8.GetString(e.MqttApplicationMessage.Payload), e.MqttApplicationMessage.QualityOfServiceLevel, e.MqttApplicationMessage.Retain); string[] topicArry = e.MqttApplicationMessage.Topic.Split('/'); if (topicArry.Length == 2) { string deviceLabel = topicArry[0]; string propertyLabel = topicArry[1]; DateTime now = DateTime.Now; DeviceMonitoringApi deviceMonitoringApi = new DeviceMonitoringApi(); GetDeviceInfoParameter par = new GetDeviceInfoParameter(); RetDeviceInfo deviceInfo = null; RetDeviceItemInfo itemInfo = null; par.DeviceLabel = deviceLabel; var resDeviceInfo = deviceMonitoringApi.GetDeviceInfo(par); if (resDeviceInfo.Code != -1) { deviceInfo = resDeviceInfo.Data; itemInfo = deviceInfo.DeviceItems.Find(s => s.PropertyLabel == propertyLabel); if (null == itemInfo) { log.ErrorFormat("根据Topic获取设备属性信息出错,无法匹配报警策略,deviceLabel{0},propertyLabel{1}", deviceLabel, propertyLabel); return; } } else { log.Error("根据deviceLabel获取设备信息失败" + resDeviceInfo.Msg); return; } //异步处理报警逻辑 AsyncHandleAlarmPolicies(deviceInfo, itemInfo, Encoding.UTF8.GetString(e.MqttApplicationMessage.Payload), now); //保存到TSDB RecordToTsdb(deviceInfo, itemInfo, Encoding.UTF8.GetString(e.MqttApplicationMessage.Payload), now); } else { log.ErrorFormat("解析TOPIC:{0} 失败,不符合规则", e.MqttApplicationMessage.Topic); } }
/// <summary> /// 物接入(研华网关方式) /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void MqttService_OnYanhuaMessageNotify(object sender, MqttMessageNotifyEventArgs e) { try { log.InfoFormat("[MQTT]收到消息:客户端:{0},主题:{1},消息:{2},Qos:{3},保留:{4}", e.ClientId, e.MqttApplicationMessage.Topic, Encoding.UTF8.GetString(e.MqttApplicationMessage.Payload), e.MqttApplicationMessage.QualityOfServiceLevel, e.MqttApplicationMessage.Retain); string[] topicArry = e.MqttApplicationMessage.Topic.Split('/'); string orgId = topicArry[0]; //组织ID //报文转换为对象 YanHuaPayload yanhuaPayload; try { yanhuaPayload = JsonHelper.JsonToEntity <YanHuaPayload>(Encoding.UTF8.GetString(e.MqttApplicationMessage.Payload)); } catch (Exception ex) { // 转换对象失败,报文格式不对,直接放弃 log.ErrorFormat("[MQTT]解析报文格式失败:{0},{1}", ex.Message, ex.StackTrace); return; } foreach (var payVal in yanhuaPayload.values) { try { DateTime uploadtime = DateTime.ParseExact(payVal.time, "yyyyMMdd-HH:mm:ss", CultureInfo.InvariantCulture); DeviceMonitoringApi deviceMonitoringApi = new DeviceMonitoringApi(); GetDeviceInfoParameter par = new GetDeviceInfoParameter(); RetDeviceInfo deviceInfo = null; RetDeviceItemInfo itemInfo = null; par.OrgID = orgId; par.Phone = payVal.device_ip; var resDeviceInfo = deviceMonitoringApi.GetDeviceInfo(par); if (resDeviceInfo.Code != -1) { deviceInfo = resDeviceInfo.Data; itemInfo = deviceInfo.DeviceItems.Find(s => s.Name == payVal.operationValue); if (null == itemInfo) { log.ErrorFormat("根据Topic获取设备属性信息出错,device_ip{0},name{1}", payVal.device_ip, payVal.name); continue; } } else { log.Error("根据deviceLabel获取设备信息失败" + resDeviceInfo.Msg); continue; } //处理报警逻辑 AsyncHandleAlarmPolicies(deviceInfo, itemInfo, payVal.data, uploadtime); //保存到TSDB RecordToTsdb(deviceInfo, itemInfo, payVal.data, uploadtime); } catch (Exception ex) { log.ErrorFormat("内部错误:{0},{1}", ex.Message, ex.StackTrace); continue; } } } catch (Exception ex) { log.ErrorFormat("内部错误:{0},{1}", ex.Message, ex.StackTrace); return; } }