终端一体化运控平台
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

pirms 1 gada
pirms 1 gada
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 1 gada
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
pirms 1 gada
pirms 1 gada
pirms 2 gadiem
pirms 1 gada
pirms 2 gadiem
pirms 2 gadiem
pirms 2 gadiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. using BPASmartClient.DRCoffee;
  2. using BPA.Helper;
  3. using BPASmartClient.Model;
  4. using BPASmartClient.Model.咖啡机.Enum;
  5. using BPASmartClient.Peripheral;
  6. using BPASmartClient.SerialPort;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Threading;
  10. using static BPA.Helper.EventBus;
  11. namespace BPASmartClient.DRCoffee
  12. {
  13. /// <summary>
  14. /// 咖啡机
  15. /// </summary>
  16. public class CoffeeMachine : BasePeripheral
  17. {
  18. //通讯代理
  19. SerialPortClient commProxy = null;
  20. //数据仓库
  21. private DataStorage<byte> dataStorage = new DataStorage<byte>();
  22. //是否下发指令,主线程等待
  23. private bool free = false;
  24. //状态询问指令
  25. private byte[] cmdAsk;
  26. //Dr咖啡机基础协议
  27. private DrCoffeePackage drinksOrder = new DrCoffeePackage();
  28. //串口COM口
  29. public string PortName { get; set; }
  30. //串口波特率
  31. public string BaudRate { get; set; }
  32. //心跳时间
  33. private DateTime lastRefreshTime = DateTime.MinValue;
  34. /// <summary>
  35. /// 是否在线
  36. /// </summary>
  37. public bool OnLine { get { return DateTime.Now.Subtract(lastRefreshTime).TotalSeconds <= 3; } }
  38. //private volatile static CoffeeMachine _Instance;
  39. //public static CoffeeMachine GetInstance => _Instance ?? (_Instance = new CoffeeMachine());
  40. public CoffeeMachine()
  41. {
  42. DrCoffeePackage package = new DrCoffeePackage();
  43. package.CommCmd = DrCoffeeCommCmd.饮品制作指令;
  44. cmdAsk = DrCoffee.Packe(package);
  45. }
  46. /// <summary>
  47. /// 主线程开始运行
  48. /// </summary>
  49. public override void Start()
  50. {
  51. try
  52. {
  53. commProxy.Start();
  54. IsConnected = true;
  55. free = false;
  56. MainLoop();
  57. }
  58. catch (Exception ex)
  59. {
  60. MessageLog.GetInstance.ShowEx($"BPASmartClient.DRCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  61. }
  62. }
  63. /// <summary>
  64. /// 停止运行
  65. /// </summary>
  66. public override void Stop()
  67. {
  68. try
  69. {
  70. commProxy.Stop();
  71. IsConnected = false;
  72. free = true;
  73. }
  74. catch (Exception ex)
  75. {
  76. MessageLog.GetInstance.ShowEx($"BPASmartClient.DRCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  77. }
  78. }
  79. /// <summary>
  80. /// 主循环,循环询问状态
  81. /// </summary>
  82. private void MainLoop()
  83. {
  84. TaskManage.GetInstance.StartLong(new Action(() =>
  85. {
  86. if (!free)
  87. {
  88. commProxy.SendData(cmdAsk);
  89. }
  90. Thread.Sleep(200);
  91. }), "咖啡机询问线程");
  92. TaskManage.GetInstance.StartLong(new Action(() =>
  93. {
  94. List<byte> temp = new List<byte>();
  95. //一系列解包
  96. while (dataStorage.GetSize() > 0)
  97. {
  98. IsConnected = true;
  99. byte item = dataStorage.GetData();
  100. if (DrCoffee.HEADER == item)
  101. {
  102. if (temp.Count == DrCoffee.LENGTH - 1)
  103. {
  104. temp.Add(item);
  105. var package = DrCoffee.UnPack(temp.ToArray());
  106. temp.Clear();
  107. ProcessPackage(package);
  108. }
  109. else
  110. {
  111. temp.Clear();
  112. temp.Add(item);
  113. }
  114. continue;
  115. }
  116. else
  117. {
  118. if (temp.Count == 1 && item != DrCoffee.LENGTH)
  119. {
  120. temp.Clear();
  121. continue;
  122. }
  123. temp.Add(item);
  124. }
  125. }
  126. Thread.Sleep(5);
  127. }), "咖啡机解析线程");
  128. }
  129. /// <summary>
  130. /// 咖啡机状态解析
  131. /// </summary>
  132. /// <param name="package"></param>
  133. public void ProcessPackage(DrCoffeePackage package)
  134. {
  135. lastRefreshTime = DateTime.Now;
  136. IsConnected = OnLine;
  137. status["CoffeeIsConnected"] = OnLine;
  138. if (((DrCoffeeStatus)status["CoffeeStatus"]) == DrCoffeeStatus.Running && package.Status != DrCoffeeStatus.Running)
  139. {
  140. status["CoffeeStatus"] = package.Status;
  141. EventBus.GetInstance().Publish(new DRCoffee_CoffeEndCookEvent() { DeviceId = DeviceId });
  142. }
  143. else status["CoffeeStatus"] = package.Status;
  144. status["CoffeeAppStatus"] = package.ApplicationStatus;
  145. status["CoffeeWarning"] = package.Warning;
  146. status["CoffeeFault"] = package.Fault;
  147. if ((DrCoffeeStatus)status["CoffeeStatus"] == DrCoffeeStatus.Warning
  148. || (DrCoffeeStatus)status["CoffeeStatus"] == DrCoffeeStatus.Fault
  149. || (DrCoffeeWarning)status["CoffeeWarning"] != DrCoffeeWarning.无警告
  150. || (DrCoffeeFault)status["CoffeeFault"] != DrCoffeeFault.无故障
  151. )
  152. {
  153. IsWork = false;
  154. }
  155. else
  156. IsWork = true;
  157. }
  158. protected override void InitStatus()
  159. {
  160. status["CoffeeStatus"] = DrCoffeeStatus.Wait;
  161. status["CoffeeAppStatus"] = DrCoffeeAppStatus.应用无状态;
  162. status["CoffeeWarning"] = DrCoffeeWarning.无警告;
  163. status["CoffeeFault"] = DrCoffeeFault.无故障;
  164. }
  165. public override void Init()
  166. {
  167. commProxy = new SerialPortClient(communicationPar.SerialPort, (BaudRates)communicationPar.BaudRate);
  168. commProxy.SetDataStorage(dataStorage);
  169. //咖博士咖啡机制作
  170. EventBus.GetInstance().Subscribe<DRCoffee_MakeCoffeeEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  171. {
  172. try
  173. {
  174. free = true;
  175. Thread.Sleep(200);
  176. drinksOrder.CommCmd = DrCoffeeCommCmd.饮品制作指令;
  177. drinksOrder.DrinksCode = ((DRCoffee_MakeCoffeeEvent)@event).DrinkCode;
  178. commProxy.SendData(DrCoffee.Packe(drinksOrder));
  179. Thread.Sleep(200);
  180. free = false;
  181. MessageLog.GetInstance.Show($"咖啡机: 制作咖啡指令");
  182. }
  183. catch (Exception ex)
  184. {
  185. MessageLog.GetInstance.ShowEx($"BPASmartClient.DRCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  186. }
  187. });
  188. //咖博士咖啡机取消制作咖啡
  189. EventBus.GetInstance().Subscribe<DRCoffee_CancelMakeCoffeeEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  190. {
  191. try
  192. {
  193. free = true;
  194. Thread.Sleep(200);
  195. drinksOrder.CommCmd = DrCoffeeCommCmd.取消应用指令;
  196. drinksOrder.DrinksCode = 0;
  197. commProxy.SendData(DrCoffee.Packe(drinksOrder));
  198. Thread.Sleep(200);
  199. free = false;
  200. MessageLog.GetInstance.Show($"咖啡机: 咖啡取消指令");
  201. }
  202. catch (Exception ex)
  203. {
  204. MessageLog.GetInstance.ShowEx($"BPASmartClient.DRCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  205. }
  206. });
  207. //咖博士咖啡机模式设置
  208. EventBus.GetInstance().Subscribe<DRCoffee_CoffeeCommCmdEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
  209. {
  210. try
  211. {
  212. free = true;
  213. Thread.Sleep(200);
  214. drinksOrder.CommCmd = ((DRCoffee_CoffeeCommCmdEvent)@event).CommCmd;
  215. commProxy.SendData(DrCoffee.Packe(drinksOrder));
  216. Thread.Sleep(200);
  217. free = false;
  218. MessageLog.GetInstance.Show($"咖啡机: 咖啡模式设置指令");
  219. }
  220. catch (Exception ex)
  221. {
  222. MessageLog.GetInstance.ShowEx($"BPASmartClient.DRCoffee 中引发错误,CoffeeMachine 类,描述:[{ex.Message}]");
  223. }
  224. });
  225. InitStatus();
  226. //测试
  227. Start();
  228. }
  229. public override void WriteData(string address, object value)
  230. {
  231. }
  232. //public override void ReadData(string address)
  233. //{
  234. //}
  235. }
  236. }