终端一体化运控平台
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.
 
 
 

200 rindas
7.5 KiB

  1. using BPASmartClient.EventBus;
  2. using BPASmartClient.Message;
  3. using BPASmartClient.Model.单片机;
  4. using BPASmartClient.Model.单片机.Enum;
  5. using BPASmartClient.SCChip;
  6. using BPASmartClient.SerialPort;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Runtime.InteropServices;
  11. using System.Text;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. using static BPASmartClient.EventBus.EventBus;
  15. namespace BPASmartClient.SCChip
  16. {
  17. /// <summary>
  18. /// 指令封装
  19. /// </summary>
  20. internal class CommandHandler
  21. {
  22. private SerialPortClient commProxy;
  23. private ICChipPackage package = new ICChipPackage();
  24. /// <summary>
  25. /// 初始化
  26. /// </summary>
  27. internal void Init(SerialPortClient commProxy)
  28. {
  29. this.commProxy = commProxy;
  30. EventBus.EventBus.GetInstance().Subscribe<SCChip_TakeCupEvent>(0,TakeCupEventHandle);
  31. EventBus.EventBus.GetInstance().Subscribe<SCChip_MakeIceCreamEvent>(0, MakeIceCreamEventHandle);
  32. EventBus.EventBus.GetInstance().Subscribe<SCChip_SESwitchCreamEvent>(0, StopMakeIceCreamEventHandle);
  33. EventBus.EventBus.GetInstance().Subscribe<SCChip_RotorSwitchEvent>(0, RotorSwitchEventHandle);
  34. EventBus.EventBus.GetInstance().Subscribe<ArticleExitsEvent>(0, ArticleExitsEventHandle);
  35. EventBus.EventBus.GetInstance().Subscribe<ArticleDistEvent>(0, ArticleDistEventHandle);
  36. }
  37. private void ArticleDistEventHandle(IEvent @event, EventCallBackHandle callBack)
  38. {
  39. package.Cmd = IC_CMD.ARTICLE_DIST;
  40. commProxy.SendData(StructureToByte(package));
  41. }
  42. private void ArticleExitsEventHandle(IEvent @event, EventCallBackHandle callBack)
  43. {
  44. ChipStatus.GetInstance().ArticleExits = false;
  45. package.Cmd = IC_CMD.ARTICLE_EXITS;
  46. commProxy.SendData(StructureToByte(package));
  47. }
  48. private void StopMakeIceCreamEventHandle(IEvent @event, EventCallBackHandle callBack)
  49. {
  50. ChipStatus.GetInstance().ArticleDist = 0;
  51. package.Cmd = (@event as SCChip_SESwitchCreamEvent).IsOpen ? IC_CMD.OPEN_SE : IC_CMD.CLOSE_SE;
  52. package.Value = (byte)(@event as SCChip_SESwitchCreamEvent).SteeringEngine;
  53. commProxy.SendData(StructureToByte(package));
  54. }
  55. private void RotorSwitchEventHandle(IEvent @event, EventCallBackHandle callBack)
  56. {
  57. package.Cmd = IC_CMD.ROTOR;
  58. package.Value = (@event as SCChip_RotorSwitchEvent).TurnOn ? (byte)IC_ROTOR.OPEN_ROTOR : (byte)IC_ROTOR.CLOSE_ROTOR;
  59. commProxy.SendData(StructureToByte(package));
  60. }
  61. private void MakeIceCreamEventHandle(IEvent @event, EventCallBackHandle callBack)
  62. {
  63. switch ((@event as SCChip_MakeIceCreamEvent).SteeringEngine)
  64. {
  65. case IC_SE.SE_1:
  66. ChipStatus.GetInstance().CompletedOpen_SE_1 = false;
  67. break;
  68. case IC_SE.SE_2:
  69. ChipStatus.GetInstance().CompletedOpen_SE_2 = false;
  70. break;
  71. case IC_SE.SE_3:
  72. ChipStatus.GetInstance().CompletedOpen_SE_3 = false;
  73. break;
  74. }
  75. package.Cmd = IC_CMD.OPEN_SE;
  76. package.Value = (byte)(@event as SCChip_MakeIceCreamEvent).SteeringEngine;
  77. commProxy.SendData(StructureToByte(package));
  78. bool wait = true;
  79. DateTime waitTimeout = DateTime.Now.AddSeconds(3);
  80. while (wait)
  81. {
  82. wait = DateTime.Now < waitTimeout;
  83. if (wait)
  84. {
  85. switch ((@event as SCChip_MakeIceCreamEvent).SteeringEngine)
  86. {
  87. case IC_SE.SE_1:
  88. wait = !ChipStatus.GetInstance().CompletedOpen_SE_1;
  89. break;
  90. case IC_SE.SE_2:
  91. wait = !ChipStatus.GetInstance().CompletedOpen_SE_2;
  92. break;
  93. case IC_SE.SE_3:
  94. wait = !ChipStatus.GetInstance().CompletedOpen_SE_3;
  95. break;
  96. }
  97. }
  98. Thread.Sleep(10);
  99. }
  100. Thread.Sleep(2000);
  101. package.Cmd = IC_CMD.CLOSE_SE;
  102. package.Value = (byte)(@event as SCChip_MakeIceCreamEvent).SteeringEngine;
  103. commProxy.SendData(StructureToByte(package));
  104. wait = true;
  105. waitTimeout = DateTime.Now.AddSeconds(3);
  106. while (wait)
  107. {
  108. wait = DateTime.Now < waitTimeout;
  109. if (wait)
  110. {
  111. switch ((@event as SCChip_MakeIceCreamEvent).SteeringEngine)
  112. {
  113. case IC_SE.SE_1:
  114. wait = !ChipStatus.GetInstance().CompletedClose_SE_1;
  115. break;
  116. case IC_SE.SE_2:
  117. wait = !ChipStatus.GetInstance().CompletedClose_SE_2;
  118. break;
  119. case IC_SE.SE_3:
  120. wait = !ChipStatus.GetInstance().CompletedClose_SE_3;
  121. break;
  122. }
  123. }
  124. Thread.Sleep(10);
  125. }
  126. }
  127. private void TakeCupEventHandle(IEvent @event, EventCallBackHandle callBack)
  128. {
  129. switch ((@event as SCChip_TakeCupEvent).Cup)
  130. {
  131. case IC_CUP.CUP_ICECREAM:
  132. ChipStatus.GetInstance().CompletedTake_CPU_CUP_ICECREAM = false;
  133. break;
  134. case IC_CUP.CUP_COFFEE:
  135. ChipStatus.GetInstance().CompletedTake_CPU_CUP_COFFEE = false;
  136. break;
  137. }
  138. package.Cmd = IC_CMD.TAKE_CUP;
  139. package.Value = (byte)(@event as SCChip_TakeCupEvent).Cup;
  140. commProxy.SendData(StructureToByte(package));
  141. MessageLog.GetInstance.Show($"发送落杯指令--{(@event as SCChip_TakeCupEvent).Cup}");
  142. bool wait = true;
  143. var waitTimeout = DateTime.Now.AddSeconds(3);
  144. while (wait)
  145. {
  146. wait = DateTime.Now < waitTimeout;
  147. if (wait)
  148. {
  149. switch ((@event as SCChip_TakeCupEvent).Cup)
  150. {
  151. case IC_CUP.CUP_ICECREAM:
  152. wait = !ChipStatus.GetInstance().CompletedTake_CPU_CUP_ICECREAM;
  153. break;
  154. case IC_CUP.CUP_COFFEE:
  155. wait = !ChipStatus.GetInstance().CompletedTake_CPU_CUP_COFFEE;
  156. break;
  157. }
  158. }
  159. Thread.Sleep(50);
  160. }
  161. }
  162. private byte[] StructureToByte(ICChipPackage structure)
  163. {
  164. structure.Header = 0xAA;
  165. structure.Sender = IC_SENDER.CONSOLE;
  166. structure.End = 0xBB;
  167. int size = Marshal.SizeOf(typeof(ICChipPackage));
  168. byte[] buffer = new byte[size];
  169. IntPtr bufferIntPtr = Marshal.AllocHGlobal(size);
  170. try
  171. {
  172. Marshal.StructureToPtr(structure, bufferIntPtr, true);
  173. Marshal.Copy(bufferIntPtr, buffer, 0, size);
  174. }
  175. finally
  176. {
  177. Marshal.FreeHGlobal(bufferIntPtr);
  178. }
  179. return buffer;
  180. }
  181. }
  182. }