using BPASmartClient.DRCoffee;
using BPASmartClient.EventBus;
using BPASmartClient.Helper;
using BPASmartClient.Message;
using BPASmartClient.Model;
using BPASmartClient.Model.咖啡机.Enum;
using BPASmartClient.Peripheral;
using BPASmartClient.SerialPort;
using System;
using System.Collections.Generic;
using System.Threading;
using static BPASmartClient.EventBus.EventBus;
namespace BPASmartClient.DRCoffee
{
///
/// 咖啡机
///
public class CoffeeMachine : BasePeripheral
{
//通讯代理
SerialPortClient commProxy = null;
//数据仓库
private DataStorage dataStorage = new DataStorage();
//是否下发指令,主线程等待
private bool free = false;
//状态询问指令
private byte[] cmdAsk;
//Dr咖啡机基础协议
private DrCoffeePackage drinksOrder = new DrCoffeePackage();
//串口COM口
public string PortName { get; set; }
//串口波特率
public string BaudRate { get; set; }
//心跳时间
private DateTime lastRefreshTime = DateTime.MinValue;
///
/// 是否在线
///
public bool OnLine { get { return DateTime.Now.Subtract(lastRefreshTime).TotalSeconds <= 3; } }
//private volatile static CoffeeMachine _Instance;
//public static CoffeeMachine GetInstance => _Instance ?? (_Instance = new CoffeeMachine());
public CoffeeMachine()
{
DrCoffeePackage package = new DrCoffeePackage();
package.CommCmd = DrCoffeeCommCmd.饮品制作指令;
cmdAsk = DrCoffee.Packe(package);
}
///
/// 主线程开始运行
///
public override void Start()
{
try
{
commProxy.Start();
IsConnected = true;
free = false;
MainLoop();
}
catch (Exception ex)
{
MessageLog.GetInstance.ShowEx($"BPASmartClient.DRCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
}
}
///
/// 停止运行
///
public override void Stop()
{
try
{
commProxy.Stop();
IsConnected = false;
free = true;
}
catch (Exception ex)
{
MessageLog.GetInstance.ShowEx($"BPASmartClient.DRCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
}
}
///
/// 主循环,循环询问状态
///
private void MainLoop()
{
ThreadManage.GetInstance().StartLong(new Action(() =>
{
if (!free)
{
commProxy.SendData(cmdAsk);
}
Thread.Sleep(200);
}), "咖啡机询问线程");
ThreadManage.GetInstance().StartLong(new Action(() =>
{
List temp = new List();
//一系列解包
while (dataStorage.GetSize() > 0)
{
IsConnected = true;
byte item = dataStorage.GetData();
if (DrCoffee.HEADER == item)
{
if (temp.Count == DrCoffee.LENGTH - 1)
{
temp.Add(item);
var package = DrCoffee.UnPack(temp.ToArray());
temp.Clear();
ProcessPackage(package);
}
else
{
temp.Clear();
temp.Add(item);
}
continue;
}
else
{
if (temp.Count == 1 && item != DrCoffee.LENGTH)
{
temp.Clear();
continue;
}
temp.Add(item);
}
}
Thread.Sleep(5);
}), "咖啡机解析线程");
}
///
/// 咖啡机状态解析
///
///
public void ProcessPackage(DrCoffeePackage package)
{
lastRefreshTime = DateTime.Now;
IsConnected = OnLine;
status["CoffeeIsConnected"] = OnLine;
if (((DrCoffeeStatus)status["CoffeeStatus"]) == DrCoffeeStatus.Running && package.Status != DrCoffeeStatus.Running)
{
status["CoffeeStatus"] = package.Status;
EventBus.EventBus.GetInstance().Publish(new DRCoffee_CoffeEndCookEvent() { DeviceId = DeviceId });
}
else status["CoffeeStatus"] = package.Status;
status["CoffeeAppStatus"] = package.ApplicationStatus;
status["CoffeeWarning"] = package.Warning;
status["CoffeeFault"] = package.Fault;
if ((DrCoffeeStatus)status["CoffeeStatus"] == DrCoffeeStatus.Warning
|| (DrCoffeeStatus)status["CoffeeStatus"] == DrCoffeeStatus.Fault
|| (DrCoffeeWarning)status["CoffeeWarning"] != DrCoffeeWarning.无警告
|| (DrCoffeeFault)status["CoffeeFault"] != DrCoffeeFault.无故障
)
{
IsWork = false;
}
else
IsWork = true;
}
protected override void InitStatus()
{
status["CoffeeStatus"] = DrCoffeeStatus.Wait;
status["CoffeeAppStatus"] = DrCoffeeAppStatus.应用无状态;
status["CoffeeWarning"] = DrCoffeeWarning.无警告;
status["CoffeeFault"] = DrCoffeeFault.无故障;
}
public override void Init()
{
commProxy = new SerialPortClient(communicationPar.SerialPort, (BaudRates)communicationPar.BaudRate);
commProxy.SetDataStorage(dataStorage);
//咖博士咖啡机制作
EventBus.EventBus.GetInstance().Subscribe(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
{
try
{
free = true;
Thread.Sleep(200);
drinksOrder.CommCmd = DrCoffeeCommCmd.饮品制作指令;
drinksOrder.DrinksCode = ((DRCoffee_MakeCoffeeEvent)@event).DrinkCode;
commProxy.SendData(DrCoffee.Packe(drinksOrder));
Thread.Sleep(200);
free = false;
MessageLog.GetInstance.Show($"咖啡机: 制作咖啡指令");
}
catch (Exception ex)
{
MessageLog.GetInstance.ShowEx($"BPASmartClient.DRCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
}
});
//咖博士咖啡机取消制作咖啡
EventBus.EventBus.GetInstance().Subscribe(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
{
try
{
free = true;
Thread.Sleep(200);
drinksOrder.CommCmd = DrCoffeeCommCmd.取消应用指令;
drinksOrder.DrinksCode = 0;
commProxy.SendData(DrCoffee.Packe(drinksOrder));
Thread.Sleep(200);
free = false;
MessageLog.GetInstance.Show($"咖啡机: 咖啡取消指令");
}
catch (Exception ex)
{
MessageLog.GetInstance.ShowEx($"BPASmartClient.DRCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
}
});
//咖博士咖啡机模式设置
EventBus.EventBus.GetInstance().Subscribe(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
{
try
{
free = true;
Thread.Sleep(200);
drinksOrder.CommCmd = ((DRCoffee_CoffeeCommCmdEvent)@event).CommCmd;
commProxy.SendData(DrCoffee.Packe(drinksOrder));
Thread.Sleep(200);
free = false;
MessageLog.GetInstance.Show($"咖啡机: 咖啡模式设置指令");
}
catch (Exception ex)
{
MessageLog.GetInstance.ShowEx($"BPASmartClient.DRCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
}
});
InitStatus();
//测试
Start();
}
public override void WriteData(string address, object value)
{
}
//public override void ReadData(string address)
//{
//}
}
}