终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

198 lines
6.6 KiB

  1. using BPASmartClient.Business;
  2. using BPASmartClient.Helper;
  3. using BPASmartClient.Message;
  4. using BPASmartDatavDeviceClient.IoT;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Threading;
  8. namespace BPASmartClient.IoT
  9. {
  10. /// <summary>
  11. /// DataV客户端数据中心
  12. /// </summary>
  13. public class DataVClient
  14. {
  15. #region 单例模式
  16. private volatile static DataVClient _Instance;
  17. public static DataVClient GetInstance() => _Instance ?? (_Instance = new DataVClient());
  18. #endregion
  19. #region 公有变量
  20. //DataV 服务地址
  21. public string DataVApiAddress { set; get; }
  22. //客户端ID
  23. public string ClientId { set; get; }
  24. //MQTT上报集合
  25. //public Dictionary<string, DataVReport> DeviceDataVs = new Dictionary<string, DataVReport>();
  26. public DataVReport DeviceDataV=new DataVReport();
  27. #endregion
  28. #region API调用
  29. /// <summary>
  30. /// 增加告警信息
  31. /// </summary>
  32. /// <param name="alarmTable"></param>
  33. /// <returns>返回ID</returns>
  34. public string HttpAddAlarm(AlarmTable alarmTable)
  35. {
  36. string id = string.Empty;
  37. try
  38. {
  39. if (DeviceDataV != null && DeviceDataV.GetIsConnected())
  40. {
  41. string url = DataVApiAddress + "/api/Alarm/Create";
  42. alarmTable.ClientId = ClientId;
  43. alarmTable.devicename = DeviceDataV.deviceTable.devicename;
  44. string redata = HttpRequestHelper.HttpPostRequest(url, Tools.JsonConvertTools(alarmTable));
  45. if (!string.IsNullOrEmpty(redata))
  46. {
  47. JsonMsg<AlarmTable> msg = Tools.JsonToObjectTools<JsonMsg<AlarmTable>>(redata);
  48. id = msg?.obj?.data?.Id;
  49. }
  50. }
  51. }
  52. catch (Exception ex)
  53. {
  54. MessageLog.GetInstance.Show(ex.Message);
  55. }
  56. return id;
  57. }
  58. /// <summary>
  59. /// 根据ID删除告警信息
  60. /// </summary>
  61. /// <param name="alarm"></param>
  62. public void HttpDeleteAlarm(string id)
  63. {
  64. try
  65. {
  66. if (string.IsNullOrEmpty(id)) { MessageLog.GetInstance.Show("API调用删除告警信息,ID不能为空!"); return; }
  67. string url = DataVApiAddress + "/api/Alarm/Delete?id=" + id;
  68. HttpRequestHelper.HttpGetRequest(url);
  69. }
  70. catch (Exception ex)
  71. {
  72. MessageLog.GetInstance.Show(ex.Message);
  73. }
  74. }
  75. /// <summary>
  76. /// 增加日志信息
  77. /// </summary>
  78. /// <param name="alarmTable"></param>
  79. /// <returns>返回ID</returns>
  80. public string HttpAddLog(LogTable logTable)
  81. {
  82. string id = string.Empty;
  83. try
  84. {
  85. if (DeviceDataV != null && DeviceDataV.GetIsConnected())
  86. {
  87. string url = DataVApiAddress + "/api/Log/Create";
  88. logTable.ClientId = ClientId;
  89. logTable.devicename = DeviceDataV.deviceTable.devicename;
  90. string redata = HttpRequestHelper.HttpPostRequest(url, Tools.JsonConvertTools(logTable));
  91. if (!string.IsNullOrEmpty(redata))
  92. {
  93. JsonMsg<LogTable> msg = Tools.JsonToObjectTools<JsonMsg<LogTable>>(redata);
  94. id = msg?.obj?.data?.Id;
  95. }
  96. }
  97. }
  98. catch (Exception ex)
  99. {
  100. MessageLog.GetInstance.Show(ex.Message);
  101. }
  102. return id;
  103. }
  104. #endregion
  105. public DataVClient()
  106. {
  107. DataVApiAddress = System.Configuration.ConfigurationManager.AppSettings["DataVServiceUri"].ToString();
  108. ClientId = System.Configuration.ConfigurationManager.AppSettings["ClientId"].ToString();
  109. Initialize();
  110. }
  111. public void Dispose()
  112. {
  113. ThreadManage.GetInstance().StopTask("DataV数据上报");
  114. }
  115. public void Initialize()
  116. {
  117. string message = string.Empty;
  118. DeviceDataV = new DataVReport();
  119. bool ret = DeviceDataV.Initialize(DataVApiAddress, ClientId,"", ref message);
  120. DeviceDataV.DataVMessageAction += DevIOTActionHandler;
  121. MessageLog.GetInstance.ShowEx(message);
  122. }
  123. /// <summary>
  124. /// 启动DataV数据上报
  125. /// </summary>
  126. public void Start()
  127. {
  128. //ThreadManage.GetInstance().StartLong(new Action(() =>
  129. //{
  130. // //Plugin.GetInstance().GetPlugin<DeviceMgr>().GetDevices()?.ForEach(device =>
  131. // //{
  132. // // Dictionary<string, object> status = device.Status.GetStatus();
  133. // // if (DeviceDataV.GetIsConnected())
  134. // // {
  135. // // //DeviceDataVs[device.DeviceId.ToString()].
  136. // // }
  137. // //});
  138. // Thread.Sleep(5000);
  139. //}), "DataV数据上报", true);
  140. }
  141. /// <summary>
  142. /// 接收云端消息
  143. /// </summary>
  144. /// <param name="topic"></param>
  145. /// <param name="message"></param>
  146. private void DevIOTActionHandler(string deviceId,string topic, string message)
  147. {
  148. //if (DeviceDataVs.ContainsKey() DeviceDataVs[deviceId].BroadcastTopic == topic && !string.IsNullOrEmpty(message))//广播主题消息,将广播消息发送到相应客户端
  149. //{
  150. // IOTCommandModel iOTCommand = Tools.JsonToObjectTools<IOTCommandModel>(message);
  151. // if (iOTCommand.deviceName == DeviceDataVs[deviceId].deviceTable.devicename)
  152. // ActionManage.GetInstance.Send("IotBroadcast", iOTCommand);
  153. //}
  154. }
  155. }
  156. //命令实体类
  157. public class IOTCommandModel
  158. {
  159. /// <summary>
  160. /// 设备名称
  161. /// </summary>
  162. public string deviceName
  163. {
  164. get;
  165. set;
  166. }
  167. /// <summary>
  168. /// 命令名称:0 控制类 1 设置属性 2 通知信息类
  169. /// </summary>
  170. public int CommandName
  171. {
  172. get;
  173. set;
  174. }
  175. /// <summary>
  176. /// 命令变量:执行变量 key为属性或时间 value为值或者消息
  177. /// </summary>
  178. public Dictionary<string, string> CommandValue
  179. {
  180. get;
  181. set;
  182. }
  183. }
  184. }