/// <summary> ///利用ID获取设备信息 /// </summary> /// <returns></returns> public ReturnItem <RetDeviceInfo> GetDeviceInfo(GetDeviceInfoParameter parameter) { WebApiPostParameter wparameter = new WebApiPostParameter() { Url = CustomConfigParam.DeviceApiUrl + "Api/EquipmentInfo/GetDeviceInfoInside" }; if (!string.IsNullOrEmpty(parameter.ID)) { wparameter.Content.Add("ID", parameter.ID.ToString());// 传递设备ID } if (!string.IsNullOrEmpty(parameter.DeviceLabel)) { wparameter.Content.Add("DeviceLabel", parameter.DeviceLabel); } if (!string.IsNullOrEmpty(parameter.Phone)) { wparameter.Content.Add("Phone", parameter.Phone); } if (!string.IsNullOrEmpty(parameter.OrgID)) { wparameter.Content.Add("OrgID", parameter.OrgID); } return(new WebApiHelper().GetEntity <RetDeviceInfo>(wparameter)); }
/// <summary> /// 获取所有设备列表 /// </summary> /// <returns></returns> public ReturnItem <List <RetDeviceInfo> > GetAllDeviceList(GetDeviceInfoParameter parameter) { WebApiPostParameter wparameter = new WebApiPostParameter() { Url = CustomConfigParam.DeviceApiUrl + "Api/EquipmentInfo/GetAllDeviceListInside" }; if (!string.IsNullOrEmpty(parameter.Status)) { wparameter.Content.Add("Status", parameter.Status); } return(new WebApiHelper().GetEntity <List <RetDeviceInfo> >(wparameter)); }
/// <summary> /// 获取当前组织设备列表 /// </summary> /// <returns></returns> public ReturnItem <List <RetDeviceInfo> > GetDeviceList(GetDeviceInfoParameter parameter) { string tokenId = ""; var getdic = JsonHelper.JsonToEntity <Hashtable>(HttpContext.Current.Request.Headers["tokenid"].ToBase64DecryptString()); tokenId = getdic["tokenid"].ToString(); parameter.TokenId = tokenId; WebApiPostParameter wparameter = new WebApiPostParameter() { Url = CustomConfigParam.DeviceApiUrl + "Api/EquipmentInfo/GetDeviceListInside" }; parameter.SetPostParameter(wparameter);//填充请求参数 if (!string.IsNullOrEmpty(parameter.Status)) { wparameter.Content.Add("Status", parameter.Status); } return(new WebApiHelper().GetEntity <List <RetDeviceInfo> >(wparameter)); }
/// <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="deviceId"></param> public void EnableDevice(long deviceId) { DeviceMonitoringApi deviceMonitoringApi = new DeviceMonitoringApi(); GetDeviceInfoParameter par = new GetDeviceInfoParameter(); par.ID = deviceId.ToString(); RetDeviceInfo deviceInfo = null; var resDeviceInfo = deviceMonitoringApi.GetDeviceInfo(par); if (resDeviceInfo.Code != -1) { deviceInfo = resDeviceInfo.Data; StartUpDeviceService(deviceInfo); } else { log.Error("获取设备信息出错:" + resDeviceInfo.Msg); return; } }
/// <summary> /// 禁用设备的处理 /// </summary> /// <param name="id"></param> public void DisableDeivce(long deviceId) { DeviceMonitoringApi deviceMonitoringApi = new DeviceMonitoringApi(); GetDeviceInfoParameter par = new GetDeviceInfoParameter(); par.ID = deviceId.ToString(); RetDeviceInfo deviceInfo = null; var resDeviceInfo = deviceMonitoringApi.GetDeviceInfo(par); if (resDeviceInfo.Code != -1) { deviceInfo = resDeviceInfo.Data; if (MqttServiceContainer.Instance.IsClientExist(long.Parse(deviceInfo.IoTHubID))) { RetIoTHubConfiguration connectInfo = GetConnectInfoById(deviceInfo.IoTHubID); if (null != connectInfo) { if (connectInfo.Type == "1") { //设备直连,删除Topics BatchUnsubMessage(deviceInfo, MqttServiceContainer.Instance.GetMqttServiceByConnectID(long.Parse(deviceInfo.IoTHubID))); } else if (connectInfo.Type == "3") { //研华网关,删除remark中的topic MqttServiceContainer.Instance.GetMqttServiceByConnectID(long.Parse(deviceInfo.IoTHubID)).UnsubscribeMessage(deviceInfo.Remark); } } log.InfoFormat("[MQTT] Device {0},Service Disable.", deviceInfo.Name); } } else { log.Error("获取设备信息出错:" + resDeviceInfo.Msg); return; } }
/// <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; } }