终端一体化运控平台
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

198 linhas
7.4 KiB

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