using BPASmartClient.EventBus;
using BPASmartClient.Model;
using BPASmartClient.Model.咖啡机.Enum;
using BPASmartClient.SerialPort;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using static BPASmartClient.EventBus.EventBus;
namespace BPASmartClient.DRCoffee
{
///
/// 指令封装
///
internal class CommandHandler
{
byte[] cmdAsk;
private SerialPortClient commProxy;
private DrCoffeePackage drinksOrder = new DrCoffeePackage();
public Action PauseAsk { get; set; }
///
/// 初始化
///
internal void Init(SerialPortClient commProxy)
{
this.commProxy = commProxy;
DrCoffeePackage package = new DrCoffeePackage();
package.CommCmd = DrCoffeeCommCmd.饮品制作指令;
cmdAsk = DrCoffee.Packe(package);
drinksOrder.CommCmd = DrCoffeeCommCmd.饮品制作指令;
EventBus.EventBus.GetInstance().Subscribe(0,MakeCoffeeEventHandle);
EventBus.EventBus.GetInstance().Subscribe(0, CancelMakeCoffeeEventHandle);
EventBus.EventBus.GetInstance().Subscribe(0, CoffeeCommCmdEventHandle);
}
private void CoffeeCommCmdEventHandle(IEvent @event, EventCallBackHandle callBack)
{
PauseAsk?.Invoke(true);
Thread.Sleep(200);
drinksOrder.CommCmd = ((DRCoffee_CoffeeCommCmdEvent)@event).CommCmd;
commProxy.SendData(DrCoffee.Packe(drinksOrder));
Thread.Sleep(200);
PauseAsk?.Invoke(false);
}
private void CancelMakeCoffeeEventHandle(IEvent @event, EventCallBackHandle callBack)
{
PauseAsk?.Invoke(true);
Thread.Sleep(200);
drinksOrder.CommCmd = DrCoffeeCommCmd.取消应用指令;
drinksOrder.DrinksCode = 0;
commProxy.SendData(DrCoffee.Packe(drinksOrder));
Thread.Sleep(200);
PauseAsk?.Invoke(false);
}
private void MakeCoffeeEventHandle(IEvent @event, EventCallBackHandle callBack)
{
PauseAsk?.Invoke(true);
Thread.Sleep(200);
drinksOrder.CommCmd = DrCoffeeCommCmd.饮品制作指令;
drinksOrder.DrinksCode = ((DRCoffee_MakeCoffeeEvent)@event).DrinkCode;
commProxy.SendData(DrCoffee.Packe(drinksOrder));
Thread.Sleep(200);
PauseAsk?.Invoke(false);
}
///
/// 发送状态询问
///
internal byte[] GetStatusAsk()
{
return cmdAsk;
}
}
}