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.
 
 

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