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.
 
 

229 lines
7.3 KiB

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