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.
 
 

194 lines
7.1 KiB

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