using Newtonsoft.Json; using System; using System.Linq; using System.ServiceProcess; using System.Text; using HslCommunication.MQTT; using HslCommunication; using DataService; using System.Collections.Generic; using System.Runtime.Remoting.Channels; using HslCommunication.Profinet.Geniitek; using HslCommunication.Profinet.AllenBradley; using System.Security.Policy; using System.Text.RegularExpressions; using LitJson; using Newtonsoft.Json.Linq; using System.Xml.Linq; using System.Runtime.InteropServices; using HslCommunication.DTU; using System.Collections; using System.Data; using System.Reflection; using System.Runtime.Serialization.Formatters; namespace Security { partial class ServiceSecurity : ServiceBase { static log4net.ILog log; //static DataService.BLL.vis_fire_ip bll_ip=new DataService.BLL.vis_fire_ip(); static MqttClient mqtt_client1; static MqttClient mqtt_client2; static MqttClient mqtt_client3; static DataService.BLL.device_info bll_info = new DataService.BLL.device_info(); static DataService.BLL.electricity_data bll = new DataService.BLL.electricity_data(); static DataService.BLL.pollution_discharge bll_pollution = new DataService.BLL.pollution_discharge(); static DataService.BLL.meteorological_station bll_meteorological = new DataService.BLL.meteorological_station(); static DataService.BLL.lighting_info bll_lighting_info = new DataService.BLL.lighting_info(); public ServiceSecurity() { InitializeComponent(); } public void DebugOnStart(string[] args) { OnStart(args); } protected override void OnStart(string[] args) { // TODO: 在此处添加代码以启动服务。 log4net.Config.XmlConfigurator.Configure(); log = log4net.LogManager.GetLogger("loginfo"); log.Info("服务开始"); // 授权示例 调用一次即可 call only once if (!HslCommunication.Authorization.SetAuthorizationCode("4772a1a2-72cf-4fcb-9a8e-9cbdd5984253")) { return; } MqttConnectionOptions options = new MqttConnectionOptions() { IpAddress = "127.0.0.1", Port = 1883, ClientId = "Service-" + Guid.NewGuid().ToString("N"), }; options.Credentials = new MqttCredential("dev", "12345"); mqtt_client1 = new MqttClient(options); log.Info("开始"); // 接收到数据的时候进行触发 mqtt_client1.OnMqttMessageReceived += Client1_OnMqttMessageReceived; // 订阅服务器的主题,在连接成功后就去订阅 mqtt_client1.OnClientConnected += m => { Console.WriteLine("连接成功"); m.SubscribeMessage("/ge/data/6afc0999-ac07-4f45-b0d3-d3763456edb4"); m.SubscribeMessage("/ge/data/9b841d78-68ca-45e5-ba5a-35dc43560c39"); m.SubscribeMessage("/ge/data/bbb05637-34f2-4c1d-864e-6bbfca39d38b"); m.SubscribeMessage("/ge/data/e7af4604-30f8-41d6-a4d0-90c969e77a62"); }; mqtt_client1.OnNetworkError += MqttClient1_OnNetworkError; // 自己来控制异常及重连的操作 OperateResult result1 = mqtt_client1.ConnectServer(); if (result1.IsSuccess) { log.Info("连接成功"); } else { log.Info("连接失败"); } MqttConnectionOptions options2 = new MqttConnectionOptions() { IpAddress = "127.0.0.1", Port = 1883, ClientId = "Service-" + Guid.NewGuid().ToString("N"), }; options2.Credentials = new MqttCredential("dev", "12345"); mqtt_client2 = new MqttClient(options2); // 接收到数据的时候进行触发 mqtt_client2.OnMqttMessageReceived += Client2_OnMqttMessageReceived; // 订阅服务器的主题,在连接成功后就去订阅 mqtt_client2.OnClientConnected += m => { Console.WriteLine("连接成功"); m.SubscribeMessage("/ge/data/4d9fab63-eecd-4c33-86f1-79abb16ae62a"); m.SubscribeMessage("/ge/data/e7af4604-30f8-41d6-a4d0-90c969e77a62"); }; mqtt_client2.OnNetworkError += MqttClient2_OnNetworkError; // 自己来控制异常及重连的操作 OperateResult result2 = mqtt_client2.ConnectServer(); if (result2.IsSuccess) { log.Info("连接成功"); } else { log.Info("连接失败"); } MqttConnectionOptions options3 = new MqttConnectionOptions() { IpAddress = "127.0.0.1", Port = 1883, ClientId = "Service-" + Guid.NewGuid().ToString("N"), }; options3.Credentials = new MqttCredential("dev", "12345"); mqtt_client3 = new MqttClient(options3); // 接收到数据的时候进行触发 mqtt_client3.OnMqttMessageReceived += Client3_OnMqttMessageReceived; // 订阅服务器的主题,在连接成功后就去订阅 mqtt_client3.OnClientConnected += m => { Console.WriteLine("连接成功"); m.SubscribeMessage("/ge/data/462f192b-a689-4246-bf79-1a9ac076a462"); m.SubscribeMessage("/ge/data/fe4086e9-d2f4-42e1-9058-908f93251c45"); m.SubscribeMessage("/ge/data/3af8becd-c5ff-48e2-9826-00f4c7df8047"); m.SubscribeMessage("/ge/data/50505de0-cf02-4ebd-b332-2031e897894f"); m.SubscribeMessage("/ge/data/690dd79a-003d-4b93-a1a5-bbac3ffbf2d5"); m.SubscribeMessage("/ge/data/d7d5fe7f-f383-45ff-a8c4-9f946f391179"); m.SubscribeMessage("/ge/data/e7592a36-e5e4-4478-89ae-dd22dcd2e779"); m.SubscribeMessage("/ge/data/6a61e575-f246-4fab-ab6c-b2d129cfb34e"); m.SubscribeMessage("/ge/data/49da8dfa-35be-40c2-a461-73a292162b17"); m.SubscribeMessage("/ge/data/f151df53-3b19-4add-88f3-c5a875e38651"); m.SubscribeMessage("/ge/data/e7af4604-30f8-41d6-a4d0-90c969e77a62"); }; mqtt_client3.OnNetworkError += MqttClient3_OnNetworkError; // 自己来控制异常及重连的操作 OperateResult result3 = mqtt_client3.ConnectServer(); if (result3.IsSuccess) { log.Info("连接成功"); } else { log.Info("连接失败"); } } private static void MqttClient1_OnNetworkError(object sender, EventArgs e) { try { // 当网络异常的时候触发,可以在此处重连服务器 if (sender is MqttClient client) { // 开始重连服务器,直到连接成功为止 log.Info("网络异常,准备10秒后重新连接。"); while (true) { // 每隔10秒重连 System.Threading.Thread.Sleep(10_000); log.Info("准备重新连接服务器..."); // 重连之前需要判断是否关闭了Client,自己重写的异常需要自己手动处理 OperateResult connect = client.ConnectServer(); if (connect.IsSuccess) { // 连接成功后,可以在下方break之前进行订阅,或是数据初始化操作 log.Info("连接服务器成功!"); client.SubscribeMessage("/ge/data/6afc0999-ac07-4f45-b0d3-d3763456edb4"); client.SubscribeMessage("/ge/data/9b841d78-68ca-45e5-ba5a-35dc43560c39"); client.SubscribeMessage("/ge/data/bbb05637-34f2-4c1d-864e-6bbfca39d38b"); client.SubscribeMessage("/ge/data/e7af4604-30f8-41d6-a4d0-90c969e77a62"); break; } log.Info("连接失败,准备10秒后重新连接。"); } } } catch (Exception ex) { log.Info("重连MQTT失败,失败原因:" + ex.Message); } } private static void MqttClient2_OnNetworkError(object sender, EventArgs e) { try { // 当网络异常的时候触发,可以在此处重连服务器 if (sender is MqttClient client) { // 开始重连服务器,直到连接成功为止 log.Info("网络异常,准备10秒后重新连接。"); while (true) { // 每隔10秒重连 System.Threading.Thread.Sleep(10_000); log.Info("准备重新连接服务器..."); // 重连之前需要判断是否关闭了Client,自己重写的异常需要自己手动处理 OperateResult connect = client.ConnectServer(); if (connect.IsSuccess) { // 连接成功后,可以在下方break之前进行订阅,或是数据初始化操作 log.Info("连接服务器成功!"); client.SubscribeMessage("/ge/data/4d9fab63-eecd-4c33-86f1-79abb16ae62a"); client.SubscribeMessage("/ge/data/e7af4604-30f8-41d6-a4d0-90c969e77a62"); break; } log.Info("连接失败,准备10秒后重新连接。"); } } } catch (Exception ex) { log.Info("重连MQTT失败,失败原因:" + ex.Message); } } private static void MqttClient3_OnNetworkError(object sender, EventArgs e) { try { // 当网络异常的时候触发,可以在此处重连服务器 if (sender is MqttClient client) { // 开始重连服务器,直到连接成功为止 log.Info("网络异常,准备10秒后重新连接。"); while (true) { // 每隔10秒重连 System.Threading.Thread.Sleep(10_000); log.Info("准备重新连接服务器..."); // 重连之前需要判断是否关闭了Client,自己重写的异常需要自己手动处理 OperateResult connect = client.ConnectServer(); if (connect.IsSuccess) { // 连接成功后,可以在下方break之前进行订阅,或是数据初始化操作 log.Info("连接服务器成功!"); client.SubscribeMessage("/ge/data/462f192b-a689-4246-bf79-1a9ac076a462"); client.SubscribeMessage("/ge/data/fe4086e9-d2f4-42e1-9058-908f93251c45"); client.SubscribeMessage("/ge/data/3af8becd-c5ff-48e2-9826-00f4c7df8047"); client.SubscribeMessage("/ge/data/50505de0-cf02-4ebd-b332-2031e897894f"); client.SubscribeMessage("/ge/data/690dd79a-003d-4b93-a1a5-bbac3ffbf2d5"); client.SubscribeMessage("/ge/data/d7d5fe7f-f383-45ff-a8c4-9f946f391179"); client.SubscribeMessage("/ge/data/e7592a36-e5e4-4478-89ae-dd22dcd2e779"); client.SubscribeMessage("/ge/data/6a61e575-f246-4fab-ab6c-b2d129cfb34e"); client.SubscribeMessage("/ge/data/49da8dfa-35be-40c2-a461-73a292162b17"); client.SubscribeMessage("/ge/data/f151df53-3b19-4add-88f3-c5a875e38651"); client.SubscribeMessage("/ge/data/e7af4604-30f8-41d6-a4d0-90c969e77a62"); break; } log.Info("连接失败,准备10秒后重新连接。"); } } } catch (Exception ex) { log.Info("重连MQTT失败,失败原因:" + ex.Message); } } private static void Client1_OnMqttMessageReceived(MqttClient client, string topic, byte[] payload) { var msg = Encoding.UTF8.GetString(payload); try { var date = DateTime.Now.ToString("yyyyMM"); var a = bll.IsExistsTable("changdao_zhanlanguang", "electricity_data_" + date); if (a == false) { bll.CreateTable(date); } //配电室低压 if (topic.Contains("/6afc0999-ac07-4f45-b0d3-d3763456edb4")) { //log.Info("配电室低压msg" + msg); //配电室低压 var j = msg.Substring(1); var data = JsonConvert.DeserializeObject(j); //log.Info("配电室低压:" + data); if (data.b != null) { foreach (var item in data.b.dvl) { var name = ""; char delimiter = '.'; // 分隔符 int lastDotIndex = item.da.LastIndexOf(delimiter); // 查找最后一个分隔符的位置 if (lastDotIndex != -1) // 如果找到分隔符 { name = item.da.Substring(lastDotIndex + 1); // 截取分隔符后面的子字符串 } if (!string.IsNullOrEmpty(name)) { var device_name = GetDeviceName1(Convert.ToInt32(name)); var now = DateTime.Now.AddHours(-1).ToString("yyyy-MM-dd HH:00:00"); var ddate = Convert.ToDateTime(now); var dtime = DateTime.Now.AddMonths(-1).ToString("yyyyMM"); var sddate = Convert.ToDateTime(ddate.AddHours(-1)); var list = bll.GetModelListDate(" Reserve1='配电室低压' ", date).Where(x => x.EntireTime == ddate).ToList(); //if (list.Count < 74) //{ // var list1 = list.Select(x => x.DeviceName).ToList(); // var list2 = bll_info.GetModelLists(list1, "配电室低压"); // var vlist = bll.GetModelListDate(" Reserve1='配电室低压' ", date); // if (vlist.Count == 0) // { // vlist = bll.GetModelListDate(" Reserve1='配电室低压' ", dtime); // } // foreach (var aitem in list2) // { // var svlist = vlist.Where(x => x.DeviceName == aitem.DeviceName && x.EntireTime < ddate).OrderByDescending(x => x.EntireTime).FirstOrDefault(); // if (svlist != null) // { // var model = new DataService.Model.electricity_data(); // model.ElectricityId = Guid.NewGuid().ToString("N"); // model.DeviceName = aitem.DeviceName; // model.EH = svlist.EH; // model.P = svlist.P; // model.Ia = svlist.Ia; // model.Ib = svlist.Ib; // model.Ic = svlist.Ic; // model.Ua = svlist.Ua; // model.Ub = svlist.Ub; // model.Uc = svlist.Uc; // model.Kvar = svlist.Kvar; // model.CreateTime = DateTime.Now; // model.EntireTime = Convert.ToDateTime(DateTime.Now.AddHours(-1).ToString("yyyy-MM-dd HH:00:00")); // model.Reserve1 = svlist.Reserve1; // model.Reserve2 = svlist.Reserve2; // bll.AddDate(model, date); // } // } //} var edate = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd HH:00:00")); var clist = bll.GetModelListDate("", date).Where(x => x.DeviceName == device_name && x.EntireTime == Convert.ToDateTime(edate)).ToList(); if (clist.Count == 0) { var model = new DataService.Model.electricity_data(); model.ElectricityId = Guid.NewGuid().ToString("N"); model.DeviceName = device_name; #region 数据添加 if (item.dl.ContainsKey("01040005")) { model.EH = item.dl["01040005"]; } if (item.dl.ContainsKey("01010001")) { model.Ua = item.dl["01010001"]; } if (item.dl.ContainsKey("01010002")) { model.Ub = item.dl["01010002"]; } if (item.dl.ContainsKey("01010003")) { model.Uc = item.dl["01010003"]; } if (item.dl.ContainsKey("01020001")) { model.Ia = item.dl["01020001"]; } if (item.dl.ContainsKey("01020002")) { model.Ib = item.dl["01020002"]; } if (item.dl.ContainsKey("01020003")) { model.Ic = item.dl["01020003"]; } if (item.dl.ContainsKey("01030004")) { model.P = item.dl["01030004"]; } if (item.dl.ContainsKey("01030012")) { model.Kvar = item.dl["01030012"]; } #endregion model.CreateTime = DateTime.Now; model.EntireTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd HH:00:00")); model.Reserve1 = "配电室低压"; model.Reserve2 = GetDeviceName4(Convert.ToInt32(name)); if (model.EH != null && model.Ua != null && model.Ub != null && model.Uc != null && model.Ia != null && model.Ib != null && model.Ic != null && model.P != null && model.Kvar != null) { bll.AddDate(model, date); } } } } } } //配电室高压 if (topic.Contains("/9b841d78-68ca-45e5-ba5a-35dc43560c39")) { //log.Info("配电室高压msg" + msg); //配电室高压 var j = msg.Substring(1); var data = JsonConvert.DeserializeObject(j); //log.Info("配电室高压:" + data); if (data.b != null) { foreach (var item in data.b.dvl) { var name = ""; char delimiter = '.'; // 分隔符 int lastDotIndex = item.da.LastIndexOf(delimiter); // 查找最后一个分隔符的位置 if (lastDotIndex != -1) // 如果找到分隔符 { name = item.da.Substring(lastDotIndex + 1); // 截取分隔符后面的子字符串 } if (!string.IsNullOrEmpty(name)) { var now = DateTime.Now.AddHours(-1).ToString("yyyy-MM-dd HH:00:00"); var ddate = Convert.ToDateTime(now); var dtime = DateTime.Now.AddMonths(-1).ToString("yyyyMM"); var sddate = Convert.ToDateTime(ddate.AddHours(-1)); var list = bll.GetModelListDate(" Reserve1='配电室高压' ", date).Where(x => x.EntireTime == ddate).ToList(); //if (list.Count < 2) //{ // var list1 = list.Select(x => x.DeviceName).ToList(); // var list2 = bll_info.GetModelLists(list1, "配电室高压"); // var vlist = bll.GetModelListDate(" Reserve1='配电室高压' ", date); // if (vlist.Count == 0) // { // vlist = bll.GetModelListDate(" Reserve1='配电室高压' ", dtime); // } // foreach (var aitem in list2) // { // var svlist = vlist.Where(x => x.DeviceName == aitem.DeviceName && x.EntireTime < ddate).OrderByDescending(x => x.EntireTime).FirstOrDefault(); // if (svlist != null) // { // var model = new DataService.Model.electricity_data(); // model.ElectricityId = Guid.NewGuid().ToString("N"); // model.DeviceName = aitem.DeviceName; // model.EH = svlist.EH; // model.P = svlist.P; // model.Ia = svlist.Ia; // model.Ib = svlist.Ib; // model.Ic = svlist.Ic; // model.Ua = svlist.Ua; // model.Ub = svlist.Ub; // model.Uc = svlist.Uc; // model.Kvar = svlist.Kvar; // model.CreateTime = DateTime.Now; // model.EntireTime = Convert.ToDateTime(DateTime.Now.AddHours(-1).ToString("yyyy-MM-dd HH:00:00")); // model.Reserve1 = svlist.Reserve1; // model.Reserve2 = svlist.Reserve2; // bll.AddDate(model, date); // } // } //} var device_name = GetDeviceName2(Convert.ToInt32(name)); var edate = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd HH:00:00")); var clist = bll.GetModelListDate("", date).Where(x => x.DeviceName == device_name && x.EntireTime == Convert.ToDateTime(edate)).ToList(); if (clist.Count == 0) { var model = new DataService.Model.electricity_data(); model.ElectricityId = Guid.NewGuid().ToString("N"); model.DeviceName = device_name; #region 数据添加 if (item.dl.ContainsKey("01040005")) { model.EH = item.dl["01040005"]; } if (item.dl.ContainsKey("01010007")) { model.Ua = item.dl["01010007"]; } if (item.dl.ContainsKey("01010008")) { model.Ub = item.dl["01010008"]; } if (item.dl.ContainsKey("01010009")) { model.Uc = item.dl["01010009"]; } if (item.dl.ContainsKey("01020001")) { model.Ia = item.dl["01020001"]; } if (item.dl.ContainsKey("01020002")) { model.Ib = item.dl["01020002"]; } if (item.dl.ContainsKey("01020003")) { model.Ic = item.dl["01020003"]; } if (item.dl.ContainsKey("01030004")) { model.P = item.dl["01030004"]; } if (item.dl.ContainsKey("01030012")) { model.Kvar = item.dl["01030012"]; } #endregion model.CreateTime = DateTime.Now; model.EntireTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd HH:00:00")); model.Reserve1 = "配电室高压"; //model.Reserve2 = GetDeviceName4(Convert.ToInt32(name)); if (model.EH != null && model.Ua != null && model.Ub != null && model.Uc != null && model.Ia != null && model.Ib != null && model.Ic != null && model.P != null && model.Kvar != null) { bll.AddDate(model, date); } } } } } } //电能-室外箱变 if (topic.Contains("/bbb05637-34f2-4c1d-864e-6bbfca39d38b")) { //log.Info("电能-室外箱变msg" + msg); //电能-室外箱变 var j = msg.Substring(1); var data = JsonConvert.DeserializeObject(j); //log.Info("电能-室外箱变:" + data); if (data.b != null) { foreach (var item in data.b.dvl) { var name = ""; char delimiter = '.'; // 分隔符 int lastDotIndex = item.da.LastIndexOf(delimiter); // 查找最后一个分隔符的位置 if (lastDotIndex != -1) // 如果找到分隔符 { name = item.da.Substring(lastDotIndex + 1); // 截取分隔符后面的子字符串 } if (!string.IsNullOrEmpty(name)) { log.Info("电能-室外箱变:" + msg); var now = DateTime.Now.AddHours(-1).ToString("yyyy-MM-dd HH:00:00"); var ddate = Convert.ToDateTime(now); var dtime = DateTime.Now.AddMonths(-1).ToString("yyyyMM"); var sddate = Convert.ToDateTime(ddate.AddHours(-1)); var list = bll.GetModelListDate(" Reserve1='电能-室外箱变' ", date).Where(x => x.EntireTime == ddate).ToList(); //if (list.Count < 8) //{ // var list1 = list.Select(x => x.DeviceName).ToList(); // var list2 = bll_info.GetModelLists(list1, "电能-室外箱变"); // var vlist = bll.GetModelListDate(" Reserve1='电能-室外箱变' ", date); // if (vlist.Count == 0) // { // vlist = bll.GetModelListDate(" Reserve1='电能-室外箱变' ", dtime); // } // foreach (var aitem in list2) // { // var svlist = vlist.Where(x => x.DeviceName == aitem.DeviceName && x.EntireTime < ddate).OrderByDescending(x => x.EntireTime).FirstOrDefault(); // if (svlist != null) // { // var model = new DataService.Model.electricity_data(); // model.ElectricityId = Guid.NewGuid().ToString("N"); // model.DeviceName = aitem.DeviceName; // model.EH = svlist.EH; // model.P = svlist.P; // model.Ia = svlist.Ia; // model.Ib = svlist.Ib; // model.Ic = svlist.Ic; // model.Ua = svlist.Ua; // model.Ub = svlist.Ub; // model.Uc = svlist.Uc; // model.Kvar = svlist.Kvar; // model.CreateTime = DateTime.Now; // model.EntireTime = Convert.ToDateTime(DateTime.Now.AddHours(-1).ToString("yyyy-MM-dd HH:00:00")); // model.Reserve1 = svlist.Reserve1; // model.Reserve2 = svlist.Reserve2; // bll.AddDate(model, date); // } // } //} var device_name = GetDeviceName3(Convert.ToInt32(name)); var edate = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd HH:00:00")); var clist = bll.GetModelListDate("", date).Where(x => x.DeviceName == device_name && x.EntireTime == Convert.ToDateTime(edate)).ToList(); if (clist.Count == 0) { var model = new DataService.Model.electricity_data(); model.ElectricityId = Guid.NewGuid().ToString("N"); model.DeviceName = device_name; #region 数据添加 if (item.dl.ContainsKey("01040005")) { model.EH = item.dl["01040005"]; } if (item.dl.ContainsKey("01010001")) { model.Ua = item.dl["01010001"]; } if (item.dl.ContainsKey("01010002")) { model.Ub = item.dl["01010002"]; } if (item.dl.ContainsKey("01010003")) { model.Uc = item.dl["01010003"]; } if (item.dl.ContainsKey("01020001")) { model.Ia = item.dl["01020001"]; } if (item.dl.ContainsKey("01020002")) { model.Ib = item.dl["01020002"]; } if (item.dl.ContainsKey("01020003")) { model.Ic = item.dl["01020003"]; } if (item.dl.ContainsKey("01030004")) { model.P = item.dl["01030004"]; } if (item.dl.ContainsKey("01030012")) { model.Kvar = item.dl["01030012"]; } #endregion model.CreateTime = DateTime.Now; model.EntireTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd HH:00:00")); model.Reserve1 = "电能-室外箱变"; //model.Reserve2 = GetDeviceName4(Convert.ToInt32(name)); if (model.EH != null && model.Ua != null && model.Ub != null && model.Uc != null && model.Ia != null && model.Ib != null && model.Ic != null && model.P != null && model.Kvar != null) { bll.AddDate(model, date); } //else //{ // log.Info("电能-室外箱变msg" + msg); //} } } } } } //气象站 if (topic.Contains("/e7af4604-30f8-41d6-a4d0-90c969e77a62")) { //气象站 //log.Info("气象站msg" + msg); var j = msg.Substring(1); var data = JsonConvert.DeserializeObject(j); //log.Info("气象站:" + data); var now = DateTime.Now; if (data.b != null) { foreach (var item in data.b.dvl) { var name = ""; char delimiter = '.'; // 分隔符 int lastDotIndex = item.da.LastIndexOf(delimiter); // 查找最后一个分隔符的位置 if (lastDotIndex != -1) // 如果找到分隔符 { name = item.da.Substring(lastDotIndex + 1); // 截取分隔符后面的子字符串 } if (name == "33") { var list = bll_meteorological.GetModelList(string.Format(" MeteorologicalNumber='{0}' ", name)); if (item.dl.ContainsKey("当前雨量")) { var meteorological_name = "雨量"; var model = list.Where(x => x.MeteorologicalName == meteorological_name).FirstOrDefault(); if (model != null) { var is_update = false; decimal? number; var stringnumber = item.dl["当前雨量"]; char delimiters = ':'; // 分隔符 int lastDotIndexs = stringnumber.LastIndexOf(delimiters); // 查找最后一个分隔符的位置 if (lastDotIndexs != -1) // 如果找到分隔符 { var num = stringnumber.Substring(lastDotIndexs + 1); number = Convert.ToDecimal(num); // 截取分隔符后面的子字符串 if (model.MeteorologicalValue != number) { model.MeteorologicalValue = number; is_update = true; } } else { var num = Convert.ToDecimal(stringnumber); if (model.MeteorologicalValue != num) { model.MeteorologicalValue = num; is_update = true; } } if (is_update) { model.EntireTime = now; bll_meteorological.Update(model); } } else { model = new DataService.Model.meteorological_station(); //气象站 model.MeteorologicalId = Guid.NewGuid().ToString("N"); model.MeteorologicalNumber = name; model.MeteorologicalName = "雨量"; decimal? number; var stringnumber = item.dl["当前雨量"]; char delimiters = ':'; // 分隔符 int lastDotIndexs = stringnumber.LastIndexOf(delimiters); // 查找最后一个分隔符的位置 if (lastDotIndexs != -1) // 如果找到分隔符 { var num = stringnumber.Substring(lastDotIndexs + 1); number = Convert.ToDecimal(num); // 截取分隔符后面的子字符串 model.MeteorologicalValue = number; } else { var num = Convert.ToDecimal(stringnumber); model.MeteorologicalValue = num; } model.CreateTime = now; model.EntireTime = now; bll_meteorological.Add(model); } } if (item.dl.ContainsKey("雨雪状态1")) { var meteorological_name = "雨雪状态"; var model = list.Where(x => x.MeteorologicalName == meteorological_name).FirstOrDefault(); if (model != null) { var is_update = false; decimal? number; var stringnumber = item.dl["雨雪状态1"]; char delimiters = ':'; // 分隔符lastDotIndexs + 1 int lastDotIndexs = stringnumber.LastIndexOf(delimiters); // 查找最后一个分隔符的位置 if (lastDotIndexs != -1) // 如果找到分隔符 { var num = stringnumber.Substring(lastDotIndexs + 1); number = Convert.ToDecimal(num); // 截取分隔符后面的子字符串 if (model.MeteorologicalValue != number) { model.MeteorologicalValue = number; is_update = true; } } else { var num = Convert.ToDecimal(stringnumber); if (model.MeteorologicalValue != num) { model.MeteorologicalValue = num; is_update = true; } } if (is_update) { model.EntireTime = now; bll_meteorological.Update(model); } } else { model = new DataService.Model.meteorological_station(); //气象站 model.MeteorologicalId = Guid.NewGuid().ToString("N"); model.MeteorologicalNumber = name; model.MeteorologicalName = "雨雪状态"; decimal? number; var stringnumber = item.dl["雨雪状态1"]; char delimiters = ':'; // 分隔符lastDotIndexs + 1 int lastDotIndexs = stringnumber.LastIndexOf(delimiters); // 查找最后一个分隔符的位置 if (lastDotIndexs != -1) // 如果找到分隔符 { var num = stringnumber.Substring(lastDotIndexs + 1); number = Convert.ToDecimal(num); // 截取分隔符后面的子字符串 model.MeteorologicalValue = number; } else { var num = Convert.ToDecimal(stringnumber); model.MeteorologicalValue = num; } model.CreateTime = now; model.EntireTime = now; bll_meteorological.Add(model); } } if (item.dl.ContainsKey("风向")) { var meteorological_name = "风向"; var model = list.Where(x => x.MeteorologicalName == meteorological_name).FirstOrDefault(); if (model != null) { var is_update = false; decimal? number; var stringnumber = item.dl["风向"]; char delimiters = ':'; // 分隔符 int lastDotIndexs = stringnumber.LastIndexOf(delimiters); // 查找最后一个分隔符的位置 if (lastDotIndexs != -1) // 如果找到分隔符 { var num = stringnumber.Substring(lastDotIndexs + 1); number = Convert.ToDecimal(num); // 截取分隔符后面的子字符串 if (model.MeteorologicalValue != number) { model.MeteorologicalValue = number; is_update = true; } } else { var num = Convert.ToDecimal(stringnumber); if (model.MeteorologicalValue != num) { model.MeteorologicalValue = num; is_update = true; } } if (is_update) { model.EntireTime = now; bll_meteorological.Update(model); } } else { model = new DataService.Model.meteorological_station(); //气象站 model.MeteorologicalId = Guid.NewGuid().ToString("N"); model.MeteorologicalNumber = name; model.MeteorologicalName = "风向"; decimal? number; var stringnumber = item.dl["风向"]; char delimiters = ':'; // 分隔符 int lastDotIndexs = stringnumber.LastIndexOf(delimiters); // 查找最后一个分隔符的位置 if (lastDotIndexs != -1) // 如果找到分隔符 { var num = stringnumber.Substring(lastDotIndexs + 1); number = Convert.ToDecimal(num); // 截取分隔符后面的子字符串 model.MeteorologicalValue = number; } else { var num = Convert.ToDecimal(stringnumber); model.MeteorologicalValue = num; } model.CreateTime = now; model.EntireTime = now; bll_meteorological.Add(model); } } if (item.dl.ContainsKey("光照度高16位")) { var meteorological_name = "光照度"; var model = list.Where(x => x.MeteorologicalName == meteorological_name).FirstOrDefault(); if (model != null) { var is_update = false; decimal? number; var stringnumber = item.dl["光照度高16位"]; char delimiters = ':'; // 分隔符 int lastDotIndexs = stringnumber.LastIndexOf(delimiters); // 查找最后一个分隔符的位置 if (lastDotIndexs != -1) // 如果找到分隔符 { var num = stringnumber.Substring(lastDotIndexs + 1); number = Convert.ToDecimal(num); // 截取分隔符后面的子字符串 if (model.MeteorologicalValue != number) { model.MeteorologicalValue = number; is_update = true; } } else { var num = Convert.ToDecimal(stringnumber); if (model.MeteorologicalValue != num) { model.MeteorologicalValue = num; is_update = true; } } if (is_update) { model.EntireTime = now; bll_meteorological.Update(model); } } else { model = new DataService.Model.meteorological_station(); //气象站 model.MeteorologicalId = Guid.NewGuid().ToString("N"); model.MeteorologicalNumber = name; model.MeteorologicalName = "光照度"; decimal? number; var stringnumber = item.dl["光照度高16位"]; char delimiters = ':'; // 分隔符 int lastDotIndexs = stringnumber.LastIndexOf(delimiters); // 查找最后一个分隔符的位置 if (lastDotIndexs != -1) // 如果找到分隔符 { var num = stringnumber.Substring(lastDotIndexs + 1); number = Convert.ToDecimal(num); // 截取分隔符后面的子字符串 model.MeteorologicalValue = number; } else { var num = Convert.ToDecimal(stringnumber); model.MeteorologicalValue = num; } model.CreateTime = now; model.EntireTime = now; bll_meteorological.Add(model); } } if (item.dl.ContainsKey("风速")) { var meteorological_name = "风速"; var model = list.Where(x => x.MeteorologicalName == meteorological_name).FirstOrDefault(); if (model != null) { var is_update = false; decimal? number; var stringnumber = item.dl["风速"]; char delimiters = ':'; // 分隔符 int lastDotIndexs = stringnumber.LastIndexOf(delimiters); // 查找最后一个分隔符的位置 if (lastDotIndexs != -1) // 如果找到分隔符 { var num = stringnumber.Substring(lastDotIndexs + 1); number = Convert.ToDecimal(num); // 截取分隔符后面的子字符串 if (model.MeteorologicalValue != number) { model.MeteorologicalValue = number; is_update = true; } } else { var num = Convert.ToDecimal(stringnumber); if (model.MeteorologicalValue != num) { model.MeteorologicalValue = num; is_update = true; } } if (is_update) { model.EntireTime = now; bll_meteorological.Update(model); } } else { model = new DataService.Model.meteorological_station(); //气象站 model.MeteorologicalId = Guid.NewGuid().ToString("N"); model.MeteorologicalNumber = name; model.MeteorologicalName = "风速"; decimal? number; var stringnumber = item.dl["风速"]; char delimiters = ':'; // 分隔符 int lastDotIndexs = stringnumber.LastIndexOf(delimiters); // 查找最后一个分隔符的位置 if (lastDotIndexs != -1) // 如果找到分隔符 { var num = stringnumber.Substring(lastDotIndexs + 1); number = Convert.ToDecimal(num); // 截取分隔符后面的子字符串 model.MeteorologicalValue = number; } else { var num = Convert.ToDecimal(stringnumber); model.MeteorologicalValue = num; } model.CreateTime = now; model.EntireTime = now; bll_meteorological.Add(model); } } if (item.dl.ContainsKey("PM2_5")) { var meteorological_name = "空气质量"; var model = list.Where(x => x.MeteorologicalName == meteorological_name).FirstOrDefault(); if (model != null) { var is_update = false; decimal? number; var stringnumber = item.dl["PM2_5"]; char delimiters = ':'; // 分隔符 int lastDotIndexs = stringnumber.LastIndexOf(delimiters); // 查找最后一个分隔符的位置 if (lastDotIndexs != -1) // 如果找到分隔符 { var num = stringnumber.Substring(lastDotIndexs + 1); number = Convert.ToDecimal(num); // 截取分隔符后面的子字符串 if (model.MeteorologicalValue != number) { model.MeteorologicalValue = number; is_update = true; } } else { var num = Convert.ToDecimal(stringnumber); if (model.MeteorologicalValue != num) { model.MeteorologicalValue = num; is_update = true; } } if (is_update) { model.EntireTime = now; bll_meteorological.Update(model); } } else { model = new DataService.Model.meteorological_station(); //气象站 model.MeteorologicalId = Guid.NewGuid().ToString("N"); model.MeteorologicalNumber = name; model.MeteorologicalName = "空气质量"; decimal? number; var stringnumber = item.dl["PM2_5"]; char delimiters = ':'; // 分隔符 int lastDotIndexs = stringnumber.LastIndexOf(delimiters); // 查找最后一个分隔符的位置 if (lastDotIndexs != -1) // 如果找到分隔符 { var num = stringnumber.Substring(lastDotIndexs + 1); number = Convert.ToDecimal(num); // 截取分隔符后面的子字符串 model.MeteorologicalValue = number; } else { var num = Convert.ToDecimal(stringnumber); model.MeteorologicalValue = num; } model.CreateTime = now; model.EntireTime = now; bll_meteorological.Add(model); } } if (item.dl.ContainsKey("空气温度")) { var meteorological_name = "空气温度"; var model = list.Where(x => x.MeteorologicalName == meteorological_name).FirstOrDefault(); if (model != null) { var is_update = false; decimal? number; var stringnumber = item.dl["空气温度"]; char delimiters = ':'; // 分隔符 int lastDotIndexs = stringnumber.LastIndexOf(delimiters); // 查找最后一个分隔符的位置 if (lastDotIndexs != -1) // 如果找到分隔符 { var num = stringnumber.Substring(lastDotIndexs + 1); number = Convert.ToDecimal(num); // 截取分隔符后面的子字符串 if (model.MeteorologicalValue != number) { model.MeteorologicalValue = number; is_update = true; } } else { var num = Convert.ToDecimal(stringnumber); if (model.MeteorologicalValue != num) { model.MeteorologicalValue = num; is_update = true; } } if (is_update) { model.EntireTime = now; bll_meteorological.Update(model); } } else { model = new DataService.Model.meteorological_station(); //气象站 model.MeteorologicalId = Guid.NewGuid().ToString("N"); model.MeteorologicalNumber = name; model.MeteorologicalName = "空气温度"; decimal? number; var stringnumber = item.dl["空气温度"]; char delimiters = ':'; // 分隔符 int lastDotIndexs = stringnumber.LastIndexOf(delimiters); // 查找最后一个分隔符的位置 if (lastDotIndexs != -1) // 如果找到分隔符 { var num = stringnumber.Substring(lastDotIndexs + 1); number = Convert.ToDecimal(num); // 截取分隔符后面的子字符串 model.MeteorologicalValue = number; } else { var num = Convert.ToDecimal(stringnumber); model.MeteorologicalValue = num; } model.CreateTime = now; model.EntireTime = now; bll_meteorological.Add(model); } } } } } } } catch (Exception ex) { //log.Info("配电室低压、高压、电能-室外箱变、气象站处理失败,数据:" + msg + ",原因:" + ex.Message); } } private static void Client2_OnMqttMessageReceived(MqttClient client, string topic, byte[] payload) { var msg = Encoding.UTF8.GetString(payload); try { //排污1 if (topic.Contains("/4d9fab63-eecd-4c33-86f1-79abb16ae62a")) { //log.Info("排污1msg" + msg); //排污1 var now = DateTime.Now; var j = msg.Substring(1); var data = JsonConvert.DeserializeObject(j); //log.Info("排污1:" + data); if (data.b != null) { foreach (var item in data.b.dvl) { var name = ""; char delimiter = '.'; // 分隔符 int lastDotIndex = item.da.LastIndexOf(delimiter); // 查找最后一个分隔符的位置 if (lastDotIndex != -1) // 如果找到分隔符 { name = item.da.Substring(lastDotIndex + 1); // 截取分隔符后面的子字符串 } if (name == "460") { var list = bll_pollution.GetModelList(" Reserve1='460' "); Dictionary new_dl = new Dictionary(); foreach (var dl_item in item.dl) { var value = dl_item.Value; if (dl_item.Value.Contains("bad")) { value = "-1"; } new_dl.Add(dl_item.Key, value); } item.dl = new_dl; #region GP01#水泵 var pollution_discharge_name1 = "GP01-1#水泵"; var model1 = list.Where(x => x.PollutionDischargeName == pollution_discharge_name1).FirstOrDefault(); if (model1 != null) { var is_update = false; if (item.dl.ContainsKey("1DI1")) { var value = Convert.ToInt32(item.dl["1DI1"]); if (model1.PollutionDischargeState != value) { model1.PollutionDischargeState = value; is_update = true; } } if (item.dl.ContainsKey("1DI2")) { var value = Convert.ToInt32(item.dl["1DI2"]); if (model1.PollutionDischargeAlarm != value) { model1.PollutionDischargeAlarm = value; is_update = true; } } if ((now - model1.EntireTime.Value).TotalMinutes >= 1) { model1.EntireTime = now; is_update = true; } if (is_update) { model1.EntireTime = now; bll_pollution.Update(model1); } } else { model1 = new DataService.Model.pollution_discharge(); model1.PollutionDischargeId = Guid.NewGuid().ToString("N"); model1.PollutionDischargeName = pollution_discharge_name1; if (item.dl.ContainsKey("1DI1")) { model1.PollutionDischargeState = Convert.ToInt32(item.dl["1DI1"]); } if (item.dl.ContainsKey("1DI2")) { model1.PollutionDischargeAlarm = Convert.ToInt32(item.dl["1DI2"]); } model1.CreateTime = now; model1.EntireTime = now; model1.Reserve1 = "460"; bll_pollution.Add(model1); } var pollution_discharge_name2 = "GP01-2#水泵"; var model2 = list.Where(x => x.PollutionDischargeName == pollution_discharge_name2).FirstOrDefault(); if (model2 != null) { var is_update = false; if (item.dl.ContainsKey("1DI3")) { var value = Convert.ToInt32(item.dl["1DI3"]); if (model2.PollutionDischargeState != value) { model2.PollutionDischargeState = value; is_update = true; } } if (item.dl.ContainsKey("1DI4")) { var value = Convert.ToInt32(item.dl["1DI4"]); if (model2.PollutionDischargeAlarm != value) { model2.PollutionDischargeAlarm = value; is_update = true; } } if ((now - model2.EntireTime.Value).TotalMinutes >= 1) { model2.EntireTime = now; is_update = true; } if (is_update) { model2.EntireTime = now; bll_pollution.Update(model2); } } else { model2 = new DataService.Model.pollution_discharge(); model2.PollutionDischargeId = Guid.NewGuid().ToString("N"); model2.PollutionDischargeName = pollution_discharge_name2; if (item.dl.ContainsKey("1DI3")) { model2.PollutionDischargeState = Convert.ToInt32(item.dl["1DI3"]); } if (item.dl.ContainsKey("1DI4")) { model2.PollutionDischargeAlarm = Convert.ToInt32(item.dl["1DI4"]); } model2.CreateTime = now; model2.EntireTime = now; model2.Reserve1 = "460"; bll_pollution.Add(model2); } #endregion #region GP02#水泵 var pollution_discharge_name3 = "GP02-1#水泵"; var model3 = list.Where(x => x.PollutionDischargeName == pollution_discharge_name3).FirstOrDefault(); if (model3 != null) { var is_update = false; if (item.dl.ContainsKey("1DI5")) { var value = Convert.ToInt32(item.dl["1DI5"]); if (model3.PollutionDischargeState != value) { model3.PollutionDischargeState = value; is_update = true; } } if (item.dl.ContainsKey("1DI6")) { var value = Convert.ToInt32(item.dl["1DI6"]); if (model3.PollutionDischargeAlarm != value) { model3.PollutionDischargeAlarm = value; is_update = true; } } if ((now - model3.EntireTime.Value).TotalMinutes >= 1) { model3.EntireTime = now; is_update = true; } if (is_update) { model3.EntireTime = now; bll_pollution.Update(model3); } } else { model3 = new DataService.Model.pollution_discharge(); model3.PollutionDischargeId = Guid.NewGuid().ToString("N"); model3.PollutionDischargeName = pollution_discharge_name3; if (item.dl.ContainsKey("1DI5")) { model3.PollutionDischargeState = Convert.ToInt32(item.dl["1DI5"]); } if (item.dl.ContainsKey("1DI6")) { model3.PollutionDischargeAlarm = Convert.ToInt32(item.dl["1DI6"]); } model3.CreateTime = now; model3.EntireTime = now; model3.Reserve1 = "460"; bll_pollution.Add(model3); } var pollution_discharge_name4 = "GP02-2#水泵"; var model4 = list.Where(x => x.PollutionDischargeName == pollution_discharge_name4).FirstOrDefault(); if (model4 != null) { var is_update = false; if (item.dl.ContainsKey("1DI7")) { var value = Convert.ToInt32(item.dl["1DI7"]); if (model4.PollutionDischargeState != value) { model4.PollutionDischargeState = value; is_update = true; } } if (item.dl.ContainsKey("1DI8")) { var value = Convert.ToInt32(item.dl["1DI8"]); if (model4.PollutionDischargeAlarm != value) { model4.PollutionDischargeAlarm = value; is_update = true; } } if ((now - model4.EntireTime.Value).TotalMinutes >= 1) { model4.EntireTime = now; is_update = true; } if (is_update) { model4.EntireTime = now; bll_pollution.Update(model4); } } else { model4 = new DataService.Model.pollution_discharge(); model4.PollutionDischargeId = Guid.NewGuid().ToString("N"); model4.PollutionDischargeName = pollution_discharge_name4; if (item.dl.ContainsKey("1DI7")) { model4.PollutionDischargeState = Convert.ToInt32(item.dl["1DI7"]); } if (item.dl.ContainsKey("1DI8")) { model4.PollutionDischargeAlarm = Convert.ToInt32(item.dl["1DI8"]); } model4.CreateTime = now; model4.EntireTime = now; model4.Reserve1 = "460"; bll_pollution.Add(model4); } #endregion #region GP03#水泵 var pollution_discharge_name5 = "GP03-1#水泵"; var model5 = list.Where(x => x.PollutionDischargeName == pollution_discharge_name5).FirstOrDefault(); if (model5 != null) { var is_update = false; if (item.dl.ContainsKey("1DI9")) { var value = Convert.ToInt32(item.dl["1DI9"]); if (model5.PollutionDischargeState != value) { model5.PollutionDischargeState = value; is_update = true; } } if (item.dl.ContainsKey("1DI10")) { var value = Convert.ToInt32(item.dl["1DI10"]); if (model5.PollutionDischargeAlarm != value) { model5.PollutionDischargeAlarm = value; is_update = true; } } if ((now - model5.EntireTime.Value).TotalMinutes >= 1) { model5.EntireTime = now; is_update = true; } if (is_update) { model5.EntireTime = now; bll_pollution.Update(model5); } } else { model5 = new DataService.Model.pollution_discharge(); model5.PollutionDischargeId = Guid.NewGuid().ToString("N"); model5.PollutionDischargeName = pollution_discharge_name5; if (item.dl.ContainsKey("1DI9")) { model5.PollutionDischargeState = Convert.ToInt32(item.dl["1DI9"]); } if (item.dl.ContainsKey("1DI10")) { model5.PollutionDischargeAlarm = Convert.ToInt32(item.dl["1DI10"]); } model5.CreateTime = now; model5.EntireTime = now; model5.Reserve1 = "460"; bll_pollution.Add(model5); } var pollution_discharge_name6 = "GP03-2#水泵"; var model6 = list.Where(x => x.PollutionDischargeName == pollution_discharge_name6).FirstOrDefault(); if (model6 != null) { var is_update = false; if (item.dl.ContainsKey("1DI11")) { var value = Convert.ToInt32(item.dl["1DI11"]); if (model6.PollutionDischargeState != value) { model6.PollutionDischargeState = value; is_update = true; } } if (item.dl.ContainsKey("1DI12")) { var value = Convert.ToInt32(item.dl["1DI12"]); if (model6.PollutionDischargeAlarm != value) { model6.PollutionDischargeAlarm = value; is_update = true; } } if ((now - model6.EntireTime.Value).TotalMinutes >= 1) { model6.EntireTime = now; is_update = true; } if (is_update) { model6.EntireTime = now; bll_pollution.Update(model6); } } else { model6 = new DataService.Model.pollution_discharge(); model6.PollutionDischargeId = Guid.NewGuid().ToString("N"); model6.PollutionDischargeName = pollution_discharge_name6; if (item.dl.ContainsKey("1DI11")) { model6.PollutionDischargeState = Convert.ToInt32(item.dl["1DI11"]); } if (item.dl.ContainsKey("1DI12")) { model6.PollutionDischargeAlarm = Convert.ToInt32(item.dl["1DI12"]); } model6.CreateTime = now; model6.EntireTime = now; model6.Reserve1 = "460"; bll_pollution.Add(model6); } #endregion #region GP04#水泵 var pollution_discharge_name7 = "GP04-1#水泵"; var model7 = list.Where(x => x.PollutionDischargeName == pollution_discharge_name7).FirstOrDefault(); if (model7 != null) { var is_update = false; if (item.dl.ContainsKey("1DI13")) { var value = Convert.ToInt32(item.dl["1DI13"]); if (model7.PollutionDischargeState != value) { model7.PollutionDischargeState = value; is_update = true; } } if (item.dl.ContainsKey("1DI14")) { var value = Convert.ToInt32(item.dl["1DI14"]); if (model7.PollutionDischargeAlarm != value) { model7.PollutionDischargeAlarm = value; is_update = true; } } if ((now - model7.EntireTime.Value).TotalMinutes >= 1) { model7.EntireTime = now; is_update = true; } if (is_update) { model7.EntireTime = now; bll_pollution.Update(model7); } } else { model7 = new DataService.Model.pollution_discharge(); model7.PollutionDischargeId = Guid.NewGuid().ToString("N"); model7.PollutionDischargeName = pollution_discharge_name7; if (item.dl.ContainsKey("1DI13")) { model7.PollutionDischargeState = Convert.ToInt32(item.dl["1DI13"]); } if (item.dl.ContainsKey("1DI14")) { model7.PollutionDischargeAlarm = Convert.ToInt32(item.dl["1DI14"]); } model7.CreateTime = now; model7.EntireTime = now; model7.Reserve1 = "460"; bll_pollution.Add(model7); } var pollution_discharge_name8 = "GP04-2#水泵"; var model8 = list.Where(x => x.PollutionDischargeName == pollution_discharge_name8).FirstOrDefault(); if (model8 != null) { var is_update = false; if (item.dl.ContainsKey("1DI15")) { var value = Convert.ToInt32(item.dl["1DI16"]); if (model8.PollutionDischargeState != value) { model8.PollutionDischargeState = value; is_update = true; } } if (item.dl.ContainsKey("1DI16")) { var value = Convert.ToInt32(item.dl["1DI17"]); if (model8.PollutionDischargeAlarm != value) { model8.PollutionDischargeAlarm = value; is_update = true; } } if ((now - model8.EntireTime.Value).TotalMinutes >= 1) { model8.EntireTime = now; is_update = true; } if (is_update) { model8.EntireTime = now; bll_pollution.Update(model8); } } else { model8 = new DataService.Model.pollution_discharge(); model8.PollutionDischargeId = Guid.NewGuid().ToString("N"); model8.PollutionDischargeName = pollution_discharge_name8; if (item.dl.ContainsKey("1DI15")) { model8.PollutionDischargeState = Convert.ToInt32(item.dl["1DI16"]); } if (item.dl.ContainsKey("1DI16")) { model8.PollutionDischargeAlarm = Convert.ToInt32(item.dl["1DI17"]); } model8.CreateTime = now; model8.EntireTime = now; model8.Reserve1 = "460"; bll_pollution.Add(model8); } #endregion #region GP05#水泵 var pollution_discharge_name9 = "GP05-1#水泵"; var model9 = list.Where(x => x.PollutionDischargeName == pollution_discharge_name9).FirstOrDefault(); if (model9 != null) { var is_update = false; if (item.dl.ContainsKey("1DI17")) { var value = Convert.ToInt32(item.dl["1DI17"]); if (model9.PollutionDischargeState != value) { model9.PollutionDischargeState = value; is_update = true; } } if (item.dl.ContainsKey("1DI18")) { var value = Convert.ToInt32(item.dl["1DI18"]); if (model9.PollutionDischargeAlarm != value) { model9.PollutionDischargeAlarm = value; is_update = true; } } if ((now - model9.EntireTime.Value).TotalMinutes >= 1) { model9.EntireTime = now; is_update = true; } if (is_update) { model9.EntireTime = now; bll_pollution.Update(model9); } } else { model9 = new DataService.Model.pollution_discharge(); model9.PollutionDischargeId = Guid.NewGuid().ToString("N"); model9.PollutionDischargeName = pollution_discharge_name9; if (item.dl.ContainsKey("1DI17")) { model9.PollutionDischargeState = Convert.ToInt32(item.dl["1DI17"]); } if (item.dl.ContainsKey("1DI18")) { model9.PollutionDischargeAlarm = Convert.ToInt32(item.dl["1DI18"]); } model9.CreateTime = now; model9.EntireTime = now; model9.Reserve1 = "460"; bll_pollution.Add(model9); } var pollution_discharge_name10 = "GP05-2#水泵"; var model10 = list.Where(x => x.PollutionDischargeName == pollution_discharge_name10).FirstOrDefault(); if (model10 != null) { var is_update = false; if (item.dl.ContainsKey("1DI19")) { var value = Convert.ToInt32(item.dl["1DI19"]); if (model10.PollutionDischargeState != value) { model10.PollutionDischargeState = value; is_update = true; } } if (item.dl.ContainsKey("1DI20")) { var value = Convert.ToInt32(item.dl["1DI20"]); if (model10.PollutionDischargeAlarm != value) { model10.PollutionDischargeAlarm = value; is_update = true; } } if ((now - model10.EntireTime.Value).TotalMinutes >= 1) { model10.EntireTime = now; is_update = true; } if (is_update) { model10.EntireTime = now; bll_pollution.Update(model10); } } else { model10 = new DataService.Model.pollution_discharge(); model10.PollutionDischargeId = Guid.NewGuid().ToString("N"); model10.PollutionDischargeName = pollution_discharge_name10; if (item.dl.ContainsKey("1DI19")) { model10.PollutionDischargeState = Convert.ToInt32(item.dl["1DI19"]); } if (item.dl.ContainsKey("1DI20")) { model10.PollutionDischargeAlarm = Convert.ToInt32(item.dl["1DI20"]); } model10.CreateTime = now; model10.EntireTime = now; model10.Reserve1 = "460"; bll_pollution.Add(model10); } #endregion } } } } //排污2 if (topic.Contains("/e7af4604-30f8-41d6-a4d0-90c969e77a62")) { //log.Info("排污2msg" + msg); //排污1 var now = DateTime.Now; var j = msg.Substring(1); var data = JsonConvert.DeserializeObject(j); //log.Info("排污2:" + data); if (data.b != null) { foreach (var item in data.b.dvl) { var name = ""; char delimiter = '.'; // 分隔符 int lastDotIndex = item.da.LastIndexOf(delimiter); // 查找最后一个分隔符的位置 if (lastDotIndex != -1) // 如果找到分隔符 { name = item.da.Substring(lastDotIndex + 1); // 截取分隔符后面的子字符串 } if (name == "465") { var list = bll_pollution.GetModelList(" Reserve1='465' "); Dictionary new_dl = new Dictionary(); foreach (var dl_item in item.dl) { var value = dl_item.Value; if (dl_item.Value.Contains("bad")) { value = "-1"; } new_dl.Add(dl_item.Key, value); } item.dl = new_dl; #region GP06#水泵 var pollution_discharge_name17 = "GP06-1#水泵"; var model17 = list.Where(x => x.PollutionDischargeName == pollution_discharge_name17).FirstOrDefault(); if (model17 != null) { var is_update = false; if (item.dl.ContainsKey("1DI1")) { var value = Convert.ToInt32(item.dl["1DI1"]); if (model17.PollutionDischargeState != value) { model17.PollutionDischargeState = value; is_update = true; } } if (item.dl.ContainsKey("1DI2")) { var value = Convert.ToInt32(item.dl["1DI2"]); if (model17.PollutionDischargeAlarm != value) { model17.PollutionDischargeAlarm = value; is_update = true; } } if ((now - model17.EntireTime.Value).TotalMinutes >= 1) { model17.EntireTime = now; is_update = true; } if (is_update) { model17.EntireTime = now; bll_pollution.Update(model17); } } else { model17 = new DataService.Model.pollution_discharge(); model17.PollutionDischargeId = Guid.NewGuid().ToString("N"); model17.PollutionDischargeName = pollution_discharge_name17; if (item.dl.ContainsKey("1DI1")) { model17.PollutionDischargeState = Convert.ToInt32(item.dl["1DI1"]); } if (item.dl.ContainsKey("1DI2")) { model17.PollutionDischargeAlarm = Convert.ToInt32(item.dl["1DI2"]); } model17.CreateTime = now; model17.EntireTime = now; model17.Reserve1 = "465"; bll_pollution.Add(model17); } var pollution_discharge_name18 = "GP06-2#水泵"; var model18 = list.Where(x => x.PollutionDischargeName == pollution_discharge_name18).FirstOrDefault(); if (model18 != null) { var is_update = false; if (item.dl.ContainsKey("1DI3")) { var value = Convert.ToInt32(item.dl["1DI3"]); if (model18.PollutionDischargeState != value) { model18.PollutionDischargeState = value; is_update = true; } } if (item.dl.ContainsKey("1DI4")) { var value = Convert.ToInt32(item.dl["1DI4"]); if (model18.PollutionDischargeAlarm != value) { model18.PollutionDischargeAlarm = value; is_update = true; } } if ((now - model18.EntireTime.Value).TotalMinutes >= 1) { model18.EntireTime = now; is_update = true; } if (is_update) { model18.EntireTime = now; bll_pollution.Update(model18); } } else { model18 = new DataService.Model.pollution_discharge(); model18.PollutionDischargeId = Guid.NewGuid().ToString("N"); model18.PollutionDischargeName = pollution_discharge_name18; if (item.dl.ContainsKey("1DI3")) { model18.PollutionDischargeState = Convert.ToInt32(item.dl["1DI3"]); } if (item.dl.ContainsKey("1DI4")) { model18.PollutionDischargeAlarm = Convert.ToInt32(item.dl["1DI4"]); } model18.CreateTime = now; model18.EntireTime = now; model18.Reserve1 = "465"; bll_pollution.Add(model18); } #endregion #region GP07#水泵 var pollution_discharge_name15 = "GP07-1#水泵"; var model15 = list.Where(x => x.PollutionDischargeName == pollution_discharge_name15).FirstOrDefault(); if (model15 != null) { var is_update = false; if (item.dl.ContainsKey("1DI5")) { var value = Convert.ToInt32(item.dl["1DI5"]); if (model15.PollutionDischargeState != value) { model15.PollutionDischargeState = value; is_update = true; } } if (item.dl.ContainsKey("1DI6")) { var value = Convert.ToInt32(item.dl["1DI6"]); if (model15.PollutionDischargeAlarm != value) { model15.PollutionDischargeAlarm = value; is_update = true; } } if ((now - model15.EntireTime.Value).TotalMinutes >= 1) { model15.EntireTime = now; is_update = true; } if (is_update) { model15.EntireTime = now; bll_pollution.Update(model15); } } else { model15 = new DataService.Model.pollution_discharge(); model15.PollutionDischargeId = Guid.NewGuid().ToString("N"); model15.PollutionDischargeName = pollution_discharge_name15; if (item.dl.ContainsKey("1DI5")) { model15.PollutionDischargeState = Convert.ToInt32(item.dl["1DI5"]); } if (item.dl.ContainsKey("1DI6")) { model15.PollutionDischargeAlarm = Convert.ToInt32(item.dl["1DI6"]); } model15.CreateTime = now; model15.EntireTime = now; model15.Reserve1 = "465"; bll_pollution.Add(model15); } var pollution_discharge_name16 = "GP07-2#水泵"; var model16 = list.Where(x => x.PollutionDischargeName == pollution_discharge_name16).FirstOrDefault(); if (model16 != null) { var is_update = false; if (item.dl.ContainsKey("1DI7")) { var value = Convert.ToInt32(item.dl["1DI7"]); if (model16.PollutionDischargeState != value) { model16.PollutionDischargeState = value; is_update = true; } } if (item.dl.ContainsKey("1DI8")) { var value = Convert.ToInt32(item.dl["1DI8"]); if (model16.PollutionDischargeAlarm != value) { model16.PollutionDischargeAlarm = value; is_update = true; } } if ((now - model16.EntireTime.Value).TotalMinutes >= 1) { model16.EntireTime = now; is_update = true; } if (is_update) { model16.EntireTime = now; bll_pollution.Update(model16); } } else { model16 = new DataService.Model.pollution_discharge(); model16.PollutionDischargeId = Guid.NewGuid().ToString("N"); model16.PollutionDischargeName = pollution_discharge_name16; if (item.dl.ContainsKey("1DI7")) { model16.PollutionDischargeState = Convert.ToInt32(item.dl["1DI7"]); } if (item.dl.ContainsKey("1DI8")) { model16.PollutionDischargeAlarm = Convert.ToInt32(item.dl["1DI8"]); } model16.CreateTime = now; model16.EntireTime = now; model16.Reserve1 = "465"; bll_pollution.Add(model16); } #endregion #region GP08#水泵 var pollution_discharge_name1 = "GP08-1#水泵"; var model1 = list.Where(x => x.PollutionDischargeName == pollution_discharge_name1).FirstOrDefault(); if (model1 != null) { var is_update = false; if (item.dl.ContainsKey("1DI9")) { var value = Convert.ToInt32(item.dl["1DI9"]); if (model1.PollutionDischargeState != value) { model1.PollutionDischargeState = value; is_update = true; } } if (item.dl.ContainsKey("1DI10")) { var value = Convert.ToInt32(item.dl["1DI10"]); if (model1.PollutionDischargeAlarm != value) { model1.PollutionDischargeAlarm = value; is_update = true; } } if ((now - model1.EntireTime.Value).TotalMinutes >= 1) { model1.EntireTime = now; is_update = true; } if (is_update) { model1.EntireTime = now; bll_pollution.Update(model1); } } else { model1 = new DataService.Model.pollution_discharge(); model1.PollutionDischargeId = Guid.NewGuid().ToString("N"); model1.PollutionDischargeName = pollution_discharge_name1; if (item.dl.ContainsKey("1DI9")) { model1.PollutionDischargeState = Convert.ToInt32(item.dl["1DI9"]); } if (item.dl.ContainsKey("1DI10")) { model1.PollutionDischargeAlarm = Convert.ToInt32(item.dl["1DI10"]); } model1.CreateTime = now; model1.EntireTime = now; model1.Reserve1 = "465"; bll_pollution.Add(model1); } var pollution_discharge_name2 = "GP08-2#水泵"; var model2 = list.Where(x => x.PollutionDischargeName == pollution_discharge_name2).FirstOrDefault(); if (model2 != null) { var is_update = false; if (item.dl.ContainsKey("1DI11")) { var value = Convert.ToInt32(item.dl["1DI11"]); if (model2.PollutionDischargeState != value) { model2.PollutionDischargeState = value; is_update = true; } } if (item.dl.ContainsKey("1DI12")) { var value = Convert.ToInt32(item.dl["1DI12"]); if (model2.PollutionDischargeAlarm != value) { model2.PollutionDischargeAlarm = value; is_update = true; } } if ((now - model2.EntireTime.Value).TotalMinutes >= 1) { model2.EntireTime = now; is_update = true; } if (is_update) { model2.EntireTime = now; bll_pollution.Update(model2); } } else { model2 = new DataService.Model.pollution_discharge(); model2.PollutionDischargeId = Guid.NewGuid().ToString("N"); model2.PollutionDischargeName = pollution_discharge_name2; if (item.dl.ContainsKey("1DI11")) { model2.PollutionDischargeState = Convert.ToInt32(item.dl["1DI11"]); } if (item.dl.ContainsKey("1DI12")) { model2.PollutionDischargeAlarm = Convert.ToInt32(item.dl["1DI12"]); } model2.CreateTime = now; model2.EntireTime = now; model2.Reserve1 = "465"; bll_pollution.Add(model2); } #endregion #region GP09#水泵 var pollution_discharge_name3 = "GP09-1#水泵"; var model3 = list.Where(x => x.PollutionDischargeName == pollution_discharge_name3).FirstOrDefault(); if (model3 != null) { var is_update = false; if (item.dl.ContainsKey("1DI13")) { var value = Convert.ToInt32(item.dl["1DI13"]); if (model3.PollutionDischargeState != value) { model3.PollutionDischargeState = value; is_update = true; } } if (item.dl.ContainsKey("1DI14")) { var value = Convert.ToInt32(item.dl["1DI14"]); if (model3.PollutionDischargeAlarm != value) { model3.PollutionDischargeAlarm = value; is_update = true; } } if ((now - model3.EntireTime.Value).TotalMinutes >= 1) { model3.EntireTime = now; is_update = true; } if (is_update) { model3.EntireTime = now; bll_pollution.Update(model3); } } else { model3 = new DataService.Model.pollution_discharge(); model3.PollutionDischargeId = Guid.NewGuid().ToString("N"); model3.PollutionDischargeName = pollution_discharge_name3; if (item.dl.ContainsKey("1DI13")) { model3.PollutionDischargeState = Convert.ToInt32(item.dl["1DI13"]); } if (item.dl.ContainsKey("1DI14")) { model3.PollutionDischargeAlarm = Convert.ToInt32(item.dl["1DI14"]); } model3.CreateTime = now; model3.EntireTime = now; model3.Reserve1 = "465"; bll_pollution.Add(model3); } var pollution_discharge_name4 = "GP09-2#水泵"; var model4 = list.Where(x => x.PollutionDischargeName == pollution_discharge_name4).FirstOrDefault(); if (model4 != null) { var is_update = false; if (item.dl.ContainsKey("1DI15")) { var value = Convert.ToInt32(item.dl["1DI15"]); if (model4.PollutionDischargeState != value) { model4.PollutionDischargeState = value; is_update = true; } } if (item.dl.ContainsKey("1DI16")) { var value = Convert.ToInt32(item.dl["1DI16"]); if (model4.PollutionDischargeAlarm != value) { model4.PollutionDischargeAlarm = value; is_update = true; } } if ((now - model4.EntireTime.Value).TotalMinutes >= 1) { model4.EntireTime = now; is_update = true; } if (is_update) { model4.EntireTime = now; bll_pollution.Update(model4); } } else { model4 = new DataService.Model.pollution_discharge(); model4.PollutionDischargeId = Guid.NewGuid().ToString("N"); model4.PollutionDischargeName = pollution_discharge_name4; if (item.dl.ContainsKey("1DI15")) { model4.PollutionDischargeState = Convert.ToInt32(item.dl["1DI15"]); } if (item.dl.ContainsKey("1DI16")) { model4.PollutionDischargeAlarm = Convert.ToInt32(item.dl["1DI16"]); } model4.CreateTime = now; model4.EntireTime = now; model4.Reserve1 = "465"; bll_pollution.Add(model4); } #endregion #region GP10#水泵 var pollution_discharge_name5 = "GP10-1#水泵"; var model5 = list.Where(x => x.PollutionDischargeName == pollution_discharge_name5).FirstOrDefault(); if (model5 != null) { var is_update = false; if (item.dl.ContainsKey("1DI17")) { var value = Convert.ToInt32(item.dl["1DI17"]); if (model5.PollutionDischargeState != value) { model5.PollutionDischargeState = value; is_update = true; } } if (item.dl.ContainsKey("1DI18")) { var value = Convert.ToInt32(item.dl["1DI18"]); if (model5.PollutionDischargeAlarm != value) { model5.PollutionDischargeAlarm = value; is_update = true; } } if ((now - model5.EntireTime.Value).TotalMinutes >= 1) { model5.EntireTime = now; is_update = true; } if (is_update) { model5.EntireTime = now; bll_pollution.Update(model5); } } else { model5 = new DataService.Model.pollution_discharge(); model5.PollutionDischargeId = Guid.NewGuid().ToString("N"); model5.PollutionDischargeName = pollution_discharge_name5; if (item.dl.ContainsKey("1DI17")) { model5.PollutionDischargeState = Convert.ToInt32(item.dl["1DI17"]); } if (item.dl.ContainsKey("1DI18")) { model5.PollutionDischargeAlarm = Convert.ToInt32(item.dl["1DI18"]); } model5.CreateTime = now; model5.EntireTime = now; model5.Reserve1 = "465"; bll_pollution.Add(model5); } var pollution_discharge_name6 = "GP10-2#水泵"; var model6 = list.Where(x => x.PollutionDischargeName == pollution_discharge_name6).FirstOrDefault(); if (model6 != null) { var is_update = false; if (item.dl.ContainsKey("1DI19")) { var value = Convert.ToInt32(item.dl["1DI19"]); if (model6.PollutionDischargeState != value) { model6.PollutionDischargeState = value; is_update = true; } } if (item.dl.ContainsKey("1DI20")) { var value = Convert.ToInt32(item.dl["1DI20"]); if (model6.PollutionDischargeAlarm != value) { model6.PollutionDischargeAlarm = value; is_update = true; } } if ((now - model6.EntireTime.Value).TotalMinutes >= 1) { model6.EntireTime = now; is_update = true; } if (is_update) { model6.EntireTime = now; bll_pollution.Update(model6); } } else { model6 = new DataService.Model.pollution_discharge(); model6.PollutionDischargeId = Guid.NewGuid().ToString("N"); model6.PollutionDischargeName = pollution_discharge_name6; if (item.dl.ContainsKey("1DI19")) { model6.PollutionDischargeState = Convert.ToInt32(item.dl["1DI19"]); } if (item.dl.ContainsKey("1DI20")) { model6.PollutionDischargeAlarm = Convert.ToInt32(item.dl["1DI20"]); } model6.CreateTime = now; model6.EntireTime = now; model6.Reserve1 = "465"; bll_pollution.Add(model6); } #endregion #region GP11#水泵 var pollution_discharge_name7 = "GP11-1#水泵"; var model7 = list.Where(x => x.PollutionDischargeName == pollution_discharge_name7).FirstOrDefault(); if (model7 != null) { var is_update = false; if (item.dl.ContainsKey("1DI21")) { var value = Convert.ToInt32(item.dl["1DI21"]); if (model7.PollutionDischargeState != value) { model7.PollutionDischargeState = value; is_update = true; } } if (item.dl.ContainsKey("1DI22")) { var value = Convert.ToInt32(item.dl["1DI22"]); if (model7.PollutionDischargeAlarm != value) { model7.PollutionDischargeAlarm = value; is_update = true; } } if ((now - model7.EntireTime.Value).TotalMinutes >= 1) { model7.EntireTime = now; is_update = true; } if (is_update) { model7.EntireTime = now; bll_pollution.Update(model7); } } else { model7 = new DataService.Model.pollution_discharge(); model7.PollutionDischargeId = Guid.NewGuid().ToString("N"); model7.PollutionDischargeName = pollution_discharge_name7; if (item.dl.ContainsKey("1DI21")) { model7.PollutionDischargeState = Convert.ToInt32(item.dl["1DI21"]); } if (item.dl.ContainsKey("1DI22")) { model7.PollutionDischargeAlarm = Convert.ToInt32(item.dl["1DI22"]); } model7.CreateTime = now; model7.EntireTime = now; model7.Reserve1 = "465"; bll_pollution.Add(model7); } var pollution_discharge_name8 = "GP11-2#水泵"; var model8 = list.Where(x => x.PollutionDischargeName == pollution_discharge_name8).FirstOrDefault(); if (model8 != null) { var is_update = false; if (item.dl.ContainsKey("1DI23")) { var value = Convert.ToInt32(item.dl["1DI23"]); if (model8.PollutionDischargeState != value) { model8.PollutionDischargeState = value; is_update = true; } } if (item.dl.ContainsKey("1DI24")) { var value = Convert.ToInt32(item.dl["1DI24"]); if (model8.PollutionDischargeAlarm != value) { model8.PollutionDischargeAlarm = value; is_update = true; } } if ((now - model8.EntireTime.Value).TotalMinutes >= 1) { model8.EntireTime = now; is_update = true; } if (is_update) { model8.EntireTime = now; bll_pollution.Update(model8); } } else { model8 = new DataService.Model.pollution_discharge(); model8.PollutionDischargeId = Guid.NewGuid().ToString("N"); model8.PollutionDischargeName = pollution_discharge_name8; if (item.dl.ContainsKey("1DI23")) { model8.PollutionDischargeState = Convert.ToInt32(item.dl["1DI23"]); } if (item.dl.ContainsKey("1DI24")) { model8.PollutionDischargeAlarm = Convert.ToInt32(item.dl["1DI24"]); } model8.CreateTime = now; model8.EntireTime = now; model8.Reserve1 = "465"; bll_pollution.Add(model8); } #endregion #region GP12#水泵 var pollution_discharge_name9 = "GP12-1#水泵"; var model9 = list.Where(x => x.PollutionDischargeName == pollution_discharge_name9).FirstOrDefault(); if (model9 != null) { var is_update = false; if (item.dl.ContainsKey("1DI25")) { var value = Convert.ToInt32(item.dl["1DI25"]); if (model9.PollutionDischargeState != value) { model9.PollutionDischargeState = value; is_update = true; } } if (item.dl.ContainsKey("1DI26")) { var value = Convert.ToInt32(item.dl["1DI26"]); if (model9.PollutionDischargeAlarm != value) { model9.PollutionDischargeAlarm = value; } } if ((now - model9.EntireTime.Value).TotalMinutes >= 1) { model9.EntireTime = now; is_update = true; } if (is_update) { model9.EntireTime = now; bll_pollution.Update(model9); } } else { model9 = new DataService.Model.pollution_discharge(); model9.PollutionDischargeId = Guid.NewGuid().ToString("N"); model9.PollutionDischargeName = pollution_discharge_name9; if (item.dl.ContainsKey("1DI25")) { model9.PollutionDischargeState = Convert.ToInt32(item.dl["1DI25"]); } if (item.dl.ContainsKey("1DI26")) { model9.PollutionDischargeAlarm = Convert.ToInt32(item.dl["1DI26"]); } model9.CreateTime = now; model9.EntireTime = now; model9.Reserve1 = "465"; bll_pollution.Add(model9); } var pollution_discharge_name10 = "GP12-2#水泵"; var model10 = list.Where(x => x.PollutionDischargeName == pollution_discharge_name10).FirstOrDefault(); if (model10 != null) { var is_update = false; if (item.dl.ContainsKey("1DI27")) { var value = Convert.ToInt32(item.dl["1DI27"]); if (model10.PollutionDischargeState != value) { model10.PollutionDischargeState = value; is_update = false; } } if (item.dl.ContainsKey("1DI28")) { var value = Convert.ToInt32(item.dl["1DI28"]); if (model10.PollutionDischargeAlarm != value) { model10.PollutionDischargeAlarm = value; is_update = false; } } if ((now - model10.EntireTime.Value).TotalMinutes >= 1) { model10.EntireTime = now; is_update = true; } if (is_update) { model10.EntireTime = now; bll_pollution.Update(model10); } } else { model10 = new DataService.Model.pollution_discharge(); model10.PollutionDischargeId = Guid.NewGuid().ToString("N"); model10.PollutionDischargeName = pollution_discharge_name10; if (item.dl.ContainsKey("1DI27")) { model10.PollutionDischargeState = Convert.ToInt32(item.dl["1DI27"]); } if (item.dl.ContainsKey("1DI28")) { model10.PollutionDischargeAlarm = Convert.ToInt32(item.dl["1DI28"]); } model10.CreateTime = now; model10.EntireTime = now; model10.Reserve1 = "465"; bll_pollution.Add(model10); } #endregion #region GP13#水泵 var pollution_discharge_name11 = "GP13-1#水泵"; var model11 = list.Where(x => x.PollutionDischargeName == pollution_discharge_name11).FirstOrDefault(); if (model11 != null) { var is_update = false; if (item.dl.ContainsKey("1DI29")) { var value = Convert.ToInt32(item.dl["1DI29"]); if (model11.PollutionDischargeState != value) { model11.PollutionDischargeState = value; is_update = true; } } if (item.dl.ContainsKey("1DI30")) { var value = Convert.ToInt32(item.dl["1DI30"]); if (model11.PollutionDischargeAlarm != value) { model11.PollutionDischargeAlarm = value; } } if ((now - model11.EntireTime.Value).TotalMinutes >= 1) { model11.EntireTime = now; is_update = true; } if (is_update) { model11.EntireTime = now; bll_pollution.Update(model11); } } else { model11 = new DataService.Model.pollution_discharge(); model11.PollutionDischargeId = Guid.NewGuid().ToString("N"); model11.PollutionDischargeName = pollution_discharge_name11; if (item.dl.ContainsKey("1DI29")) { model11.PollutionDischargeState = Convert.ToInt32(item.dl["1DI29"]); } if (item.dl.ContainsKey("1DI30")) { model11.PollutionDischargeAlarm = Convert.ToInt32(item.dl["1DI30"]); } model11.CreateTime = now; model11.EntireTime = now; model11.Reserve1 = "465"; bll_pollution.Add(model11); } var pollution_discharge_name12 = "GP13-2#水泵"; var model12 = list.Where(x => x.PollutionDischargeName == pollution_discharge_name12).FirstOrDefault(); if (model12 != null) { var is_update = false; if (item.dl.ContainsKey("1DI31")) { var value = Convert.ToInt32(item.dl["1DI31"]); if (model12.PollutionDischargeState != value) { model12.PollutionDischargeState = value; is_update = true; } } if (item.dl.ContainsKey("1DI32")) { var value = Convert.ToInt32(item.dl["1DI32"]); if (model12.PollutionDischargeAlarm != value) { model12.PollutionDischargeAlarm = value; is_update = true; } } if ((now - model12.EntireTime.Value).TotalMinutes >= 1) { model12.EntireTime = now; is_update = true; } if (is_update) { model12.EntireTime = now; bll_pollution.Update(model12); } } else { model12 = new DataService.Model.pollution_discharge(); model12.PollutionDischargeId = Guid.NewGuid().ToString("N"); model12.PollutionDischargeName = pollution_discharge_name12; if (item.dl.ContainsKey("1DI31")) { model12.PollutionDischargeState = Convert.ToInt32(item.dl["1DI31"]); } if (item.dl.ContainsKey("1DI32")) { model12.PollutionDischargeAlarm = Convert.ToInt32(item.dl["1DI32"]); } model12.CreateTime = now; model12.EntireTime = now; model12.Reserve1 = "465"; bll_pollution.Add(model12); } #endregion #region GP14#水泵 var pollution_discharge_name13 = "GP14-1#水泵"; var model13 = list.Where(x => x.PollutionDischargeName == pollution_discharge_name13).FirstOrDefault(); if (model13 != null) { var is_update = false; if (item.dl.ContainsKey("1DI33")) { var value = Convert.ToInt32(item.dl["1DI33"]); if (model13.PollutionDischargeState != value) { model13.PollutionDischargeState = value; is_update = true; } } if (item.dl.ContainsKey("1DI34")) { var value = Convert.ToInt32(item.dl["1DI34"]); if (model13.PollutionDischargeAlarm != value) { model13.PollutionDischargeAlarm = value; is_update = true; } } if ((now - model13.EntireTime.Value).TotalMinutes >= 1) { model13.EntireTime = now; is_update = true; } if (is_update) { model13.EntireTime = now; bll_pollution.Update(model13); } } else { model13 = new DataService.Model.pollution_discharge(); model13.PollutionDischargeId = Guid.NewGuid().ToString("N"); model13.PollutionDischargeName = pollution_discharge_name13; if (item.dl.ContainsKey("1DI33")) { model13.PollutionDischargeState = Convert.ToInt32(item.dl["1DI33"]); } if (item.dl.ContainsKey("1DI34")) { model13.PollutionDischargeAlarm = Convert.ToInt32(item.dl["1DI34"]); } model13.CreateTime = now; model13.EntireTime = now; model13.Reserve1 = "465"; bll_pollution.Add(model13); } var pollution_discharge_name14 = "GP14-2#水泵"; var model14 = list.Where(x => x.PollutionDischargeName == pollution_discharge_name14).FirstOrDefault(); if (model14 != null) { var is_update = false; if (item.dl.ContainsKey("1DI35")) { var value = Convert.ToInt32(item.dl["1DI35"]); if (model14.PollutionDischargeState != value) { model14.PollutionDischargeState = value; is_update = true; } } if (item.dl.ContainsKey("1DI36")) { var value = Convert.ToInt32(item.dl["1DI36"]); if (model14.PollutionDischargeAlarm != value) { model14.PollutionDischargeAlarm = value; is_update = true; } } if ((now - model14.EntireTime.Value).TotalMinutes >= 1) { model14.EntireTime = now; is_update = true; } if (is_update) { model14.EntireTime = now; bll_pollution.Update(model14); } } else { model14 = new DataService.Model.pollution_discharge(); model14.PollutionDischargeId = Guid.NewGuid().ToString("N"); model14.PollutionDischargeName = pollution_discharge_name14; if (item.dl.ContainsKey("1DI35")) { model14.PollutionDischargeState = Convert.ToInt32(item.dl["1DI35"]); } if (item.dl.ContainsKey("1DI36")) { model14.PollutionDischargeAlarm = Convert.ToInt32(item.dl["1DI36"]); } model14.CreateTime = now; model14.EntireTime = now; model14.Reserve1 = "465"; bll_pollution.Add(model14); } #endregion } } } } } catch (Exception ex) { //log.Info("排污处理失败,数据:" + msg + ",原因:" + ex.Message); } } private static void Client3_OnMqttMessageReceived(MqttClient client, string topic, byte[] payload) { var msg = Encoding.UTF8.GetString(payload); try { //照明 if (topic.Contains("/462f192b-a689-4246-bf79-1a9ac076a462") || topic.Contains("/fe4086e9-d2f4-42e1-9058-908f93251c45") || topic.Contains("/3af8becd-c5ff-48e2-9826-00f4c7df8047") || topic.Contains("/50505de0-cf02-4ebd-b332-2031e897894f") || topic.Contains("/690dd79a-003d-4b93-a1a5-bbac3ffbf2d5") || topic.Contains("/d7d5fe7f-f383-45ff-a8c4-9f946f391179") || topic.Contains("/e7592a36-e5e4-4478-89ae-dd22dcd2e779") || topic.Contains("/6a61e575-f246-4fab-ab6c-b2d129cfb34e") || topic.Contains("/49da8dfa-35be-40c2-a461-73a292162b17") || topic.Contains("/f151df53-3b19-4add-88f3-c5a875e38651") || topic.Contains("/e7af4604-30f8-41d6-a4d0-90c969e77a62") ) { //log.Info("照明msg" + msg); var now = DateTime.Now; var ClientId = topic.Replace("/ge/data/", ""); //照明 var j = msg.Substring(1); var data = JsonConvert.DeserializeObject(j); //log.Info("照明:" + data); if (data.b != null) { foreach (var item in data.b.dvl) { //var name = ""; //char delimiter = '.'; // 分隔符 //int lastDotIndex = item.da.LastIndexOf(delimiter); // 查找最后一个分隔符的位置 //if (lastDotIndex != -1) // 如果找到分隔符 //{ // name = item.da.Substring(lastDotIndex + 1); // 截取分隔符后面的子字符串 //} if (!string.IsNullOrEmpty(item.da)) { var list = bll_lighting_info.GetModelList(" LightingNumber='" + item.da + "' and ClientId='" + ClientId + "' "); foreach (var dl_item in item.dl) { var model = list.Where(c => c.PointerCode == dl_item.Key).FirstOrDefault(); if (model != null) { var is_update = false; if (model.LightingState != dl_item.Value) { model.LightingState = dl_item.Value; is_update = true; } if (model.LightingNumber != item.da) { model.LightingNumber = item.da; is_update = true; } if (model.UpdateTime == null) { model.UpdateTime = now; is_update = true; } else { if ((now - model.UpdateTime.Value).TotalMinutes >= 1) { model.UpdateTime = now; is_update = true; } } if (is_update) { bll_lighting_info.Update(model); } } } } } } } } catch (Exception ex) { log.Info("照明处理失败,数据:" + msg + ",原因:" + ex.Message); } } protected override void OnStop() { // TODO: 在此处添加代码以执行停止服务所需的关闭操作。 } /// /// 时间戳转换为DATETIME格式 /// /// 时间戳字符串 /// public static DateTime GetDateTime(string timeStamp) { DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); string ts = timeStamp + "0000000"; if (timeStamp.Length == 13) { ts = timeStamp + "0000"; } long lTime = long.Parse(ts); TimeSpan toNow = new TimeSpan(lTime); return dtStart.Add(toNow); } /// /// 获取配电室低压设备名称 /// /// /// public static string GetDeviceName1(int code) { var list = new Dictionary(); list.Add(1, "b1AL2"); list.Add(2, "3ATL-ZT1-1"); list.Add(3, "2ATL-ZT1-1"); list.Add(4, "1ATL-ZT1"); list.Add(5, "1ATL-ZT2-1"); list.Add(6, "夜景照明"); list.Add(7, "3ATL-JZ-1"); list.Add(8, "b1AT-YD12"); list.Add(9, "1AT-YD21-1 2AT-YD2-1"); list.Add(10, "3AP-KT21"); list.Add(11, "3AP-KT24"); list.Add(12, "b1AP-KT25"); list.Add(13, "b1AP-CF"); list.Add(14, "停车场"); list.Add(15, "3AT-DT1-1"); list.Add(16, "3AT-DT2-1"); list.Add(17, "3AT-HT-1"); list.Add(18, "b1AT-QW-1"); list.Add(19, "b1AT-RD-1"); list.Add(20, "b1AT-JK"); list.Add(21, "b1ATE-XF-1"); list.Add(22, "b1AT-BD"); list.Add(23, "b1AP-KT26"); list.Add(24, "2AP12"); list.Add(25, "2AP13"); list.Add(26, "2AP21"); list.Add(27, "2AP14"); list.Add(28, "2AP31"); list.Add(29, "b1ATE-XB-1"); list.Add(30, "b2ATE10"); list.Add(31, "b2ATE8-1"); list.Add(32, "b2ATE9-1"); list.Add(33, "b2ATE9-3"); list.Add(34, "3ATE-FJ1"); list.Add(35, "123ATE1"); list.Add(36, "2ATE-FJ1-1"); list.Add(37, "2ATE-FJ3-1"); list.Add(38, "1ATE2-1"); list.Add(39, "b1AL1-3AL1"); list.Add(40, "3ATL-ZT1-2"); list.Add(41, "2ATL-ZT1-2"); list.Add(42, "b1ATL"); list.Add(43, "1ATL-ZT2-2"); list.Add(44, "MWL7"); list.Add(45, "3ATL-JZ-2"); list.Add(46, "b1AT-YD1 3AT-YD1"); list.Add(47, "1AT-YD21-2 2AT-YD2-2"); list.Add(48, "b1AT-KT21"); list.Add(49, "b1AP-KT22"); list.Add(50, "b1AT-KT23"); list.Add(51, "3AT-DT1-2"); list.Add(52, "3AT-DT2-2"); list.Add(53, "3AT-HT-2"); list.Add(54, "b1AT-QW-2"); list.Add(55, "b1AT-RD-2"); list.Add(56, "b2AT-JK"); list.Add(57, "b1ATE-XF-2"); list.Add(58, "b2AT-BD"); list.Add(59, "AC1"); list.Add(60, "3AP-DBR"); list.Add(61, "1AP-LHD"); list.Add(62, "2AP11"); list.Add(63, "1AO-LED"); list.Add(64, "喷泉"); list.Add(65, "b1ATE-XB-2"); list.Add(66, "b1ATE10"); list.Add(67, "b1ATE8-1"); list.Add(68, "b1ATE9-1"); list.Add(69, "b1ATE9-3"); list.Add(70, "3ATE-PJ1"); list.Add(71, "1-3ATE1"); list.Add(72, "2ATE-FJ1-2"); list.Add(73, "2ATE-FJ3-2"); list.Add(74, "1ATE2-2"); if (!list.ContainsKey(code)) { return "未知"; } return list[code]; } /// /// 获取配电室高压设备名称 /// /// /// public static string GetDeviceName2(int code) { var list = new Dictionary(); list.Add(211, "场馆负一配电室高压进线柜-1"); list.Add(212, "场馆负一配电室高压进线柜-2"); if (!list.ContainsKey(code)) { return "未知"; } return list[code]; } /// /// 获取电能-室外箱变设备名称 /// /// /// public static string GetDeviceName3(int code) { var list = new Dictionary(); list.Add(1, "1-3号热泵机组"); list.Add(2, "4-6号热泵机组"); list.Add(3, "7-9号热泵机组"); list.Add(4, "10-12号热泵机组"); list.Add(5, "13-15号热泵机组"); list.Add(6, "16-18号热泵机组"); list.Add(7, "19-21号热泵机组"); list.Add(8, "22-24号热泵机组"); if (!list.ContainsKey(code)) { return "未知"; } return list[code]; } /// /// 获取配电室分类 /// /// /// public static string GetDeviceName4(int code) { var list = new Dictionary(); list.Add(1, "普通照明"); list.Add(2, "展览用电"); list.Add(3, "展览用电"); list.Add(4, "展览用电"); list.Add(5, "展览用电"); list.Add(6, "夜景照明"); list.Add(7, "基站用电"); list.Add(8, "室分机房"); list.Add(9, "室分机房"); list.Add(10, "空调动力"); list.Add(11, "空调动力"); list.Add(12, "空调动力"); list.Add(13, "扶梯"); list.Add(14, "停车场"); list.Add(15, "普通电梯"); list.Add(16, "普通电梯"); list.Add(17, "货梯"); list.Add(18, "潜污泵"); list.Add(19, "弱电"); list.Add(20, "监控室"); list.Add(21, "消防控制室"); list.Add(22, "变配电室"); list.Add(23, "空调室外机"); list.Add(24, "展陈用电"); list.Add(25, "展陈用电"); list.Add(26, "展陈用电"); list.Add(27, "展陈用电"); list.Add(28, "展陈用电"); list.Add(29, "消防水泵房"); list.Add(30, "消防风机"); list.Add(31, "消防风机"); list.Add(32, ""); list.Add(33, "消防风机"); list.Add(34, "消防风机"); list.Add(35, "消防风机"); list.Add(36, "消防风机"); list.Add(37, "消防风机"); list.Add(38, "消防电梯"); list.Add(39, "负一层普通照明"); list.Add(40, "台球厅展览用电"); list.Add(41, "展览用电"); list.Add(42, "展览用电"); list.Add(43, "展览用电"); list.Add(44, "夜景照明"); list.Add(45, "基站用电"); list.Add(46, "室分机房"); list.Add(47, "室分机房"); list.Add(48, "空调动力"); list.Add(49, "空调动力"); list.Add(50, "空调动力"); list.Add(51, "普通电梯"); list.Add(52, "普通电梯"); list.Add(53, "货梯"); list.Add(54, "潜污泵"); list.Add(55, "弱电"); list.Add(56, "监控室"); list.Add(57, "消防控制室"); list.Add(58, "变配电室"); list.Add(59, "空调动力站"); list.Add(60, "电伴热"); list.Add(61, "景观用电"); list.Add(62, "展陈用电"); list.Add(63, "老海岛展陈用电"); list.Add(64, "喷泉"); list.Add(65, "消防水泵房"); list.Add(66, "消防风机"); list.Add(67, "消防风机"); list.Add(68, ""); list.Add(69, "消防风机"); list.Add(70, "消防风机"); list.Add(71, "消防风机"); list.Add(72, "消防风机"); list.Add(73, "消防风机"); list.Add(74, "消防电梯"); if (!list.ContainsKey(code)) { return "未知"; } return list[code]; } } public class IdName { public string Id { get; set; } public string Name { get; set; } } }