终端一体化运控平台
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

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