终端一体化运控平台
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

DataVClient.cs 18 KiB

2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
2年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. ClientId = Plugin.GetInstance().GetPlugin<ConfigMgr>().ClientId.ToString();
  38. DeviceName = System.Configuration.ConfigurationManager.AppSettings["DeviceName"].ToString();
  39. ProductKey = System.Configuration.ConfigurationManager.AppSettings["ProductKey"].ToString();
  40. DeviceSecret = System.Configuration.ConfigurationManager.AppSettings["DeviceSecret"].ToString();
  41. StartupMode = System.Configuration.ConfigurationManager.AppSettings["StartupMode"].ToString();
  42. BroadcastPubTopic = InternetInfo.BroadcastPubTopic;
  43. //MaintainTable maintainTable = new MaintainTable();
  44. //maintainTable.Id = Guid.NewGuid().ToString();
  45. //maintainTable.MaintainTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  46. //maintainTable.MaintainType = "一般";
  47. //maintainTable.MaintainVla = "MorkS";
  48. //maintainTable.MaintainMessage = "xxxxx故障需要维修";
  49. //maintainTable.DeviceId = "";
  50. //maintainTable.ClientId = ClientId;
  51. //maintainTable.devicename = DeviceDataV.deviceTable.devicename;
  52. //maintainTables.Add(maintainTable);
  53. }
  54. #endregion
  55. #region 公有变量
  56. public string StartupMode { set; get; }
  57. public string DeviceName { set; get; }
  58. public string ProductKey { set; get; }
  59. public string DeviceSecret { set; get; }
  60. public string BroadcastPubTopic { set; get; }
  61. /// <summary>
  62. /// DataV 服务地址
  63. /// </summary>
  64. public string DataVApiAddress { set; get; }
  65. /// <summary>
  66. /// 客户端ID
  67. /// </summary>
  68. public string ClientId { set; get; }
  69. public List<int> ListDeviceId { set; get; } = new List<int>();
  70. /// <summary>
  71. /// MQTT上报集合
  72. /// </summary>
  73. public DataVReport DeviceDataV = new DataVReport();
  74. /// <summary>
  75. /// 大屏上报Model
  76. /// </summary>
  77. public DataVAPI.Tool.IOT.IOTDevSXModel iOTDevSXModel = new DataVAPI.Tool.IOT.IOTDevSXModel() { };
  78. /// <summary>
  79. /// key值
  80. /// </summary>
  81. public Dictionary<string, string> keyValues = new Dictionary<string, string>();
  82. /// <summary>
  83. /// 维保通知
  84. /// </summary>
  85. public List<MaintainTable> maintainTables = new List<MaintainTable>();
  86. #endregion
  87. #region API调用
  88. /// <summary>
  89. /// 刷新店铺列表
  90. /// </summary>
  91. public void RefreshTheListOfStores()
  92. {
  93. try
  94. {
  95. if (DeviceDataV != null && DeviceDataV.GetIsConnected() && DeviceDataV.deviceTable != null)
  96. {
  97. string api = DataVApiAddress + "/api/Device/FlushedDevice";
  98. HttpRequestHelper.HttpGetRequest(api, 1000);
  99. //DeviceDataV.IOT_Publish(BroadcastPubTopic, Tools.JsonConvertTools("刷新店铺列表"));
  100. }
  101. }
  102. catch (Exception ex)
  103. {
  104. MessageLog.GetInstance.Show(ex.Message);
  105. }
  106. }
  107. /// <summary>
  108. /// 增加告警信息
  109. /// </summary>
  110. /// <param name="alarmTable"></param>
  111. /// <returns>返回ID</returns>
  112. public string HttpAddAlarm(AlarmTable alarmTable)
  113. {
  114. try
  115. {
  116. if (DeviceDataV != null && DeviceDataV.GetIsConnected() && DeviceDataV.deviceTable != null)
  117. {
  118. alarmTable.ClientId = ClientId;
  119. alarmTable.devicename = DeviceDataV.deviceTable.devicename;
  120. DeviceDataV.IOT_Publish(BroadcastPubTopic, Tools.JsonConvertTools(alarmTable));
  121. }
  122. }
  123. catch (Exception ex)
  124. {
  125. MessageLog.GetInstance.Show(ex.Message);
  126. }
  127. return alarmTable.KeyID;
  128. }
  129. /// <summary>
  130. /// 增加维保信息
  131. /// </summary>
  132. /// <param name="maintainTable"></param>
  133. /// <returns></returns>
  134. public string HttpAddMaintain(MaintainTable maintainTable)
  135. {
  136. try
  137. {
  138. if (DeviceDataV != null && DeviceDataV.GetIsConnected() && DeviceDataV.deviceTable != null)
  139. {
  140. maintainTable.Id = Guid.NewGuid().ToString();
  141. //maintainTable.MaintainTime = DateTime.Now.ToString();
  142. //maintainTable.MaintainType = "一般";
  143. //maintainTable.MaintainVla = "MorkS";
  144. //maintainTable.MaintainMessage = "xxxxx故障需要维修";
  145. //maintainTable.DeviceId = "";
  146. maintainTable.ClientId = ClientId;
  147. maintainTable.devicename = DeviceDataV.deviceTable.devicename;
  148. maintainTables.Add(maintainTable);
  149. }
  150. }
  151. catch (Exception ex)
  152. {
  153. MessageLog.GetInstance.Show(ex.Message);
  154. }
  155. return maintainTable.KeyID;
  156. }
  157. /// <summary>
  158. /// 增加日志信息
  159. /// </summary>
  160. /// <param name="alarmTable"></param>
  161. /// <returns>返回ID</returns>
  162. public string HttpAddLog(LogTable logTable)
  163. {
  164. string id = string.Empty;
  165. try
  166. {
  167. if (DeviceDataV != null && DeviceDataV.GetIsConnected() && DeviceDataV.deviceTable != null)
  168. {
  169. logTable.ClientId = ClientId;
  170. logTable.devicename = DeviceDataV.deviceTable.devicename;
  171. DeviceDataV.IOT_Publish(BroadcastPubTopic, Tools.JsonConvertTools(logTable));
  172. }
  173. }
  174. catch (Exception ex)
  175. {
  176. MessageLog.GetInstance.Show(ex.Message);
  177. }
  178. return id;
  179. }
  180. #endregion
  181. #region 公用
  182. /// <summary>
  183. /// 释放
  184. /// </summary>
  185. public void Dispose()
  186. {
  187. ThreadManage.GetInstance().StopTask("DataV数据上报");
  188. }
  189. /// <summary>
  190. /// 初始化
  191. /// </summary>
  192. public void Initialize()
  193. {
  194. string message = string.Empty;
  195. if (StartupMode == "API")
  196. {
  197. while (ListDeviceId.Count == 0)
  198. {
  199. Plugin.GetInstance()?.GetPlugin<DeviceMgr>()?.GetDevices()?.ForEach(device =>
  200. {
  201. if (device != null)
  202. {
  203. ListDeviceId.Add(device.DeviceId);
  204. }
  205. });
  206. }
  207. if (DeviceDataV.Initialize(DataVApiAddress, ClientId, ListDeviceId.Count==0?"": ListDeviceId?.First().ToString(), ref message))
  208. {
  209. ProductKey = DeviceDataV.deviceTable.productkey;
  210. DeviceName = DeviceDataV.deviceTable.devicename;
  211. DeviceSecret = DeviceDataV.deviceTable.devicesecret;
  212. DeviceDataV.DataVMessageAction += DevIOTActionHandler;
  213. MessageLog.GetInstance.Show($"客户端:【{ClientId}】,设备名称{DeviceName}阿里云连接成功");
  214. UpDataFile();
  215. }
  216. else
  217. {
  218. MessageLog.GetInstance.ShowEx(message);
  219. }
  220. }
  221. else
  222. {
  223. if (DeviceDataV.InitializeNo(ProductKey, DeviceName, DeviceSecret, ref message))
  224. {
  225. MessageLog.GetInstance.Show($"客户端:【{ClientId}】,设备名称{DeviceName}阿里云连接成功");
  226. DeviceDataV.DataVMessageAction += DevIOTActionHandler;
  227. UpDataFile();
  228. }
  229. else
  230. MessageLog.GetInstance.ShowEx(message);
  231. }
  232. Plugin.GetInstance()?.GetPlugin<DeviceMgr>()?.GetDevices()?.ForEach(device =>
  233. {
  234. device.AddErrorAction += AddErrorAction;
  235. device.DeleteErrorAction += DeleteErrorAction;
  236. });
  237. }
  238. /// <summary>
  239. /// 启动DataV数据上报
  240. /// </summary>
  241. public void Start()
  242. {
  243. ThreadManage.GetInstance().StartLong(new Action(() =>
  244. {
  245. if (DeviceDataV != null && DeviceDataV.GetIsConnected() && DeviceDataV.deviceTable != null)
  246. {
  247. iOTDevSXModel.Device1 = String.Empty; iOTDevSXModel.Device2 = String.Empty; iOTDevSXModel.Device3 = String.Empty;
  248. List<object> dataVNode = new List<object>();
  249. Plugin.GetInstance()?.GetPlugin<DeviceMgr>()?.GetDevices()?.ForEach(device =>
  250. {
  251. var obj = new
  252. {
  253. DeviceId = device.DeviceId.ToString(),
  254. devicename = DeviceDataV.deviceTable.devicename,
  255. Name = device.Name,
  256. DeviceType = device.DeviceType.ToString(),
  257. IsBusy = device.IsBusy ? "忙碌" : "空闲",
  258. IsBusyColor = device.IsBusy ? new ALYColor { r = 255, g = 0, b = 0, a = 1 } : new ALYColor { r = 51, g = 232, b = 34, a = 1 },
  259. IsHealth = device.IsHealth ? "健康" : "故障",
  260. IsHealthColor = !device.IsHealth ? new ALYColor { r = 255, g = 0, b = 0, a = 1 } : new ALYColor { r = 51, g = 232, b = 34, a = 1 },
  261. Status = device.Status.GetIOTStatus(),
  262. gjxx = device.GetError(),
  263. rzxx = device.GetLog(),
  264. VariableMonitor = device.GetVariableMonitor(),
  265. };
  266. dataVNode.Add(obj);
  267. if (string.IsNullOrEmpty(iOTDevSXModel.Device1)) iOTDevSXModel.Device1 = $"[{device.Name}]:{(device.IsBusy ? "忙碌" : "空闲")}-{(device.IsHealth ? "健康" : "故障")}";
  268. else if (string.IsNullOrEmpty(iOTDevSXModel.Device2)) iOTDevSXModel.Device2 = $"[{device.Name}]:{(device.IsBusy ? "忙碌" : "空闲")}-{(device.IsHealth ? "健康" : "故障")}";
  269. else if (string.IsNullOrEmpty(iOTDevSXModel.Device3)) iOTDevSXModel.Device3 = $"[{device.Name}]:{(device.IsBusy ? "忙碌" : "空闲")}-{(device.IsHealth ? "健康" : "故障")}";
  270. });
  271. if (dataVNode.Count > 0)
  272. {
  273. if (maintainTables.Count > 0)
  274. {
  275. iOTDevSXModel.Maintain = Tools.JsonConvertTools(maintainTables);
  276. }
  277. iOTDevSXModel.NodeStatus = Tools.JsonConvertTools(new { data = dataVNode });
  278. DeviceDataV.IOT_Publish(DeviceDataV.PubTopic, iOTDevSXModel.Tojson());
  279. }
  280. }
  281. Thread.Sleep(3000);
  282. }), "DataV数据上报", true);
  283. }
  284. /// <summary>
  285. /// 文件上传请求
  286. /// </summary>
  287. public void UpDataFile()
  288. {
  289. try
  290. {
  291. if (DeviceDataV != null && DeviceDataV.GetIsConnected() && DeviceDataV.deviceTable != null)
  292. {
  293. FileUpload.FileRequest(DeviceDataV);
  294. }
  295. }
  296. catch (Exception ex)
  297. {
  298. MessageLog.GetInstance.Show(ex.Message);
  299. }
  300. }
  301. #endregion
  302. #region 私有
  303. /// <summary>
  304. /// 增加告警
  305. /// </summary>
  306. /// <param name="obj"></param>
  307. private void AddErrorAction(int Devid, object obj)
  308. {
  309. string id = Guid.NewGuid().ToString();
  310. HttpAddAlarm(new AlarmTable
  311. {
  312. AlarmTime = GetPropertyValue(obj, "Time").ToString(),
  313. AlarmType = GetPropertyValue(obj, "Type").ToString(),
  314. AlarmMessage = GetPropertyValue(obj, "Text").ToString(),
  315. AlarmVla = "告警",
  316. DeviceId = Devid.ToString(),
  317. KeyID = id,
  318. });
  319. keyValues[GetPropertyValue(obj, "Time").ToString() + GetPropertyValue(obj, "Type").ToString() + GetPropertyValue(obj, "Text").ToString()] = id;
  320. //MessageLog.GetInstance.AddDeviceAlarmLogShow(GetPropertyValue(obj, "Time").ToString() + GetPropertyValue(obj, "Type").ToString() + GetPropertyValue(obj, "Text").ToString(),id);
  321. }
  322. /// <summary>
  323. /// 删除告警
  324. /// </summary>
  325. /// <param name="obj"></param>
  326. private void DeleteErrorAction(int Devid, object obj)
  327. {
  328. string message = GetPropertyValue(obj, "Time").ToString() + GetPropertyValue(obj, "Type").ToString() + GetPropertyValue(obj, "Text").ToString();
  329. if (keyValues.ContainsKey(message))
  330. {
  331. HttpAddAlarm(new AlarmTable
  332. {
  333. AlarmTime = GetPropertyValue(obj, "Time").ToString(),
  334. AlarmType = GetPropertyValue(obj, "Type").ToString(),
  335. AlarmMessage = GetPropertyValue(obj, "Text").ToString(),
  336. AlarmVla = "告警",
  337. DeviceId = Devid.ToString(),
  338. KeyID = keyValues[message],
  339. State = "n"
  340. });
  341. //MessageLog.GetInstance.DeleteDeviceAlarmLogShow(message, keyValues[message]);
  342. }
  343. }
  344. /// <summary>
  345. /// 接收云端消息
  346. /// </summary>
  347. /// <param name="topic"></param>
  348. /// <param name="message"></param>
  349. private void DevIOTActionHandler(string deviceId, string topic, string message)
  350. {
  351. if (DeviceDataV.BroadcastTopic == topic && !string.IsNullOrEmpty(message))//广播主题消息,将广播消息发送到相应客户端
  352. {
  353. IOTCommandModel iOTCommand = Tools.JsonToObjectTools<IOTCommandModel>(message);
  354. if (iOTCommand.deviceName == DeviceDataV.deviceTable.devicename)
  355. ActionManage.GetInstance.Send("IotBroadcast", iOTCommand);
  356. }
  357. else if (DeviceDataV.FileUpLoadReplyTopic == topic)//文件请求上传响应Topic
  358. {
  359. FileUploadModelResult result = Tools.JsonToObjectTools<FileUploadModelResult>(message);
  360. if (result.code == 200)
  361. {
  362. string FileName = result.data?.fileName;
  363. string uploadId = result.data?.uploadId;
  364. FileUpload.FileSend(DeviceDataV, uploadId, result.data.offset);
  365. //MessageLog.GetInstance.Show($"[阿里云上传]:请求上传云回执成功.");
  366. }
  367. else
  368. MessageLog.GetInstance.Show($"[阿里云上传]:请求上传云回执失败.原因:{result.message}");
  369. }
  370. else if (DeviceDataV.FileUpLoadSendReplyTopic == topic)//文件上传Topic
  371. {
  372. FileUploadModelResult result = Tools.JsonToObjectTools<FileUploadModelResult>(message);
  373. FileUpload.modelResult = result;
  374. }
  375. else if (DeviceDataV.CancelFileUpLoadSendReplyTopic == topic)//取消文件上传Topic
  376. {
  377. FileUploadModelResult result = Tools.JsonToObjectTools<FileUploadModelResult>(message);
  378. }
  379. }
  380. /// <summary>
  381. /// 获取某个对象中的属性值
  382. /// </summary>
  383. /// <param name="info"></param>
  384. /// <param name="field"></param>
  385. public object GetPropertyValue(object info, string field)
  386. {
  387. if (info == null) return null;
  388. Type t = info.GetType();
  389. IEnumerable<System.Reflection.PropertyInfo> property = from pi in t.GetProperties() where pi.Name.ToLower() == field.ToLower() select pi;
  390. return property.First().GetValue(info, null);
  391. }
  392. #endregion
  393. }
  394. //命令实体类
  395. public class IOTCommandModel
  396. {
  397. /// <summary>
  398. /// 设备名称
  399. /// </summary>
  400. public string deviceName
  401. {
  402. get;
  403. set;
  404. }
  405. /// <summary>
  406. /// 命令名称:0 控制类 1 设置属性 2 通知信息类
  407. /// </summary>
  408. public int CommandName
  409. {
  410. get;
  411. set;
  412. }
  413. /// <summary>
  414. /// 命令变量:执行变量 key为属性或时间 value为值或者消息
  415. /// </summary>
  416. public Dictionary<string, string> CommandValue
  417. {
  418. get;
  419. set;
  420. }
  421. }
  422. }