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.
 
 

150 lines
5.0 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. }
  30. private void MakeIceCreamEventHandle(IEvent @event, EventCallBackHandle callBack)
  31. {
  32. switch ((@event as MakeIceCreamEvent).SteeringEngine)
  33. {
  34. case IC_SE.SE_1:
  35. ChipStatus.GetInstance().CompletedOpen_SE_1 = false;
  36. break;
  37. case IC_SE.SE_2:
  38. ChipStatus.GetInstance().CompletedOpen_SE_2 = false;
  39. break;
  40. case IC_SE.SE_3:
  41. ChipStatus.GetInstance().CompletedOpen_SE_3 = false;
  42. break;
  43. }
  44. package.Cmd = IC_CMD.OPEN_SE;
  45. package.Value = (byte)(@event as MakeIceCreamEvent).SteeringEngine;
  46. commProxy.SendData(StructureToByte(package));
  47. Thread.Sleep(500);
  48. DateTime timeOut = DateTime.Now.AddSeconds(3);
  49. bool loop = true;
  50. while (loop)
  51. {
  52. loop = DateTime.Now < timeOut;
  53. switch ((@event as MakeIceCreamEvent).SteeringEngine)
  54. {
  55. case IC_SE.SE_1:
  56. loop = !ChipStatus.GetInstance().CompletedOpen_SE_1;
  57. break;
  58. case IC_SE.SE_2:
  59. loop = !ChipStatus.GetInstance().CompletedOpen_SE_2;
  60. break;
  61. case IC_SE.SE_3:
  62. loop = !ChipStatus.GetInstance().CompletedOpen_SE_3;
  63. break;
  64. }
  65. }
  66. switch ((@event as MakeIceCreamEvent).SteeringEngine)
  67. {
  68. case IC_SE.SE_1:
  69. ChipStatus.GetInstance().CompletedClose_SE_1 = false;
  70. break;
  71. case IC_SE.SE_2:
  72. ChipStatus.GetInstance().CompletedClose_SE_2 = false;
  73. break;
  74. case IC_SE.SE_3:
  75. ChipStatus.GetInstance().CompletedClose_SE_3 = false;
  76. break;
  77. }
  78. package.Cmd = IC_CMD.CLOSE_SE;
  79. package.Value = (byte)(@event as MakeIceCreamEvent).SteeringEngine;
  80. commProxy.SendData(StructureToByte(package));
  81. Thread.Sleep(500);
  82. timeOut = DateTime.Now.AddSeconds(3);
  83. loop = true;
  84. while (loop)
  85. {
  86. loop = DateTime.Now < timeOut;
  87. switch ((@event as MakeIceCreamEvent).SteeringEngine)
  88. {
  89. case IC_SE.SE_1:
  90. loop = !ChipStatus.GetInstance().CompletedClose_SE_1;
  91. break;
  92. case IC_SE.SE_2:
  93. loop = !ChipStatus.GetInstance().CompletedClose_SE_2;
  94. break;
  95. case IC_SE.SE_3:
  96. loop = !ChipStatus.GetInstance().CompletedClose_SE_3;
  97. break;
  98. }
  99. }
  100. }
  101. private void TakeCupEventHandle(IEvent @event, EventCallBackHandle callBack)
  102. {
  103. switch ((@event as TakeCupEvent).Cup)
  104. {
  105. case IC_CUP.CUP_ICECREAM:
  106. ChipStatus.GetInstance().CompletedTake_CPU_CUP_ICECREAM = false;
  107. break;
  108. case IC_CUP.CUP_COFFEE:
  109. ChipStatus.GetInstance().CompletedTake_CPU_CUP_COFFEE = false;
  110. break;
  111. }
  112. package.Cmd = IC_CMD.TAKE_CUP;
  113. package.Value = (byte)(@event as TakeCupEvent).Cup;
  114. commProxy.SendData(StructureToByte(package));
  115. }
  116. private byte[] StructureToByte(ICChipPackage structure)
  117. {
  118. structure.Header = 0xAA;
  119. structure.End = 0xBB;
  120. int size = Marshal.SizeOf(typeof(ICChipPackage));
  121. byte[] buffer = new byte[size];
  122. IntPtr bufferIntPtr = Marshal.AllocHGlobal(size);
  123. try
  124. {
  125. Marshal.StructureToPtr(structure, bufferIntPtr, true);
  126. Marshal.Copy(bufferIntPtr, buffer, 0, size);
  127. }
  128. finally
  129. {
  130. Marshal.FreeHGlobal(bufferIntPtr);
  131. }
  132. return buffer;
  133. }
  134. }
  135. }