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.
 
 

195 lines
7.1 KiB

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