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

163 lines
6.3 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 (InternetInfo.IsEnableTest)
  39. {
  40. ThreadManage.GetInstance().StartLong(new Action(() =>
  41. {
  42. while (morkOrderPushes.Count > 0)
  43. {
  44. if (morkOrderPushes.TryDequeue(out MorkOrderPush mork))
  45. {
  46. Thread.Sleep(3000);
  47. new OrderStatusChangedEvent() { Status = ORDER_STATUS.COOKING, SubOrderId = mork.SuborderId }.Publish();
  48. Thread.Sleep(5000);
  49. new OrderStatusChangedEvent() { Status = ORDER_STATUS.COMPLETED_COOK, SubOrderId = mork.SuborderId }.Publish();
  50. Thread.Sleep(5000);
  51. new OrderStatusChangedEvent() { Status = ORDER_STATUS.COMPLETED_TAKE, SubOrderId = mork.SuborderId }.Publish();
  52. Thread.Sleep(2000);
  53. }
  54. }
  55. Thread.Sleep(1000);
  56. }), "sim order");
  57. }
  58. }
  59. public void Start()
  60. {
  61. running = true;
  62. Plugin.GetInstance().GetPlugin<MQTTMgr>().SetMessageReciveHandler(delegate (IMessage message)
  63. {
  64. if (message == null) return;
  65. //订单消息
  66. if (message is MorkOrderPush morkOrderpush)
  67. {
  68. if (!orders.ContainsKey(morkOrderpush.DeviceId))
  69. {
  70. orders.TryAdd(morkOrderpush.DeviceId, new ConcurrentQueue<MorkOrderPush>());
  71. StartTargetDeviceOrderJob(morkOrderpush.DeviceId);
  72. }
  73. orders[morkOrderpush.DeviceId].Enqueue(morkOrderpush);
  74. }
  75. //辅料信息
  76. else if (message is RecipeBoms recipe)
  77. {
  78. }
  79. //物料消息
  80. else if (message is OrderMaterialDelivery delivery)
  81. {
  82. }
  83. });
  84. EventBus.EventBus.GetInstance().Subscribe<OrderStatusChangedEvent>(0, OrderStatusChangedHandle);
  85. }
  86. private void StartTargetDeviceOrderJob(int deviceId)
  87. {
  88. ThreadManage.GetInstance().Start(() =>
  89. {
  90. var device = deviceMgr.GetDevices().FirstOrDefault(p => p.DeviceId == deviceId);
  91. while (running)
  92. {
  93. if (device.IsBusy || !device.IsHealth)
  94. {
  95. Thread.Sleep(100);
  96. continue;
  97. }
  98. while (orders[deviceId].Count > 0)
  99. {
  100. if (orders[deviceId].TryDequeue(out MorkOrderPush temp))
  101. {
  102. if (InternetInfo.IsEnableTest) morkOrderPushes.Enqueue(temp);
  103. var orderEvent = DoOrderEvent.Make(temp);
  104. orderEvent.DeviceId = device.DeviceId;
  105. orderEvent.Publish();
  106. ActionManage.GetInstance.Send("AddOrder", new object[] { temp, device });
  107. }
  108. }
  109. Thread.Sleep(50);
  110. }
  111. }, $"MQTT 订单接收处理-设备[{deviceId}]", true);
  112. }
  113. public void OrderStatusChangedHandle(IEvent @event, EventCallBackHandle callBack)
  114. {
  115. OrderStatusChangedEvent orderStatusChangedEvent = @event as OrderStatusChangedEvent;
  116. string result = string.Empty;
  117. OrderStatusChange orderStatusChange = new OrderStatusChange() { CookingStatus = orderStatusChangedEvent.Status, SuborderId = orderStatusChangedEvent.SubOrderId };
  118. #region API 订单状态修改
  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. #endregion
  133. #region MQTT 订单状态修改
  134. var clientId = Plugin.GetInstance().GetPlugin<ConfigMgr>().ClientId;
  135. var temp = BPAPackage.Make(orderStatusChange, clientId, orderStatusChangedEvent.deviceClientType);
  136. Plugin.GetInstance().GetPlugin<MQTTMgr>().Publish(TOPIC.GetInstance.GetAppTopic(clientId.ToString()), temp.Serialize(false));
  137. #endregion
  138. }
  139. /// <summary>
  140. /// 资源释放
  141. /// </summary>
  142. public void Dispose()
  143. {
  144. running = false;
  145. }
  146. }
  147. }