终端一体化运控平台
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

CoffeeMachine.cs 9.2 KiB

2 år sedan
2 år sedan
2 år sedan
2 år sedan
2 år sedan
2 år sedan
2 år sedan
2 år sedan
2 år sedan
2 år sedan
2 år sedan
2 år sedan
2 år sedan
2 år sedan
2 år sedan
2 år sedan
2 år sedan
2 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. lastRefreshTime = DateTime.Now;
  138. IsConnected = OnLine;
  139. status["CoffeeIsConnected"] = OnLine;
  140. if (((DrCoffeeStatus)status["CoffeeStatus"]) == DrCoffeeStatus.Running && package.Status != DrCoffeeStatus.Running)
  141. {
  142. status["CoffeeStatus"] = package.Status;
  143. EventBus.EventBus.GetInstance().Publish(new DRCoffee_CoffeEndCookEvent() { DeviceId = DeviceId });
  144. }
  145. else status["CoffeeStatus"] = package.Status;
  146. status["CoffeeAppStatus"] = package.ApplicationStatus;
  147. status["CoffeeWarning"] = package.Warning;
  148. status["CoffeeFault"] = package.Fault;
  149. if ((DrCoffeeStatus)status["CoffeeStatus"] == DrCoffeeStatus.Warning
  150. || (DrCoffeeStatus)status["CoffeeStatus"] == DrCoffeeStatus.Fault
  151. || (DrCoffeeWarning)status["CoffeeWarning"] != DrCoffeeWarning.无警告
  152. || (DrCoffeeFault)status["CoffeeFault"] != DrCoffeeFault.无故障
  153. )
  154. {
  155. IsWork = false;
  156. }
  157. else
  158. IsWork = true;
  159. }
  160. protected override void InitStatus()
  161. {
  162. status["CoffeeStatus"] = DrCoffeeStatus.Wait;
  163. status["CoffeeAppStatus"] = DrCoffeeAppStatus.应用无状态;
  164. status["CoffeeWarning"] = DrCoffeeWarning.无警告;
  165. status["CoffeeFault"] = 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. MessageLog.GetInstance.Show($"咖啡机: 制作咖啡指令");
  184. }
  185. catch (Exception ex)
  186. {
  187. MessageLog.GetInstance.ShowEx($"BPASmartClient.DRCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  188. }
  189. });
  190. //咖博士咖啡机取消制作咖啡
  191. EventBus.EventBus.GetInstance().Subscribe<DRCoffee_CancelMakeCoffeeEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  192. {
  193. try
  194. {
  195. free = true;
  196. Thread.Sleep(200);
  197. drinksOrder.CommCmd = DrCoffeeCommCmd.取消应用指令;
  198. drinksOrder.DrinksCode = 0;
  199. commProxy.SendData(DrCoffee.Packe(drinksOrder));
  200. Thread.Sleep(200);
  201. free = false;
  202. MessageLog.GetInstance.Show($"咖啡机: 咖啡取消指令");
  203. }
  204. catch (Exception ex)
  205. {
  206. MessageLog.GetInstance.ShowEx($"BPASmartClient.DRCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  207. }
  208. });
  209. //咖博士咖啡机模式设置
  210. EventBus.EventBus.GetInstance().Subscribe<DRCoffee_CoffeeCommCmdEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  211. {
  212. try
  213. {
  214. free = true;
  215. Thread.Sleep(200);
  216. drinksOrder.CommCmd = ((DRCoffee_CoffeeCommCmdEvent)@event).CommCmd;
  217. commProxy.SendData(DrCoffee.Packe(drinksOrder));
  218. Thread.Sleep(200);
  219. free = false;
  220. MessageLog.GetInstance.Show($"咖啡机: 咖啡模式设置指令");
  221. }
  222. catch (Exception ex)
  223. {
  224. MessageLog.GetInstance.ShowEx($"BPASmartClient.DRCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  225. }
  226. });
  227. InitStatus();
  228. //测试
  229. Start();
  230. }
  231. public override void WriteData(string address, object value)
  232. {
  233. }
  234. //public override void ReadData(string address)
  235. //{
  236. //}
  237. }
  238. }