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