using BPA.Message;
using BPASmartClient.EventBus;
using BPASmartClient.Helper;
using BPASmartClient.Model;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BPASmartClient.Business
{
///
/// 订单代理
///
public class OrderProxy : IPlugin
{
//订单队列
private ConcurrentQueue orders = new ConcurrentQueue();
//运行标识
private bool running = false;
//设备管理
private DeviceMgr deviceMgr;
///
/// 初始化
///
public void Initialize()
{
deviceMgr = Plugin.GetInstance().GetPlugin();
Plugin.GetInstance().GetPlugin().SetMessageReciveHandler(delegate (IMessage orderInfo)
{
if (orderInfo == null) return;
if (orderInfo is MorkOrderPush morkOrderpush)
{
orders.Enqueue(morkOrderpush);
}
});
ThreadManage.GetInstance().StartLong(() =>
{
while (running)
{
while (orders.Count > 0)
{
var temp = orders.ElementAt(0);
var device = deviceMgr.GetDevices().FirstOrDefault(p => p.DeviceId == temp.DeviceId);
if (null != device)
{
if (!device.IsBusy && device.IsHealth)
{
if (orders.TryDequeue(out temp))
{
var orderEvent = DoOrderEvent.Make(temp);
orderEvent.Id = device.DeviceId;
orderEvent.Publish();
}
}
}
}
Thread.Sleep(50);
}
}, "MQTT 消息处理");
}
///
/// 资源释放
///
public void Dispose()
{
running = false;
}
}
}