终端一体化运控平台
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

CoffeeMachine.cs 12 KiB

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