终端一体化运控平台
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

OrderProxy.cs 7.9 KiB

il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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() { SortNum = mork.SortNum.ToString(), Status = ORDER_STATUS.COOKING, SubOrderId = mork.SuborderId, GoodName = mork.GoodsName }.Publish();
  48. Thread.Sleep(5000);
  49. new OrderStatusChangedEvent() { SortNum = mork.SortNum.ToString(), Status = ORDER_STATUS.COMPLETED_COOK, SubOrderId = mork.SuborderId, GoodName = mork.GoodsName }.Publish();
  50. Thread.Sleep(5000);
  51. new OrderStatusChangedEvent() { SortNum = mork.SortNum.ToString(), Status = ORDER_STATUS.COMPLETED_TAKE, SubOrderId = mork.SuborderId, GoodName = mork.GoodsName }.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. new RecipeBomEvent()
  79. {
  80. DeviceId = recipe.DeviceId,
  81. recipeBoms = recipe
  82. }.Publish();
  83. MessageLog.GetInstance.Show("接收到 【 MQTT 】 的辅料信息");
  84. }
  85. //物料消息
  86. else if (message is OrderMaterialDelivery delivery)
  87. {
  88. new MaterialDeliveryEvent()
  89. {
  90. DeviceId = delivery.DeviceId,
  91. orderMaterialDelivery = delivery
  92. }.Publish();
  93. MessageLog.GetInstance.Show("接收到 【 MQTT 】 的物料信息");
  94. delivery?.BatchingInfo?.ForEach(x =>
  95. {
  96. MessageLog.GetInstance.Show($"物料ID:=[{x.BatchingId}],{x.BatchingLoc}号位置:{x.BatchingCount}");
  97. });
  98. }
  99. });
  100. EventBus.EventBus.GetInstance().Subscribe<OrderStatusChangedEvent>(0, OrderStatusChangedHandle);
  101. }
  102. private void StartTargetDeviceOrderJob(int deviceId)
  103. {
  104. ThreadManage.GetInstance().Start(() =>
  105. {
  106. var device = deviceMgr.GetDevices().FirstOrDefault(p => p.DeviceId == deviceId);
  107. while (running)
  108. {
  109. if (device.IsBusy /*|| !device.IsHealth*/)
  110. {
  111. Thread.Sleep(100);
  112. continue;
  113. }
  114. while (orders[deviceId].Count > 0)
  115. {
  116. if (orders[deviceId].TryDequeue(out MorkOrderPush temp))
  117. {
  118. if (InternetInfo.IsEnableTest) morkOrderPushes.Enqueue(temp);
  119. var orderEvent = DoOrderEvent.Make(temp);
  120. orderEvent.DeviceId = device.DeviceId;
  121. orderEvent.Publish();
  122. ActionManage.GetInstance.Send("AddOrder", new object[] { temp, device });
  123. }
  124. }
  125. Thread.Sleep(50);
  126. }
  127. }, $"MQTT 订单接收处理-设备[{deviceId}]", true);
  128. }
  129. public void OrderStatusChangedHandle(IEvent @event, EventCallBackHandle callBack)
  130. {
  131. OrderStatusChangedEvent orderStatusChangedEvent = @event as OrderStatusChangedEvent;
  132. string result = string.Empty;
  133. OrderStatusChange orderStatusChange = new OrderStatusChange()
  134. {
  135. CookingStatus = orderStatusChangedEvent.Status,
  136. SuborderId = orderStatusChangedEvent.SubOrderId,
  137. GoodName = orderStatusChangedEvent.GoodName,
  138. SortNum = orderStatusChangedEvent.SortNum
  139. };
  140. #region API 订单状态修改
  141. if (orderStatusChange.SuborderId.Length > 0 && orderStatusChange.CookingStatus != ORDER_STATUS.WAIT)
  142. {
  143. try
  144. {
  145. string header = $"[{InternetInfo.OrderServer}/order/robotstatuschange]_[{DateTime.Now.Ticks}]".AESEncrypt();
  146. string url = $"{InternetInfo.OrderServer}order/robotstatuschange";
  147. result = APIHelper.GetInstance.HttpRequest(url, header, orderStatusChange, RequestType.POST);
  148. }
  149. catch (Exception ex)
  150. {
  151. MessageLog.GetInstance.ShowEx(ex.ToString());
  152. }
  153. var res = JsonConvert.DeserializeObject<OrderStatusRsp>(result);
  154. ActionManage.GetInstance.Send("OrderStatusChange", orderStatusChangedEvent);
  155. bool isSuccess = res == null ? false : res.isSuccess;
  156. MessageLog.GetInstance.Show($"订单:{orderStatusChange.SuborderId} 状态:{orderStatusChange.CookingStatus} 执行结果:{isSuccess}");
  157. //MessageLog.GetInstance.Show(string.Format("订单状态改变,调用API执行结果{0}", res == null ? false : res.isSuccess));
  158. }
  159. #endregion
  160. #region MQTT 订单状态修改
  161. var clientId = Plugin.GetInstance().GetPlugin<ConfigMgr>().ClientId;
  162. var temp = BPAPackage.Make(orderStatusChange, clientId, orderStatusChangedEvent.deviceClientType);
  163. var q = temp.Serialize(false);
  164. Plugin.GetInstance().GetPlugin<MQTTMgr>().Publish(TOPIC.GetInstance.GetAppTopic(clientId.ToString()), temp.Serialize(false));
  165. #endregion
  166. }
  167. /// <summary>
  168. /// 资源释放
  169. /// </summary>
  170. public void Dispose()
  171. {
  172. running = false;
  173. }
  174. }
  175. }