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

116 line
4.3 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. running = true;
  34. deviceMgr = Plugin.GetInstance().GetPlugin<DeviceMgr>();
  35. Plugin.GetInstance().GetPlugin<MQTTMgr>().SetMessageReciveHandler(delegate (IMessage orderInfo)
  36. {
  37. if (orderInfo == null) return;
  38. if (orderInfo is MorkOrderPush morkOrderpush)
  39. {
  40. if (!orders.ContainsKey(morkOrderpush.DeviceId))
  41. {
  42. orders.TryAdd(morkOrderpush.DeviceId, new ConcurrentQueue<MorkOrderPush>());
  43. StartTargetDeviceOrderJob(morkOrderpush.DeviceId);
  44. }
  45. orders[morkOrderpush.DeviceId].Enqueue(morkOrderpush);
  46. }
  47. });
  48. EventBus.EventBus.GetInstance().Subscribe<OrderStatusChangedEvent>(0, OrderStatusChangedHandle);
  49. }
  50. private void StartTargetDeviceOrderJob(int deviceId)
  51. {
  52. ThreadManage.GetInstance().Start(() =>
  53. {
  54. var device = deviceMgr.GetDevices().FirstOrDefault(p => p.DeviceId == deviceId);
  55. while (running)
  56. {
  57. if (device.IsBusy || !device.IsHealth)
  58. {
  59. Thread.Sleep(100);
  60. continue;
  61. }
  62. while (orders[deviceId].Count > 0)
  63. {
  64. if (orders[deviceId].TryDequeue(out MorkOrderPush temp))
  65. {
  66. var orderEvent = DoOrderEvent.Make(temp);
  67. orderEvent.Id = device.DeviceId;
  68. orderEvent.Publish();
  69. Json<KeepDataBase>.Data.orderLists.Add(new OrderData()
  70. {
  71. IsSelected = true,
  72. OrderPush = temp,
  73. OrderStatus = BPA.Message.Enum.ORDER_STATUS.WAIT
  74. });
  75. }
  76. }
  77. Thread.Sleep(50);
  78. }
  79. }, $"MQTT 订单接收处理-设备[{deviceId}]");
  80. }
  81. public void OrderStatusChangedHandle(IEvent @event, EventCallBackHandle callBack)
  82. {
  83. OrderStatusChangedEvent orderStatusChangedEvent = @event as OrderStatusChangedEvent;
  84. string result = string.Empty;
  85. OrderStatusChange orderStatusChange = new OrderStatusChange() { CookingStatus = orderStatusChangedEvent.Status, SuborderId = orderStatusChangedEvent.SubOrderId };
  86. try
  87. {
  88. string header = $"[{InternetInfo.OrderServer}/order/robotstatuschange]_[{DateTime.Now.Ticks}]".AESEncrypt();
  89. string url = $"{InternetInfo.OrderServer}/order/robotstatuschange";
  90. result = APIHelper.GetInstance.HttpRequest(url, header, orderStatusChange, RequestType.POST);
  91. }
  92. catch (Exception ex)
  93. {
  94. MessageLog.GetInstance.ShowEx(ex.ToString());
  95. }
  96. var res = JsonConvert.DeserializeObject<OrderStatusRsp>(result);
  97. MessageLog.GetInstance.ShowEx(string.Format("订单状态改变,调用API执行结果{0}", res == null ? false : res.isSuccess));
  98. }
  99. /// <summary>
  100. /// 资源释放
  101. /// </summary>
  102. public void Dispose()
  103. {
  104. running = false;
  105. }
  106. }
  107. }