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.
 
 

179 lines
6.4 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. }
  32. private void StopMakeIceCreamEventHandle(IEvent @event, EventCallBackHandle callBack)
  33. {
  34. package.Cmd = (@event as SESwitchCreamEvent).IsOpen ? IC_CMD.OPEN_SE : IC_CMD.CLOSE_SE;
  35. package.Value = (byte)(@event as SESwitchCreamEvent).SteeringEngine;
  36. commProxy.SendData(StructureToByte(package));
  37. }
  38. private void RotorSwitchEventHandle(IEvent @event, EventCallBackHandle callBack)
  39. {
  40. package.Cmd = IC_CMD.ROTOR;
  41. package.Value = (@event as RotorSwitchEvent).TurnOn ? (byte)IC_ROTOR.OPEN_ROTOR : (byte)IC_ROTOR.CLOSE_ROTOR;
  42. commProxy.SendData(StructureToByte(package));
  43. }
  44. private void MakeIceCreamEventHandle(IEvent @event, EventCallBackHandle callBack)
  45. {
  46. switch ((@event as MakeIceCreamEvent).SteeringEngine)
  47. {
  48. case IC_SE.SE_1:
  49. ChipStatus.GetInstance().CompletedOpen_SE_1 = false;
  50. break;
  51. case IC_SE.SE_2:
  52. ChipStatus.GetInstance().CompletedOpen_SE_2 = false;
  53. break;
  54. case IC_SE.SE_3:
  55. ChipStatus.GetInstance().CompletedOpen_SE_3 = false;
  56. break;
  57. }
  58. package.Cmd = IC_CMD.OPEN_SE;
  59. package.Value = (byte)(@event as MakeIceCreamEvent).SteeringEngine;
  60. commProxy.SendData(StructureToByte(package));
  61. bool wait = true;
  62. DateTime waitTimeout = DateTime.Now.AddSeconds(3);
  63. while (wait)
  64. {
  65. wait = DateTime.Now < waitTimeout;
  66. if (wait)
  67. {
  68. switch ((@event as MakeIceCreamEvent).SteeringEngine)
  69. {
  70. case IC_SE.SE_1:
  71. wait = !ChipStatus.GetInstance().CompletedOpen_SE_1;
  72. break;
  73. case IC_SE.SE_2:
  74. wait = !ChipStatus.GetInstance().CompletedOpen_SE_2;
  75. break;
  76. case IC_SE.SE_3:
  77. wait = !ChipStatus.GetInstance().CompletedOpen_SE_3;
  78. break;
  79. }
  80. }
  81. Thread.Sleep(10);
  82. }
  83. Thread.Sleep(1000);
  84. package.Cmd = IC_CMD.CLOSE_SE;
  85. package.Value = (byte)(@event as MakeIceCreamEvent).SteeringEngine;
  86. commProxy.SendData(StructureToByte(package));
  87. wait = true;
  88. waitTimeout = DateTime.Now.AddSeconds(3);
  89. while (wait)
  90. {
  91. wait = DateTime.Now < waitTimeout;
  92. if (wait)
  93. {
  94. switch ((@event as MakeIceCreamEvent).SteeringEngine)
  95. {
  96. case IC_SE.SE_1:
  97. wait = !ChipStatus.GetInstance().CompletedClose_SE_1;
  98. break;
  99. case IC_SE.SE_2:
  100. wait = !ChipStatus.GetInstance().CompletedClose_SE_2;
  101. break;
  102. case IC_SE.SE_3:
  103. wait = !ChipStatus.GetInstance().CompletedClose_SE_3;
  104. break;
  105. }
  106. }
  107. Thread.Sleep(10);
  108. }
  109. }
  110. private void TakeCupEventHandle(IEvent @event, EventCallBackHandle callBack)
  111. {
  112. switch ((@event as TakeCupEvent).Cup)
  113. {
  114. case IC_CUP.CUP_ICECREAM:
  115. ChipStatus.GetInstance().CompletedTake_CPU_CUP_ICECREAM = false;
  116. break;
  117. case IC_CUP.CUP_COFFEE:
  118. ChipStatus.GetInstance().CompletedTake_CPU_CUP_COFFEE = false;
  119. break;
  120. }
  121. package.Cmd = IC_CMD.TAKE_CUP;
  122. package.Value = (byte)(@event as TakeCupEvent).Cup;
  123. commProxy.SendData(StructureToByte(package));
  124. bool wait = true;
  125. var waitTimeout = DateTime.Now.AddSeconds(3);
  126. while (wait)
  127. {
  128. wait = DateTime.Now < waitTimeout;
  129. if (wait)
  130. {
  131. switch ((@event as TakeCupEvent).Cup)
  132. {
  133. case IC_CUP.CUP_ICECREAM:
  134. wait = !ChipStatus.GetInstance().CompletedTake_CPU_CUP_ICECREAM;
  135. break;
  136. case IC_CUP.CUP_COFFEE:
  137. wait = !ChipStatus.GetInstance().CompletedTake_CPU_CUP_COFFEE;
  138. break;
  139. }
  140. }
  141. Thread.Sleep(10);
  142. }
  143. }
  144. private byte[] StructureToByte(ICChipPackage structure)
  145. {
  146. structure.Header = 0xAA;
  147. structure.Sender = IC_SENDER.CONSOLE;
  148. structure.End = 0xBB;
  149. int size = Marshal.SizeOf(typeof(ICChipPackage));
  150. byte[] buffer = new byte[size];
  151. IntPtr bufferIntPtr = Marshal.AllocHGlobal(size);
  152. try
  153. {
  154. Marshal.StructureToPtr(structure, bufferIntPtr, true);
  155. Marshal.Copy(bufferIntPtr, buffer, 0, size);
  156. }
  157. finally
  158. {
  159. Marshal.FreeHGlobal(bufferIntPtr);
  160. }
  161. return buffer;
  162. }
  163. }
  164. }