From 49715224e7ab3b767ddbea29e1d4670258779d67 Mon Sep 17 00:00:00 2001 From: fyf Date: Thu, 5 May 2022 11:59:27 +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 | 163 +++++++++++++----- BPASmartClient.IoT/Model/DataVReport.cs | 10 +- .../BPASmartClient.Message.csproj | 4 + BPASmartClient.Message/MessageLog.cs | 35 +++- BPASmartClient/BPASmartClient.csproj | 1 + BPASmartClient/Control/LogView.xaml | 8 + BPASmartClient/MainWindow.xaml.cs | 2 + 7 files changed, 173 insertions(+), 50 deletions(-) diff --git a/BPASmartClient.IoT/DataVClient.cs b/BPASmartClient.IoT/DataVClient.cs index acc481b6..bf8bd8f8 100644 --- a/BPASmartClient.IoT/DataVClient.cs +++ b/BPASmartClient.IoT/DataVClient.cs @@ -11,11 +11,11 @@ namespace BPASmartClient.IoT /// /// DataV客户端数据中心 /// - public class DataVClient : IPlugin + public class DataVClient { #region 单例模式 private volatile static DataVClient _Instance; - public static DataVClient GetInstance => _Instance ?? (_Instance = new DataVClient()); + public static DataVClient GetInstance() => _Instance ?? (_Instance = new DataVClient()); #endregion #region 公有变量 @@ -24,13 +24,94 @@ namespace BPASmartClient.IoT //客户端ID public string ClientId { set; get; } //MQTT上报集合 - public Dictionary DeviceDataVs = new Dictionary(); + //public Dictionary DeviceDataVs = new Dictionary(); + public DataVReport DeviceDataV=new DataVReport(); #endregion + #region API调用 + /// + /// 增加告警信息 + /// + /// + /// 返回ID + public string HttpAddAlarm(AlarmTable alarmTable) + { + string id = string.Empty; + try + { + 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; + } + } + } + catch (Exception ex) + { + MessageLog.GetInstance.Show(ex.Message); + } + 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); + } + } + + /// + /// 增加日志信息 + /// + /// + /// 返回ID + public string HttpAddLog(LogTable logTable) + { + string id = string.Empty; + try + { + 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; + } + } + } + catch (Exception ex) + { + MessageLog.GetInstance.Show(ex.Message); + } + return id; + } + #endregion public DataVClient() { DataVApiAddress = System.Configuration.ConfigurationManager.AppSettings["DataVServiceUri"].ToString(); ClientId = System.Configuration.ConfigurationManager.AppSettings["ClientId"].ToString(); + Initialize(); } public void Dispose() @@ -40,16 +121,11 @@ namespace BPASmartClient.IoT public void Initialize() { - Plugin.GetInstance().GetPlugin().GetDevices()?.ForEach(device => - { - string message=string.Empty; - DataVReport dataV = new DataVReport(); - bool ret= dataV.Initialize(DataVApiAddress, ClientId, device.DeviceId.ToString(),ref message); - dataV.DataVMessageAction += DevIOTActionHandler; - MessageLog.GetInstance.ShowEx(message); - if (!ret) - DeviceDataVs[device.DeviceId.ToString()] = dataV; - }); + string message = string.Empty; + DeviceDataV = new DataVReport(); + bool ret = DeviceDataV.Initialize(DataVApiAddress, ClientId,"", ref message); + DeviceDataV.DataVMessageAction += DevIOTActionHandler; + MessageLog.GetInstance.ShowEx(message); } /// @@ -57,22 +133,19 @@ namespace BPASmartClient.IoT /// public void Start() { - ThreadManage.GetInstance().StartLong(new Action(() => - { - Plugin.GetInstance().GetPlugin().GetDevices()?.ForEach(device => - { - if (DeviceDataVs.ContainsKey(device.DeviceId.ToString())) - { - Dictionary status= device.Status.GetStatus(); - if (DeviceDataVs[device.DeviceId.ToString()].GetIsConnected()) - { - //DeviceDataVs[device.DeviceId.ToString()]. - } - } - }); + //ThreadManage.GetInstance().StartLong(new Action(() => + //{ + // //Plugin.GetInstance().GetPlugin().GetDevices()?.ForEach(device => + // //{ + // // Dictionary status = device.Status.GetStatus(); + // // if (DeviceDataV.GetIsConnected()) + // // { + // // //DeviceDataVs[device.DeviceId.ToString()]. + // // } + // //}); - Thread.Sleep(5000); - }), "DataV数据上报", true); + // Thread.Sleep(5000); + //}), "DataV数据上报", true); } /// @@ -82,41 +155,39 @@ namespace BPASmartClient.IoT /// private void DevIOTActionHandler(string deviceId,string topic, string message) { - if (DeviceDataVs[deviceId].BroadcastTopic == topic && !string.IsNullOrEmpty(message))//广播主题消息,将广播消息发送到相应客户端 - { - IOTCommandModel iOTCommand = Tools.JsonToObjectTools(message); - if (iOTCommand.deviceName == DeviceDataVs[deviceId].deviceTable.devicename) - ActionManage.GetInstance.Send("IotBroadcast", iOTCommand); - } + //if (DeviceDataVs.ContainsKey() DeviceDataVs[deviceId].BroadcastTopic == topic && !string.IsNullOrEmpty(message))//广播主题消息,将广播消息发送到相应客户端 + //{ + // IOTCommandModel iOTCommand = Tools.JsonToObjectTools(message); + // if (iOTCommand.deviceName == DeviceDataVs[deviceId].deviceTable.devicename) + // ActionManage.GetInstance.Send("IotBroadcast", iOTCommand); + //} } } - // - // 摘要: - // 命令实体类 + //命令实体类 public class IOTCommandModel { - // - // 摘要: - // 设备名称 + /// + /// 设备名称 + /// public string deviceName { get; set; } - // - // 摘要: - // 命令名称:0 控制类 1 设置属性 2 通知信息类 + /// + /// 命令名称:0 控制类 1 设置属性 2 通知信息类 + /// public int CommandName { get; set; } - // - // 摘要: - // 命令变量:执行变量 key为属性或时间 value为值或者消息 + /// + /// 命令变量:执行变量 key为属性或时间 value为值或者消息 + /// public Dictionary CommandValue { get; diff --git a/BPASmartClient.IoT/Model/DataVReport.cs b/BPASmartClient.IoT/Model/DataVReport.cs index ed8ce439..ce83dd6d 100644 --- a/BPASmartClient.IoT/Model/DataVReport.cs +++ b/BPASmartClient.IoT/Model/DataVReport.cs @@ -25,7 +25,7 @@ namespace BPASmartDatavDeviceClient.IoT { if(string.IsNullOrEmpty(url))return false; DeviceTable device; deviceId = _deviceId; - if (!CreateLinks(url, _clientId, _deviceId, out device)) + if (!CreateLinks(url, _clientId,out device, _deviceId)) { message += $"客户端{_clientId}设备{_deviceId}阿里云上没有该设备。"; return false; @@ -118,11 +118,15 @@ namespace BPASmartDatavDeviceClient.IoT /// /// 创建连接 /// - private bool CreateLinks(string url, string clientId, string deviceId, out DeviceTable device) + private bool CreateLinks(string url, string clientId, out DeviceTable device ,string deviceId = "") { try { - string json = HttpRequestHelper.HttpGetRequest($"{url}/api/Device/Query?clientId={clientId}&deviceId={deviceId}"); + string json = string.Empty; + if (string.IsNullOrEmpty(deviceId)) + json = HttpRequestHelper.HttpGetRequest($"{url}/api/Device/Query?clientId={clientId}"); + else + json = HttpRequestHelper.HttpGetRequest($"{url}/api/Device/Query?clientId={clientId}&deviceId={deviceId}"); JsonMsg> jsonMsg = Tools.JsonToObjectTools>>(json); if (jsonMsg.obj != null && jsonMsg.obj.data != null) { diff --git a/BPASmartClient.Message/BPASmartClient.Message.csproj b/BPASmartClient.Message/BPASmartClient.Message.csproj index dbc15171..bd08bd1d 100644 --- a/BPASmartClient.Message/BPASmartClient.Message.csproj +++ b/BPASmartClient.Message/BPASmartClient.Message.csproj @@ -4,4 +4,8 @@ net6.0 + + + + diff --git a/BPASmartClient.Message/MessageLog.cs b/BPASmartClient.Message/MessageLog.cs index c86144d6..7b764519 100644 --- a/BPASmartClient.Message/MessageLog.cs +++ b/BPASmartClient.Message/MessageLog.cs @@ -1,7 +1,9 @@ -using System; +using BPASmartClient.Peripheral; +using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; +using System.Reflection; using System.Text; using System.Threading.Tasks; @@ -59,7 +61,38 @@ namespace BPASmartClient.Message } #endregion + #region 查找设备ID + /// + /// 查询设备ID + /// + /// + public object ReturnDeviceID() + { + object DeviceId = null; + StackTrace trace = new StackTrace(); + for (int i = 1; i < 10; i++) + { + Type type = trace.GetFrame(i).GetMethod().ReflectedType; + try + { + object obj = Activator.CreateInstance(type); + if (obj is IPeripheral) + { + IPeripheral peripheral = obj as IPeripheral; + DeviceId = type?.GetProperty("DeviceId")?.GetValue(peripheral, null); + } + if (DeviceId != null) break; + } + catch (Exception ex) + { + } + + } + + return DeviceId; + } + #endregion diff --git a/BPASmartClient/BPASmartClient.csproj b/BPASmartClient/BPASmartClient.csproj index b74a8e16..013ca764 100644 --- a/BPASmartClient/BPASmartClient.csproj +++ b/BPASmartClient/BPASmartClient.csproj @@ -21,6 +21,7 @@ + diff --git a/BPASmartClient/Control/LogView.xaml b/BPASmartClient/Control/LogView.xaml index 0cb1f666..a4918f84 100644 --- a/BPASmartClient/Control/LogView.xaml +++ b/BPASmartClient/Control/LogView.xaml @@ -42,6 +42,14 @@ + + diff --git a/BPASmartClient/MainWindow.xaml.cs b/BPASmartClient/MainWindow.xaml.cs index 4271b6e6..1f02420a 100644 --- a/BPASmartClient/MainWindow.xaml.cs +++ b/BPASmartClient/MainWindow.xaml.cs @@ -5,6 +5,7 @@ using BPASmartClient.CustomResource.UserControls.MessageShow; using BPASmartClient.Device; using BPASmartClient.EventBus; using BPASmartClient.Helper; +using BPASmartClient.IoT; using BPASmartClient.Message; using BPASmartClient.Model; using BPASmartClient.Model.冰淇淋.Enum; @@ -60,6 +61,7 @@ namespace BPASmartClient ThreadManage.GetInstance().Start(new Action(() => { + DataVClient.GetInstance().Start(); new MainConsole().Start(); }), "启动主控制台"); }