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

DataVClient.cs 9.8 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. 
  2. using BPA.Message.Enum;
  3. using BPA.Message.IOT;
  4. using BPASmartClient.Business;
  5. using BPASmartClient.Device;
  6. using BPASmartClient.Helper;
  7. using BPASmartClient.Message;
  8. using BPASmartDatavDeviceClient.IoT;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Threading;
  13. namespace BPASmartClient.IoT
  14. {
  15. /// <summary>
  16. /// DataV客户端数据中心
  17. /// </summary>
  18. public class DataVClient
  19. {
  20. #region 单例模式
  21. private volatile static DataVClient _Instance;
  22. public static DataVClient GetInstance() => _Instance ?? (_Instance = new DataVClient());
  23. public DataVClient()
  24. {
  25. DataVApiAddress = System.Configuration.ConfigurationManager.AppSettings["DataVServiceUri"].ToString();
  26. ClientId = System.Configuration.ConfigurationManager.AppSettings["ClientId"].ToString();
  27. Initialize();
  28. }
  29. #endregion
  30. #region 公有变量
  31. /// <summary>
  32. /// DataV 服务地址
  33. /// </summary>
  34. public string DataVApiAddress { set; get; }
  35. /// <summary>
  36. /// 客户端ID
  37. /// </summary>
  38. public string ClientId { set; get; }
  39. /// <summary>
  40. /// MQTT上报集合
  41. /// </summary>
  42. public DataVReport DeviceDataV = new DataVReport();
  43. /// <summary>
  44. /// 大屏上报Model
  45. /// </summary>
  46. public DataVAPI.Tool.IOT.IOTDevSXModel iOTDevSXModel = new DataVAPI.Tool.IOT.IOTDevSXModel() { };
  47. /// <summary>
  48. /// 广播
  49. /// </summary>
  50. public string PubTopic = "/broadcast/" + "grgpECHSL7q" + "/" + "Transit_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. }