终端一体化运控平台
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.
 
 
 

304 Zeilen
11 KiB

  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. 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.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.EventBus.GetInstance().Subscribe<KLMCoffee_MakeCoffeeEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  173. {
  174. try
  175. {
  176. free = true;
  177. Thread.Sleep(200);
  178. byte[] data = command.ReturnsCommandData(K95CommandEnum.配方咖啡制作.GetString(), new RecipeModel().Packe(((KLMCoffee_MakeCoffeeEvent)@event).DrinkCode));
  179. commProxy.SendData(data);
  180. Thread.Sleep(200);
  181. free = false;
  182. }
  183. catch (Exception ex)
  184. {
  185. MessageLog.GetInstance.ShowEx($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  186. }
  187. });
  188. //伽乐美咖啡机取消制作咖啡
  189. EventBus.EventBus.GetInstance().Subscribe<KLMCoffee_CancelMakeCoffeeEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  190. {
  191. try
  192. {
  193. free = true;
  194. Thread.Sleep(200);
  195. byte[] data = command.ReturnsCancelMake();
  196. commProxy.SendData(data);
  197. Thread.Sleep(200);
  198. free = false;
  199. }
  200. catch (Exception ex)
  201. {
  202. MessageLog.GetInstance.ShowEx($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  203. }
  204. });
  205. //伽乐美咖啡机清洗冲泡器
  206. EventBus.EventBus.GetInstance().Subscribe<KLMCoffee_WashCPJEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  207. {
  208. try
  209. {
  210. free = true;
  211. Thread.Sleep(200);
  212. byte[] data = command.ReturnsWashCPJ();
  213. commProxy.SendData(data);
  214. Thread.Sleep(200);
  215. free = false;
  216. }
  217. catch (Exception ex)
  218. {
  219. MessageLog.GetInstance.ShowEx($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  220. }
  221. });
  222. //伽乐美咖啡机放杯确认
  223. EventBus.EventBus.GetInstance().Subscribe<KLMCoffee_CupIsOKEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  224. {
  225. try
  226. {
  227. free = true;
  228. Thread.Sleep(200);
  229. byte[] data = command.ReturnsCupIsOK();
  230. commProxy.SendData(data);
  231. Thread.Sleep(200);
  232. free = false;
  233. }
  234. catch (Exception ex)
  235. {
  236. MessageLog.GetInstance.ShowEx($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  237. }
  238. });
  239. //伽乐美咖啡机清洗奶沫器
  240. EventBus.EventBus.GetInstance().Subscribe<KLMCoffee_WashNMJEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  241. {
  242. try
  243. {
  244. free = true;
  245. Thread.Sleep(200);
  246. byte[] data = command.ReturnsWashNMJ();
  247. commProxy.SendData(data);
  248. Thread.Sleep(200);
  249. free = false;
  250. }
  251. catch (Exception ex)
  252. {
  253. MessageLog.GetInstance.ShowEx($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  254. }
  255. });
  256. //伽乐美咖啡机清洗奶沫器确认
  257. EventBus.EventBus.GetInstance().Subscribe<KLMCoffee_WashNMJIsOKEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  258. {
  259. try
  260. {
  261. free = true;
  262. Thread.Sleep(200);
  263. byte[] data = command.ReturnsWashNMJIsOK();
  264. commProxy.SendData(data);
  265. Thread.Sleep(200);
  266. free = false;
  267. }
  268. catch (Exception ex)
  269. {
  270. MessageLog.GetInstance.ShowEx($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  271. }
  272. });
  273. InitStatus();
  274. Start();
  275. }
  276. public override void WriteData(string address, object value)
  277. {
  278. }
  279. }
  280. }