终端一体化运控平台
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

234 řádky
7.6 KiB

  1. using BPASmartClient.DRCoffee;
  2. using BPASmartClient.EventBus;
  3. using BPASmartClient.Helper;
  4. using BPASmartClient.Message;
  5. using BPASmartClient.Model;
  6. using BPASmartClient.Peripheral;
  7. using BPASmartClient.SerialPort;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Threading;
  11. using static BPASmartClient.EventBus.EventBus;
  12. namespace BPASmartClient.DRCoffee
  13. {
  14. /// <summary>
  15. /// 咖啡机
  16. /// </summary>
  17. public class CoffeeMachine: BasePeripheral
  18. {
  19. //通讯代理
  20. SerialPortClient commProxy = null;
  21. //数据仓库
  22. private DataStorage<byte> dataStorage = new DataStorage<byte>();
  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. /// <summary>
  70. /// Dr咖啡机基础协议
  71. /// </summary>
  72. private DrCoffeePackage drinksOrder = new DrCoffeePackage();
  73. /// <summary>
  74. /// 串口COM口
  75. /// </summary>
  76. public string PortName { get; set; }
  77. /// <summary>
  78. /// 串口波特率
  79. /// </summary>
  80. public string BaudRate { get; set; }
  81. public CoffeeMachine()
  82. {
  83. }
  84. /// <summary>
  85. /// 主线程开始运行
  86. /// </summary>
  87. public override void Start()
  88. {
  89. commProxy.Start();
  90. running = true;
  91. MainLoop();
  92. }
  93. /// <summary>
  94. /// 停止运行
  95. /// </summary>
  96. public override void Stop()
  97. {
  98. commProxy.Stop();
  99. running = false;
  100. }
  101. /// <summary>
  102. /// 主循环,循环询问状态
  103. /// </summary>
  104. private void MainLoop()
  105. {
  106. //ThreadManage.GetInstance.StartLong(new Action(() =>
  107. //{
  108. // if (free)
  109. // {
  110. // commProxy.SendData(commandHandler.GetStatusAsk());
  111. // SendCallback?.Invoke(BitConverter.ToString(commandHandler.GetStatusAsk()));
  112. // }
  113. // Thread.Sleep(200);
  114. //}),"咖啡机询问线程");
  115. ThreadManage.GetInstance.StartLong(new Action(() =>
  116. {
  117. List<byte> temp = new List<byte>();
  118. //一系列解包
  119. while (dataStorage.GetSize() > 0)
  120. {
  121. byte item = dataStorage.GetData();
  122. if (DrCoffee.HEADER == item)
  123. {
  124. if (temp.Count == DrCoffee.LENGTH - 1)
  125. {
  126. temp.Add(item);
  127. var package = DrCoffee.UnPack(temp.ToArray());
  128. ReciveCallback?.Invoke(BitConverter.ToString(temp.ToArray()));
  129. temp.Clear();
  130. MorkCStatus.GetInstance().ProcessPackage(package);
  131. }
  132. else
  133. {
  134. temp.Clear();
  135. temp.Add(item);
  136. }
  137. continue;
  138. }
  139. else
  140. {
  141. if (temp.Count == 1 && item != DrCoffee.LENGTH)
  142. {
  143. temp.Clear();
  144. continue;
  145. }
  146. temp.Add(item);
  147. }
  148. }
  149. Thread.Sleep(5);
  150. }), "咖啡机解析线程");
  151. }
  152. protected override void InitStatus()
  153. {
  154. }
  155. public override void Init()
  156. {
  157. commProxy = new SerialPortClient(PortName,(BaudRates)Enum.Parse(typeof(BaudRates),BaudRate));
  158. commProxy.SetDataStorage(dataStorage);
  159. //咖博士咖啡机制作
  160. EventBus.EventBus.GetInstance().Subscribe<DRCoffee_MakeCoffeeEvent>(DeviceId,delegate (IEvent @event,EventCallBackHandle callBack)
  161. {
  162. try
  163. {
  164. free = true;
  165. Thread.Sleep(200);
  166. drinksOrder.CommCmd = DrCoffeeCommCmd.饮品制作指令;
  167. //drinksOrder.DrinksCode = ((DRCoffee_MakeCoffeeEvent)@event).CommCmd;
  168. commProxy.SendData(DrCoffee.Packe(drinksOrder));
  169. Thread.Sleep(200);
  170. free = false;
  171. }
  172. catch (Exception ex)
  173. {
  174. MessageLog.GetInstance.Show($"BPASmartClient.DRCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  175. }
  176. });
  177. //咖博士咖啡机取消制作咖啡
  178. EventBus.EventBus.GetInstance().Subscribe<DRCoffee_CancelMakeCoffeeEvent>(DeviceId,delegate (IEvent @event,EventCallBackHandle callBack)
  179. {
  180. try
  181. {
  182. free = true;
  183. Thread.Sleep(200);
  184. drinksOrder.CommCmd = DrCoffeeCommCmd.取消应用指令;
  185. drinksOrder.DrinksCode = 0;
  186. commProxy.SendData(DrCoffee.Packe(drinksOrder));
  187. Thread.Sleep(200);
  188. free = false;
  189. }
  190. catch (Exception ex)
  191. {
  192. MessageLog.GetInstance.Show($"BPASmartClient.DRCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  193. }
  194. });
  195. //咖博士咖啡机模式设置
  196. EventBus.EventBus.GetInstance().Subscribe<DRCoffee_CoffeeCommCmdEvent>(DeviceId,delegate (IEvent @event,EventCallBackHandle callBack)
  197. {
  198. try
  199. {
  200. free = true;
  201. Thread.Sleep(200);
  202. //drinksOrder.CommCmd = (DrCoffeeCommCmd)int.Parse(e.obj_MessageObj.ToString());
  203. commProxy.SendData(DrCoffee.Packe(drinksOrder));
  204. Thread.Sleep(200);
  205. free = false;
  206. }
  207. catch (Exception ex)
  208. {
  209. MessageLog.GetInstance.Show($"BPASmartClient.DRCoffee 中引发错误,CoffeeCommCmdHandler 类,描述:[{ex.Message}]");
  210. }
  211. });
  212. }
  213. }
  214. }