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

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