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

246 lines
8.6 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. //private volatile static CoffeeMachine _Instance;
  41. //public static CoffeeMachine GetInstance => _Instance ?? (_Instance = new CoffeeMachine());
  42. public CoffeeMachine()
  43. {
  44. DrCoffeePackage package = new DrCoffeePackage();
  45. package.CommCmd = DrCoffeeCommCmd.饮品制作指令;
  46. cmdAsk = DrCoffee.Packe(package);
  47. }
  48. /// <summary>
  49. /// 主线程开始运行
  50. /// </summary>
  51. public override void Start()
  52. {
  53. try
  54. {
  55. commProxy.Start();
  56. IsConnected = true;
  57. free = false;
  58. MainLoop();
  59. }
  60. catch (Exception ex)
  61. {
  62. MessageLog.GetInstance.ShowEx($"BPASmartClient.DRCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  63. }
  64. }
  65. /// <summary>
  66. /// 停止运行
  67. /// </summary>
  68. public override void Stop()
  69. {
  70. try
  71. {
  72. commProxy.Stop();
  73. IsConnected = false;
  74. free = true;
  75. }
  76. catch (Exception ex)
  77. {
  78. MessageLog.GetInstance.ShowEx($"BPASmartClient.DRCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  79. }
  80. }
  81. /// <summary>
  82. /// 主循环,循环询问状态
  83. /// </summary>
  84. private void MainLoop()
  85. {
  86. ThreadManage.GetInstance().StartLong(new Action(() =>
  87. {
  88. if (!free)
  89. {
  90. commProxy.SendData(cmdAsk);
  91. }
  92. Thread.Sleep(200);
  93. }),"咖啡机询问线程");
  94. ThreadManage.GetInstance().StartLong(new Action(() =>
  95. {
  96. List<byte> temp = new List<byte>();
  97. //一系列解包
  98. while (dataStorage.GetSize() > 0)
  99. {
  100. IsConnected = true;
  101. byte item = dataStorage.GetData();
  102. if (DrCoffee.HEADER == item)
  103. {
  104. if (temp.Count == DrCoffee.LENGTH - 1)
  105. {
  106. temp.Add(item);
  107. var package = DrCoffee.UnPack(temp.ToArray());
  108. temp.Clear();
  109. ProcessPackage(package);
  110. }
  111. else
  112. {
  113. temp.Clear();
  114. temp.Add(item);
  115. }
  116. continue;
  117. }
  118. else
  119. {
  120. if (temp.Count == 1 && item != DrCoffee.LENGTH)
  121. {
  122. temp.Clear();
  123. continue;
  124. }
  125. temp.Add(item);
  126. }
  127. }
  128. Thread.Sleep(5);
  129. }),"咖啡机解析线程");
  130. }
  131. /// <summary>
  132. /// 咖啡机状态解析
  133. /// </summary>
  134. /// <param name="package"></param>
  135. public void ProcessPackage(DrCoffeePackage package)
  136. {
  137. if (((DrCoffeeStatus)status["Status"]) == DrCoffeeStatus.Running && package.Status != DrCoffeeStatus.Running)
  138. {
  139. status["Status"] = package.Status;
  140. lastRefreshTime = DateTime.Now;
  141. IsConnected = OnLine;
  142. status["CoffeConnected"] = OnLine;
  143. new DRCoffee_CoffeEndCookEvent() { DeviceId = DeviceId }.Publish();
  144. }
  145. else status["Status"] = package.Status;
  146. status["AppStatus"] = package.ApplicationStatus;
  147. status["Warning"] = package.Warning;
  148. status["Fault"] = package.Fault;
  149. if ((DrCoffeeStatus)status["Status"] == DrCoffeeStatus.Warning
  150. || (DrCoffeeStatus)status["Status"] == DrCoffeeStatus.Fault
  151. || (DrCoffeeWarning)status["Warning"] != DrCoffeeWarning.无警告
  152. || (DrCoffeeFault)status["Fault"] != DrCoffeeFault.无故障
  153. )
  154. {
  155. IsWork = false;
  156. }
  157. else
  158. IsWork = true;
  159. }
  160. protected override void InitStatus()
  161. {
  162. status["Status"] = DrCoffeeStatus.Wait;
  163. status["AppStatus"] = DrCoffeeAppStatus.应用无状态;
  164. status["Warning"] = DrCoffeeWarning.无警告;
  165. status["Fault"] = DrCoffeeFault.无故障;
  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<DRCoffee_MakeCoffeeEvent>(DeviceId,delegate (IEvent @event,EventCallBackHandle callBack)
  173. {
  174. try
  175. {
  176. free = true;
  177. Thread.Sleep(200);
  178. drinksOrder.CommCmd = DrCoffeeCommCmd.饮品制作指令;
  179. drinksOrder.DrinksCode = ((DRCoffee_MakeCoffeeEvent)@event).DrinkCode;
  180. commProxy.SendData(DrCoffee.Packe(drinksOrder));
  181. Thread.Sleep(200);
  182. free = false;
  183. }
  184. catch (Exception ex)
  185. {
  186. MessageLog.GetInstance.ShowEx($"BPASmartClient.DRCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  187. }
  188. });
  189. //咖博士咖啡机取消制作咖啡
  190. EventBus.EventBus.GetInstance().Subscribe<DRCoffee_CancelMakeCoffeeEvent>(DeviceId,delegate (IEvent @event,EventCallBackHandle callBack)
  191. {
  192. try
  193. {
  194. free = true;
  195. Thread.Sleep(200);
  196. drinksOrder.CommCmd = DrCoffeeCommCmd.取消应用指令;
  197. drinksOrder.DrinksCode = 0;
  198. commProxy.SendData(DrCoffee.Packe(drinksOrder));
  199. Thread.Sleep(200);
  200. free = false;
  201. }
  202. catch (Exception ex)
  203. {
  204. MessageLog.GetInstance.ShowEx($"BPASmartClient.DRCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  205. }
  206. });
  207. //咖博士咖啡机模式设置
  208. EventBus.EventBus.GetInstance().Subscribe<DRCoffee_CoffeeCommCmdEvent>(DeviceId,delegate (IEvent @event,EventCallBackHandle callBack)
  209. {
  210. try
  211. {
  212. free = true;
  213. Thread.Sleep(200);
  214. drinksOrder.CommCmd = ((DRCoffee_CoffeeCommCmdEvent)@event).CommCmd;
  215. commProxy.SendData(DrCoffee.Packe(drinksOrder));
  216. Thread.Sleep(200);
  217. free = false;
  218. }
  219. catch (Exception ex)
  220. {
  221. MessageLog.GetInstance.ShowEx($"BPASmartClient.DRCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  222. }
  223. });
  224. InitStatus();
  225. //测试
  226. Start();
  227. }
  228. }
  229. }