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

76 lines
2.3 KiB

  1. using BPA.Message;
  2. using BPASmartClient.EventBus;
  3. using BPASmartClient.Helper;
  4. using BPASmartClient.Model;
  5. using System;
  6. using System.Collections.Concurrent;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace BPASmartClient.Business
  12. {
  13. /// <summary>
  14. /// 订单代理
  15. /// </summary>
  16. public class OrderProxy : IPlugin
  17. {
  18. //订单队列
  19. private ConcurrentQueue<MorkOrderPush> orders = new ConcurrentQueue<MorkOrderPush>();
  20. //运行标识
  21. private bool running = false;
  22. //设备管理
  23. private DeviceMgr deviceMgr;
  24. /// <summary>
  25. /// 初始化
  26. /// </summary>
  27. public void Initialize()
  28. {
  29. deviceMgr = Plugin.GetInstance().GetPlugin<DeviceMgr>();
  30. Plugin.GetInstance().GetPlugin<MQTTMgr>().SetMessageReciveHandler(delegate (IMessage orderInfo)
  31. {
  32. if (orderInfo == null) return;
  33. if (orderInfo is MorkOrderPush morkOrderpush)
  34. {
  35. orders.Enqueue(morkOrderpush);
  36. }
  37. });
  38. ThreadManage.GetInstance().StartLong(() =>
  39. {
  40. while (running)
  41. {
  42. while (orders.Count > 0)
  43. {
  44. var temp = orders.ElementAt(0);
  45. var device = deviceMgr.GetDevices().FirstOrDefault(p => p.DeviceId == temp.DeviceId);
  46. if (null != device)
  47. {
  48. if (!device.IsBusy && device.IsHealth)
  49. {
  50. if (orders.TryDequeue(out temp))
  51. {
  52. var orderEvent = DoOrderEvent.Make(temp);
  53. orderEvent.Id = device.DeviceId;
  54. orderEvent.Publish();
  55. }
  56. }
  57. }
  58. }
  59. Thread.Sleep(50);
  60. }
  61. }, "MQTT 消息处理");
  62. }
  63. /// <summary>
  64. /// 资源释放
  65. /// </summary>
  66. public void Dispose()
  67. {
  68. running = false;
  69. }
  70. }
  71. }