终端一体化运控平台
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 

201 行
7.4 KiB

  1. using BPA.Helper;
  2. using BPASmartClient.Model.单片机;
  3. using BPASmartClient.Model.单片机.Enum;
  4. using BPASmartClient.SCChip;
  5. using BPASmartClient.SerialPort;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Runtime.InteropServices;
  10. using System.Text;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. using static BPA.Helper.EventBus;
  14. namespace BPASmartClient.SCChip
  15. {
  16. /// <summary>
  17. /// 指令封装
  18. /// </summary>
  19. internal class CommandHandler
  20. {
  21. private SerialPortClient commProxy;
  22. private ICChipPackage package = new ICChipPackage();
  23. /// <summary>
  24. /// 初始化
  25. /// </summary>
  26. internal void Init(SerialPortClient commProxy)
  27. {
  28. this.commProxy = commProxy;
  29. EventBus.GetInstance().Subscribe<SCChip_TakeCupEvent>(0, TakeCupEventHandle);
  30. EventBus.GetInstance().Subscribe<SCChip_MakeIceCreamEvent>(0, MakeIceCreamEventHandle);
  31. EventBus.GetInstance().Subscribe<SCChip_SESwitchCreamEvent>(0, StopMakeIceCreamEventHandle);
  32. EventBus.GetInstance().Subscribe<SCChip_RotorSwitchEvent>(0, RotorSwitchEventHandle);
  33. EventBus.GetInstance().Subscribe<ArticleExitsEvent>(0, ArticleExitsEventHandle);
  34. EventBus.GetInstance().Subscribe<ArticleDistEvent>(0, ArticleDistEventHandle);
  35. }
  36. private void ArticleDistEventHandle(IEvent @event, EventCallBackHandle callBack)
  37. {
  38. package.Cmd = IC_CMD.ARTICLE_DIST;
  39. commProxy.SendData(StructureToByte(package));
  40. }
  41. private void ArticleExitsEventHandle(IEvent @event, EventCallBackHandle callBack)
  42. {
  43. ChipStatus.GetInstance().ArticleExits = false;
  44. package.Cmd = IC_CMD.ARTICLE_EXITS;
  45. commProxy.SendData(StructureToByte(package));
  46. }
  47. private void StopMakeIceCreamEventHandle(IEvent @event, EventCallBackHandle callBack)
  48. {
  49. ChipStatus.GetInstance().ArticleDist = 0;
  50. package.Cmd = (@event as SCChip_SESwitchCreamEvent).IsOpen ? IC_CMD.OPEN_SE : IC_CMD.CLOSE_SE;
  51. package.Value = (byte)(@event as SCChip_SESwitchCreamEvent).SteeringEngine;
  52. commProxy.SendData(StructureToByte(package));
  53. }
  54. private void RotorSwitchEventHandle(IEvent @event, EventCallBackHandle callBack)
  55. {
  56. package.Cmd = IC_CMD.ROTOR;
  57. package.Value = (@event as SCChip_RotorSwitchEvent).TurnOn ? (byte)IC_ROTOR.OPEN_ROTOR : (byte)IC_ROTOR.CLOSE_ROTOR;
  58. commProxy.SendData(StructureToByte(package));
  59. }
  60. private void MakeIceCreamEventHandle(IEvent @event, EventCallBackHandle callBack)
  61. {
  62. switch ((@event as SCChip_MakeIceCreamEvent).SteeringEngine)
  63. {
  64. case IC_SE.SE_1:
  65. ChipStatus.GetInstance().CompletedOpen_SE_1 = false;
  66. break;
  67. case IC_SE.SE_2:
  68. ChipStatus.GetInstance().CompletedOpen_SE_2 = false;
  69. break;
  70. case IC_SE.SE_3:
  71. ChipStatus.GetInstance().CompletedOpen_SE_3 = false;
  72. break;
  73. }
  74. package.Cmd = IC_CMD.OPEN_SE;
  75. package.Value = (byte)(@event as SCChip_MakeIceCreamEvent).SteeringEngine;
  76. commProxy.SendData(StructureToByte(package));
  77. bool wait = true;
  78. DateTime waitTimeout = DateTime.Now.AddSeconds(3);
  79. while (wait)
  80. {
  81. wait = DateTime.Now < waitTimeout;
  82. if (wait)
  83. {
  84. switch ((@event as SCChip_MakeIceCreamEvent).SteeringEngine)
  85. {
  86. case IC_SE.SE_1:
  87. wait = !ChipStatus.GetInstance().CompletedOpen_SE_1;
  88. break;
  89. case IC_SE.SE_2:
  90. wait = !ChipStatus.GetInstance().CompletedOpen_SE_2;
  91. break;
  92. case IC_SE.SE_3:
  93. wait = !ChipStatus.GetInstance().CompletedOpen_SE_3;
  94. break;
  95. }
  96. }
  97. Thread.Sleep(10);
  98. }
  99. Thread.Sleep(2000);
  100. package.Cmd = IC_CMD.CLOSE_SE;
  101. package.Value = (byte)(@event as SCChip_MakeIceCreamEvent).SteeringEngine;
  102. commProxy.SendData(StructureToByte(package));
  103. wait = true;
  104. waitTimeout = DateTime.Now.AddSeconds(3);
  105. while (wait)
  106. {
  107. wait = DateTime.Now < waitTimeout;
  108. if (wait)
  109. {
  110. switch ((@event as SCChip_MakeIceCreamEvent).SteeringEngine)
  111. {
  112. case IC_SE.SE_1:
  113. wait = !ChipStatus.GetInstance().CompletedClose_SE_1;
  114. break;
  115. case IC_SE.SE_2:
  116. wait = !ChipStatus.GetInstance().CompletedClose_SE_2;
  117. break;
  118. case IC_SE.SE_3:
  119. wait = !ChipStatus.GetInstance().CompletedClose_SE_3;
  120. break;
  121. }
  122. }
  123. Thread.Sleep(10);
  124. }
  125. }
  126. private void TakeCupEventHandle(IEvent @event, EventCallBackHandle callBack)
  127. {
  128. switch ((@event as SCChip_TakeCupEvent).Cup)
  129. {
  130. case IC_CUP.CUP_ICECREAM:
  131. ChipStatus.GetInstance().CompletedTake_CPU_CUP_ICECREAM = false;
  132. break;
  133. case IC_CUP.CUP_COFFEE:
  134. ChipStatus.GetInstance().CompletedTake_CPU_CUP_COFFEE = false;
  135. break;
  136. }
  137. package.Cmd = IC_CMD.TAKE_CUP;
  138. package.Value = (byte)(@event as SCChip_TakeCupEvent).Cup;
  139. commProxy.SendData(StructureToByte(package));
  140. MessageLog.GetInstance.Show($"发送落杯指令--{(@event as SCChip_TakeCupEvent).Cup}");
  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(50);
  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. }