Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

CoffeeMachine.cs 7.3 KiB

il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using BPA.Utility;
  2. using HBLConsole.Communication;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using HBLConsole.Service;
  10. using HBLConsole.Model;
  11. namespace HBLDevice.Coffee
  12. {
  13. /// <summary>
  14. /// 咖啡机
  15. /// </summary>
  16. public class CoffeeMachine
  17. {
  18. //通讯代理
  19. SerialPortClient commProxy = null;
  20. //数据仓库
  21. private DataStorage<byte> dataStorage = new DataStorage<byte>();
  22. //指令组装
  23. private CommandHandler commandHandler = new CommandHandler();
  24. //主线程运行标识
  25. private bool running = false;
  26. //是否下发指令,主线程等待
  27. private bool free = true;
  28. private DrCoffeeStatus drCoffeeStatus;
  29. /// <summary>
  30. /// 咖啡机状态
  31. /// </summary>
  32. public DrCoffeeStatus CurrentCoffeeStatus
  33. {
  34. get { return drCoffeeStatus; }
  35. set
  36. {
  37. if (drCoffeeStatus != value)
  38. {
  39. drCoffeeStatus = value;
  40. CoffeeStatusChanged?.Invoke(value);
  41. }
  42. }
  43. }
  44. private DrCoffeeAppStatus coffeeAppStatus;
  45. /// <summary>
  46. /// 应用状态
  47. /// </summary>
  48. public DrCoffeeAppStatus CurrentCoffeeAppStatus
  49. {
  50. get { return coffeeAppStatus; }
  51. set
  52. {
  53. if (coffeeAppStatus != value)
  54. {
  55. coffeeAppStatus = value;
  56. CoffeeAppStatusChanged?.Invoke(value);
  57. }
  58. }
  59. }
  60. public Action<string> SendCallback;
  61. public Action<string> ReciveCallback;
  62. /// <summary>
  63. /// 咖啡机状态改变回调
  64. /// </summary>
  65. public Action<DrCoffeeStatus> CoffeeStatusChanged;
  66. /// <summary>
  67. /// 应用状态改变回调
  68. /// </summary>
  69. public Action<DrCoffeeAppStatus> CoffeeAppStatusChanged;
  70. public CoffeeMachine(string portName, BaudRates baud)
  71. {
  72. commProxy = new SerialPortClient(portName, baud);
  73. commProxy.SetDataStorage(dataStorage);
  74. commandHandler.Init(commProxy);
  75. commandHandler.PauseAsk = delegate (bool pause)
  76. {
  77. free = !pause;
  78. };
  79. }
  80. /// <summary>
  81. /// 主线程开始运行
  82. /// </summary>
  83. public void Start()
  84. {
  85. commProxy.Start();
  86. running = true;
  87. MainLoop();
  88. }
  89. /// <summary>
  90. /// 停止运行
  91. /// </summary>
  92. public void Stop()
  93. {
  94. commProxy.Stop();
  95. running = false;
  96. }
  97. ///// <summary>
  98. ///// 下单制作
  99. ///// </summary>
  100. ///// <param name="drinksCode">饮品代码</param>
  101. //public void PlaceOrder(DrCoffeeDrinksCode drinksCode)
  102. //{
  103. // free = false;
  104. // Thread.Sleep(200);
  105. // commProxy.SendData(commandHandler.GetDrinksOrder(drinksCode));
  106. // SendCallback?.Invoke(BitConverter.ToString(commandHandler.GetDrinksOrder(drinksCode)));
  107. // Thread.Sleep(200);
  108. // free = true;
  109. //}
  110. /// <summary>
  111. /// 主循环,循环询问状态
  112. /// </summary>
  113. private void MainLoop()
  114. {
  115. ThreadManage.GetInstance.StartLong(new Action(() =>
  116. {
  117. if (free)
  118. {
  119. commProxy.SendData(commandHandler.GetStatusAsk());
  120. SendCallback?.Invoke(BitConverter.ToString(commandHandler.GetStatusAsk()));
  121. }
  122. Thread.Sleep(200);
  123. }), "咖啡机询问线程");
  124. //Executer.GetInstance().Start(() =>
  125. //{
  126. // while (running)
  127. // {
  128. // if (free)
  129. // {
  130. // commProxy.SendData(commandHandler.GetStatusAsk());
  131. // SendCallback?.Invoke(BitConverter.ToString(commandHandler.GetStatusAsk()));
  132. // }
  133. // Thread.Sleep(200);
  134. // }
  135. //}, "咖啡机询问线程");
  136. ThreadManage.GetInstance.StartLong(new Action(() =>
  137. {
  138. List<byte> temp = new List<byte>();
  139. //一系列解包
  140. while (dataStorage.GetSize() > 0)
  141. {
  142. byte item = dataStorage.GetData();
  143. if (DrCoffee.HEADER == item)
  144. {
  145. if (temp.Count == DrCoffee.LENGTH - 1)
  146. {
  147. temp.Add(item);
  148. var package = DrCoffee.UnPack(temp.ToArray());
  149. ReciveCallback?.Invoke(BitConverter.ToString(temp.ToArray()));
  150. temp.Clear();
  151. MorkCStatus.GetInstance().ProcessPackage(package);
  152. }
  153. else
  154. {
  155. temp.Clear();
  156. temp.Add(item);
  157. }
  158. continue;
  159. }
  160. else
  161. {
  162. if (temp.Count == 1 && item != DrCoffee.LENGTH)
  163. {
  164. temp.Clear();
  165. continue;
  166. }
  167. temp.Add(item);
  168. }
  169. }
  170. Thread.Sleep(5);
  171. }), "咖啡机解析线程");
  172. //Executer.GetInstance().Start(() =>
  173. //{
  174. // while (running)
  175. // {
  176. // List<byte> temp = new List<byte>();
  177. // //一系列解包
  178. // while (dataStorage.GetSize() > 0)
  179. // {
  180. // byte item = dataStorage.GetData();
  181. // if (DrCoffee.HEADER == item)
  182. // {
  183. // if (temp.Count == DrCoffee.LENGTH - 1)
  184. // {
  185. // temp.Add(item);
  186. // var package = DrCoffee.UnPack(temp.ToArray());
  187. // ReciveCallback?.Invoke(BitConverter.ToString(temp.ToArray()));
  188. // temp.Clear();
  189. // MorkCStatus.GetInstance().ProcessPackage(package);
  190. // }
  191. // else
  192. // {
  193. // temp.Clear();
  194. // temp.Add(item);
  195. // }
  196. // continue;
  197. // }
  198. // else
  199. // {
  200. // if (temp.Count == 1 && item != DrCoffee.LENGTH)
  201. // {
  202. // temp.Clear();
  203. // continue;
  204. // }
  205. // temp.Add(item);
  206. // }
  207. // }
  208. // Thread.Sleep(5);
  209. // }
  210. //}, "咖啡机解析线程");
  211. }
  212. }
  213. }