|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- 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
- {
- /// <summary>
- /// 咖啡机
- /// </summary>
- public class CoffeeMachine : BasePeripheral
- {
- //通讯代理
- SerialPortClient commProxy = null;
- //数据仓库
- private DataStorage<byte> dataStorage = new DataStorage<byte>();
- //是否下发指令,主线程等待
- 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;
- /// <summary>
- /// 是否在线
- /// </summary>
- 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);
- }
-
- /// <summary>
- /// 主线程开始运行
- /// </summary>
- public override void Start()
- {
- try
- {
- commProxy.Start();
- IsConnected = true;
- free = false;
- MainLoop();
- }
- catch (Exception ex)
- {
- MessageLog.GetInstance.ShowEx($"BPASmartClient.DRCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
- }
- }
-
- /// <summary>
- /// 停止运行
- /// </summary>
- public override void Stop()
- {
- try
- {
- commProxy.Stop();
- IsConnected = false;
- free = true;
- }
- catch (Exception ex)
- {
- MessageLog.GetInstance.ShowEx($"BPASmartClient.DRCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
- }
- }
-
- /// <summary>
- /// 主循环,循环询问状态
- /// </summary>
- private void MainLoop()
- {
- ThreadManage.GetInstance().StartLong(new Action(() =>
- {
- if (!free)
- {
- commProxy.SendData(cmdAsk);
- }
- Thread.Sleep(200);
- }), "咖啡机询问线程");
-
- ThreadManage.GetInstance().StartLong(new Action(() =>
- {
- List<byte> temp = new List<byte>();
- //一系列解包
- 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);
- }), "咖啡机解析线程");
- }
-
- /// <summary>
- /// 咖啡机状态解析
- /// </summary>
- /// <param name="package"></param>
- 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<DRCoffee_MakeCoffeeEvent>(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<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;
- MessageLog.GetInstance.Show($"咖啡机: 咖啡取消指令");
- }
- catch (Exception ex)
- {
- MessageLog.GetInstance.ShowEx($"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 = ((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)
- //{
- //}
- }
- }
|