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

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