|
- using BPASmartClient.DRCoffee;
- using BPASmartClient.EventBus;
- using BPASmartClient.Helper;
- using BPASmartClient.Message;
- using BPASmartClient.Model;
- using BPASmartClient.Peripheral;
- using BPASmartClient.SerialPort;
- using System;
- using System.Collections.Generic;
- using System.Threading;
- using static BPASmartClient.EventBus.EventBus;
-
- namespace BPASmartClient.DRCoffee
- {
- /// <summary>
- /// 咖啡机
- /// </summary>
- public class CoffeeMachine: BasePeripheral
- {
- //通讯代理
- SerialPortClient commProxy = null;
- //数据仓库
- private DataStorage<byte> dataStorage = new DataStorage<byte>();
- //主线程运行标识
- private bool running = false;
- //是否下发指令,主线程等待
- private bool free = true;
-
- private DrCoffeeStatus drCoffeeStatus;
- /// <summary>
- /// 咖啡机状态
- /// </summary>
- public DrCoffeeStatus CurrentCoffeeStatus
- {
- get { return drCoffeeStatus; }
- set
- {
- if (drCoffeeStatus != value)
- {
- drCoffeeStatus = value;
- CoffeeStatusChanged?.Invoke(value);
- }
- }
- }
- private DrCoffeeAppStatus coffeeAppStatus;
- /// <summary>
- /// 应用状态
- /// </summary>
- public DrCoffeeAppStatus CurrentCoffeeAppStatus
- {
- get { return coffeeAppStatus; }
- set
- {
- if (coffeeAppStatus != value)
- {
- coffeeAppStatus = value;
- CoffeeAppStatusChanged?.Invoke(value);
- }
- }
- }
-
- public Action<string> SendCallback;
- public Action<string> ReciveCallback;
-
- /// <summary>
- /// 咖啡机状态改变回调
- /// </summary>
- public Action<DrCoffeeStatus> CoffeeStatusChanged;
- /// <summary>
- /// 应用状态改变回调
- /// </summary>
- public Action<DrCoffeeAppStatus> CoffeeAppStatusChanged;
- /// <summary>
- /// Dr咖啡机基础协议
- /// </summary>
- private DrCoffeePackage drinksOrder = new DrCoffeePackage();
-
- /// <summary>
- /// 串口COM口
- /// </summary>
- public string PortName { get; set; }
- /// <summary>
- /// 串口波特率
- /// </summary>
- public string BaudRate { get; set; }
-
- public CoffeeMachine()
- {
-
- }
-
- /// <summary>
- /// 主线程开始运行
- /// </summary>
- public override void Start()
- {
-
- commProxy.Start();
- running = true;
- MainLoop();
- }
-
- /// <summary>
- /// 停止运行
- /// </summary>
- public override void Stop()
- {
- commProxy.Stop();
- running = false;
- }
-
- /// <summary>
- /// 主循环,循环询问状态
- /// </summary>
- private void MainLoop()
- {
- //ThreadManage.GetInstance.StartLong(new Action(() =>
- //{
- // if (free)
- // {
- // commProxy.SendData(commandHandler.GetStatusAsk());
- // SendCallback?.Invoke(BitConverter.ToString(commandHandler.GetStatusAsk()));
- // }
- // Thread.Sleep(200);
- //}),"咖啡机询问线程");
-
- ThreadManage.GetInstance.StartLong(new Action(() =>
- {
- List<byte> temp = new List<byte>();
- //一系列解包
- 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);
- }), "咖啡机解析线程");
- }
-
- protected override void InitStatus()
- {
-
- }
-
-
- public override void Init()
- {
- commProxy = new SerialPortClient(PortName,(BaudRates)Enum.Parse(typeof(BaudRates),BaudRate));
- commProxy.SetDataStorage(dataStorage);
-
- //咖博士咖啡机制作
- EventBus.EventBus.GetInstance().Subscribe<DRCoffee_MakeCoffeeEvent>(DeviceId,delegate (IEvent @event,EventCallBackHandle callBack)
- {
- try
- {
- free = true;
- Thread.Sleep(200);
- drinksOrder.CommCmd = DrCoffeeCommCmd.饮品制作指令;
- //drinksOrder.DrinksCode = ((DRCoffee_MakeCoffeeEvent)@event).CommCmd;
- commProxy.SendData(DrCoffee.Packe(drinksOrder));
- Thread.Sleep(200);
- free = false;
- }
- catch (Exception ex)
- {
- MessageLog.GetInstance.Show($"BPASmartClient.DRCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
- }
- });
-
- //咖博士咖啡机取消制作咖啡
- EventBus.EventBus.GetInstance().Subscribe<DRCoffee_CancelMakeCoffeeEvent>(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;
- }
- catch (Exception ex)
- {
- MessageLog.GetInstance.Show($"BPASmartClient.DRCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
- }
- });
-
- //咖博士咖啡机模式设置
- EventBus.EventBus.GetInstance().Subscribe<DRCoffee_CoffeeCommCmdEvent>(DeviceId,delegate (IEvent @event,EventCallBackHandle callBack)
- {
- try
- {
- free = true;
- Thread.Sleep(200);
- //drinksOrder.CommCmd = (DrCoffeeCommCmd)int.Parse(e.obj_MessageObj.ToString());
- commProxy.SendData(DrCoffee.Packe(drinksOrder));
- Thread.Sleep(200);
- free = false;
- }
- catch (Exception ex)
- {
- MessageLog.GetInstance.Show($"BPASmartClient.DRCoffee 中引发错误,CoffeeCommCmdHandler 类,描述:[{ex.Message}]");
- }
- });
- }
- }
- }
|