终端一体化运控平台
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

220 satır
9.2 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. 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. DeviceId = device.DeviceId,
  114. stirFrymessage = frybom
  115. }.Publish();
  116. MessageLog.GetInstance.Show("接受到【MQTT】的小炒流程信息");
  117. }
  118. else if (message is MORKMWPushMessage morkMWBom)
  119. {
  120. IDevice device = deviceMgr.GetDevices().FirstOrDefault(x => x.DeviceId == 113);
  121. new MorkMWGoodsEvent()
  122. {
  123. DeviceId = device.DeviceId,
  124. morkMWPushMessage = morkMWBom
  125. }.Publish();
  126. MessageLog.GetInstance.Show("接收到【MQTT】获取的调酒机配方信息");
  127. }
  128. });
  129. EventBus.EventBus.GetInstance().Subscribe<OrderStatusChangedEvent>(0, OrderStatusChangedHandle);
  130. }
  131. private void StartTargetDeviceOrderJob(int deviceId)
  132. {
  133. ThreadManage.GetInstance().Start(() =>
  134. {
  135. var device = deviceMgr.GetDevices().FirstOrDefault(p => p.DeviceId == deviceId);
  136. while (running)
  137. {
  138. if (device.IsBusy /*|| !device.IsHealth*/)
  139. {
  140. Thread.Sleep(100);
  141. continue;
  142. }
  143. while (orders[deviceId].Count > 0)
  144. {
  145. if (orders[deviceId].TryDequeue(out MorkOrderPush temp))
  146. {
  147. if (InternetInfo.IsEnableTest) morkOrderPushes.Enqueue(temp);
  148. var orderEvent = DoOrderEvent.Make(temp);
  149. orderEvent.DeviceId = device.DeviceId;
  150. orderEvent.Publish();
  151. ActionManage.GetInstance.Send("AddOrder", new object[] { temp, device });
  152. }
  153. }
  154. Thread.Sleep(50);
  155. }
  156. }, $"MQTT 订单接收处理-设备[{deviceId}]", true);
  157. }
  158. public void OrderStatusChangedHandle(IEvent @event, EventCallBackHandle callBack)
  159. {
  160. OrderStatusChangedEvent orderStatusChangedEvent = @event as OrderStatusChangedEvent;
  161. string result = string.Empty;
  162. OrderStatusChange orderStatusChange = new OrderStatusChange()
  163. {
  164. CookingStatus = orderStatusChangedEvent.Status,
  165. SuborderId = orderStatusChangedEvent.SubOrderId,
  166. GoodName = orderStatusChangedEvent.GoodName,
  167. SortNum = orderStatusChangedEvent.SortNum
  168. };
  169. #region API 订单状态修改
  170. if (orderStatusChange.SuborderId.Length > 0 && orderStatusChange.CookingStatus != ORDER_STATUS.WAIT)
  171. {
  172. try
  173. {
  174. string header = $"[{InternetInfo.OrderServer}/order/robotstatuschange]_[{DateTime.Now.Ticks}]".AESEncrypt();
  175. string url = $"{InternetInfo.OrderServer}order/robotstatuschange";
  176. result = APIHelper.GetInstance.HttpRequest(url, header, orderStatusChange, RequestType.POST);
  177. }
  178. catch (Exception ex)
  179. {
  180. MessageLog.GetInstance.ShowEx(ex.ToString());
  181. }
  182. var res = JsonConvert.DeserializeObject<OrderStatusRsp>(result);
  183. ActionManage.GetInstance.Send("OrderStatusChange", orderStatusChangedEvent);
  184. bool isSuccess = res == null ? false : res.isSuccess;
  185. MessageLog.GetInstance.Show($"订单:{orderStatusChange.SuborderId} 状态:{orderStatusChange.CookingStatus} 执行结果:{isSuccess}");
  186. //MessageLog.GetInstance.Show(string.Format("订单状态改变,调用API执行结果{0}", res == null ? false : res.isSuccess));
  187. }
  188. #endregion
  189. #region MQTT 订单状态修改
  190. var clientId = Plugin.GetInstance().GetPlugin<ConfigMgr>().ClientId;
  191. var temp = BPAPackage.Make(orderStatusChange, clientId, orderStatusChangedEvent.deviceClientType);
  192. var q = temp.Serialize(false);
  193. Plugin.GetInstance().GetPlugin<MQTTMgr>().Publish(TOPIC.GetInstance.GetAppTopic(clientId.ToString()), temp.Serialize(false));
  194. #endregion
  195. }
  196. /// <summary>
  197. /// 资源释放
  198. /// </summary>
  199. public void Dispose()
  200. {
  201. running = false;
  202. }
  203. }
  204. }