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 HBLConsole.Service.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);
EventBus.GetInstance().Subscribe(StopMakeIceCreamEventHandle);
EventBus.GetInstance().Subscribe(RotorSwitchEventHandle);
EventBus.GetInstance().Subscribe(ArticleExitsEventHandle);
EventBus.GetInstance().Subscribe(ArticleDistEventHandle);
}
private void ArticleDistEventHandle(IEvent @event, EventCallBackHandle callBack)
{
package.Cmd = IC_CMD.ARTICLE_DIST;
commProxy.SendData(StructureToByte(package));
}
private void ArticleExitsEventHandle(IEvent @event, EventCallBackHandle callBack)
{
ChipStatus.GetInstance().ArticleExits = false;
package.Cmd = IC_CMD.ARTICLE_EXITS;
commProxy.SendData(StructureToByte(package));
}
private void StopMakeIceCreamEventHandle(IEvent @event, EventCallBackHandle callBack)
{
ChipStatus.GetInstance().ArticleDist = 0;
package.Cmd = (@event as SESwitchCreamEvent).IsOpen ? IC_CMD.OPEN_SE : IC_CMD.CLOSE_SE;
package.Value = (byte)(@event as SESwitchCreamEvent).SteeringEngine;
commProxy.SendData(StructureToByte(package));
}
private void RotorSwitchEventHandle(IEvent @event, EventCallBackHandle callBack)
{
package.Cmd = IC_CMD.ROTOR;
package.Value = (@event as RotorSwitchEvent).TurnOn ? (byte)IC_ROTOR.OPEN_ROTOR : (byte)IC_ROTOR.CLOSE_ROTOR;
commProxy.SendData(StructureToByte(package));
}
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));
bool wait = true;
DateTime waitTimeout = DateTime.Now.AddSeconds(3);
while (wait)
{
wait = DateTime.Now < waitTimeout;
if (wait)
{
switch ((@event as MakeIceCreamEvent).SteeringEngine)
{
case IC_SE.SE_1:
wait = !ChipStatus.GetInstance().CompletedOpen_SE_1;
break;
case IC_SE.SE_2:
wait = !ChipStatus.GetInstance().CompletedOpen_SE_2;
break;
case IC_SE.SE_3:
wait = !ChipStatus.GetInstance().CompletedOpen_SE_3;
break;
}
}
Thread.Sleep(10);
}
Thread.Sleep(2000);
package.Cmd = IC_CMD.CLOSE_SE;
package.Value = (byte)(@event as MakeIceCreamEvent).SteeringEngine;
commProxy.SendData(StructureToByte(package));
wait = true;
waitTimeout = DateTime.Now.AddSeconds(3);
while (wait)
{
wait = DateTime.Now < waitTimeout;
if (wait)
{
switch ((@event as MakeIceCreamEvent).SteeringEngine)
{
case IC_SE.SE_1:
wait = !ChipStatus.GetInstance().CompletedClose_SE_1;
break;
case IC_SE.SE_2:
wait = !ChipStatus.GetInstance().CompletedClose_SE_2;
break;
case IC_SE.SE_3:
wait = !ChipStatus.GetInstance().CompletedClose_SE_3;
break;
}
}
Thread.Sleep(10);
}
}
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));
bool wait = true;
var waitTimeout = DateTime.Now.AddSeconds(3);
while (wait)
{
wait = DateTime.Now < waitTimeout;
if (wait)
{
switch ((@event as TakeCupEvent).Cup)
{
case IC_CUP.CUP_ICECREAM:
wait = !ChipStatus.GetInstance().CompletedTake_CPU_CUP_ICECREAM;
break;
case IC_CUP.CUP_COFFEE:
wait = !ChipStatus.GetInstance().CompletedTake_CPU_CUP_COFFEE;
break;
}
}
Thread.Sleep(10);
}
}
private byte[] StructureToByte(ICChipPackage structure)
{
structure.Header = 0xAA;
structure.Sender = IC_SENDER.CONSOLE;
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;
}
}
}