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

153 lines
5.7 KiB

  1. #define test
  2. using BPA.Message;
  3. using BPA.Message.Enum;
  4. using BPASmartClient.EventBus;
  5. using BPASmartClient.Helper;
  6. using BPASmartClient.Http;
  7. using BPASmartClient.Message;
  8. using BPASmartClient.Model;
  9. using Newtonsoft.Json;
  10. using System;
  11. using System.Collections.Concurrent;
  12. using System.Collections.Generic;
  13. using System.Collections.ObjectModel;
  14. using System.Linq;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. using static BPASmartClient.EventBus.EventBus;
  18. namespace BPASmartClient.Business
  19. {
  20. /// <summary>
  21. /// 订单代理
  22. /// </summary>
  23. public class OrderProxy : IPlugin
  24. {
  25. //订单队列
  26. private ConcurrentDictionary<int, ConcurrentQueue<MorkOrderPush>> orders = new ConcurrentDictionary<int, ConcurrentQueue<MorkOrderPush>>();
  27. //运行标识
  28. private bool running = false;
  29. //设备管理
  30. private DeviceMgr deviceMgr;
  31. ConcurrentQueue<MorkOrderPush> morkOrderPushes = new ConcurrentQueue<MorkOrderPush>();
  32. /// <summary>
  33. /// 初始化
  34. /// </summary>
  35. public void Initialize()
  36. {
  37. deviceMgr = Plugin.GetInstance().GetPlugin<DeviceMgr>();
  38. #if test
  39. ThreadManage.GetInstance().StartLong(new Action(() =>
  40. {
  41. while (morkOrderPushes.Count > 0)
  42. {
  43. if (morkOrderPushes.TryDequeue(out MorkOrderPush mork))
  44. {
  45. Thread.Sleep(3000);
  46. new OrderStatusChangedEvent() { Status = ORDER_STATUS.COOKING, SubOrderId = mork.SuborderId }.Publish();
  47. Thread.Sleep(5000);
  48. new OrderStatusChangedEvent() { Status = ORDER_STATUS.COMPLETED_COOK, SubOrderId = mork.SuborderId }.Publish();
  49. Thread.Sleep(5000);
  50. new OrderStatusChangedEvent() { Status = ORDER_STATUS.COMPLETED_TAKE, SubOrderId = mork.SuborderId }.Publish();
  51. Thread.Sleep(2000);
  52. }
  53. }
  54. Thread.Sleep(1000);
  55. }), "sim order");
  56. #endif
  57. }
  58. public void Start()
  59. {
  60. running = true;
  61. Plugin.GetInstance().GetPlugin<MQTTMgr>().SetMessageReciveHandler(delegate (IMessage message)
  62. {
  63. if (message == null) return;
  64. //订单消息
  65. if (message is MorkOrderPush morkOrderpush)
  66. {
  67. if (!orders.ContainsKey(morkOrderpush.DeviceId))
  68. {
  69. orders.TryAdd(morkOrderpush.DeviceId, new ConcurrentQueue<MorkOrderPush>());
  70. StartTargetDeviceOrderJob(morkOrderpush.DeviceId);
  71. }
  72. orders[morkOrderpush.DeviceId].Enqueue(morkOrderpush);
  73. }
  74. //辅料信息
  75. else if (message is RecipeBoms recipe)
  76. {
  77. }
  78. //物料消息
  79. else if (message is OrderMaterialDelivery delivery)
  80. {
  81. }
  82. });
  83. EventBus.EventBus.GetInstance().Subscribe<OrderStatusChangedEvent>(0, OrderStatusChangedHandle);
  84. }
  85. private void StartTargetDeviceOrderJob(int deviceId)
  86. {
  87. ThreadManage.GetInstance().Start(() =>
  88. {
  89. var device = deviceMgr.GetDevices().FirstOrDefault(p => p.DeviceId == deviceId);
  90. while (running)
  91. {
  92. if (device.IsBusy || !device.IsHealth)
  93. {
  94. Thread.Sleep(100);
  95. continue;
  96. }
  97. while (orders[deviceId].Count > 0)
  98. {
  99. if (orders[deviceId].TryDequeue(out MorkOrderPush temp))
  100. {
  101. #if test
  102. morkOrderPushes.Enqueue(temp);
  103. #endif
  104. var orderEvent = DoOrderEvent.Make(temp);
  105. orderEvent.Id = device.DeviceId;
  106. orderEvent.Publish();
  107. ActionManage.GetInstance.Send("AddOrder", new object[] { temp, device });
  108. }
  109. }
  110. Thread.Sleep(50);
  111. }
  112. }, $"MQTT 订单接收处理-设备[{deviceId}]", true);
  113. }
  114. public void OrderStatusChangedHandle(IEvent @event, EventCallBackHandle callBack)
  115. {
  116. OrderStatusChangedEvent orderStatusChangedEvent = @event as OrderStatusChangedEvent;
  117. string result = string.Empty;
  118. OrderStatusChange orderStatusChange = new OrderStatusChange() { CookingStatus = orderStatusChangedEvent.Status, SuborderId = orderStatusChangedEvent.SubOrderId };
  119. try
  120. {
  121. string header = $"[{InternetInfo.OrderServer}/order/robotstatuschange]_[{DateTime.Now.Ticks}]".AESEncrypt();
  122. string url = $"{InternetInfo.OrderServer}order/robotstatuschange";
  123. result = APIHelper.GetInstance.HttpRequest(url, header, orderStatusChange, RequestType.POST);
  124. }
  125. catch (Exception ex)
  126. {
  127. MessageLog.GetInstance.ShowEx(ex.ToString());
  128. }
  129. var res = JsonConvert.DeserializeObject<OrderStatusRsp>(result);
  130. ActionManage.GetInstance.Send("OrderStatusChange", orderStatusChangedEvent);
  131. MessageLog.GetInstance.Show(string.Format("订单状态改变,调用API执行结果{0}", res == null ? false : res.isSuccess));
  132. }
  133. /// <summary>
  134. /// 资源释放
  135. /// </summary>
  136. public void Dispose()
  137. {
  138. running = false;
  139. }
  140. }
  141. }