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

302 lines
12 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.Show($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  60. MessageLog.GetInstance.ShowEx($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  61. }
  62. }
  63. /// <summary>
  64. /// 停止运行
  65. /// </summary>
  66. public override void Stop()
  67. {
  68. try
  69. {
  70. commProxy.Stop();
  71. IsConnected = false;
  72. free = true;
  73. }
  74. catch (Exception ex)
  75. {
  76. MessageLog.GetInstance.Show($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  77. MessageLog.GetInstance.ShowEx($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  78. }
  79. }
  80. /// <summary>
  81. /// 主循环,循环询问状态
  82. /// </summary>
  83. private void MainLoop()
  84. {
  85. ThreadManage.GetInstance().StartLong(new Action(() =>
  86. {
  87. if (!free)
  88. {
  89. commProxy.SendData(cmdAsk);
  90. }
  91. Thread.Sleep(200);
  92. }),"咖啡机询问线程");
  93. ThreadManage.GetInstance().StartLong(new Action(() =>
  94. {
  95. ResolveMsg();
  96. }),"咖啡机解析线程");
  97. }
  98. private void ResolveMsg()
  99. {
  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 (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. //帧尾
  115. if (Encoding.ASCII.GetString(vs.ToArray()).ToLower() == "\\r\\n" || Encoding.ASCII.GetString(vs.ToArray()).ToLower() == "\r\n")
  116. {
  117. var package = Encoding.ASCII.GetString(temp.ToArray());
  118. ProcessMsg(package);
  119. }
  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["temStatus"] = systemStatus.temStatus;
  133. status["drinkType"] = systemStatus.drinkType;
  134. status["taskIndex"] = systemStatus.taskIndex;
  135. status["progress"] = systemStatus.progress;
  136. status["faultMessage"] = systemStatus.faultMessage;
  137. status["upkeepMessage"] = systemStatus.upkeepMessage;
  138. if (systemStatus.faultMessage.IsFault() || systemStatus.upkeepMessage.IsUpkeep())
  139. IsWork=false;
  140. else
  141. IsWork=true;
  142. }
  143. }
  144. catch (Exception ex)
  145. {
  146. MessageLog.GetInstance.Show($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  147. MessageLog.GetInstance.ShowEx($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  148. }
  149. }
  150. protected override void InitStatus()
  151. {
  152. status["temStatus"] = K95SysTemStatus.空闲状态;
  153. status["drinkType"] = DrinkType.意式;
  154. status["taskIndex"] = TaskIndex.无任务;
  155. status["progress"] = 0;
  156. status["faultMessage"] = new FaultMessage(0x00,0x00);
  157. status["upkeepMessage"] = new UpkeepMessage(0x00);
  158. }
  159. public override void Init()
  160. {
  161. commProxy = new SerialPortClient(PortName,(BaudRates)Enum.Parse(typeof(BaudRates),BaudRate));
  162. commProxy.SetDataStorage(dataStorage);
  163. //string sdas = ":010510000000000000000000F3A\r\n";
  164. //byte[] sdsd = new K95Command().ascii2Hex(sdas);
  165. //dataStorage.PutData(sdsd);
  166. //ResolveMsg();
  167. //伽乐美咖啡机制作
  168. EventBus.EventBus.GetInstance().Subscribe<KLMCoffee_MakeCoffeeEvent>(DeviceId,delegate (IEvent @event,EventCallBackHandle callBack)
  169. {
  170. try
  171. {
  172. free = true;
  173. Thread.Sleep(200);
  174. byte[] data=command.ReturnsCommandData(K95CommandEnum.配方咖啡制作.GetString(),new RecipeModel().Packe(((KLMCoffee_MakeCoffeeEvent)@event).DrinkCode));
  175. commProxy.SendData(data);
  176. Thread.Sleep(200);
  177. free = false;
  178. }
  179. catch (Exception ex)
  180. {
  181. MessageLog.GetInstance.Show($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  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.Show($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  200. MessageLog.GetInstance.ShowEx($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  201. }
  202. });
  203. //伽乐美咖啡机清洗冲泡器
  204. EventBus.EventBus.GetInstance().Subscribe<KLMCoffee_WashCPJEvent>(DeviceId,delegate (IEvent @event,EventCallBackHandle callBack)
  205. {
  206. try
  207. {
  208. free = true;
  209. Thread.Sleep(200);
  210. byte[] data = command.ReturnsWashCPJ();
  211. commProxy.SendData(data);
  212. Thread.Sleep(200);
  213. free = false;
  214. }
  215. catch (Exception ex)
  216. {
  217. MessageLog.GetInstance.Show($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  218. MessageLog.GetInstance.ShowEx($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  219. }
  220. });
  221. //伽乐美咖啡机放杯确认
  222. EventBus.EventBus.GetInstance().Subscribe<KLMCoffee_CupIsOKEvent>(DeviceId,delegate (IEvent @event,EventCallBackHandle callBack)
  223. {
  224. try
  225. {
  226. free = true;
  227. Thread.Sleep(200);
  228. byte[] data = command.ReturnsCupIsOK();
  229. commProxy.SendData(data);
  230. Thread.Sleep(200);
  231. free = false;
  232. }
  233. catch (Exception ex)
  234. {
  235. MessageLog.GetInstance.Show($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  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.Show($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  254. MessageLog.GetInstance.ShowEx($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  255. }
  256. });
  257. //伽乐美咖啡机清洗奶沫器确认
  258. EventBus.EventBus.GetInstance().Subscribe<KLMCoffee_WashNMJIsOKEvent>(DeviceId,delegate (IEvent @event,EventCallBackHandle callBack)
  259. {
  260. try
  261. {
  262. free = true;
  263. Thread.Sleep(200);
  264. byte[] data = command.ReturnsWashNMJIsOK();
  265. commProxy.SendData(data);
  266. Thread.Sleep(200);
  267. free = false;
  268. }
  269. catch (Exception ex)
  270. {
  271. MessageLog.GetInstance.Show($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  272. MessageLog.GetInstance.ShowEx($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  273. }
  274. });
  275. }
  276. }
  277. }