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

79 regels
2.5 KiB

  1. using BPA.Message;
  2. using BPA.Helper;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace BPASmartClient.Business
  9. {
  10. /// <summary>
  11. /// 状态管理器
  12. /// </summary>
  13. public class StatusMgr : IPlugin
  14. {
  15. //运行标识
  16. private bool running = false;
  17. //设备管理
  18. private DeviceMgr deviceMgr;
  19. private MQTTMgr mqttMgr;
  20. DeviceStatus deviceStatus = new DeviceStatus();
  21. private bool wholeDeviceHealth;
  22. private Dictionary<int, Dictionary<string, object>> wholeDeviceStatus = new Dictionary<int, Dictionary<string, object>>();
  23. public void Initialize()
  24. {
  25. deviceMgr = Plugin.GetInstance().GetPlugin<DeviceMgr>();
  26. mqttMgr = Plugin.GetInstance().GetPlugin<MQTTMgr>();
  27. }
  28. public void Start()
  29. {
  30. running = true;
  31. TaskManage.GetInstance.Start(() =>
  32. {
  33. while (running)
  34. {
  35. wholeDeviceHealth = true;
  36. foreach (var device in deviceMgr.GetDevices())
  37. {
  38. wholeDeviceStatus[device.DeviceId] = device.Status.GetStatus();
  39. }
  40. Thread.Sleep(50);
  41. }
  42. }, "设备状态收集", isRestart: true);
  43. deviceStatus.BatchingInfo = new List<BPA.Models.BatchingInfo>();
  44. TaskManage.GetInstance.Start(() =>
  45. {
  46. while (running)
  47. {
  48. wholeDeviceHealth = true;
  49. foreach (var device in deviceMgr.GetDevices())
  50. {
  51. wholeDeviceStatus[device.DeviceId] = device.Status.GetStatus();
  52. deviceStatus.Healthy = device.IsHealth ? BPA.Message.Enum.DeviceHealthy.Health : BPA.Message.Enum.DeviceHealthy.UnHealth;
  53. deviceStatus.DeviceType = device.DeviceType;
  54. deviceStatus.BatchingInfo = device.BatchingInfos;
  55. var msg = BPAPackage.Make(deviceStatus, device.DeviceId, device.DeviceType);
  56. mqttMgr.Publish(TopicDefine.GetInstance().PushHeartbeatTopics[device.DeviceType], msg.Serialize());
  57. }
  58. Thread.Sleep(1000);
  59. }
  60. }, "设备心跳上报", isRestart: true);
  61. }
  62. public void Dispose()
  63. {
  64. running = false;
  65. }
  66. }
  67. }