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