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

CommandHandler.cs 7.4 KiB

пре 2 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. 
  2. using BPASmartClient.EventBus;
  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. bool wait = true;
  142. var waitTimeout = DateTime.Now.AddSeconds(3);
  143. while (wait)
  144. {
  145. wait = DateTime.Now < waitTimeout;
  146. if (wait)
  147. {
  148. switch ((@event as SCChip_TakeCupEvent).Cup)
  149. {
  150. case IC_CUP.CUP_ICECREAM:
  151. wait = !ChipStatus.GetInstance().CompletedTake_CPU_CUP_ICECREAM;
  152. break;
  153. case IC_CUP.CUP_COFFEE:
  154. wait = !ChipStatus.GetInstance().CompletedTake_CPU_CUP_COFFEE;
  155. break;
  156. }
  157. }
  158. Thread.Sleep(10);
  159. }
  160. }
  161. private byte[] StructureToByte(ICChipPackage structure)
  162. {
  163. structure.Header = 0xAA;
  164. structure.Sender = IC_SENDER.CONSOLE;
  165. structure.End = 0xBB;
  166. int size = Marshal.SizeOf(typeof(ICChipPackage));
  167. byte[] buffer = new byte[size];
  168. IntPtr bufferIntPtr = Marshal.AllocHGlobal(size);
  169. try
  170. {
  171. Marshal.StructureToPtr(structure, bufferIntPtr, true);
  172. Marshal.Copy(bufferIntPtr, buffer, 0, size);
  173. }
  174. finally
  175. {
  176. Marshal.FreeHGlobal(bufferIntPtr);
  177. }
  178. return buffer;
  179. }
  180. }
  181. }