终端一体化运控平台
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 years ago
1 year ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. 
  2. using BPA.Helper;
  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 BPA.Helper.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.GetInstance.Subscribe<SCChip_TakeCupEvent>(0, TakeCupEventHandle);
  31. EventBus.GetInstance.Subscribe<SCChip_MakeIceCreamEvent>(0, MakeIceCreamEventHandle);
  32. EventBus.GetInstance.Subscribe<SCChip_SESwitchCreamEvent>(0, StopMakeIceCreamEventHandle);
  33. EventBus.GetInstance.Subscribe<SCChip_RotorSwitchEvent>(0, RotorSwitchEventHandle);
  34. EventBus.GetInstance.Subscribe<ArticleExitsEvent>(0, ArticleExitsEventHandle);
  35. 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. MessageLog.GetInstance.Show($"发送落杯指令--{(@event as SCChip_TakeCupEvent).Cup}");
  142. bool wait = true;
  143. var waitTimeout = DateTime.Now.AddSeconds(3);
  144. while (wait)
  145. {
  146. wait = DateTime.Now < waitTimeout;
  147. if (wait)
  148. {
  149. switch ((@event as SCChip_TakeCupEvent).Cup)
  150. {
  151. case IC_CUP.CUP_ICECREAM:
  152. wait = !ChipStatus.GetInstance.CompletedTake_CPU_CUP_ICECREAM;
  153. break;
  154. case IC_CUP.CUP_COFFEE:
  155. wait = !ChipStatus.GetInstance.CompletedTake_CPU_CUP_COFFEE;
  156. break;
  157. }
  158. }
  159. Thread.Sleep(50);
  160. }
  161. }
  162. private byte[] StructureToByte(ICChipPackage structure)
  163. {
  164. structure.Header = 0xAA;
  165. structure.Sender = IC_SENDER.CONSOLE;
  166. structure.End = 0xBB;
  167. int size = Marshal.SizeOf(typeof(ICChipPackage));
  168. byte[] buffer = new byte[size];
  169. IntPtr bufferIntPtr = Marshal.AllocHGlobal(size);
  170. try
  171. {
  172. Marshal.StructureToPtr(structure, bufferIntPtr, true);
  173. Marshal.Copy(bufferIntPtr, buffer, 0, size);
  174. }
  175. finally
  176. {
  177. Marshal.FreeHGlobal(bufferIntPtr);
  178. }
  179. return buffer;
  180. }
  181. }
  182. }