using BPA.Message;
using BPA.Message.Enum;
using BPASmartClient.Device;
using BPASmartClient.EventBus;
using BPASmartClient.Helper;
using BPASmartClient.Message;
using BPASmartClient.Model;
using BPASmartClient.Model.PLC;
using static BPASmartClient.EventBus.EventBus;
namespace BPASmartClient.MorkF
{
public class Control_MorkF : BaseDevice
{
public override DeviceClientType DeviceType => DeviceClientType.MORKSF;
GVL_MorkF morkF = new GVL_MorkF();//全局对象声明
//private OrderMaterialDelivery orderMaterialDelivery { get; set; } = new OrderMaterialDelivery();
//private RecipeBoms recipeBoms { get; set; } = new RecipeBoms();
//int OrderCount;
///
/// 入口
///
public override void DoMain()
{
ReadData(); //plc状态量读取
DataParse();//数据解析
//Main();
//ResetProgram();
MessageLog.GetInstance.Show("MORKF 设备初始化完成");
}
public override void ResetProgram()
{
morkF = null;
morkF = new GVL_MorkF();
}
///
/// PLC数据读取
///
public override void ReadData()
{
//ThreadManage.GetInstance().StartLong(new Action(() =>
//{
GetStatus("M0.1", new Action((bools) =>
{
morkF.InitialComplete = bools[0];
}));
GetStatus("M1.0", new Action((bools) =>
{
morkF.ProvidePlateModule = bools[0];
morkF.ProvidePlateComplete = bools[2];
morkF.CleanModule = bools[3];
morkF.CleanComplete = bools[5];
}));
GetStatus("M2.0", new Action((bools) =>
{
morkF.KitchenOneStatus = bools[1];
morkF.FallOilComplete = bools[8];
}));
GetStatus("M8.0", new Action((bools) =>
{
morkF.TurnMachineUpComplete = bools[1];
morkF.TurnMachineDownComplete = bools[3];
}));
GetStatus("M13.5", new Action((bools) =>
{
morkF.MaterialArriveComplete = bools[0];
}));
GetStatus("M16.0", new Action((bools) =>
{
morkF.PutPotToKitchenComlete = bools[1];
morkF.TakeMaterialComplete = bools[2];
morkF.PlaceRinseTableComplete = bools[5];
morkF.RoobotIdle = bools[6];
}));
// Thread.Sleep(500);
//}), "ReadPLCData");
}
private void ServerInit()
{
//物料信息
EventBus.EventBus.GetInstance().Subscribe(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
{
if (@event == null) return;
if (@event is MaterialDeliveryEvent material)
{
orderMaterialDelivery = material.orderMaterialDelivery;
}
});
//配方数据信息
EventBus.EventBus.GetInstance().Subscribe(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
{
if (@event == null) return;
if (@event is RecipeBomEvent recipe)
{
recipeBoms = recipe.recipeBoms;
WriteRecipeBoms();
}
});
}
private void OrderChange(string subid, ORDER_STATUS oRDER_STATUS)
{
EventBus.EventBus.GetInstance().Publish(new OrderStatusChangedEvent() { Id = DeviceId, Status = oRDER_STATUS, SubOrderId = subid });
}
///
/// 写入配方数据到 PLC
///
private void WriteRecipeBoms()
{
//List recipeBoms = new List();
//if (this.recipeBoms == null) return;
//foreach (var item in this.recipeBoms.RecipeIds)
//{
// foreach (var rec in item.Recipes)
// {
// recipeBoms.Add((ushort)rec);
// }
//}
//if (recipeBoms.Count > 0)
//{
// //配方数据地址范围:VW2000 - VW2278
// WriteData("VW2000", recipeBoms.ToArray());
//}
//else { MessageLog.GetInstance.Show("配方数据为空"); }
}
///
/// 数据解析
///
private void DataParse()
{
EventBus.EventBus.GetInstance().Subscribe(DeviceId, delegate (IEvent @event, EventCallBackHandle callBackHandle)
{
if (@event == null) return;
if (@event is DoOrderEvent order)
{
if (order.MorkOrder.GoodBatchings == null) return;
OrderCount++;
MessageLog.GetInstance.Show($"接收到{OrderCount}次订单");
foreach (var item in order.MorkOrder.GoodBatchings)
{
var res = orderMaterialDelivery?.BatchingInfo?.FirstOrDefault(p => p.BatchingId == item.BatchingId);
if (res != null)
{
if (ushort.TryParse(res.BatchingLoc, out ushort loc))
{
if (loc >= 1 && loc <= 5)
{
if (morkF.TakeMaterialQueue.FirstOrDefault(p => p.SuborderId == order.MorkOrder.SuborderId) == null)
morkF.TakeMaterialQueue.Enqueue(new OrderLocInfo() { Loc = ushort.Parse(res.BatchingLoc), SuborderId = order.MorkOrder.SuborderId, BatchingId = res.BatchingId });
}
else if (loc >= 10 && loc <= 11)
{
int index = 0;
if (recipeBoms != null)
{
index = Array.FindIndex(recipeBoms.RecipeIds?.ToArray(), p => p.RecipeId == order.MorkOrder.RecipeId);
index++;
}
if (morkF.TakePlateQueue.FirstOrDefault(p => p.SuborderId == order.MorkOrder.SuborderId) == null)
morkF.TakePlateQueue.Enqueue(new OrderLocInfo()
{
Loc = ushort.Parse(res.BatchingLoc),
SuborderId = order.MorkOrder.SuborderId,
RecipeNumber = (index >= 1 && index <= 10) ? (ushort)index : (ushort)0
});
}
}
}
}
}
});
}
///
/// 流程控制
///
public override void MainTask()
{
//ThreadManage.GetInstance().StartLong(new Action(() =>
//{
IsHealth = true; //心跳
TakePlatelTask();
TakePotTask();
TakeVegBurdenATask();
TakeMeatBurdenATask();
TakeVegBurdenBTask();
TakeBurdenCTask();
StartCookingTask();
OutDishTask();
CleanPotTask();
TurntableControl();
SingleProcess();
//}), "MainTask");
}
///
/// 信号处理
///
private void SingleProcess()
{
//转台互锁解除
if (morkF.TakeMaterialComplete && morkF.TurnTableLock)
{
morkF.TurnTableLock = false;
}
//待定
if (morkF.TakeVegALock && morkF.TakeMeatALock && morkF.TakeVegBLock)
{
morkF.TakeAllBurdenComplete = true;
}
//出餐完成,相应变量复位
if (morkF.OutMealComplete)
{
morkF.TakePlateLock = false;
morkF.TakePotLock = false;
morkF.TakeVegALock = false;
morkF.TakeMeatALock = false;
morkF.TakeVegBLock = false;
morkF.TakeBurdenCLcok = false;
}
}
///
/// 转台控制
///
private void TurntableControl()
{
//正常轮询
if (morkF.InitialComplete && morkF.TakeMaterialQueue.Count > 0 && !morkF.TurnTableLock)
{
var result = orderMaterialDelivery.BatchingInfo.Where(p => p.BatchingId == morkF.TakeMaterialQueue.ElementAt(0).BatchingId).ToList();
if (result != null)
{
if (ushort.TryParse(result[0].BatchingLoc, out ushort loc))
{
TurnMaterialStore(loc);
MessageLog.GetInstance.Show($"转台转到【{loc}】位置");
morkF.TurnTableLock = true; //取料完成后置false
}
}
else MessageLog.GetInstance.Show("未找到可用的物料信息");
}
}
///
/// 供盘
///
///
private void TakePlatelTask()
{
if (!morkF.RoobotIdle && morkF.InitialComplete && morkF.TakePlateQueue.Count > 0 && !morkF.ProvidePlateModule && !morkF.TakePlateLock)
{
if (morkF.TakePlateQueue.TryDequeue(out OrderLocInfo order))
{
StartTakePlate();
morkF.CurrentOrderId = order.SuborderId;
morkF.TakePlateLock = true; //订单完成后置false
MessageLog.GetInstance.Show($"订单【{ morkF.CurrentOrderId}】执行取碗控制,位置:[{order.Loc}]");
}
}
}
///
/// 取锅到灶台
///
///
private void TakePotTask()
{
if (!morkF.RoobotIdle && morkF.InitialComplete && morkF.TakePlateQueue.Count > 0 && !morkF.CleanModule && morkF.CleanComplete && !morkF.KitchenOneStatus && !morkF.TakePotLock)
{
TakePotToKitchen();
morkF.TakePotLock = true; //订单完成后置false
MessageLog.GetInstance.Show($"订单【{ morkF.CurrentOrderId}】执行取锅到灶台控制");
}
}
///
/// 取素菜A料
///
private void TakeVegBurdenATask()
{
if (!morkF.RoobotIdle && morkF.PotInPlace && morkF.TurnMachineUpComplete && !morkF.TakeVegALock && morkF.AllowTakeMaterial)
{
if (morkF.TakeMaterialQueue.TryDequeue(out OrderLocInfo order))
{
int loc = order.Loc;
TakeBurden(loc);
morkF.TakeVegALock = true;//订单完成置false
morkF.AllowTakeMaterial = false;
MessageLog.GetInstance.Show($"订单【{ order.SuborderId},翻转机上升完成,执行取素菜A控制");
}
}
}
///
/// 取荤菜A料
///
private void TakeMeatBurdenATask()
{
if (!morkF.RoobotIdle && morkF.PotInPlace && morkF.TurnMachineUpComplete && morkF.TakeVegALock && !morkF.TakeMeatALock && morkF.AllowTakeMaterial)
{
if (morkF.TakeMaterialQueue.TryDequeue(out OrderLocInfo order))
{
int loc = order.Loc;
TakeBurden(loc);
morkF.TakeMeatALock = true;//订单完成置false
morkF.AllowTakeMaterial = false;
MessageLog.GetInstance.Show($"订单【{ order.SuborderId}】,,翻转机上升完成,执行取荤菜A控制");
}
}
}
///
/// 取素菜B料
///
private void TakeVegBurdenBTask()
{
if (!morkF.RoobotIdle && morkF.PotInPlace && morkF.TurnMachineUpComplete && morkF.TakeMeatALock && !morkF.TakeVegBLock && morkF.AllowTakeMaterial)
{
if (morkF.TakeMaterialQueue.TryDequeue(out OrderLocInfo order))
{
int loc = order.Loc;
TakeBurden(loc);
morkF.TakeVegBLock = true;//订单完成置false
morkF.AllowTakeMaterial = false;
MessageLog.GetInstance.Show($"订单【{ order.SuborderId}】,,翻转机上升完成,执行取素菜B控制");
}
}
}
///
/// 取调味品C料
///
///
private void TakeBurdenCTask()
{
if (!morkF.RoobotIdle && morkF.PotInPlace && morkF.TurnMachineUpComplete && morkF.TakeAllBurdenComplete && !morkF.TakeBurdenCLcok && morkF.AllowTakeMaterial)
{
if (morkF.TakeMaterialQueue.TryDequeue(out OrderLocInfo order))
{
int loc = order.Loc;
TakeBurden(loc);
morkF.TakeBurdenCLcok = true;//订单完成置false
morkF.AllowTakeMaterial = false;
morkF.TakeAllBurdenComplete = false;
MessageLog.GetInstance.Show($"订单【{ order.SuborderId}】,,翻转机上升完成,执行取调味品C料控制");
}
}
}
///
/// 开始烹饪
///
///
private void StartCookingTask()
{
if (morkF.InitialComplete && morkF.TakeMaterialQueue.Count > 0 && morkF.PutPotToKitchenComlete && !morkF.CookingLock)
{
MessageLog.GetInstance.Show($"订单【{morkF.CurrentOrderId}】开始烹饪");
morkF.CookingLock = true;
morkF.PotInPlace = morkF.PutPotToKitchenComlete;//锅到位
Task.Run(new Action(() =>
{
KitchenAdjustGears(3);
Task.Delay(1000).Wait(); //大火加热1s
AddOil();//注油
KitchenAdjustGears(2);
Task.Delay(2000).Wait(); //中火2s
TurnDownStatusDetect();//翻转机下降及检测
KitchenAdjustGears(1);//灶台1挡
TurnMachineGearsControl(1);//翻炒机1挡
Task.Delay(2000).Wait();
TurnMachineGearsControl(0);//关闭翻炒机
TurnUpStatusDetect();//翻转机上升及检测
TurnDownStatusDetect();//翻转机下降及检测
KitchenAdjustGears(3);//灶台3挡
TurnMachineGearsControl(3);//翻炒机3挡
Task.Delay(2000).Wait();//葱姜蒜断生
KitchenAdjustGears(1);//灶台1挡
TurnMachineGearsControl(0);//关闭翻炒机
TurnUpStatusDetect();//翻转机上升及检测
TurnDownStatusDetect();//翻转机下降及检测
KitchenAdjustGears(3);//灶台3挡
TurnMachineGearsControl(3);//翻炒机3挡
Task.Delay(2000).Wait();//B料断生
KitchenAdjustGears(1);//灶台1挡
TurnMachineGearsControl(0);//关闭翻炒机
TurnUpStatusDetect();//翻转机上升及检测
TurnDownStatusDetect();//翻转机下降及检测
KitchenAdjustGears(2);//灶台3挡
TurnMachineGearsControl(3);//翻炒机3挡
Task.Delay(2000).Wait();//煸炒入味
KitchenAdjustGears(0);//灶台0挡
TurnMachineGearsControl(0);//关闭翻炒机
TurnMachineOrientControl("Top");//翻转机上升
while (!morkF.TurnMachineUpComplete)
{
Task.Delay(5).Wait();
}
EventBus.EventBus.GetInstance().Publish(new WriteModel() { Address = "M8.1", Value = false });
morkF.CookingComplete = true; //烹饪完成
MessageLog.GetInstance.Show($"订单【{morkF.CurrentOrderId}】烹饪完成");
}));
}
}
///
/// 出餐
///
private void OutDishTask()
{
if (!morkF.RoobotIdle && morkF.CookingComplete && morkF.ProvidePlateComplete && !morkF.CleanModule)
{
RobotOutMeal();
MessageLog.GetInstance.Show($"订单【{morkF.CurrentOrderId}】正在出餐");
}
}
///
/// 洗锅放锅
///
private void CleanPotTask()
{
if (morkF.PlaceRinseTableComplete && !morkF.CleanModule)
{
CleanModuleControl("Start");
Task.Delay(5000).Wait();
CleanModuleControl("Stop");
}
}
///
/// 任务复位重启
///
//private void ResetProgram()
//{
// ThreadManage.GetInstance().StartLong(new Action(() =>
// {
// if (RTrig.GetInstance("ResetProgram").Start(morkF.DevicDataInite))
// {
// ThreadManage.GetInstance().StopTask("MainTask", new Action(() =>
// {
// ThreadManage.GetInstance().StopTask("ReadPLCData", new Action(() =>
// {
// morkF = null;
// morkF = new GVL_MorkF();
// ActionManage.GetInstance.Send("ResetProgram");
// ActionManage.GetInstance.Send("ClearOrders");
// ReadData();
// Main();
// }));
// }));
// }
// Thread.Sleep(10);
// }), "ResetProgram");
//}
///
/// 翻转机下降及状态检测
///
public void TurnDownStatusDetect()
{
TurnMachineOrientControl("Down");
while (!morkF.TurnMachineDownComplete)//等待翻转机下降完成
{
Task.Delay(5).Wait();
}
EventBus.EventBus.GetInstance().Publish(new WriteModel() { Address = "M8.3", Value = false });//复位
MessageLog.GetInstance.Show($"翻转机下降执行完成");
}
///
/// 翻转机上升及状态检测
///
public void TurnUpStatusDetect()
{
TurnMachineOrientControl("Top");//翻转机上升
morkF.AllowTakeMaterial = true;
while (!morkF.TurnMachineUpComplete || !morkF.TakeMaterialComplete)//等待翻转机上升完成以及取料完成
{
Task.Delay(5).Wait();
}
EventBus.EventBus.GetInstance().Publish(new WriteModel() { Address = "M8.1", Value = false });
MessageLog.GetInstance.Show("翻转机上升执行完成");
}
private void GetStatus(string key, Action action)
{
if (peripheralStatus.ContainsKey(key))
{
action((bool[])peripheralStatus[key]);//获取PLC指定地址的状态值
}
}
///
/// 写数据
///
///
///
private void WriteData(string address, object value)
{
EventBus.EventBus.GetInstance().Publish(new WriteModel() { Id = DeviceId, Address = address, Value = value });
}
///
/// 灶台档位调节
///
public void KitchenAdjustGears(int number)
{
switch (number)
{
case 0:
WriteData("352", new bool[] { false, false, false, false, false, false, false, false });//0000 0000
break;
case 1:
WriteData("352", new bool[] { true, false, false, false, false, false, false, false });//0000 0001
break;
case 2:
WriteData("352", new bool[] { false, true, false, false, false, false, false, false });//0000 0010
break;
case 3:
WriteData("352", new bool[] { true, true, false, false, false, false, false, false });//0000 0011
break;
case 4:
WriteData("352", new bool[] { false, false, true, false, false, false, false, false });//0000 0100
break;
case 5:
WriteData("352", new bool[] { true, false, true, false, false, false, false, false });//0000 0101
break;
case 6:
WriteData("352", new bool[] { false, true, true, false, false, false, false, false });//0000 0110
break;
case 7:
WriteData("352", new bool[] { true, true, true, false, false, false, false, false });//0000 0111
break;
case 8:
WriteData("355", new bool[] { false, false, false, true, false, false, false, false });//0000 1000
break;
case 9:
WriteData("352", new bool[] { true, false, false, true, false, false, false, false });//0000 1001
break;
}
MessageLog.GetInstance.Show($"订单【{morkF.CurrentOrderId}】,加热档位调至{number}挡");
}
public void TurnMaterialStore(int loc)
{
switch (loc)
{
case 1:
WriteData("M13.0", true);
break;
case 2:
WriteData("M13.1", true);
break;
case 3:
WriteData("M13.2", true);
break;
case 4:
WriteData("M13.3", true);
break;
case 5:
WriteData("M13.4", true);
break;
}
}
///
/// 供盘启动
///
public void StartTakePlate()
{
WriteData("M1.1", true);
}
///
/// 取锅到灶台
///
public void TakePotToKitchen()
{
WriteData("M14.0", true);
}
///
/// 机器人出餐并将锅放置清洗台位置
///
public void RobotOutMeal()
{
WriteData("M14.2", true);
}
///
/// 注油
///
public void AddOil()
{
WriteData("M2.7", true);
MessageLog.GetInstance.Show("开始注油");
while (!morkF.FallOilComplete)
{
Task.Delay(5).Wait();
}
MessageLog.GetInstance.Show("注油完成");
}
///
/// 翻转机方向控制
///
///
public void TurnMachineOrientControl(string orientation)
{
switch (orientation)
{
case "Top": WriteData("M8.0", true); break;
// case "Middle": WriteData("", 1); break;
case "Down": WriteData("M8.2", true); break;
}
MessageLog.GetInstance.Show($"翻转机执行{orientation}操作");
}
///
/// 翻转机档位控制
///
public void TurnMachineGearsControl(int gear)
{
switch (gear)
{
case 0: WriteData("M7.0", true); break;//关闭翻炒机
case 1: WriteData("M7.1", true); break;//翻炒机1挡
case 2: WriteData("M7.2", true); break;//翻炒机2挡
case 3: WriteData("M7.3", true); break;//翻炒机3挡
}
MessageLog.GetInstance.Show($"翻转机档位调至{gear}挡");
}
///
/// 取A,B,C料
///
public void TakeBurden(int loc)
{
WriteData("M14.1", true);//机器人取料
}
///
/// 清洗模组1启停控制
///
public void CleanModuleControl(string status)
{
if (status == "Start")
{
WriteData("M1.4", true);
}
if (status == "Stop")
{
WriteData("M1.4", false);
}
MessageLog.GetInstance.Show($"清洗模组执行【{status}】操作");
}
public override void Stop()
{
}
}
}