终端一体化运控平台
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 

119 rader
4.4 KiB

  1. using BPA.Message;
  2. using BPASmartClient.EventBus;
  3. using BPASmartClient.Helper;
  4. using BPASmartClient.Http;
  5. using BPASmartClient.Message;
  6. using BPASmartClient.Model;
  7. using Newtonsoft.Json;
  8. using System;
  9. using System.Collections.Concurrent;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using static BPASmartClient.EventBus.EventBus;
  15. namespace BPASmartClient.Business
  16. {
  17. /// <summary>
  18. /// 订单代理
  19. /// </summary>
  20. public class OrderProxy : IPlugin
  21. {
  22. //订单队列
  23. private ConcurrentDictionary<int, ConcurrentQueue<MorkOrderPush>> orders = new ConcurrentDictionary<int, ConcurrentQueue<MorkOrderPush>>();
  24. //运行标识
  25. private bool running = false;
  26. //设备管理
  27. private DeviceMgr deviceMgr;
  28. /// <summary>
  29. /// 初始化
  30. /// </summary>
  31. public void Initialize()
  32. {
  33. deviceMgr = Plugin.GetInstance().GetPlugin<DeviceMgr>();
  34. }
  35. public void Start()
  36. {
  37. running = true;
  38. Plugin.GetInstance().GetPlugin<MQTTMgr>().SetMessageReciveHandler(delegate (IMessage orderInfo)
  39. {
  40. if (orderInfo == null) return;
  41. if (orderInfo is MorkOrderPush morkOrderpush)
  42. {
  43. if (!orders.ContainsKey(morkOrderpush.DeviceId))
  44. {
  45. orders.TryAdd(morkOrderpush.DeviceId, new ConcurrentQueue<MorkOrderPush>());
  46. StartTargetDeviceOrderJob(morkOrderpush.DeviceId);
  47. }
  48. orders[morkOrderpush.DeviceId].Enqueue(morkOrderpush);
  49. }
  50. });
  51. EventBus.EventBus.GetInstance().Subscribe<OrderStatusChangedEvent>(0, OrderStatusChangedHandle);
  52. }
  53. private void StartTargetDeviceOrderJob(int deviceId)
  54. {
  55. ThreadManage.GetInstance().Start(() =>
  56. {
  57. var device = deviceMgr.GetDevices().FirstOrDefault(p => p.DeviceId == deviceId);
  58. while (running)
  59. {
  60. if (device.IsBusy || !device.IsHealth)
  61. {
  62. Thread.Sleep(100);
  63. continue;
  64. }
  65. while (orders[deviceId].Count > 0)
  66. {
  67. if (orders[deviceId].TryDequeue(out MorkOrderPush temp))
  68. {
  69. var orderEvent = DoOrderEvent.Make(temp);
  70. orderEvent.Id = device.DeviceId;
  71. orderEvent.Publish();
  72. Json<KeepDataBase>.Data.orderLists.Add(new OrderData()
  73. {
  74. IsSelected = true,
  75. OrderPush = temp,
  76. OrderStatus = BPA.Message.Enum.ORDER_STATUS.WAIT
  77. });
  78. }
  79. }
  80. Thread.Sleep(50);
  81. }
  82. }, $"MQTT 订单接收处理-设备[{deviceId}]");
  83. }
  84. public void OrderStatusChangedHandle(IEvent @event, EventCallBackHandle callBack)
  85. {
  86. OrderStatusChangedEvent orderStatusChangedEvent = @event as OrderStatusChangedEvent;
  87. string result = string.Empty;
  88. OrderStatusChange orderStatusChange = new OrderStatusChange() { CookingStatus = orderStatusChangedEvent.Status, SuborderId = orderStatusChangedEvent.SubOrderId };
  89. try
  90. {
  91. string header = $"[{InternetInfo.OrderServer}/order/robotstatuschange]_[{DateTime.Now.Ticks}]".AESEncrypt();
  92. string url = $"{InternetInfo.OrderServer}/order/robotstatuschange";
  93. result = APIHelper.GetInstance.HttpRequest(url, header, orderStatusChange, RequestType.POST);
  94. }
  95. catch (Exception ex)
  96. {
  97. MessageLog.GetInstance.ShowEx(ex.ToString());
  98. }
  99. var res = JsonConvert.DeserializeObject<OrderStatusRsp>(result);
  100. MessageLog.GetInstance.ShowEx(string.Format("订单状态改变,调用API执行结果{0}", res == null ? false : res.isSuccess));
  101. }
  102. /// <summary>
  103. /// 资源释放
  104. /// </summary>
  105. public void Dispose()
  106. {
  107. running = false;
  108. }
  109. }
  110. }