diff --git a/BPASmartClient.KLMCoffee/CoffeeMachine.cs b/BPASmartClient.KLMCoffee/CoffeeMachine.cs index 4742fc4d..e3e890a7 100644 --- a/BPASmartClient.KLMCoffee/CoffeeMachine.cs +++ b/BPASmartClient.KLMCoffee/CoffeeMachine.cs @@ -1,14 +1,25 @@ -using BPASmartClient.Helper; +using BPASmartClient.EventBus; +using BPASmartClient.Helper; +using BPASmartClient.KLMCoffee.Protocal; +using BPASmartClient.Message; +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 BPASmartClient.EventBus.EventBus; namespace BPASmartClient.KLMCoffee { - public class CoffeeMachine + /// + /// 伽乐美咖啡机 + /// + public class CoffeeMachine :BasePeripheral { //通讯代理 SerialPortClient commProxy = null; @@ -26,9 +37,254 @@ namespace BPASmartClient.KLMCoffee 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(); + } + + /// + /// 主线程开始运行 + /// + public override void Start() + { + try + { + commProxy.Start(); + IsConnected = true; + free = false; + MainLoop(); + } + catch (Exception ex) + { + MessageLog.GetInstance.Show($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]"); + } + } + + /// + /// 停止运行 + /// + public override void Stop() + { + try + { + commProxy.Stop(); + IsConnected = false; + free = true; + } + catch (Exception ex) + { + MessageLog.GetInstance.Show($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]"); + } + } + + /// + /// 主循环,循环询问状态 + /// + 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() + { + List temp = new List(); + //一系列解包 + while (dataStorage.GetSize() > 0) + { + byte item = dataStorage.GetData(); + List data = new List() { item }; + if (Encoding.ASCII.GetString(data.ToArray()) == ":") + { + temp.Add(item); + while (temp.Count < 32) + { + temp.Add(dataStorage.GetData()); + } + List vs = new List() { temp[temp.Count - 4],temp[temp.Count - 3],temp[temp.Count - 2],temp[temp.Count - 1] }; + + //帧尾 + if (Encoding.ASCII.GetString(vs.ToArray()).ToLower() == "\\r\\n" || Encoding.ASCII.GetString(vs.ToArray()).ToLower() == "\r\n") + { + var package = Encoding.ASCII.GetString(temp.ToArray()); + ProcessMsg(package); + } + } + continue; + } + Thread.Sleep(5); + } + + public void ProcessMsg(string data) + { + try + { + SystemStatusModel systemStatus = new K95Command().StateResolution(data); + if (systemStatus != null) + { + status["temStatus"] = systemStatus.temStatus; + status["drinkType"] = systemStatus.drinkType; + status["taskIndex"] = systemStatus.taskIndex; + status["progress"] = systemStatus.progress; + status["faultMessage"] = systemStatus.faultMessage; + status["upkeepMessage"] = systemStatus.upkeepMessage; + if (systemStatus.faultMessage.IsFault() || systemStatus.upkeepMessage.IsUpkeep()) + IsWork=false; + else + IsWork=true; + } + + } + catch (Exception ex) + { + MessageLog.GetInstance.Show($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]"); + } + } + + protected override void InitStatus() + { + status["temStatus"] = K95SysTemStatus.空闲状态; + status["drinkType"] = DrinkType.意式; + status["taskIndex"] = TaskIndex.无任务; + status["progress"] = 0; + status["faultMessage"] = new FaultMessage(0x00,0x00); + status["upkeepMessage"] = new UpkeepMessage(0x00); + + } + + public override void Init() + { + commProxy = new SerialPortClient(PortName,(BaudRates)Enum.Parse(typeof(BaudRates),BaudRate)); + commProxy.SetDataStorage(dataStorage); + + //string sdas = ":010510000000000000000000F3A\r\n"; + //byte[] sdsd = new K95Command().ascii2Hex(sdas); + //dataStorage.PutData(sdsd); + //ResolveMsg(); + + //伽乐美咖啡机制作 + EventBus.EventBus.GetInstance().Subscribe(DeviceId,delegate (IEvent @event,EventCallBackHandle callBack) + { + try + { + free = true; + Thread.Sleep(200); + byte[] data=command.ReturnsCommandData(K95CommandEnum.配方咖啡制作.GetString(),new RecipeModel().Packe(((KLMCoffee_MakeCoffeeEvent)@event).DrinkCode)); + commProxy.SendData(data); + Thread.Sleep(200); + free = false; + } + catch (Exception ex) + { + MessageLog.GetInstance.Show($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]"); + } + }); + + //伽乐美咖啡机取消制作咖啡 + EventBus.EventBus.GetInstance().Subscribe(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.Show($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]"); + } + }); + + //伽乐美咖啡机清洗冲泡器 + EventBus.EventBus.GetInstance().Subscribe(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.Show($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]"); + } + }); + + //伽乐美咖啡机放杯确认 + EventBus.EventBus.GetInstance().Subscribe(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.Show($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]"); + } + }); + + + //伽乐美咖啡机清洗奶沫器 + EventBus.EventBus.GetInstance().Subscribe(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.Show($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]"); + } + }); + + //伽乐美咖啡机清洗奶沫器确认 + EventBus.EventBus.GetInstance().Subscribe(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.Show($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]"); + } + }); } diff --git a/BPASmartClient.KLMCoffee/Protocal/FaultMessage.cs b/BPASmartClient.KLMCoffee/Protocal/FaultMessage.cs index 2fb9f2d6..8f4152b9 100644 --- a/BPASmartClient.KLMCoffee/Protocal/FaultMessage.cs +++ b/BPASmartClient.KLMCoffee/Protocal/FaultMessage.cs @@ -92,5 +92,17 @@ namespace BPASmartClient.KLMCoffee.Protocal ERROR6 = GetBitValue(valueH,6); 牛奶已用尽 = GetBitValue(valueH,7); } + + public bool IsFault() + { + foreach (System.Reflection.PropertyInfo info in this.GetType().GetProperties()) + { + if ((bool)info.GetValue(this) == true) + { + return true; + } + } + return false; + } } } diff --git a/BPASmartClient.KLMCoffee/Protocal/K95Command.cs b/BPASmartClient.KLMCoffee/Protocal/K95Command.cs index b3ed5059..888d21c5 100644 --- a/BPASmartClient.KLMCoffee/Protocal/K95Command.cs +++ b/BPASmartClient.KLMCoffee/Protocal/K95Command.cs @@ -1,4 +1,5 @@ -using System; +using BPASmartClient.Model.咖啡机.Enum; +using System; using System.Collections.Generic; using System.Linq; using System.Reflection; @@ -84,7 +85,7 @@ namespace BPASmartClient.KLMCoffee.Protocal /// /// 返回放杯确认 /// - public byte[] ReturnsIsOK() + public byte[] ReturnsCupIsOK() { try { @@ -127,7 +128,7 @@ namespace BPASmartClient.KLMCoffee.Protocal } /// - /// 清洗奶沫器 + /// 清洗奶沫器确认 /// public byte[] ReturnsWashNMJIsOK() { @@ -164,16 +165,15 @@ namespace BPASmartClient.KLMCoffee.Protocal /// :010510000000000000000000F3A\r\n /// /// - public void StateResolution(string message) + public SystemStatusModel StateResolution(string message) { try { - SystemStatusModel systemStatus = new SystemStatusModel(message); + return new SystemStatusModel(message); } catch (Exception) { - - throw; + return null; } } #endregion diff --git a/BPASmartClient.KLMCoffee/Protocal/K95commandEnumExtensions.cs b/BPASmartClient.KLMCoffee/Protocal/K95commandEnumExtensions.cs index 5c8caa29..ddca4769 100644 --- a/BPASmartClient.KLMCoffee/Protocal/K95commandEnumExtensions.cs +++ b/BPASmartClient.KLMCoffee/Protocal/K95commandEnumExtensions.cs @@ -1,4 +1,5 @@ -using System; +using BPASmartClient.Model.咖啡机.Enum; +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/BPASmartClient.KLMCoffee/Protocal/RecipeModel.cs b/BPASmartClient.KLMCoffee/Protocal/RecipeModel.cs index a947d627..5665fd69 100644 --- a/BPASmartClient.KLMCoffee/Protocal/RecipeModel.cs +++ b/BPASmartClient.KLMCoffee/Protocal/RecipeModel.cs @@ -1,4 +1,5 @@ -using System; +using BPASmartClient.Model.咖啡机.Enum; +using System; using System.Collections.Generic; using System.Linq; using System.Text; diff --git a/BPASmartClient.KLMCoffee/Protocal/SystemStatusModel.cs b/BPASmartClient.KLMCoffee/Protocal/SystemStatusModel.cs index 49ff81bf..8a44c5de 100644 --- a/BPASmartClient.KLMCoffee/Protocal/SystemStatusModel.cs +++ b/BPASmartClient.KLMCoffee/Protocal/SystemStatusModel.cs @@ -1,4 +1,5 @@ -using System; +using BPASmartClient.Model.咖啡机.Enum; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -46,45 +47,61 @@ namespace BPASmartClient.KLMCoffee.Protocal public SystemStatusModel(string cmd) { // 去掉字符串首尾处的空格 - if (string.IsNullOrEmpty(cmd) || cmd.Length < 21) + if (string.IsNullOrEmpty(cmd) || cmd.Length < 32) return; - string str = cmd.Trim().Substring(5,cmd.Trim().Length - 7).Replace(@"\R","").Replace("\r","").Replace(@"\n","").Replace(@"\N",""); - string systemStatus = str.Substring(0,4);//系统状态 - string makeSta = str.Substring(4,4);//制作状态 - string makeProgress = str.Substring(8,4);//制作进度 - string makeProgress1 = str.Substring(12,4);//制作进度 - byte ErrorH = byte.Parse(str.Substring(16,2));//故障信息 - byte ErrorL = byte.Parse(str.Substring(18,2));//故障信息 - byte InFormation = byte.Parse(str.Substring(20,1));//保养信息 - - foreach (var item in Enum.GetNames(typeof(K95SysTemStatus))) + string str = cmd.Trim().Replace(@"\","").Replace("r","").Replace(@"R","").Replace(@"n","").Replace(@"N",""); + int length = str.Length; + str = str.Substring(5,length - 7); + try { - if (systemStatus.Substring(0,1) == ((K95SysTemStatus)Enum.Parse(typeof(K95SysTemStatus),item)).GetString()) - temStatus = ((K95SysTemStatus)Enum.Parse(typeof(K95SysTemStatus),item)); - } + string systemStatus = str.Substring(0,4);//系统状态 + string makeSta = str.Substring(4,4);//制作状态 + string makeProgress = str.Substring(8,4);//制作进度 + string makeProgress1 = str.Substring(12,4);//制作进度 + byte ErrorH = (byte)Convert.ToInt32(str.Substring(16,2),16);//故障信息 + byte ErrorL = (byte)Convert.ToInt32(str.Substring(18,2),16);//故障信息 + byte InFormation = (byte)Convert.ToInt32(str.Substring(20,1),16);//保养信息 - foreach (var item in Enum.GetNames(typeof(DrinkType))) - { - if ("0" + makeSta.Substring(0,1) == ((DrinkType)Enum.Parse(typeof(DrinkType),item)).GetString()) - drinkType = ((DrinkType)Enum.Parse(typeof(DrinkType),item)); - } + System.Array values = System.Enum.GetValues(typeof(K95SysTemStatus)); + string[] nameList = System.Enum.GetNames(typeof(K95SysTemStatus)); - foreach (var item in Enum.GetNames(typeof(MakeStatus))) - { - if (makeSta.Substring(1,1) == ((MakeStatus)Enum.Parse(typeof(MakeStatus),item)).GetString()) - makeStatus = ((MakeStatus)Enum.Parse(typeof(MakeStatus),item)); - } + string[] sds = Enum.GetNames(typeof(K95SysTemStatus)); + + foreach (var item in Enum.GetNames(typeof(K95SysTemStatus))) + { + if (systemStatus.Substring(0,1) == ((K95SysTemStatus)Enum.Parse(typeof(K95SysTemStatus),item)).GetString()) + temStatus = ((K95SysTemStatus)Enum.Parse(typeof(K95SysTemStatus),item)); + } - foreach (var item in Enum.GetNames(typeof(TaskIndex))) + foreach (var item in Enum.GetNames(typeof(DrinkType))) + { + if ("0" + makeSta.Substring(0,1) == ((DrinkType)Enum.Parse(typeof(DrinkType),item)).GetString()) + drinkType = ((DrinkType)Enum.Parse(typeof(DrinkType),item)); + } + + foreach (var item in Enum.GetNames(typeof(MakeStatus))) + { + if (makeSta.Substring(1,1) == ((MakeStatus)Enum.Parse(typeof(MakeStatus),item)).GetString()) + makeStatus = ((MakeStatus)Enum.Parse(typeof(MakeStatus),item)); + } + + foreach (var item in Enum.GetNames(typeof(TaskIndex))) + { + if (makeSta.Substring(2,1) == ((TaskIndex)Enum.Parse(typeof(TaskIndex),item)).GetString()) + taskIndex = ((TaskIndex)Enum.Parse(typeof(TaskIndex),item)); + } + + if (makeProgress1 != "0000") + progress = Convert.ToInt32(makeProgress,16) / Convert.ToInt32(makeProgress1,16); + faultMessage = new FaultMessage(ErrorL,ErrorH); + upkeepMessage = new UpkeepMessage(InFormation); + } + catch (Exception ex) { - if (makeSta.Substring(2,1) == ((TaskIndex)Enum.Parse(typeof(TaskIndex),item)).GetString()) - taskIndex = ((TaskIndex)Enum.Parse(typeof(TaskIndex),item)); + } - if (makeProgress1 != "0000") - progress = Convert.ToInt32(makeProgress,16) / Convert.ToInt32(makeProgress1,16); - faultMessage = new FaultMessage(ErrorL,ErrorH); - upkeepMessage = new UpkeepMessage(InFormation); + } } } diff --git a/BPASmartClient.KLMCoffee/Protocal/UpkeepMessage.cs b/BPASmartClient.KLMCoffee/Protocal/UpkeepMessage.cs index cf1c757b..db20ef79 100644 --- a/BPASmartClient.KLMCoffee/Protocal/UpkeepMessage.cs +++ b/BPASmartClient.KLMCoffee/Protocal/UpkeepMessage.cs @@ -32,5 +32,17 @@ namespace BPASmartClient.KLMCoffee.Protocal 咖啡机需要除垢 = GetBitValue(value,1); 需要更换滤芯 = GetBitValue(value,0); } + + public bool IsUpkeep() + { + foreach (System.Reflection.PropertyInfo info in this.GetType().GetProperties()) + { + if ((bool)info.GetValue(this) == true) + { + return true; + } + } + return false; + } } } diff --git a/BPASmartClient.KLMCoffee/Protocal/DrinkType.cs b/BPASmartClient.Model/咖啡机/Enum/DrinkType.cs similarity index 88% rename from BPASmartClient.KLMCoffee/Protocal/DrinkType.cs rename to BPASmartClient.Model/咖啡机/Enum/DrinkType.cs index 7709540b..b8c5ce24 100644 --- a/BPASmartClient.KLMCoffee/Protocal/DrinkType.cs +++ b/BPASmartClient.Model/咖啡机/Enum/DrinkType.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace BPASmartClient.KLMCoffee.Protocal +namespace BPASmartClient.Model.咖啡机.Enum { /// /// 饮品类型 diff --git a/BPASmartClient.Model/咖啡机/KLMCoffeeEvent.cs b/BPASmartClient.Model/咖啡机/KLMCoffeeEvent.cs index 05dbf030..b2b05948 100644 --- a/BPASmartClient.Model/咖啡机/KLMCoffeeEvent.cs +++ b/BPASmartClient.Model/咖啡机/KLMCoffeeEvent.cs @@ -1,4 +1,5 @@ -using System; +using BPASmartClient.Model.咖啡机.Enum; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -17,7 +18,10 @@ namespace BPASmartClient.Model /// public class KLMCoffee_MakeCoffeeEvent :BaseEvent { - + /// + /// 饮品类型 + /// + public DrinkType DrinkCode { get; set; } } /// @@ -29,9 +33,33 @@ namespace BPASmartClient.Model } /// - /// 伽乐美咖啡机模式设置 + /// 伽乐美咖啡机清洗冲泡器 + /// + public class KLMCoffee_WashCPJEvent :BaseEvent + { + + } + + /// + /// 伽乐美咖啡机放杯确认 + /// + public class KLMCoffee_CupIsOKEvent :BaseEvent + { + + } + + /// + /// 伽乐美咖啡机清洗奶沫器 + /// + public class KLMCoffee_WashNMJEvent :BaseEvent + { + + } + + /// + /// 伽乐美咖啡机清洗奶沫器确认 /// - public class KLMCoffee_CoffeeCommEvent :BaseEvent + public class KLMCoffee_WashNMJIsOKEvent :BaseEvent { } diff --git a/BPASmartClient/DeviceInfo.xml b/BPASmartClient/DeviceInfo.xml index 22ff6ea2..fb2772b6 100644 --- a/BPASmartClient/DeviceInfo.xml +++ b/BPASmartClient/DeviceInfo.xml @@ -21,6 +21,13 @@ 9600 + + + COM5 + 9600 + + +