终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CoffeeMachine.cs 12 KiB

1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
2 years ago
1 year ago
2 years ago
2 years ago
1 year ago
2 years ago
2 years ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. 
  2. using BPA.Helper;
  3. using BPASmartClient.KLMCoffee.Protocal;
  4. using BPASmartClient.Model;
  5. using BPASmartClient.Model.咖啡机.Enum;
  6. using BPASmartClient.Peripheral;
  7. using BPASmartClient.SerialPort;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. using static BPA.Helper.EventBus;
  15. namespace BPASmartClient.KLMCoffee
  16. {
  17. /// <summary>
  18. /// 伽乐美咖啡机
  19. /// </summary>
  20. public class CoffeeMachine : BasePeripheral
  21. {
  22. //通讯代理
  23. SerialPortClient commProxy = null;
  24. //数据仓库
  25. private DataStorage<byte> dataStorage = new DataStorage<byte>();
  26. //是否下发指令,主线程等待
  27. private bool free = false;
  28. //状态询问指令
  29. private byte[] cmdAsk;
  30. //串口COM口
  31. public string PortName { get; set; }
  32. //串口波特率
  33. public string BaudRate { get; set; }
  34. //心跳时间
  35. private DateTime lastRefreshTime = DateTime.MinValue;
  36. //是否在线
  37. public bool OnLine { get { return DateTime.Now.Subtract(lastRefreshTime).TotalSeconds <= 3; } }
  38. //命令
  39. public K95Command command = new K95Command();
  40. public CoffeeMachine()
  41. {
  42. cmdAsk = new K95Command().ReturnsStatusInquire();
  43. }
  44. /// <summary>
  45. /// 主线程开始运行
  46. /// </summary>
  47. public override void Start()
  48. {
  49. try
  50. {
  51. commProxy.Start();
  52. IsConnected = true;
  53. free = false;
  54. MainLoop();
  55. }
  56. catch (Exception ex)
  57. {
  58. MessageLog.GetInstance.ShowEx($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  59. }
  60. }
  61. /// <summary>
  62. /// 停止运行
  63. /// </summary>
  64. public override void Stop()
  65. {
  66. try
  67. {
  68. commProxy.Stop();
  69. IsConnected = false;
  70. free = true;
  71. }
  72. catch (Exception ex)
  73. {
  74. MessageLog.GetInstance.ShowEx($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  75. }
  76. }
  77. /// <summary>
  78. /// 主循环,循环询问状态
  79. /// </summary>
  80. private void MainLoop()
  81. {
  82. TaskManage.GetInstance.StartLong(new Action(() =>
  83. {
  84. if (!free)
  85. {
  86. commProxy.SendData(cmdAsk);
  87. }
  88. Thread.Sleep(200);
  89. }), "咖啡机询问线程");
  90. TaskManage.GetInstance.StartLong(new Action(() =>
  91. {
  92. ResolveMsg();
  93. }), "咖啡机解析线程");
  94. }
  95. private void ResolveMsg()
  96. {
  97. status["CoffeeIsConnected"] = OnLine;
  98. IsConnected = OnLine;
  99. List<byte> temp = new List<byte>();
  100. //一系列解包
  101. while (dataStorage.GetSize() > 0)
  102. {
  103. byte item = dataStorage.GetData();
  104. List<byte> data = new List<byte>() { item };
  105. if (Encoding.ASCII.GetString(data.ToArray()) == ":")
  106. {
  107. temp.Add(item);
  108. while (dataStorage.GetSize() < 32) { Thread.Sleep(5); }
  109. while (temp.Count < 32)
  110. {
  111. temp.Add(dataStorage.GetData());
  112. }
  113. List<byte> vs = new List<byte>() { temp[temp.Count - 4], temp[temp.Count - 3], temp[temp.Count - 2], temp[temp.Count - 1] };
  114. string t = Encoding.ASCII.GetString(vs.ToArray()).ToLower();
  115. //帧尾
  116. //if (Encoding.ASCII.GetString(vs.ToArray()).ToLower() == "\\r\\n" || Encoding.ASCII.GetString(vs.ToArray()).ToLower() == "\r\n")
  117. var package = Encoding.ASCII.GetString(temp.ToArray());
  118. if (package.Contains("\\r\\n"))
  119. {
  120. ProcessMsg(package);
  121. }
  122. temp.Clear();
  123. }
  124. continue;
  125. }
  126. Thread.Sleep(5);
  127. }
  128. public void ProcessMsg(string data)
  129. {
  130. try
  131. {
  132. lastRefreshTime = DateTime.Now;
  133. SystemStatusModel systemStatus = new K95Command().StateResolution(data);
  134. if (systemStatus != null)
  135. {
  136. status["CoffeeIsConnected"] = OnLine;
  137. if ((K95SysTemStatus)status["CoffeeStatus"] == K95SysTemStatus.正在制作咖啡 && systemStatus.temStatus != K95SysTemStatus.正在制作咖啡)
  138. {
  139. status["CoffeeStatus"] = systemStatus.temStatus;
  140. EventBus.GetInstance().Publish(new KLMCoffee_CoffeEndCookEvent { DeviceId = DeviceId });
  141. }
  142. else status["CoffeeStatus"] = systemStatus.temStatus;
  143. status["CoffeedrinkType"] = systemStatus.drinkType;
  144. status["CoffeeAppStatus"] = systemStatus.taskIndex;
  145. status["Coffeeprogress"] = systemStatus.progress;
  146. status["CoffeeWarning"] = systemStatus.faultMessage?.dataFault();
  147. status["CoffeeKeep"] = systemStatus.upkeepMessage;
  148. //if (systemStatus.faultMessage.IsFault() || systemStatus.upkeepMessage.IsUpkeep())
  149. // IsWork = false;
  150. //else
  151. // IsWork = true;
  152. }
  153. }
  154. catch (Exception ex)
  155. {
  156. MessageLog.GetInstance.ShowEx($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  157. }
  158. }
  159. protected override void InitStatus()
  160. {
  161. status["CoffeeStatus"] = K95SysTemStatus.空闲状态;
  162. status["CoffeedrinkType"] = DrinkType.意式咖啡;
  163. status["CoffeeAppStatus"] = TaskIndex.无任务;
  164. status["Coffeeprogress"] = 0;
  165. status["CoffeeWarning"] = new FaultMessage(0x00, 0x00).dataFault();
  166. status["CoffeeKeep"] = new UpkeepMessage(0x00).dataFault();
  167. }
  168. public override void Init()
  169. {
  170. commProxy = new SerialPortClient(communicationPar.SerialPort, (BaudRates)communicationPar.BaudRate);
  171. commProxy.SetDataStorage(dataStorage);
  172. //伽乐美咖啡机制作
  173. EventBus.GetInstance().Subscribe<KLMCoffee_MakeCoffeeEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  174. {
  175. try
  176. {
  177. free = true;
  178. Thread.Sleep(300);
  179. K95deFaultCoffeeEnum K95Code = (K95deFaultCoffeeEnum)((KLMCoffee_MakeCoffeeEvent)@event).KLMDrinkFaultCode;
  180. //byte[] data = command.ReturnsCommandData(K95CommandEnum.配方咖啡制作.GetString(), new RecipeModel().Packe(((KLMCoffee_MakeCoffeeEvent)@event).DrinkCode));
  181. byte[] data = command.ReturnsCommandData(K95CommandEnum.咖啡制作.GetString(), K95Code.GetString());
  182. commProxy.SendData(data);
  183. MessageLog.GetInstance.Show($"制作{K95Code}数据发送完成");
  184. Thread.Sleep(200);
  185. free = false;
  186. }
  187. catch (Exception ex)
  188. {
  189. MessageLog.GetInstance.ShowEx($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  190. }
  191. });
  192. //伽乐美咖啡机取消制作咖啡
  193. EventBus.GetInstance().Subscribe<KLMCoffee_CancelMakeCoffeeEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  194. {
  195. try
  196. {
  197. free = true;
  198. Thread.Sleep(200);
  199. byte[] data = command.ReturnsCancelMake();
  200. commProxy.SendData(data);
  201. Thread.Sleep(200);
  202. free = false;
  203. }
  204. catch (Exception ex)
  205. {
  206. MessageLog.GetInstance.ShowEx($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  207. }
  208. });
  209. //伽乐美咖啡机清洗冲泡器
  210. EventBus.GetInstance().Subscribe<KLMCoffee_WashCPJEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  211. {
  212. try
  213. {
  214. free = true;
  215. Thread.Sleep(200);
  216. byte[] data = command.ReturnsWashCPJ();
  217. commProxy.SendData(data);
  218. Thread.Sleep(200);
  219. free = false;
  220. }
  221. catch (Exception ex)
  222. {
  223. MessageLog.GetInstance.ShowEx($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  224. }
  225. });
  226. //伽乐美咖啡机放杯确认
  227. EventBus.GetInstance().Subscribe<KLMCoffee_CupIsOKEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  228. {
  229. try
  230. {
  231. free = true;
  232. Thread.Sleep(200);
  233. byte[] data = command.ReturnsCupIsOK();
  234. commProxy.SendData(data);
  235. Thread.Sleep(200);
  236. free = false;
  237. }
  238. catch (Exception ex)
  239. {
  240. MessageLog.GetInstance.ShowEx($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  241. }
  242. });
  243. //伽乐美咖啡机清洗奶沫器
  244. EventBus.GetInstance().Subscribe<KLMCoffee_WashNMJEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  245. {
  246. try
  247. {
  248. free = true;
  249. Thread.Sleep(200);
  250. byte[] data = command.ReturnsWashNMJ();
  251. commProxy.SendData(data);
  252. Thread.Sleep(200);
  253. free = false;
  254. }
  255. catch (Exception ex)
  256. {
  257. MessageLog.GetInstance.ShowEx($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  258. }
  259. });
  260. //伽乐美咖啡机清洗奶沫器确认
  261. EventBus.GetInstance().Subscribe<KLMCoffee_WashNMJIsOKEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  262. {
  263. try
  264. {
  265. free = true;
  266. Thread.Sleep(200);
  267. byte[] data = command.ReturnsWashNMJIsOK();
  268. commProxy.SendData(data);
  269. Thread.Sleep(200);
  270. free = false;
  271. }
  272. catch (Exception ex)
  273. {
  274. MessageLog.GetInstance.ShowEx($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  275. }
  276. });
  277. InitStatus();
  278. Start();
  279. }
  280. public override void WriteData(string address, object value)
  281. {
  282. }
  283. }
  284. }