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.
 
 

230 lines
7.3 KiB

  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. }