using BPA.Message.Enum; using BPASmartClient.Device; using BPASmartClient.MorkCL.Model.Json; using BPASmartClient.MorkCL.Server; using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BPASmartClient.MorkCL { public class Control_MorkCL : BaseDevice { public override DeviceClientType DeviceType => DeviceClientType.MORKCL; GVL_MorkCL morkCL = new GVL_MorkCL(); Alarm alarm = new Alarm(); /// /// 设备信息集合 /// ConcurrentDictionary devices { get; set; } = new(); /// /// 任务集合 /// ConcurrentDictionary TaskList { get; set; } = new(); public override void DoMain() { MonitorViewModel.DeviceId = DeviceId; //注册本地配方接收 ActionManage.GetInstance.Register(new Action(o => { if (o != null && o is ControlData cd) morkCL.cds.Enqueue(cd); }), NotifyTopic.FormulaDistribution); //初始化通讯对象 devices.TryAdd(EDeviceType.炒锅1, new FryingPanServer()); devices.TryAdd(EDeviceType.炒锅2, new FryingPanServer()); devices.TryAdd(EDeviceType.机器人, new RobotServer()); devices.TryAdd(EDeviceType.压力锅, new PressureCookerServer()); devices.TryAdd(EDeviceType.外部设备, new OtherServer()); //devices[EDeviceType.炒锅1].Init(ConnectPar.FryingPanIP1); //devices[EDeviceType.炒锅2].Init(ConnectPar.FryingPanIP2); //devices[EDeviceType.机器人].Init(ConnectPar.RobotIP); //devices[EDeviceType.压力锅].Init(ConnectPar.PressureCookerIP); //devices[EDeviceType.外部设备].Init(ConnectPar.PPortName); #region 读取本地文件数据 Json.Read(); #endregion } public override void MainTask() { if (morkCL.cds.Count > 0 && !TaskList.ContainsKey(morkCL.cds.ElementAt(0).DeviceType)) { if (morkCL.cds.TryDequeue(out ControlData cd)) { TaskList.TryAdd(cd.DeviceType, new TaskServer()); TaskList[cd.DeviceType].TaskName = $"{cd.DeviceType.ToString()}-{cd.Name}"; TaskList[cd.DeviceType].RunTask = new Task(new Action(() => { FryingPanControl(cd.DeviceType, cd); })); TaskList[cd.DeviceType].RunTask.Start(); } } //检查到有任务完成后将对已完成的任务进行清理 var res = TaskList.FirstOrDefault(p => p.Value.IsComplete).Key; if (TaskList.ContainsKey(res)) TaskList.TryRemove(res, out _); } private void FryingPanControl(EDeviceType et, ControlData cd) { DeviceProcessLogShow($"开始执行-{cd.Name} 任务"); if (TaskList.ContainsKey(et) && TaskList[et].Cts.IsCancellationRequested) return; while (cd.ControlFuncs.Count > 0) { if (cd.ControlFuncs.TryDequeue(out FuncModel fm)) { var device = (FryingPanServer)devices[et]; var robot = (RobotServer)devices[EDeviceType.机器人]; int index = (ushort)et - 1; DeviceProcessLogShow($"{cd.Name}-任务执行-{fm.eFunc.ToString()}-步骤"); switch (fm.eFunc) { case EFunc.搅拌启动: device.MixingFrequencySet(fm.funcPars.ElementAt(0).ParValue); device.StirStartOrStop = true; break; case EFunc.搅拌停止: device.StirStartOrStop = false; break; case EFunc.加热启动: device.HeatingGearSet(fm.funcPars.ElementAt(0).ParValue); device.HeatStartOrStop = true; break; case EFunc.加热停止: device.HeatStartOrStop = false; break; case EFunc.添加调料: var Seasoning = SqliteHelper.GetInstance.GetSeasoning().FirstOrDefault(p => p.Id == fm.funcPars.ElementAt(0).Id); if (Seasoning != null) { device.CuttingControl(Seasoning.Loc.ToString(), fm.funcPars.ElementAt(1).ParValue); } break; case EFunc.添加主料: case EFunc.添加辅料: MaterialBase mb = new MaterialBase(); if (fm.eFunc == EFunc.添加辅料) mb = SqliteHelper.GetInstance.GetAccessories().FirstOrDefault(p => p.Id == fm.funcPars.ElementAt(0).Id); else mb = SqliteHelper.GetInstance.GetIngredients().FirstOrDefault(p => p.Id == fm.funcPars.ElementAt(0).Id); robot.RobotTaskControl(mb.Loc.ToString(), et); device.FeedingLocFB.Wait(Cts: TaskList[et].Cts);//等待炒锅到投料位置 robot.MaterialPouringRequest[index].Wait(Cts: TaskList[et].Cts); //等待机器人到投料位置 robot.AllowPourVegetables(et);//允许机器人开始投料 robot.MaterialPouringComplete[index].Wait(Cts: TaskList[et].Cts); //等待机器人投料完成 break; case EFunc.炒锅回原点位: device.FryingPanHome = true; break; case EFunc.出餐启动: robot.RobotTaskControl("1", et);//取空盆 robot.DiningOutRequest[index].Wait(Cts: TaskList[et].Cts);//等待机器人到出菜位置 device.DiningOutStart = true; break; case EFunc.炒锅清洗: device.FryingPanClear = true; break; case EFunc.炒锅回投料位置: device.FryingPanFeedingLoc = true; break; case EFunc.去指定炒制位: device.SetStirFryingLoc(fm.funcPars.ElementAt(0).ParValue); break; case EFunc.炒制: if (int.TryParse(fm.funcPars.ElementAt(0).ParValue, out int time)) Task.Delay(time * 1000).Wait(TaskList[et].Cts.Token); break; default: break; } } } } public override void ReadData() { } public override void ResetProgram() { morkCL = null; morkCL = new GVL_MorkCL(); } public override void SimOrder() { } public override void Stop() { } } }