终端一体化运控平台
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

CoffeeMachine.cs 11 KiB

před 2 roky
před 2 roky
před 2 roky
před 2 roky
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. List<byte> temp = new List<byte>();
  99. //一系列解包
  100. while (dataStorage.GetSize() > 0)
  101. {
  102. byte item = dataStorage.GetData();
  103. List<byte> data = new List<byte>() { item };
  104. if (Encoding.ASCII.GetString(data.ToArray()) == ":")
  105. {
  106. temp.Add(item);
  107. while (dataStorage.GetSize() < 32) { Thread.Sleep(5); }
  108. while (temp.Count < 32)
  109. {
  110. temp.Add(dataStorage.GetData());
  111. }
  112. List<byte> vs = new List<byte>() { temp[temp.Count - 4], temp[temp.Count - 3], temp[temp.Count - 2], temp[temp.Count - 1] };
  113. //帧尾
  114. if (Encoding.ASCII.GetString(vs.ToArray()).ToLower() == "\\r\\n" || Encoding.ASCII.GetString(vs.ToArray()).ToLower() == "\r\n")
  115. {
  116. var package = Encoding.ASCII.GetString(temp.ToArray());
  117. ProcessMsg(package);
  118. }
  119. temp.Clear();
  120. }
  121. continue;
  122. }
  123. Thread.Sleep(5);
  124. }
  125. public void ProcessMsg(string data)
  126. {
  127. try
  128. {
  129. SystemStatusModel systemStatus = new K95Command().StateResolution(data);
  130. if (systemStatus != null)
  131. {
  132. status["CoffeeIsconnected"] = OnLine;
  133. if((K95SysTemStatus)status["Status"] == K95SysTemStatus.正在制作咖啡&&systemStatus.temStatus != K95SysTemStatus.正在制作咖啡)
  134. {
  135. status["Status"] = systemStatus.temStatus;
  136. EventBus.EventBus.GetInstance().Publish(new KLMCoffee_CoffeEndCookEvent { DeviceId = DeviceId });
  137. }
  138. else status["CoffeeStatus"] = systemStatus.temStatus;
  139. status["CoffeedrinkType"] = systemStatus.drinkType;
  140. status["CoffeeAppStatus"] = systemStatus.taskIndex;
  141. status["Coffeeprogress"] = systemStatus.progress;
  142. status["CoffeeWarning"] = systemStatus.faultMessage.dataFault();
  143. status["CoffeeKeep"] = systemStatus.upkeepMessage;
  144. if (systemStatus.faultMessage.IsFault() || systemStatus.upkeepMessage.IsUpkeep())
  145. IsWork = false;
  146. else
  147. IsWork = true;
  148. }
  149. }
  150. catch (Exception ex)
  151. {
  152. MessageLog.GetInstance.ShowEx($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  153. }
  154. }
  155. protected override void InitStatus()
  156. {
  157. status["CoffeeStatus"] = K95SysTemStatus.空闲状态;
  158. status["CoffeedrinkType"] = DrinkType.意式;
  159. status["CoffeeAppStatus"] = TaskIndex.无任务;
  160. status["Coffeeprogress"] = 0;
  161. status["CoffeeWarning"] = new FaultMessage(0x00, 0x00).dataFault();
  162. status["CoffeeKeep"] = new UpkeepMessage(0x00).dataFault();
  163. }
  164. public override void Init()
  165. {
  166. commProxy = new SerialPortClient(communicationPar.SerialPort, (BaudRates)communicationPar.BaudRate);
  167. commProxy.SetDataStorage(dataStorage);
  168. //伽乐美咖啡机制作
  169. EventBus.EventBus.GetInstance().Subscribe<KLMCoffee_MakeCoffeeEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  170. {
  171. try
  172. {
  173. free = true;
  174. Thread.Sleep(200);
  175. byte[] data = command.ReturnsCommandData(K95CommandEnum.配方咖啡制作.GetString(), new RecipeModel().Packe(((KLMCoffee_MakeCoffeeEvent)@event).DrinkCode));
  176. commProxy.SendData(data);
  177. Thread.Sleep(200);
  178. free = false;
  179. }
  180. catch (Exception ex)
  181. {
  182. MessageLog.GetInstance.ShowEx($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  183. }
  184. });
  185. //伽乐美咖啡机取消制作咖啡
  186. EventBus.EventBus.GetInstance().Subscribe<KLMCoffee_CancelMakeCoffeeEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  187. {
  188. try
  189. {
  190. free = true;
  191. Thread.Sleep(200);
  192. byte[] data = command.ReturnsCancelMake();
  193. commProxy.SendData(data);
  194. Thread.Sleep(200);
  195. free = false;
  196. }
  197. catch (Exception ex)
  198. {
  199. MessageLog.GetInstance.ShowEx($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  200. }
  201. });
  202. //伽乐美咖啡机清洗冲泡器
  203. EventBus.EventBus.GetInstance().Subscribe<KLMCoffee_WashCPJEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  204. {
  205. try
  206. {
  207. free = true;
  208. Thread.Sleep(200);
  209. byte[] data = command.ReturnsWashCPJ();
  210. commProxy.SendData(data);
  211. Thread.Sleep(200);
  212. free = false;
  213. }
  214. catch (Exception ex)
  215. {
  216. MessageLog.GetInstance.ShowEx($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  217. }
  218. });
  219. //伽乐美咖啡机放杯确认
  220. EventBus.EventBus.GetInstance().Subscribe<KLMCoffee_CupIsOKEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  221. {
  222. try
  223. {
  224. free = true;
  225. Thread.Sleep(200);
  226. byte[] data = command.ReturnsCupIsOK();
  227. commProxy.SendData(data);
  228. Thread.Sleep(200);
  229. free = false;
  230. }
  231. catch (Exception ex)
  232. {
  233. MessageLog.GetInstance.ShowEx($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  234. }
  235. });
  236. //伽乐美咖啡机清洗奶沫器
  237. EventBus.EventBus.GetInstance().Subscribe<KLMCoffee_WashNMJEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  238. {
  239. try
  240. {
  241. free = true;
  242. Thread.Sleep(200);
  243. byte[] data = command.ReturnsWashNMJ();
  244. commProxy.SendData(data);
  245. Thread.Sleep(200);
  246. free = false;
  247. }
  248. catch (Exception ex)
  249. {
  250. MessageLog.GetInstance.ShowEx($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  251. }
  252. });
  253. //伽乐美咖啡机清洗奶沫器确认
  254. EventBus.EventBus.GetInstance().Subscribe<KLMCoffee_WashNMJIsOKEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  255. {
  256. try
  257. {
  258. free = true;
  259. Thread.Sleep(200);
  260. byte[] data = command.ReturnsWashNMJIsOK();
  261. commProxy.SendData(data);
  262. Thread.Sleep(200);
  263. free = false;
  264. }
  265. catch (Exception ex)
  266. {
  267. MessageLog.GetInstance.ShowEx($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  268. }
  269. });
  270. InitStatus();
  271. }
  272. public override void WriteData(string address, object value)
  273. {
  274. }
  275. }
  276. }