From 1cb7c07df9a3222ece8130459e0cd5ccdd97c18d Mon Sep 17 00:00:00 2001 From: fyf Date: Mon, 9 May 2022 10:27:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0IOT?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BPASmartClient.IoT/DataVClient.cs | 58 ++++++------------------ BPASmartClient.IoT/Model/DataVReport.cs | 37 +++++++++++++++ BPASmartClient.ViewModel/LogViewModel.cs | 2 +- BPASmartClient/App.config | 1 - 4 files changed, 52 insertions(+), 46 deletions(-) diff --git a/BPASmartClient.IoT/DataVClient.cs b/BPASmartClient.IoT/DataVClient.cs index e6feaa29..50967f0c 100644 --- a/BPASmartClient.IoT/DataVClient.cs +++ b/BPASmartClient.IoT/DataVClient.cs @@ -45,6 +45,10 @@ namespace BPASmartClient.IoT /// 大屏上报Model /// public DataVAPI.Tool.IOT.IOTDevSXModel iOTDevSXModel = new DataVAPI.Tool.IOT.IOTDevSXModel() { }; + /// + /// 广播 + /// + public string PubTopic = "/broadcast/" + "grgpECHSL7q" + "/" + "Transit_SetDevice"; #endregion #region API调用 @@ -60,15 +64,9 @@ namespace BPASmartClient.IoT { if (DeviceDataV != null && DeviceDataV.GetIsConnected()) { - string url = DataVApiAddress + "/api/Alarm/Create"; alarmTable.ClientId = ClientId; alarmTable.devicename = DeviceDataV.deviceTable.devicename; - string redata = HttpRequestHelper.HttpPostRequest(url, Tools.JsonConvertTools(alarmTable)); - if (!string.IsNullOrEmpty(redata)) - { - JsonMsg msg = Tools.JsonToObjectTools>(redata); - id = msg?.obj?.data?.Id; - } + DeviceDataV.IOT_Publish(PubTopic, Tools.JsonConvertTools(alarmTable)); } } catch (Exception ex) @@ -78,24 +76,6 @@ namespace BPASmartClient.IoT return id; } - /// - /// 根据ID删除告警信息 - /// - /// - public void HttpDeleteAlarm(string id) - { - try - { - if (string.IsNullOrEmpty(id)) { MessageLog.GetInstance.Show("API调用删除告警信息,ID不能为空!"); return; } - string url = DataVApiAddress + "/api/Alarm/Delete?id=" + id; - HttpRequestHelper.HttpGetRequest(url); - } - catch (Exception ex) - { - MessageLog.GetInstance.Show(ex.Message); - } - } - /// /// 增加日志信息 /// @@ -108,15 +88,9 @@ namespace BPASmartClient.IoT { if (DeviceDataV != null && DeviceDataV.GetIsConnected()) { - string url = DataVApiAddress + "/api/Log/Create"; logTable.ClientId = ClientId; logTable.devicename = DeviceDataV.deviceTable.devicename; - string redata = HttpRequestHelper.HttpPostRequest(url, Tools.JsonConvertTools(logTable)); - if (!string.IsNullOrEmpty(redata)) - { - JsonMsg msg = Tools.JsonToObjectTools>(redata); - id = msg?.obj?.data?.Id; - } + DeviceDataV.IOT_Publish(PubTopic, Tools.JsonConvertTools(logTable)); } } catch (Exception ex) @@ -147,7 +121,10 @@ namespace BPASmartClient.IoT DeviceDataV.DataVMessageAction += DevIOTActionHandler; MessageLog.GetInstance.Show($"客户端:【{ClientId}】,设备名称{DeviceDataV.deviceTable.devicename}阿里云连接成功"); } - else MessageLog.GetInstance.ShowEx(message); + else + { + MessageLog.GetInstance.ShowEx(message); + } } /// @@ -159,8 +136,7 @@ namespace BPASmartClient.IoT { if (DeviceDataV != null && DeviceDataV.GetIsConnected()) { - DataVNodeStatus dataVNode = new DataVNodeStatus(); - dataVNode.data = new List(); + List dataVNode = new List(); Plugin.GetInstance()?.GetPlugin()?.GetDevices()?.ForEach(device => { var obj=new @@ -174,13 +150,13 @@ namespace BPASmartClient.IoT IsHealthColor = !device.IsHealth ? new ALYColor { r = 255, g = 0, b = 0, a = 1 } : new ALYColor { r = 51, g = 232, b = 34, a = 1 }, Status = device.Status.GetStatus(), }; - dataVNode.data.Add(obj); + dataVNode.Add(obj); }); - if (dataVNode.data.Count > 0) + if (dataVNode.Count > 0) { iOTDevSXModel.KZSX = Tools.JsonConvertTools(new DevSX { data = new List { new DevSXBase { SXMC = "", SXLX = "" } } }); iOTDevSXModel.JBSX = Tools.JsonConvertTools(new DevSX { data = new List { new DevSXBase { SXMC = "", SXLX = "" } } }); - iOTDevSXModel.NodeStatus = Tools.JsonConvertTools(dataVNode); + iOTDevSXModel.NodeStatus = Tools.JsonConvertTools(new { data = dataVNode }); DeviceDataV.IOT_Publish(DeviceDataV.PubTopic, iOTDevSXModel.Tojson()); } } @@ -237,10 +213,4 @@ namespace BPASmartClient.IoT set; } } - - public class DataVNodeStatus - { - public List data { get; set; } - } - } diff --git a/BPASmartClient.IoT/Model/DataVReport.cs b/BPASmartClient.IoT/Model/DataVReport.cs index 5b2897cb..faf8236b 100644 --- a/BPASmartClient.IoT/Model/DataVReport.cs +++ b/BPASmartClient.IoT/Model/DataVReport.cs @@ -36,6 +36,43 @@ namespace BPASmartDatavDeviceClient.IoT return DatavDeviceClient.IsConnected; } + /// + /// 初始化IOT连接 + /// + public bool InitializeNo(string _productkey, string _devicename, string _devicesecret, ref string message) + { + try + { + deviceTable.devicename = _devicename; + SetValue(_productkey, _devicename, _devicesecret); + IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName()); + string _clientIp = host.AddressList.FirstOrDefault( + ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).ToString(); + string t = Convert.ToString(DateTimeOffset.Now.ToUnixTimeMilliseconds()); + string signmethod = "hmacmd5"; + + Dictionary dict = new Dictionary(); + dict.Add("productKey", ProductKey); + dict.Add("deviceName", DeviceName); + dict.Add("clientId", _clientIp); + dict.Add("timestamp", t); + + mqttUserName = DeviceName + "&" + ProductKey; + mqttPassword = IotSignUtils.sign(dict, DeviceSecret, signmethod); + mqttClientId = _clientIp + "|securemode=3,signmethod=" + signmethod + ",timestamp=" + t + "|"; + targetServer = ProductKey + ".iot-as-mqtt." + RegionId + ".aliyuncs.com"; + ConnectMqtt(targetServer, mqttClientId, mqttUserName, mqttPassword); + } + catch (Exception ex) + { + message += $"阿里云{_devicename}连接失败,{ex.Message}"; + return false; + } + IOT_Subscribe(BroadcastTopic);//订阅广播主题 + if (!DatavDeviceClient.IsConnected) message += $"客户端:【】,设备名称{deviceTable.devicename}阿里云连接失败.不能上报业务信息"; + return DatavDeviceClient.IsConnected; + } + /// /// 获取连接状态 /// diff --git a/BPASmartClient.ViewModel/LogViewModel.cs b/BPASmartClient.ViewModel/LogViewModel.cs index b9dff589..d365f4ed 100644 --- a/BPASmartClient.ViewModel/LogViewModel.cs +++ b/BPASmartClient.ViewModel/LogViewModel.cs @@ -60,7 +60,7 @@ namespace BPASmartClient.ViewModel DataVClient.GetInstance().HttpAddLog(new LogTable { ClientId = ClientId, - LogTime = DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss"), + LogTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), LogType = "严重", LogMessage = s, LogVla = "错误日志", diff --git a/BPASmartClient/App.config b/BPASmartClient/App.config index 51c9ab2b..7cfec926 100644 --- a/BPASmartClient/App.config +++ b/BPASmartClient/App.config @@ -19,7 +19,6 @@ -