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.
 
 

95 lines
3.1 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. EventBus.GetInstance().Subscribe<RotorSwitchEvent>(RotorSwitchEventHandle);
  30. }
  31. private void RotorSwitchEventHandle(IEvent @event, EventCallBackHandle callBack)
  32. {
  33. package.Cmd = IC_CMD.ROTOR;
  34. package.Value = (@event as RotorSwitchEvent).TurnOn ? (byte)IC_ROTOR.OPEN_ROTOR : (byte)IC_ROTOR.OPEN_ROTOR;
  35. commProxy.SendData(StructureToByte(package));
  36. }
  37. private void MakeIceCreamEventHandle(IEvent @event, EventCallBackHandle callBack)
  38. {
  39. package.Cmd = IC_CMD.OPEN_SE;
  40. package.Value = (byte)(@event as MakeIceCreamEvent).SteeringEngine;
  41. commProxy.SendData(StructureToByte(package));
  42. Thread.Sleep(3000);
  43. package.Cmd = IC_CMD.CLOSE_SE;
  44. package.Value = (byte)(@event as MakeIceCreamEvent).SteeringEngine;
  45. commProxy.SendData(StructureToByte(package));
  46. }
  47. private void TakeCupEventHandle(IEvent @event, EventCallBackHandle callBack)
  48. {
  49. switch ((@event as TakeCupEvent).Cup)
  50. {
  51. case IC_CUP.CUP_ICECREAM:
  52. ChipStatus.GetInstance().CompletedTake_CPU_CUP_ICECREAM = false;
  53. break;
  54. case IC_CUP.CUP_COFFEE:
  55. ChipStatus.GetInstance().CompletedTake_CPU_CUP_COFFEE = false;
  56. break;
  57. }
  58. package.Cmd = IC_CMD.TAKE_CUP;
  59. package.Value = (byte)(@event as TakeCupEvent).Cup;
  60. commProxy.SendData(StructureToByte(package));
  61. }
  62. private byte[] StructureToByte(ICChipPackage structure)
  63. {
  64. structure.Header = 0xAA;
  65. structure.Sender = IC_SENDER.CONSOLE;
  66. structure.End = 0xBB;
  67. int size = Marshal.SizeOf(typeof(ICChipPackage));
  68. byte[] buffer = new byte[size];
  69. IntPtr bufferIntPtr = Marshal.AllocHGlobal(size);
  70. try
  71. {
  72. Marshal.StructureToPtr(structure, bufferIntPtr, true);
  73. Marshal.Copy(bufferIntPtr, buffer, 0, size);
  74. }
  75. finally
  76. {
  77. Marshal.FreeHGlobal(bufferIntPtr);
  78. }
  79. return buffer;
  80. }
  81. }
  82. }