终端一体化运控平台
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

310 Zeilen
12 KiB

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