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

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