using BPA.Utility;
using HBLConsole.Communication;
using HBLConsole.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using static BPA.Utility.EventBus;
namespace HBLDevice.ICChip
{
///
/// 指令封装
///
internal class CommandHandler
{
private SerialPortClient commProxy;
private ICChipPackage package = new ICChipPackage();
///
/// 初始化
///
internal void Init(SerialPortClient commProxy)
{
this.commProxy = commProxy;
EventBus.GetInstance().Subscribe(TakeCupEventHandle);
EventBus.GetInstance().Subscribe(MakeIceCreamEventHandle);
}
private void MakeIceCreamEventHandle(IEvent @event, EventCallBackHandle callBack)
{
switch ((@event as MakeIceCreamEvent).SteeringEngine)
{
case IC_SE.SE_1:
ChipStatus.GetInstance().CompletedOpen_SE_1 = false;
break;
case IC_SE.SE_2:
ChipStatus.GetInstance().CompletedOpen_SE_2 = false;
break;
case IC_SE.SE_3:
ChipStatus.GetInstance().CompletedOpen_SE_3 = false;
break;
}
package.Cmd = IC_CMD.OPEN_SE;
package.Value = (byte)(@event as MakeIceCreamEvent).SteeringEngine;
commProxy.SendData(StructureToByte(package));
Thread.Sleep(500);
DateTime timeOut = DateTime.Now.AddSeconds(3);
bool loop = true;
while (loop)
{
loop = DateTime.Now < timeOut;
switch ((@event as MakeIceCreamEvent).SteeringEngine)
{
case IC_SE.SE_1:
loop = !ChipStatus.GetInstance().CompletedOpen_SE_1;
break;
case IC_SE.SE_2:
loop = !ChipStatus.GetInstance().CompletedOpen_SE_2;
break;
case IC_SE.SE_3:
loop = !ChipStatus.GetInstance().CompletedOpen_SE_3;
break;
}
}
switch ((@event as MakeIceCreamEvent).SteeringEngine)
{
case IC_SE.SE_1:
ChipStatus.GetInstance().CompletedClose_SE_1 = false;
break;
case IC_SE.SE_2:
ChipStatus.GetInstance().CompletedClose_SE_2 = false;
break;
case IC_SE.SE_3:
ChipStatus.GetInstance().CompletedClose_SE_3 = false;
break;
}
package.Cmd = IC_CMD.CLOSE_SE;
package.Value = (byte)(@event as MakeIceCreamEvent).SteeringEngine;
commProxy.SendData(StructureToByte(package));
Thread.Sleep(500);
timeOut = DateTime.Now.AddSeconds(3);
loop = true;
while (loop)
{
loop = DateTime.Now < timeOut;
switch ((@event as MakeIceCreamEvent).SteeringEngine)
{
case IC_SE.SE_1:
loop = !ChipStatus.GetInstance().CompletedClose_SE_1;
break;
case IC_SE.SE_2:
loop = !ChipStatus.GetInstance().CompletedClose_SE_2;
break;
case IC_SE.SE_3:
loop = !ChipStatus.GetInstance().CompletedClose_SE_3;
break;
}
}
}
private void TakeCupEventHandle(IEvent @event, EventCallBackHandle callBack)
{
switch ((@event as TakeCupEvent).Cup)
{
case IC_CUP.CUP_ICECREAM:
ChipStatus.GetInstance().CompletedTake_CPU_CUP_ICECREAM = false;
break;
case IC_CUP.CUP_COFFEE:
ChipStatus.GetInstance().CompletedTake_CPU_CUP_COFFEE = false;
break;
}
package.Cmd = IC_CMD.TAKE_CUP;
package.Value = (byte)(@event as TakeCupEvent).Cup;
commProxy.SendData(StructureToByte(package));
}
private byte[] StructureToByte(ICChipPackage structure)
{
structure.Header = 0xAA;
structure.End = 0xBB;
int size = Marshal.SizeOf(typeof(ICChipPackage));
byte[] buffer = new byte[size];
IntPtr bufferIntPtr = Marshal.AllocHGlobal(size);
try
{
Marshal.StructureToPtr(structure, bufferIntPtr, true);
Marshal.Copy(bufferIntPtr, buffer, 0, size);
}
finally
{
Marshal.FreeHGlobal(bufferIntPtr);
}
return buffer;
}
}
}