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

204 lines
8.5 KiB

  1. //#define test
  2. using BPA.Message;
  3. using BPA.Message.Enum;
  4. using BPA.Models;
  5. using BPASmartClient.Device;
  6. using BPASmartClient.EventBus;
  7. using BPASmartClient.Helper;
  8. using BPASmartClient.Http;
  9. using BPASmartClient.Message;
  10. using BPASmartClient.Model;
  11. using BPASmartClient.Model.小炒机;
  12. using Newtonsoft.Json;
  13. using System;
  14. using System.Collections.Concurrent;
  15. using System.Collections.Generic;
  16. using System.Collections.ObjectModel;
  17. using System.Linq;
  18. using System.Text;
  19. using System.Threading.Tasks;
  20. using static BPASmartClient.EventBus.EventBus;
  21. namespace BPASmartClient.Business
  22. {
  23. /// <summary>
  24. /// 订单代理
  25. /// </summary>
  26. public class OrderProxy : IPlugin
  27. {
  28. //订单队列
  29. private ConcurrentDictionary<int, ConcurrentQueue<MorkOrderPush>> orders = new ConcurrentDictionary<int, ConcurrentQueue<MorkOrderPush>>();
  30. //运行标识
  31. private bool running = false;
  32. //设备管理
  33. private DeviceMgr deviceMgr;
  34. ConcurrentQueue<MorkOrderPush> morkOrderPushes = new ConcurrentQueue<MorkOrderPush>();
  35. /// <summary>
  36. /// 初始化
  37. /// </summary>
  38. public void Initialize()
  39. {
  40. deviceMgr = Plugin.GetInstance().GetPlugin<DeviceMgr>();
  41. if (InternetInfo.IsEnableTest)
  42. {
  43. ThreadManage.GetInstance().StartLong(new Action(() =>
  44. {
  45. while (morkOrderPushes.Count > 0)
  46. {
  47. if (morkOrderPushes.TryDequeue(out MorkOrderPush mork))
  48. {
  49. Thread.Sleep(3000);
  50. new OrderStatusChangedEvent() { SortNum = mork.SortNum.ToString(), Status = ORDER_STATUS.COOKING, SubOrderId = mork.SuborderId, GoodName = mork.GoodsName }.Publish();
  51. Thread.Sleep(5000);
  52. new OrderStatusChangedEvent() { SortNum = mork.SortNum.ToString(), Status = ORDER_STATUS.COMPLETED_COOK, SubOrderId = mork.SuborderId, GoodName = mork.GoodsName }.Publish();
  53. Thread.Sleep(5000);
  54. new OrderStatusChangedEvent() { SortNum = mork.SortNum.ToString(), Status = ORDER_STATUS.COMPLETED_TAKE, SubOrderId = mork.SuborderId, GoodName = mork.GoodsName }.Publish();
  55. Thread.Sleep(2000);
  56. }
  57. }
  58. Thread.Sleep(1000);
  59. }), "sim order");
  60. }
  61. }
  62. public void Start()
  63. {
  64. running = true;
  65. Plugin.GetInstance().GetPlugin<MQTTMgr>().SetMessageReciveHandler(delegate (IMessage message)
  66. {
  67. if (message == null) return;
  68. //订单消息
  69. if (message is MorkOrderPush morkOrderpush)
  70. {
  71. if (!orders.ContainsKey(morkOrderpush.DeviceId))
  72. {
  73. orders.TryAdd(morkOrderpush.DeviceId, new ConcurrentQueue<MorkOrderPush>());
  74. StartTargetDeviceOrderJob(morkOrderpush.DeviceId);
  75. }
  76. orders[morkOrderpush.DeviceId].Enqueue(morkOrderpush);
  77. }
  78. //辅料信息
  79. else if (message is RecipeBoms recipe)
  80. {
  81. new RecipeBomEvent()
  82. {
  83. DeviceId = recipe.DeviceId,
  84. recipeBoms = recipe
  85. }.Publish();
  86. MessageLog.GetInstance.Show("接收到 【 MQTT 】 的辅料信息");
  87. }
  88. //物料消息
  89. else if (message is OrderMaterialDelivery delivery)
  90. {
  91. new MaterialDeliveryEvent()
  92. {
  93. DeviceId = delivery.DeviceId,
  94. orderMaterialDelivery = delivery
  95. }.Publish();
  96. MessageLog.GetInstance.Show("接收到 【 MQTT 】 的物料信息");
  97. delivery?.BatchingInfo?.ForEach(x =>
  98. {
  99. MessageLog.GetInstance.Show($"物料ID:=[{x.BatchingId}],{x.BatchingLoc}号位置:{x.BatchingCount}");
  100. });
  101. }
  102. //小炒流程信息
  103. else if (message is StirFryPushMessage frybom)
  104. {
  105. IDevice device = deviceMgr.GetDevices().FirstOrDefault(x => x.DeviceId == 28);
  106. new StirFryGoodsEvent() {
  107. DeviceId = device.DeviceId,
  108. stirFrymessage =frybom
  109. }.Publish();
  110. MessageLog.GetInstance.Show("接受到【MQTT】的小炒流程信息");
  111. }
  112. });
  113. EventBus.EventBus.GetInstance().Subscribe<OrderStatusChangedEvent>(0, OrderStatusChangedHandle);
  114. }
  115. private void StartTargetDeviceOrderJob(int deviceId)
  116. {
  117. ThreadManage.GetInstance().Start(() =>
  118. {
  119. var device = deviceMgr.GetDevices().FirstOrDefault(p => p.DeviceId == deviceId);
  120. while (running)
  121. {
  122. if (device.IsBusy /*|| !device.IsHealth*/)
  123. {
  124. Thread.Sleep(100);
  125. continue;
  126. }
  127. while (orders[deviceId].Count > 0)
  128. {
  129. if (orders[deviceId].TryDequeue(out MorkOrderPush temp))
  130. {
  131. if (InternetInfo.IsEnableTest) morkOrderPushes.Enqueue(temp);
  132. var orderEvent = DoOrderEvent.Make(temp);
  133. orderEvent.DeviceId = device.DeviceId;
  134. orderEvent.Publish();
  135. ActionManage.GetInstance.Send("AddOrder", new object[] { temp, device });
  136. }
  137. }
  138. Thread.Sleep(50);
  139. }
  140. }, $"MQTT 订单接收处理-设备[{deviceId}]", true);
  141. }
  142. public void OrderStatusChangedHandle(IEvent @event, EventCallBackHandle callBack)
  143. {
  144. OrderStatusChangedEvent orderStatusChangedEvent = @event as OrderStatusChangedEvent;
  145. string result = string.Empty;
  146. OrderStatusChange orderStatusChange = new OrderStatusChange()
  147. {
  148. CookingStatus = orderStatusChangedEvent.Status,
  149. SuborderId = orderStatusChangedEvent.SubOrderId,
  150. GoodName = orderStatusChangedEvent.GoodName,
  151. SortNum = orderStatusChangedEvent.SortNum
  152. };
  153. #region API 订单状态修改
  154. if (orderStatusChange.SuborderId.Length > 0 && orderStatusChange.CookingStatus != ORDER_STATUS.WAIT)
  155. {
  156. try
  157. {
  158. string header = $"[{InternetInfo.OrderServer}/order/robotstatuschange]_[{DateTime.Now.Ticks}]".AESEncrypt();
  159. string url = $"{InternetInfo.OrderServer}order/robotstatuschange";
  160. result = APIHelper.GetInstance.HttpRequest(url, header, orderStatusChange, RequestType.POST);
  161. }
  162. catch (Exception ex)
  163. {
  164. MessageLog.GetInstance.ShowEx(ex.ToString());
  165. }
  166. var res = JsonConvert.DeserializeObject<OrderStatusRsp>(result);
  167. ActionManage.GetInstance.Send("OrderStatusChange", orderStatusChangedEvent);
  168. bool isSuccess = res == null ? false : res.isSuccess;
  169. MessageLog.GetInstance.Show($"订单:{orderStatusChange.SuborderId} 状态:{orderStatusChange.CookingStatus} 执行结果:{isSuccess}");
  170. //MessageLog.GetInstance.Show(string.Format("订单状态改变,调用API执行结果{0}", res == null ? false : res.isSuccess));
  171. }
  172. #endregion
  173. #region MQTT 订单状态修改
  174. var clientId = Plugin.GetInstance().GetPlugin<ConfigMgr>().ClientId;
  175. var temp = BPAPackage.Make(orderStatusChange, clientId, orderStatusChangedEvent.deviceClientType);
  176. var q = temp.Serialize(false);
  177. Plugin.GetInstance().GetPlugin<MQTTMgr>().Publish(TOPIC.GetInstance.GetAppTopic(clientId.ToString()), temp.Serialize(false));
  178. #endregion
  179. }
  180. /// <summary>
  181. /// 资源释放
  182. /// </summary>
  183. public void Dispose()
  184. {
  185. running = false;
  186. }
  187. }
  188. }