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

184 lines
7.5 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() { 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. try
  142. {
  143. string header = $"[{InternetInfo.OrderServer}/order/robotstatuschange]_[{DateTime.Now.Ticks}]".AESEncrypt();
  144. string url = $"{InternetInfo.OrderServer}order/robotstatuschange";
  145. result = APIHelper.GetInstance.HttpRequest(url, header, orderStatusChange, RequestType.POST);
  146. }
  147. catch (Exception ex)
  148. {
  149. MessageLog.GetInstance.ShowEx(ex.ToString());
  150. }
  151. var res = JsonConvert.DeserializeObject<OrderStatusRsp>(result);
  152. ActionManage.GetInstance.Send("OrderStatusChange", orderStatusChangedEvent);
  153. MessageLog.GetInstance.Show(string.Format("订单状态改变,调用API执行结果{0}", res == null ? false : res.isSuccess));
  154. #endregion
  155. #region MQTT 订单状态修改
  156. var clientId = Plugin.GetInstance().GetPlugin<ConfigMgr>().ClientId;
  157. var temp = BPAPackage.Make(orderStatusChange, clientId, orderStatusChangedEvent.deviceClientType);
  158. var q = temp.Serialize(false);
  159. Plugin.GetInstance().GetPlugin<MQTTMgr>().Publish(TOPIC.GetInstance.GetAppTopic(clientId.ToString()), temp.Serialize(false));
  160. #endregion
  161. }
  162. /// <summary>
  163. /// 资源释放
  164. /// </summary>
  165. public void Dispose()
  166. {
  167. running = false;
  168. }
  169. }
  170. }