终端一体化运控平台
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 

292 rindas
10 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. //帧尾
  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["Status"] = systemStatus.temStatus;
  133. status["drinkType"] = systemStatus.drinkType;
  134. status["AppStatus"] = systemStatus.taskIndex;
  135. status["progress"] = systemStatus.progress;
  136. status["Warning"] = systemStatus.faultMessage.dataFault();
  137. status["Keep"] = 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.ShowEx($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  147. }
  148. }
  149. protected override void InitStatus()
  150. {
  151. status["Status"] = K95SysTemStatus.空闲状态;
  152. status["drinkType"] = DrinkType.意式;
  153. status["AppStatus"] = TaskIndex.无任务;
  154. status["progress"] = 0;
  155. status["Warning"] = new FaultMessage(0x00,0x00).dataFault();
  156. status["Keep"] = new UpkeepMessage(0x00).dataFault();
  157. }
  158. public override void Init()
  159. {
  160. commProxy = new SerialPortClient(PortName,(BaudRates)Enum.Parse(typeof(BaudRates),BaudRate));
  161. commProxy.SetDataStorage(dataStorage);
  162. //伽乐美咖啡机制作
  163. EventBus.EventBus.GetInstance().Subscribe<KLMCoffee_MakeCoffeeEvent>(DeviceId,delegate (IEvent @event,EventCallBackHandle callBack)
  164. {
  165. try
  166. {
  167. free = true;
  168. Thread.Sleep(200);
  169. byte[] data=command.ReturnsCommandData(K95CommandEnum.配方咖啡制作.GetString(),new RecipeModel().Packe(((KLMCoffee_MakeCoffeeEvent)@event).DrinkCode));
  170. commProxy.SendData(data);
  171. Thread.Sleep(200);
  172. free = false;
  173. }
  174. catch (Exception ex)
  175. {
  176. MessageLog.GetInstance.ShowEx($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  177. }
  178. });
  179. //伽乐美咖啡机取消制作咖啡
  180. EventBus.EventBus.GetInstance().Subscribe<KLMCoffee_CancelMakeCoffeeEvent>(DeviceId,delegate (IEvent @event,EventCallBackHandle callBack)
  181. {
  182. try
  183. {
  184. free = true;
  185. Thread.Sleep(200);
  186. byte[] data = command.ReturnsCancelMake();
  187. commProxy.SendData(data);
  188. Thread.Sleep(200);
  189. free = false;
  190. }
  191. catch (Exception ex)
  192. {
  193. MessageLog.GetInstance.ShowEx($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  194. }
  195. });
  196. //伽乐美咖啡机清洗冲泡器
  197. EventBus.EventBus.GetInstance().Subscribe<KLMCoffee_WashCPJEvent>(DeviceId,delegate (IEvent @event,EventCallBackHandle callBack)
  198. {
  199. try
  200. {
  201. free = true;
  202. Thread.Sleep(200);
  203. byte[] data = command.ReturnsWashCPJ();
  204. commProxy.SendData(data);
  205. Thread.Sleep(200);
  206. free = false;
  207. }
  208. catch (Exception ex)
  209. {
  210. MessageLog.GetInstance.ShowEx($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  211. }
  212. });
  213. //伽乐美咖啡机放杯确认
  214. EventBus.EventBus.GetInstance().Subscribe<KLMCoffee_CupIsOKEvent>(DeviceId,delegate (IEvent @event,EventCallBackHandle callBack)
  215. {
  216. try
  217. {
  218. free = true;
  219. Thread.Sleep(200);
  220. byte[] data = command.ReturnsCupIsOK();
  221. commProxy.SendData(data);
  222. Thread.Sleep(200);
  223. free = false;
  224. }
  225. catch (Exception ex)
  226. {
  227. MessageLog.GetInstance.ShowEx($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  228. }
  229. });
  230. //伽乐美咖啡机清洗奶沫器
  231. EventBus.EventBus.GetInstance().Subscribe<KLMCoffee_WashNMJEvent>(DeviceId,delegate (IEvent @event,EventCallBackHandle callBack)
  232. {
  233. try
  234. {
  235. free = true;
  236. Thread.Sleep(200);
  237. byte[] data = command.ReturnsWashNMJ();
  238. commProxy.SendData(data);
  239. Thread.Sleep(200);
  240. free = false;
  241. }
  242. catch (Exception ex)
  243. {
  244. MessageLog.GetInstance.ShowEx($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  245. }
  246. });
  247. //伽乐美咖啡机清洗奶沫器确认
  248. EventBus.EventBus.GetInstance().Subscribe<KLMCoffee_WashNMJIsOKEvent>(DeviceId,delegate (IEvent @event,EventCallBackHandle callBack)
  249. {
  250. try
  251. {
  252. free = true;
  253. Thread.Sleep(200);
  254. byte[] data = command.ReturnsWashNMJIsOK();
  255. commProxy.SendData(data);
  256. Thread.Sleep(200);
  257. free = false;
  258. }
  259. catch (Exception ex)
  260. {
  261. MessageLog.GetInstance.ShowEx($"BPASmartClient.KLMCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  262. }
  263. });
  264. InitStatus();
  265. }
  266. }
  267. }