终端一体化运控平台
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.
 
 
 

287 line
9.9 KiB

  1. using BPA.Message.Enum;
  2. using BPA.Message.IOT;
  3. using BPASmartClient.Business;
  4. using BPASmartClient.Device;
  5. using BPASmartClient.Helper;
  6. using BPASmartClient.Message;
  7. using BPASmartDatavDeviceClient.IoT;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Threading;
  12. namespace BPASmartClient.IoT
  13. {
  14. /// <summary>
  15. /// DataV客户端数据中心
  16. /// </summary>
  17. public class DataVClient
  18. {
  19. #region 单例模式
  20. private volatile static DataVClient _Instance;
  21. public static DataVClient GetInstance() => _Instance ?? (_Instance = new DataVClient());
  22. public DataVClient()
  23. {
  24. DataVApiAddress = System.Configuration.ConfigurationManager.AppSettings["DataVServiceUri"].ToString();
  25. ClientId = System.Configuration.ConfigurationManager.AppSettings["ClientId"].ToString();
  26. Initialize();
  27. }
  28. #endregion
  29. #region 公有变量
  30. /// <summary>
  31. /// DataV 服务地址
  32. /// </summary>
  33. public string DataVApiAddress { set; get; }
  34. /// <summary>
  35. /// 客户端ID
  36. /// </summary>
  37. public string ClientId { set; get; }
  38. /// <summary>
  39. /// MQTT上报集合
  40. /// </summary>
  41. public DataVReport DeviceDataV = new DataVReport();
  42. /// <summary>
  43. /// 大屏上报Model
  44. /// </summary>
  45. public DataVAPI.Tool.IOT.IOTDevSXModel iOTDevSXModel = new DataVAPI.Tool.IOT.IOTDevSXModel() { };
  46. /// <summary>
  47. /// 广播
  48. /// </summary>
  49. //public string PubTopic = "/broadcast/" + "grgpECHSL7q" + "/" + "Transit_SetDevice";
  50. public string PubTopic = "/broadcast/" + "grgpECHSL7q" + "/" + "Transit_Test_SetDevice";
  51. /// <summary>
  52. /// key值
  53. /// </summary>
  54. public Dictionary<string,string> keyValues = new Dictionary<string, string>();
  55. #endregion
  56. #region API调用
  57. /// <summary>
  58. /// 增加告警信息
  59. /// </summary>
  60. /// <param name="alarmTable"></param>
  61. /// <returns>返回ID</returns>
  62. public string HttpAddAlarm(AlarmTable alarmTable)
  63. {
  64. try
  65. {
  66. if (DeviceDataV != null && DeviceDataV.GetIsConnected())
  67. {
  68. alarmTable.ClientId = ClientId;
  69. alarmTable.devicename = DeviceDataV.deviceTable.devicename;
  70. DeviceDataV.IOT_Publish(PubTopic, Tools.JsonConvertTools(alarmTable));
  71. }
  72. }
  73. catch (Exception ex)
  74. {
  75. MessageLog.GetInstance.Show(ex.Message);
  76. }
  77. return alarmTable.KeyID;
  78. }
  79. /// <summary>
  80. /// 增加日志信息
  81. /// </summary>
  82. /// <param name="alarmTable"></param>
  83. /// <returns>返回ID</returns>
  84. public string HttpAddLog(LogTable logTable)
  85. {
  86. string id = string.Empty;
  87. try
  88. {
  89. if (DeviceDataV != null && DeviceDataV.GetIsConnected())
  90. {
  91. logTable.ClientId = ClientId;
  92. logTable.devicename = DeviceDataV.deviceTable.devicename;
  93. DeviceDataV.IOT_Publish(PubTopic, Tools.JsonConvertTools(logTable));
  94. }
  95. }
  96. catch (Exception ex)
  97. {
  98. MessageLog.GetInstance.Show(ex.Message);
  99. }
  100. return id;
  101. }
  102. #endregion
  103. #region 公用
  104. /// <summary>
  105. /// 释放
  106. /// </summary>
  107. public void Dispose()
  108. {
  109. ThreadManage.GetInstance().StopTask("DataV数据上报");
  110. }
  111. /// <summary>
  112. /// 初始化
  113. /// </summary>
  114. public void Initialize()
  115. {
  116. string message = string.Empty;
  117. if (DeviceDataV.Initialize(DataVApiAddress, ClientId, "", ref message))
  118. {
  119. DeviceDataV.DataVMessageAction += DevIOTActionHandler;
  120. MessageLog.GetInstance.Show($"客户端:【{ClientId}】,设备名称{DeviceDataV.deviceTable.devicename}阿里云连接成功");
  121. }
  122. else
  123. {
  124. MessageLog.GetInstance.ShowEx(message);
  125. }
  126. Plugin.GetInstance()?.GetPlugin<DeviceMgr>()?.GetDevices()?.ForEach(device =>
  127. {
  128. device.AddErrorAction+= AddErrorAction;
  129. device.DeleteErrorAction += DeleteErrorAction;
  130. });
  131. }
  132. /// <summary>
  133. /// 启动DataV数据上报
  134. /// </summary>
  135. public void Start()
  136. {
  137. ThreadManage.GetInstance().StartLong(new Action(() =>
  138. {
  139. if (DeviceDataV != null && DeviceDataV.GetIsConnected())
  140. {
  141. List<object> dataVNode = new List<object>();
  142. Plugin.GetInstance()?.GetPlugin<DeviceMgr>()?.GetDevices()?.ForEach(device =>
  143. {
  144. var obj = new
  145. {
  146. DeviceId = device.DeviceId.ToString(),
  147. devicename = DeviceDataV.deviceTable.devicename,
  148. Name = device.Name,
  149. DeviceType = device.DeviceType.ToString(),
  150. IsBusy = device.IsBusy ? "忙碌" : "空闲",
  151. IsBusyColor = device.IsBusy ? new ALYColor { r = 255, g = 0, b = 0, a = 1 } : new ALYColor { r = 51, g = 232, b = 34, a = 1 },
  152. IsHealth = device.IsHealth ? "健康" : "故障",
  153. IsHealthColor = !device.IsHealth ? new ALYColor { r = 255, g = 0, b = 0, a = 1 } : new ALYColor { r = 51, g = 232, b = 34, a = 1 },
  154. Status = device.Status.GetIOTStatus(),
  155. gjxx = device.GetError(),
  156. rzxx = device.GetLog(),
  157. VariableMonitor = device.GetVariableMonitor(),
  158. };
  159. dataVNode.Add(obj);
  160. });
  161. if (dataVNode.Count > 0)
  162. {
  163. iOTDevSXModel.NodeStatus = Tools.JsonConvertTools(new { data = dataVNode });
  164. DeviceDataV.IOT_Publish(DeviceDataV.PubTopic, iOTDevSXModel.Tojson());
  165. }
  166. }
  167. Thread.Sleep(3000);
  168. }), "DataV数据上报", true);
  169. }
  170. #endregion
  171. #region 私有
  172. /// <summary>
  173. /// 增加告警
  174. /// </summary>
  175. /// <param name="obj"></param>
  176. private void AddErrorAction(object obj)
  177. {
  178. string id = Guid.NewGuid().ToString();
  179. HttpAddAlarm(new AlarmTable
  180. {
  181. AlarmTime = GetPropertyValue(obj, "Time").ToString(),
  182. AlarmType = GetPropertyValue(obj, "Type").ToString(),
  183. AlarmMessage = GetPropertyValue(obj, "Text").ToString(),
  184. AlarmVla = "告警",
  185. KeyID = id,
  186. });
  187. keyValues[GetPropertyValue(obj, "Time").ToString() + GetPropertyValue(obj, "Type").ToString() + GetPropertyValue(obj, "Text").ToString()] =id ;
  188. }
  189. /// <summary>
  190. /// 删除告警
  191. /// </summary>
  192. /// <param name="obj"></param>
  193. private void DeleteErrorAction(object obj)
  194. {
  195. string message = GetPropertyValue(obj, "Time").ToString() + GetPropertyValue(obj, "Type").ToString() + GetPropertyValue(obj, "Text").ToString();
  196. if (keyValues.ContainsKey(message))
  197. {
  198. HttpAddAlarm(new AlarmTable
  199. {
  200. AlarmTime = GetPropertyValue(obj, "Time").ToString(),
  201. AlarmType = GetPropertyValue(obj, "Type").ToString(),
  202. AlarmMessage = GetPropertyValue(obj, "Text").ToString(),
  203. AlarmVla = "告警",
  204. KeyID = keyValues[message],
  205. State="n"
  206. });
  207. }
  208. }
  209. /// <summary>
  210. /// 接收云端消息
  211. /// </summary>
  212. /// <param name="topic"></param>
  213. /// <param name="message"></param>
  214. private void DevIOTActionHandler(string deviceId, string topic, string message)
  215. {
  216. if (DeviceDataV.BroadcastTopic == topic && !string.IsNullOrEmpty(message))//广播主题消息,将广播消息发送到相应客户端
  217. {
  218. IOTCommandModel iOTCommand = Tools.JsonToObjectTools<IOTCommandModel>(message);
  219. if (iOTCommand.deviceName == DeviceDataV.deviceTable.devicename)
  220. ActionManage.GetInstance.Send("IotBroadcast", iOTCommand);
  221. }
  222. }
  223. /// <summary>
  224. /// 获取某个对象中的属性值
  225. /// </summary>
  226. /// <param name="info"></param>
  227. /// <param name="field"></param>
  228. public object GetPropertyValue(object info, string field)
  229. {
  230. if (info == null) return null;
  231. Type t = info.GetType();
  232. IEnumerable<System.Reflection.PropertyInfo> property = from pi in t.GetProperties() where pi.Name.ToLower() == field.ToLower() select pi;
  233. return property.First().GetValue(info, null);
  234. }
  235. #endregion
  236. }
  237. //命令实体类
  238. public class IOTCommandModel
  239. {
  240. /// <summary>
  241. /// 设备名称
  242. /// </summary>
  243. public string deviceName
  244. {
  245. get;
  246. set;
  247. }
  248. /// <summary>
  249. /// 命令名称:0 控制类 1 设置属性 2 通知信息类
  250. /// </summary>
  251. public int CommandName
  252. {
  253. get;
  254. set;
  255. }
  256. /// <summary>
  257. /// 命令变量:执行变量 key为属性或时间 value为值或者消息
  258. /// </summary>
  259. public Dictionary<string, string> CommandValue
  260. {
  261. get;
  262. set;
  263. }
  264. }
  265. }