|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- using BPA.Message;
- using BPASmartClient.EventBus;
- using BPASmartClient.Helper;
- using BPASmartClient.Http;
- using BPASmartClient.Message;
- using BPASmartClient.Model;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using static BPASmartClient.EventBus.EventBus;
-
- namespace BPASmartClient.Business
- {
- /// <summary>
- /// 订单代理
- /// </summary>
- public class OrderProxy : IPlugin
- {
- //订单队列
- private ConcurrentDictionary<int, ConcurrentQueue<MorkOrderPush>> orders = new ConcurrentDictionary<int, ConcurrentQueue<MorkOrderPush>>();
- //运行标识
- private bool running = false;
- //设备管理
- private DeviceMgr deviceMgr;
- /// <summary>
- /// 初始化
- /// </summary>
- public void Initialize()
- {
- deviceMgr = Plugin.GetInstance().GetPlugin<DeviceMgr>();
- }
-
- public void Start()
- {
- running = true;
- Plugin.GetInstance().GetPlugin<MQTTMgr>().SetMessageReciveHandler(delegate (IMessage orderInfo)
- {
- if (orderInfo == null) return;
- if (orderInfo is MorkOrderPush morkOrderpush)
- {
- if (!orders.ContainsKey(morkOrderpush.DeviceId))
- {
- orders.TryAdd(morkOrderpush.DeviceId, new ConcurrentQueue<MorkOrderPush>());
- StartTargetDeviceOrderJob(morkOrderpush.DeviceId);
- }
- orders[morkOrderpush.DeviceId].Enqueue(morkOrderpush);
- }
- });
-
- EventBus.EventBus.GetInstance().Subscribe<OrderStatusChangedEvent>(0, OrderStatusChangedHandle);
- }
-
- private void StartTargetDeviceOrderJob(int deviceId)
- {
- ThreadManage.GetInstance().Start(() =>
- {
- var device = deviceMgr.GetDevices().FirstOrDefault(p => p.DeviceId == deviceId);
- while (running)
- {
- if (device.IsBusy || !device.IsHealth)
- {
- Thread.Sleep(100);
- continue;
- }
- while (orders[deviceId].Count > 0)
- {
- if (orders[deviceId].TryDequeue(out MorkOrderPush temp))
- {
- var orderEvent = DoOrderEvent.Make(temp);
- orderEvent.Id = device.DeviceId;
- orderEvent.Publish();
- Json<KeepDataBase>.Data.orderLists.Add(new OrderData()
- {
- IsSelected = true,
- OrderPush = temp,
- OrderStatus = BPA.Message.Enum.ORDER_STATUS.WAIT
- });
- }
- }
- Thread.Sleep(50);
- }
- }, $"MQTT 订单接收处理-设备[{deviceId}]");
- }
-
- public void OrderStatusChangedHandle(IEvent @event, EventCallBackHandle callBack)
- {
- OrderStatusChangedEvent orderStatusChangedEvent = @event as OrderStatusChangedEvent;
- string result = string.Empty;
- OrderStatusChange orderStatusChange = new OrderStatusChange() { CookingStatus = orderStatusChangedEvent.Status, SuborderId = orderStatusChangedEvent.SubOrderId };
- try
- {
- string header = $"[{InternetInfo.OrderServer}/order/robotstatuschange]_[{DateTime.Now.Ticks}]".AESEncrypt();
- string url = $"{InternetInfo.OrderServer}/order/robotstatuschange";
- result = APIHelper.GetInstance.HttpRequest(url, header, orderStatusChange, RequestType.POST);
- }
- catch (Exception ex)
- {
- MessageLog.GetInstance.ShowEx(ex.ToString());
- }
- var res = JsonConvert.DeserializeObject<OrderStatusRsp>(result);
- MessageLog.GetInstance.ShowEx(string.Format("订单状态改变,调用API执行结果{0}", res == null ? false : res.isSuccess));
- }
-
- /// <summary>
- /// 资源释放
- /// </summary>
- public void Dispose()
- {
- running = false;
- }
-
-
- }
- }
|