using HBLConsole.Communication;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using HBLConsole.Service;
using HBLConsole.Model;
namespace HBLDevice.Coffee
{
///
/// 咖啡机
///
public class CoffeeMachine
{
//通讯代理
SerialPortClient commProxy = null;
//数据仓库
private DataStorage dataStorage = new DataStorage();
//指令组装
private CommandHandler commandHandler = new CommandHandler();
//主线程运行标识
private bool running = false;
//是否下发指令,主线程等待
private bool free = true;
private DrCoffeeStatus drCoffeeStatus;
///
/// 咖啡机状态
///
public DrCoffeeStatus CurrentCoffeeStatus
{
get { return drCoffeeStatus; }
set
{
if (drCoffeeStatus != value)
{
drCoffeeStatus = value;
CoffeeStatusChanged?.Invoke(value);
}
}
}
private DrCoffeeAppStatus coffeeAppStatus;
///
/// 应用状态
///
public DrCoffeeAppStatus CurrentCoffeeAppStatus
{
get { return coffeeAppStatus; }
set
{
if (coffeeAppStatus != value)
{
coffeeAppStatus = value;
CoffeeAppStatusChanged?.Invoke(value);
}
}
}
public Action SendCallback;
public Action ReciveCallback;
///
/// 咖啡机状态改变回调
///
public Action CoffeeStatusChanged;
///
/// 应用状态改变回调
///
public Action CoffeeAppStatusChanged;
public CoffeeMachine(string portName, BaudRates baud)
{
commProxy = new SerialPortClient(portName, baud);
commProxy.SetDataStorage(dataStorage);
commandHandler.Init(commProxy);
commandHandler.PauseAsk = delegate (bool pause)
{
free = !pause;
};
}
///
/// 主线程开始运行
///
public void Start()
{
commProxy.Start();
running = true;
MainLoop();
}
///
/// 停止运行
///
public void Stop()
{
commProxy.Stop();
running = false;
}
/////
///// 下单制作
/////
///// 饮品代码
//public void PlaceOrder(DrCoffeeDrinksCode drinksCode)
//{
// free = false;
// Thread.Sleep(200);
// commProxy.SendData(commandHandler.GetDrinksOrder(drinksCode));
// SendCallback?.Invoke(BitConverter.ToString(commandHandler.GetDrinksOrder(drinksCode)));
// Thread.Sleep(200);
// free = true;
//}
///
/// 主循环,循环询问状态
///
private void MainLoop()
{
ThreadManage.GetInstance.StartLong(new Action(() =>
{
if (free)
{
commProxy.SendData(commandHandler.GetStatusAsk());
SendCallback?.Invoke(BitConverter.ToString(commandHandler.GetStatusAsk()));
}
Thread.Sleep(200);
}), "咖啡机询问线程");
//Executer.GetInstance().Start(() =>
//{
// while (running)
// {
// if (free)
// {
// commProxy.SendData(commandHandler.GetStatusAsk());
// SendCallback?.Invoke(BitConverter.ToString(commandHandler.GetStatusAsk()));
// }
// Thread.Sleep(200);
// }
//}, "咖啡机询问线程");
ThreadManage.GetInstance.StartLong(new Action(() =>
{
List temp = new List();
//一系列解包
while (dataStorage.GetSize() > 0)
{
byte item = dataStorage.GetData();
if (DrCoffee.HEADER == item)
{
if (temp.Count == DrCoffee.LENGTH - 1)
{
temp.Add(item);
var package = DrCoffee.UnPack(temp.ToArray());
ReciveCallback?.Invoke(BitConverter.ToString(temp.ToArray()));
temp.Clear();
MorkCStatus.GetInstance().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);
}), "咖啡机解析线程");
//Executer.GetInstance().Start(() =>
//{
// while (running)
// {
// List temp = new List();
// //一系列解包
// while (dataStorage.GetSize() > 0)
// {
// byte item = dataStorage.GetData();
// if (DrCoffee.HEADER == item)
// {
// if (temp.Count == DrCoffee.LENGTH - 1)
// {
// temp.Add(item);
// var package = DrCoffee.UnPack(temp.ToArray());
// ReciveCallback?.Invoke(BitConverter.ToString(temp.ToArray()));
// temp.Clear();
// MorkCStatus.GetInstance().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);
// }
//}, "咖啡机解析线程");
}
}
}