using BPASmartClient.EventBus;
using BPASmartClient.GSIceCream;
using BPASmartClient.Message;
using BPASmartClient.Model;
using BPASmartClient.Model.冰淇淋.Enum;
using BPASmartClient.SerialPort;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using static BPASmartClient.EventBus.EventBus;
namespace BPASmartClient.DRCoffee
{
///
/// 指令封装
///
internal class CommandHandler
{
byte[] cmdHeartDW;
private SerialPortClient commProxy;
private GSIceCream_EndCookEvent iceCreamEndCook = new GSIceCream_EndCookEvent();
public Action PauseAsk { get; set; }
///
/// 初始化
///
internal void Init(SerialPortClient commProxy)
{
this.commProxy = commProxy;
ICMSG_Heart_DW heartDW = new ICMSG_Heart_DW();
cmdHeartDW = IcPack.StructureToByte(heartDW);
EventBus.EventBus.GetInstance().Subscribe(0, ModeSetEventHandle);
EventBus.EventBus.GetInstance().Subscribe(0,DischargeEventHandle);
}
///
/// 发送心跳
///
internal byte[] GetHeartDW()
{
return cmdHeartDW;
}
public void ModeSetEventHandle(IEvent @event, EventCallBackHandle callBack = null)
{
PauseAsk?.Invoke(true);
Thread.Sleep(200);
var data = IcPack.StructureToByte(ICMSG_MODE_DW.Build(((GSIceCream_ModeSetEvent)@event).Mode));
commProxy.SendData(data);
Thread.Sleep(200);
PauseAsk?.Invoke(false);
MessageLog.GetInstance.Show(string.Format("设置模式[{0}]", ((GSIceCream_ModeSetEvent)@event).Mode));
}
public void DischargeEventHandle(IEvent @event, EventCallBackHandle callBack = null)
{
if (MorkIStatus.GetInstance().Fault != MORKI_FAULT.未发生故障)
{
MessageLog.GetInstance.Show(string.Format("当前存在故障[{0}%],不允许制作", MorkIStatus.GetInstance().Fault));
callBack?.Invoke(false);
return;
}
if (MorkIStatus.GetInstance().CXB <= 86)
{
MessageLog.GetInstance.Show(string.Format("当前成型比[{0}%],低于86%,不允许制作", MorkIStatus.GetInstance().CXB));
callBack?.Invoke(false);
return;
}
bool modeRight = MorkIStatus.GetInstance().CurrentMode == MORKI_MODE.制冷模式;
if (!modeRight)
{
PauseAsk?.Invoke(true);
Thread.Sleep(200);
var temp = IcPack.StructureToByte(ICMSG_MODE_DW.Build(MORKI_MODE.制冷模式));
commProxy.SendData(temp);
Thread.Sleep(200);
PauseAsk?.Invoke(false);
MessageLog.GetInstance.Show(string.Format("出料操作->设置模式[{0}]", MORKI_MODE.制冷模式));
DateTime freeTime = DateTime.Now.AddSeconds(5);
while (DateTime.Now < freeTime)
{
Thread.Sleep(10);
modeRight = MorkIStatus.GetInstance().CurrentMode == MORKI_MODE.制冷模式;
if (modeRight)
break;
}
}
if (modeRight)
{
PauseAsk?.Invoke(true);
Thread.Sleep(200);
var data = IcPack.StructureToByte(ICMSG_MODE_DW.Build(MORKI_MODE.打料));
commProxy.SendData(data);
Thread.Sleep(200);
PauseAsk?.Invoke(false);
iceCreamEndCook.Publish();
MessageLog.GetInstance.Show(string.Format("出料操作->设置模式[{0}]", MORKI_MODE.打料));
callBack?.Invoke(true);
}
else
{
MessageLog.GetInstance.Show(string.Format("出料操作->模式切换失败,当前模式[{0}],不允许出料", MorkIStatus.GetInstance().CurrentMode));
callBack?.Invoke(false);
}
}
}
}