//#define test
using BPA.Message;
using BPA.Message.Enum;
using BPA.Models;
using BPASmartClient.Device;
using BPA.Helper;
using BPASmartClient.Model;
using BPASmartClient.Model.小炒机;
using BPASmartClient.Model.调酒机;
using Newtonsoft.Json;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static BPA.Helper.EventBus;
namespace BPASmartClient.Business
{
///
/// 订单代理
///
public class OrderProxy : IPlugin
{
//订单队列
private ConcurrentDictionary> orders = new ConcurrentDictionary>();
///
/// 运行标识
///
private bool running = false;
//设备管理
private DeviceMgr deviceMgr;
ConcurrentQueue morkOrderPushes = new ConcurrentQueue();
///
/// 初始化
///
public void Initialize()
{
deviceMgr = Plugin.GetInstance().GetPlugin();
if (InternetInfo.IsEnableTest)
{
TaskManage.GetInstance.StartLong(new Action(() =>
{
while (morkOrderPushes.Count > 0)
{
if (morkOrderPushes.TryDequeue(out MorkOrderPush mork))
{
Thread.Sleep(3000);
new OrderStatusChangedEvent() { SortNum = mork.SortNum.ToString(), Status = ORDER_STATUS.COOKING, SubOrderId = mork.SuborderId, GoodName = mork.GoodsName }.Publish();
Thread.Sleep(5000);
new OrderStatusChangedEvent() { SortNum = mork.SortNum.ToString(), Status = ORDER_STATUS.COMPLETED_COOK, SubOrderId = mork.SuborderId, GoodName = mork.GoodsName }.Publish();
Thread.Sleep(5000);
new OrderStatusChangedEvent() { SortNum = mork.SortNum.ToString(), Status = ORDER_STATUS.COMPLETED_TAKE, SubOrderId = mork.SuborderId, GoodName = mork.GoodsName }.Publish();
string num = mork.SortNum.ToString().Substring(0, mork.SortNum.ToString().Length - 1);
if (int.TryParse(num, out int sort))
ActionManage.GetInstance.Send("COMPLETED_TAKE_Notify", sort.ToString("0000"));
Thread.Sleep(2000);
}
}
Thread.Sleep(1000);
}), "sim order");
}
}
public void Start()
{
running = true;
Plugin.GetInstance().GetPlugin().SetMessageReciveHandler(delegate (IMessage message)
{
if (message == null) return;
//订单消息
if (message is MorkOrderPush morkOrderpush)
{
if (!orders.ContainsKey(morkOrderpush.DeviceId))
{
orders.TryAdd(morkOrderpush.DeviceId, new ConcurrentQueue());
StartTargetDeviceOrderJob(morkOrderpush.DeviceId);
}
if (orders[morkOrderpush.DeviceId].FirstOrDefault(p => p.SuborderId == morkOrderpush.SuborderId) == null)
{
orders[morkOrderpush.DeviceId].Enqueue(morkOrderpush);
}
}
//辅料信息
else if (message is RecipeBoms recipe)
{
new RecipeBomEvent()
{
DeviceId = recipe.DeviceId,
recipeBoms = recipe
}.Publish();
MessageLog.GetInstance.Show("接收到 【 MQTT 】 的辅料信息");
}
//物料消息
else if (message is OrderMaterialDelivery delivery)
{
new MaterialDeliveryEvent()
{
DeviceId = delivery.DeviceId,
orderMaterialDelivery = delivery
}.Publish();
MessageLog.GetInstance.Show("接收到 【 MQTT 】 的物料信息");
delivery?.BatchingInfo?.ForEach(x =>
{
MessageLog.GetInstance.Show($"物料ID:=[{x.BatchingId}],{x.BatchingLoc}号位置:{x.BatchingCount}");
});
}
//小炒流程信息
else if (message is StirFryPushMessage frybom)
{
IDevice device = deviceMgr.GetDevices().FirstOrDefault(x => x.DeviceId == 28);
new StirFryGoodsEvent()
{
DeviceId = device.DeviceId,
stirFrymessage = frybom
}.Publish();
MessageLog.GetInstance.Show("接受到【MQTT】的小炒流程信息");
}
else if (message is MORKMWPushMessage morkMWBom)
{
IDevice device = deviceMgr.GetDevices().FirstOrDefault(x => x.DeviceId == 113);
new MorkMWGoodsEvent()
{
DeviceId = device.DeviceId,
morkMWPushMessage = morkMWBom
}.Publish();
MessageLog.GetInstance.Show("接收到【MQTT】获取的调酒机配方信息");
}
});
EventBus.GetInstance().Subscribe(0, OrderStatusChangedHandle);
}
private void StartTargetDeviceOrderJob(int deviceId)
{
TaskManage.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))
{
if (InternetInfo.IsEnableTest) morkOrderPushes.Enqueue(temp);
var orderEvent = DoOrderEvent.Make(temp);
orderEvent.DeviceId = device.DeviceId;
orderEvent.Publish();
ActionManage.GetInstance.Send("AddOrder", new object[] { temp, device });
}
}
Thread.Sleep(50);
}
}, $"MQTT 订单接收处理-设备[{deviceId}]", isRestart: true);
}
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,
GoodName = orderStatusChangedEvent.GoodName,
SortNum = orderStatusChangedEvent.SortNum
};
try
{
bool isSuccess = false;
#region API 订单状态修改
if (orderStatusChange.SuborderId.Length > 0 && orderStatusChange.CookingStatus != ORDER_STATUS.WAIT)
{
string header = $"[{InternetInfo.OrderServer}/order/robotstatuschange]_[{DateTime.Now.Ticks}]".AESEncrypt();
string url = $"{InternetInfo.OrderServer}order/robotstatuschange";
result = APIHelper.GetInstance.HttpRequest(url, orderStatusChange, header, RequestType.POST);
var res = JsonConvert.DeserializeObject(result);
ActionManage.GetInstance.Send("OrderStatusChange", orderStatusChangedEvent);
isSuccess = res == null ? false : res.isSuccess;
//MessageLog.GetInstance.Show($"订单:{orderStatusChange.SuborderId} 状态:{orderStatusChange.CookingStatus} 执行结果:{isSuccess}");
//MessageLog.GetInstance.Show(string.Format("订单状态改变,调用API执行结果{0}", res == null ? false : res.isSuccess));
}
#endregion
#region MQTT 订单状态修改
var clientId = Plugin.GetInstance().GetPlugin().ClientId;
var temp = BPAPackage.Make(orderStatusChange, clientId, orderStatusChangedEvent.deviceClientType);
var q = temp.Serialize(false);
Plugin.GetInstance().GetPlugin().Publish(TOPIC.GetInstance.GetAppTopic(clientId.ToString()), temp.Serialize(false));
if (orderStatusChange.CookingStatus == ORDER_STATUS.COMPLETED_TAKE)
MessageLog.GetInstance.Show($"通知叫号:{orderStatusChange.GoodName}--{orderStatusChange.SortNum}");
#endregion
if (isSuccess) callBack?.Invoke();
}
catch (Exception ex)
{
MessageLog.GetInstance.ShowEx(ex.ToString());
}
}
///
/// 资源释放
///
public void Dispose()
{
running = false;
}
}
}