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

236 lines
8.3 KiB

  1. using BPASmartClient.DRCoffee;
  2. using BPASmartClient.EventBus;
  3. using BPASmartClient.Helper;
  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.Threading;
  12. using static BPASmartClient.EventBus.EventBus;
  13. namespace BPASmartClient.DRCoffee
  14. {
  15. /// <summary>
  16. /// 咖啡机
  17. /// </summary>
  18. public class CoffeeMachine :BasePeripheral
  19. {
  20. //通讯代理
  21. SerialPortClient commProxy = null;
  22. //数据仓库
  23. private DataStorage<byte> dataStorage = new DataStorage<byte>();
  24. //是否下发指令,主线程等待
  25. private bool free = false;
  26. //状态询问指令
  27. private byte[] cmdAsk;
  28. //Dr咖啡机基础协议
  29. private DrCoffeePackage drinksOrder = new DrCoffeePackage();
  30. //串口COM口
  31. public string PortName { get; set; }
  32. //串口波特率
  33. public string BaudRate { get; set; }
  34. //心跳时间
  35. private DateTime lastRefreshTime = DateTime.MinValue;
  36. /// <summary>
  37. /// 是否在线
  38. /// </summary>
  39. public bool OnLine { get { return DateTime.Now.Subtract(lastRefreshTime).TotalSeconds <= 3; } }
  40. public CoffeeMachine()
  41. {
  42. DrCoffeePackage package = new DrCoffeePackage();
  43. package.CommCmd = DrCoffeeCommCmd.饮品制作指令;
  44. cmdAsk = DrCoffee.Packe(package);
  45. }
  46. /// <summary>
  47. /// 主线程开始运行
  48. /// </summary>
  49. public override void Start()
  50. {
  51. try
  52. {
  53. commProxy.Start();
  54. IsConnected = true;
  55. free = false;
  56. MainLoop();
  57. }
  58. catch (Exception ex)
  59. {
  60. MessageLog.GetInstance.ShowEx($"BPASmartClient.DRCoffee 中引发错误,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.ShowEx($"BPASmartClient.DRCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  77. }
  78. }
  79. /// <summary>
  80. /// 主循环,循环询问状态
  81. /// </summary>
  82. private void MainLoop()
  83. {
  84. ThreadManage.GetInstance().StartLong(new Action(() =>
  85. {
  86. if (!free)
  87. {
  88. commProxy.SendData(cmdAsk);
  89. }
  90. Thread.Sleep(200);
  91. }),"咖啡机询问线程");
  92. ThreadManage.GetInstance().StartLong(new Action(() =>
  93. {
  94. List<byte> temp = new List<byte>();
  95. //一系列解包
  96. while (dataStorage.GetSize() > 0)
  97. {
  98. byte item = dataStorage.GetData();
  99. if (DrCoffee.HEADER == item)
  100. {
  101. if (temp.Count == DrCoffee.LENGTH - 1)
  102. {
  103. temp.Add(item);
  104. var package = DrCoffee.UnPack(temp.ToArray());
  105. temp.Clear();
  106. ProcessPackage(package);
  107. }
  108. else
  109. {
  110. temp.Clear();
  111. temp.Add(item);
  112. }
  113. continue;
  114. }
  115. else
  116. {
  117. if (temp.Count == 1 && item != DrCoffee.LENGTH)
  118. {
  119. temp.Clear();
  120. continue;
  121. }
  122. temp.Add(item);
  123. }
  124. }
  125. Thread.Sleep(5);
  126. }),"咖啡机解析线程");
  127. }
  128. /// <summary>
  129. /// 咖啡机状态解析
  130. /// </summary>
  131. /// <param name="package"></param>
  132. public void ProcessPackage(DrCoffeePackage package)
  133. {
  134. if (((DrCoffeeStatus)status["CoffeeStatus"]) == DrCoffeeStatus.Running && package.Status != DrCoffeeStatus.Running)
  135. {
  136. status["CoffeeStatus"] = package.Status;
  137. lastRefreshTime = DateTime.Now;
  138. new DRCoffee_CoffeEndCookEvent() { Id = DeviceId }.Publish();
  139. }
  140. else status["CoffeeStatus"] = package.Status;
  141. status["AppStatus"] = package.ApplicationStatus;
  142. status["Warning"] = package.Warning;
  143. status["Fault"] = package.Fault;
  144. if ((DrCoffeeStatus)status["CoffeeStatus"] == DrCoffeeStatus.Warning
  145. || (DrCoffeeStatus)status["CoffeeStatus"] == DrCoffeeStatus.Fault
  146. || (DrCoffeeWarning)status["Warning"] != DrCoffeeWarning.无警告
  147. || (DrCoffeeFault)status["Fault"] != DrCoffeeFault.无故障
  148. )
  149. {
  150. IsWork = false;
  151. }
  152. else
  153. IsWork = true;
  154. }
  155. protected override void InitStatus()
  156. {
  157. status["CoffeeStatus"] = DrCoffeeStatus.Wait;
  158. status["AppStatus"] = DrCoffeeAppStatus.应用无状态;
  159. status["Warning"] = DrCoffeeWarning.无警告;
  160. status["Fault"] = DrCoffeeFault.无故障;
  161. }
  162. public override void Init()
  163. {
  164. commProxy = new SerialPortClient(PortName,(BaudRates)Enum.Parse(typeof(BaudRates),BaudRate));
  165. commProxy.SetDataStorage(dataStorage);
  166. //咖博士咖啡机制作
  167. EventBus.EventBus.GetInstance().Subscribe<DRCoffee_MakeCoffeeEvent>(DeviceId,delegate (IEvent @event,EventCallBackHandle callBack)
  168. {
  169. try
  170. {
  171. free = true;
  172. Thread.Sleep(200);
  173. drinksOrder.CommCmd = DrCoffeeCommCmd.饮品制作指令;
  174. drinksOrder.DrinksCode = ((DRCoffee_MakeCoffeeEvent)@event).DrinkCode;
  175. commProxy.SendData(DrCoffee.Packe(drinksOrder));
  176. Thread.Sleep(200);
  177. free = false;
  178. }
  179. catch (Exception ex)
  180. {
  181. MessageLog.GetInstance.ShowEx($"BPASmartClient.DRCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  182. }
  183. });
  184. //咖博士咖啡机取消制作咖啡
  185. EventBus.EventBus.GetInstance().Subscribe<DRCoffee_CancelMakeCoffeeEvent>(DeviceId,delegate (IEvent @event,EventCallBackHandle callBack)
  186. {
  187. try
  188. {
  189. free = true;
  190. Thread.Sleep(200);
  191. drinksOrder.CommCmd = DrCoffeeCommCmd.取消应用指令;
  192. drinksOrder.DrinksCode = 0;
  193. commProxy.SendData(DrCoffee.Packe(drinksOrder));
  194. Thread.Sleep(200);
  195. free = false;
  196. }
  197. catch (Exception ex)
  198. {
  199. MessageLog.GetInstance.ShowEx($"BPASmartClient.DRCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  200. }
  201. });
  202. //咖博士咖啡机模式设置
  203. EventBus.EventBus.GetInstance().Subscribe<DRCoffee_CoffeeCommCmdEvent>(DeviceId,delegate (IEvent @event,EventCallBackHandle callBack)
  204. {
  205. try
  206. {
  207. free = true;
  208. Thread.Sleep(200);
  209. drinksOrder.CommCmd = ((DRCoffee_CoffeeCommCmdEvent)@event).CommCmd;
  210. commProxy.SendData(DrCoffee.Packe(drinksOrder));
  211. Thread.Sleep(200);
  212. free = false;
  213. }
  214. catch (Exception ex)
  215. {
  216. MessageLog.GetInstance.ShowEx($"BPASmartClient.DRCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  217. }
  218. });
  219. InitStatus();
  220. }
  221. }
  222. }