|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
-
- using BPA.Helper;
- using BPASmartClient.KLMCoffee.Protocal;
-
- using BPASmartClient.Model;
- using BPASmartClient.Model.咖啡机.Enum;
- using BPASmartClient.Peripheral;
- using BPASmartClient.SerialPort;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using static BPA.Helper.EventBus;
-
- namespace BPASmartClient.KLMCoffee
- {
- /// <summary>
- /// 伽乐美咖啡机
- /// </summary>
- public class CoffeeMachine : BasePeripheral
- {
- //通讯代理
- SerialPortClient commProxy = null;
- //数据仓库
- private DataStorage<byte> dataStorage = new DataStorage<byte>();
- //是否下发指令,主线程等待
- private bool free = false;
- //状态询问指令
- private byte[] cmdAsk;
- //串口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; } }
- //命令
- public K95Command command = new K95Command();
-
- public CoffeeMachine()
- {
- cmdAsk = new K95Command().ReturnsStatusInquire();
- }
-
- /// <summary>
- /// 主线程开始运行
- /// </summary>
- public override void Start()
- {
- try
- {
- commProxy.Start();
- IsConnected = true;
- free = false;
- MainLoop();
- }
- catch (Exception ex)
- {
- MessageLog.GetInstance.ShowEx($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
- }
-
-
- }
-
- /// <summary>
- /// 停止运行
- /// </summary>
- public override void Stop()
- {
- try
- {
- commProxy.Stop();
- IsConnected = false;
- free = true;
- }
- catch (Exception ex)
- {
- MessageLog.GetInstance.ShowEx($"BPASmartClient.KLMCoffee 中引发错误,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(() =>
- {
- ResolveMsg();
- }), "咖啡机解析线程");
- }
-
- private void ResolveMsg()
- {
- status["CoffeeIsConnected"] = OnLine;
- IsConnected = OnLine;
- List<byte> temp = new List<byte>();
- //一系列解包
- while (dataStorage.GetSize() > 0)
- {
- byte item = dataStorage.GetData();
- List<byte> data = new List<byte>() { item };
- if (Encoding.ASCII.GetString(data.ToArray()) == ":")
- {
- temp.Add(item);
- while (dataStorage.GetSize() < 32) { Thread.Sleep(5); }
-
- while (temp.Count < 32)
- {
- temp.Add(dataStorage.GetData());
- }
- List<byte> vs = new List<byte>() { temp[temp.Count - 4], temp[temp.Count - 3], temp[temp.Count - 2], temp[temp.Count - 1] };
-
- string t = Encoding.ASCII.GetString(vs.ToArray()).ToLower();
- //帧尾
- //if (Encoding.ASCII.GetString(vs.ToArray()).ToLower() == "\\r\\n" || Encoding.ASCII.GetString(vs.ToArray()).ToLower() == "\r\n")
- var package = Encoding.ASCII.GetString(temp.ToArray());
- if (package.Contains("\\r\\n"))
- {
- ProcessMsg(package);
- }
- temp.Clear();
- }
- continue;
- }
- Thread.Sleep(5);
- }
-
- public void ProcessMsg(string data)
- {
- try
- {
- lastRefreshTime = DateTime.Now;
- SystemStatusModel systemStatus = new K95Command().StateResolution(data);
- if (systemStatus != null)
- {
- status["CoffeeIsConnected"] = OnLine;
- if ((K95SysTemStatus)status["CoffeeStatus"] == K95SysTemStatus.正在制作咖啡 && systemStatus.temStatus != K95SysTemStatus.正在制作咖啡)
- {
- status["CoffeeStatus"] = systemStatus.temStatus;
- EventBus.GetInstance().Publish(new KLMCoffee_CoffeEndCookEvent { DeviceId = DeviceId });
- }
- else status["CoffeeStatus"] = systemStatus.temStatus;
- status["CoffeedrinkType"] = systemStatus.drinkType;
- status["CoffeeAppStatus"] = systemStatus.taskIndex;
- status["Coffeeprogress"] = systemStatus.progress;
- status["CoffeeWarning"] = systemStatus.faultMessage?.dataFault();
- status["CoffeeKeep"] = systemStatus.upkeepMessage;
- //if (systemStatus.faultMessage.IsFault() || systemStatus.upkeepMessage.IsUpkeep())
- // IsWork = false;
- //else
- // IsWork = true;
- }
-
- }
- catch (Exception ex)
- {
- MessageLog.GetInstance.ShowEx($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
- }
- }
-
- protected override void InitStatus()
- {
- status["CoffeeStatus"] = K95SysTemStatus.空闲状态;
- status["CoffeedrinkType"] = DrinkType.意式咖啡;
- status["CoffeeAppStatus"] = TaskIndex.无任务;
- status["Coffeeprogress"] = 0;
- status["CoffeeWarning"] = new FaultMessage(0x00, 0x00).dataFault();
- status["CoffeeKeep"] = new UpkeepMessage(0x00).dataFault();
- }
-
- public override void Init()
- {
- commProxy = new SerialPortClient(communicationPar.SerialPort, (BaudRates)communicationPar.BaudRate);
- commProxy.SetDataStorage(dataStorage);
-
- //伽乐美咖啡机制作
- EventBus.GetInstance().Subscribe<KLMCoffee_MakeCoffeeEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
- {
- try
- {
- free = true;
- Thread.Sleep(300);
- K95deFaultCoffeeEnum K95Code = (K95deFaultCoffeeEnum)((KLMCoffee_MakeCoffeeEvent)@event).KLMDrinkFaultCode;
- //byte[] data = command.ReturnsCommandData(K95CommandEnum.配方咖啡制作.GetString(), new RecipeModel().Packe(((KLMCoffee_MakeCoffeeEvent)@event).DrinkCode));
- byte[] data = command.ReturnsCommandData(K95CommandEnum.咖啡制作.GetString(), K95Code.GetString());
- commProxy.SendData(data);
- MessageLog.GetInstance.Show($"制作{K95Code}数据发送完成");
- Thread.Sleep(200);
- free = false;
- }
- catch (Exception ex)
- {
- MessageLog.GetInstance.ShowEx($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
- }
- });
-
- //伽乐美咖啡机取消制作咖啡
- EventBus.GetInstance().Subscribe<KLMCoffee_CancelMakeCoffeeEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
- {
- try
- {
- free = true;
- Thread.Sleep(200);
- byte[] data = command.ReturnsCancelMake();
- commProxy.SendData(data);
- Thread.Sleep(200);
- free = false;
- }
- catch (Exception ex)
- {
- MessageLog.GetInstance.ShowEx($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
- }
- });
-
- //伽乐美咖啡机清洗冲泡器
- EventBus.GetInstance().Subscribe<KLMCoffee_WashCPJEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
- {
- try
- {
- free = true;
- Thread.Sleep(200);
- byte[] data = command.ReturnsWashCPJ();
- commProxy.SendData(data);
- Thread.Sleep(200);
- free = false;
- }
- catch (Exception ex)
- {
- MessageLog.GetInstance.ShowEx($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
- }
- });
-
- //伽乐美咖啡机放杯确认
- EventBus.GetInstance().Subscribe<KLMCoffee_CupIsOKEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
- {
- try
- {
- free = true;
- Thread.Sleep(200);
- byte[] data = command.ReturnsCupIsOK();
- commProxy.SendData(data);
- Thread.Sleep(200);
- free = false;
- }
- catch (Exception ex)
- {
- MessageLog.GetInstance.ShowEx($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
- }
- });
-
-
- //伽乐美咖啡机清洗奶沫器
- EventBus.GetInstance().Subscribe<KLMCoffee_WashNMJEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
- {
- try
- {
- free = true;
- Thread.Sleep(200);
- byte[] data = command.ReturnsWashNMJ();
- commProxy.SendData(data);
- Thread.Sleep(200);
- free = false;
- }
- catch (Exception ex)
- {
- MessageLog.GetInstance.ShowEx($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
- }
- });
-
-
- //伽乐美咖啡机清洗奶沫器确认
- EventBus.GetInstance().Subscribe<KLMCoffee_WashNMJIsOKEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
- {
- try
- {
- free = true;
- Thread.Sleep(200);
- byte[] data = command.ReturnsWashNMJIsOK();
- commProxy.SendData(data);
- Thread.Sleep(200);
- free = false;
- }
- catch (Exception ex)
- {
- MessageLog.GetInstance.ShowEx($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
- }
- });
- InitStatus();
- Start();
- }
-
- public override void WriteData(string address, object value)
- {
- }
- }
- }
|