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

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