|
- 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 BPA.Models;
- using static BPASmartClient.EventBus.EventBus;
- using BPASmartClient.Model.小炒机;
- using BPASmartClient.MorkF.Model;
- using System.Text;
- using System.Collections.Concurrent;
- using System.Windows.Documents;
- using BPASmartClient.Business;
-
- namespace BPASmartClient.MorkF
- {
- public class Control_MorkF : BaseDevice
- {
-
- /// <summary>
- /// while循环最大sleep次数
- /// </summary>
- private const int sleepCount = 20;
-
- /// <summary>
- /// while循环每次sleep时间
- /// </summary>
- private const int sleepTime = 500;
-
- /// <summary>
- /// 菜品库while循环最大sleep次数
- /// </summary>
- private const int materialSleepCount = 600;
-
- /// <summary>
- /// 菜品库while循环每次sleep时间
- /// </summary>
- private const int materialSleepTime = 100;
-
- /// <summary>
- /// 初始化炒锅数量
- /// </summary>
- private int count = 2;
-
- /// <summary>
- /// 菜品盒下移高度,相对于坐标系
- /// </summary>
- private const int materialBoxHigh = 4080;
-
- //菜品库一层的高度
- private const int materialHigh = 34500;
-
- /// <summary>
- /// 菜品库第一层的起始基点,为最下层盒子的中间点
- /// </summary>
- private const int materialOne = 34500;
-
- /// <summary>
- /// 菜品库第二层的起始基点,为最下层盒子的中间点
- /// </summary>
- private const int materialTwo = 34500;
-
- /// <summary>
- /// 菜品库第三层的起始基点,为最下层盒子的中间点
- /// </summary>
- private const int materialThree = 34500;
-
- //传感器到菜品库的高度(坐标系)
- private const int materialLaserHigh = 34500;
-
- //传感器到菜品库的高度(厘米)
- private const int materialLaserHighCM = 29;
-
- /// <summary>
- /// 传感器到隔板的距离(坐标系)
- /// </summary>
- private const int clapboardLaserHigh = 10000;
-
- //炒锅1倒菜的xy坐标
- private const int materialPutPositionXFry1 = 70000;
- private const int materialPutPositionYFry1 = 40000;
-
- //炒锅2倒菜的xy坐标
- private const int materialPutPositionXFry2 = 70000;
- private const int materialPutPositionYFry2 = 40000;
-
- //菜品库机械爪放置菜品距离
- private const int materialToFryDistance = 10000;
-
- //当前炒锅,默认为1号炒锅
- private int fryIndex = 0;
-
- public override DeviceClientType DeviceType => DeviceClientType.MORKCS;
- public AutoResetEvent minorReset = new AutoResetEvent(false);
- public AutoResetEvent mainReset = new AutoResetEvent(false);
-
- /// <summary>
- /// 炒锅编号与炒锅实例
- /// </summary>
- Dictionary<int, GVL_MorkF> morkFs = new Dictionary<int, GVL_MorkF>();//全局对象声明
-
- /// <summary>
- /// 菜品库对象
- /// </summary>
- ML_MorkF ml_morkf = new ML_MorkF();
-
- /// <summary>
- /// 小炒菜单集合
- /// </summary>
- public static List<StirFryGoods> LocalstirFryGoods = new List<StirFryGoods>();
-
- /// <summary>
- /// 待炒小炒队列
- /// </summary>
- private ConcurrentQueue<OrderLocInfo> StirFryGoodsQuenes = new ConcurrentQueue<OrderLocInfo>();
-
- /// <summary>
- /// 炒锅炒制线程名称
- /// </summary>
- private const String striConst = "炒锅{0}炒制{1}线程";
-
- /// <summary>
- /// 当前炒制菜品
- /// </summary>
- private OrderLocInfo[] nowStirFryGood = new OrderLocInfo[2];
-
- /// <summary>
- /// 待取菜队列
- /// </summary>
- private ConcurrentQueue<MaterialOperation> materialOperationQuenes = new ConcurrentQueue<MaterialOperation>();
-
- List<int> resultorder = new List<int>();//调试变量
-
- /// <summary>
- /// 入口
- /// </summary>
- public override void DoMain()
- {
- IsHealth = true;
-
- for (int i = 0; i < count; i++)
- {
- morkFs.Add(i, new GVL_MorkF());
- }
-
- DataParse();//数据解析
- CommandRegist();//调试
- ServerInit();
- DeviceProcessLogShow("MORKF 设备初始化完成");
- ///初始化菜品库
- //FoodLibInit();
- Task2ReadMaterialData();
-
- //Json<MaterialSurplus>.Read();
- }
-
- /// <summary>
- /// 主任务
- /// </summary>
- public override void MainTask()
- {
- //炒锅炒制线程
- MainProcessExcute();
- //菜品库操作线程
- MainProcessMaterial();
- //MinorProcessExcute();
- //SingleProcess();
- }
-
- private void Task2ReadMaterialData()
- {
- ThreadManage.GetInstance().StartLong(new Action(() => {
-
- //获取定位到达状态
- GetStatus("M10.0", new Action<object>((objects) =>
- {
- if (objects is bool[] bools)
- {
-
- //ml_morkf.ArriveComplete = bools[i];
- //小炒定点到达上升沿信号捕获
- if (RTrig.GetInstance("SmartArriveComplete").Start(bools[0]))
- {
- if (isInitialArrive == false)
- {
- MessageLog.GetInstance.Show("到达上升沿为true,isInitialArrive is false");
- isInitialArrive = true;
- }
- else
- {
- MessageLog.GetInstance.Show("到达上升沿为true");
- ml_morkf.ArriveComplete = true;
- isInitialArrive = true;
- }
- }
- }
-
- }), 2);
- }), "ReadMaterialSmartArriveCompleteData", true);
-
- ThreadManage.GetInstance().StartLong(new Action(() => {
- ReadMaterialData();
-
- Thread.Sleep(10);
- }), "ReadMaterialData", true);
- }
-
- private void ReadMaterialData()
- {
- //获取激光距离
- GetStatus("VW270", new Action<object>((objects) =>
- {
- if (objects is ushort[] bools)
- {
- for (int i = 0; i < 1; i++)
- {
- ml_morkf.LaserDistance = bools[i];
- }
- }
- }), 2);
-
- //获取坐标X
- GetStatus("VD828", new Action<object>((objects) =>
- {
- if (objects is int bools)
- {
- ml_morkf.ArmPositionX = bools;
- }
- }), 2);
-
- //获取坐标Y
- GetStatus("VD832", new Action<object>((objects) =>
- {
- if (objects is int bools)
- {
- ml_morkf.ArmPositionY = bools;
- }
- }), 2);
-
- //获取爪子到达位置
- GetStatus("M11.0", new Action<object>((objects) =>
- {
- if (objects is bool[] bools && bools.Length > 3)
- {
- ml_morkf.PawArrivePortOne = bools[0];
- ml_morkf.PawArrivePortTwo = bools[1];
- ml_morkf.PawArrivePortThree = bools[2];
- }
- }), 2);
-
- //获取总初始化完成
- GetStatus("M10.2", new Action<object>((objects) =>
- {
- if (objects is bool[] bools)
- {
- for (int i = 0; i < 1; i++)
- {
- ml_morkf.InitialComplete = bools[i];
- }
- }
- }), 2);
-
- //抓手初始化完成
- GetStatus("M10.3", new Action<object>((objects) =>
- {
- if (objects is bool[] bools)
- {
- for (int i = 0; i < 1; i++)
- {
- ml_morkf.PawInitialComplete = bools[i];
- }
- }
- }), 2);
-
- }
-
- public override void Stop()
- {
- IsHealth = false;
- }
-
- #region 调试代码
- public void CommandRegist()
- {
- #region 设备控制
- ActionManage.GetInstance.Register(PLCInite, "InitCommand");
- ActionManage.GetInstance.Register(StartOrder, "StartOrder");
- ActionManage.GetInstance.Register(StartOrderMain, "开始下单");
- ActionManage.GetInstance.Register(StartLocalOrder, "StartLocalOrder");
- ActionManage.GetInstance.Register(StopLocalOrder, "StopLocalOrder");
- #endregion
-
- #region 菜品库
- ActionManage.GetInstance.Register(new Action(() =>
- {
- ThreadManage.GetInstance().Start(new Action(() =>
- {
- FoodLibInit();
- }), "FoodLibInit");
- }), "FoodLibInit");
- ActionManage.GetInstance.Register(new Action<object>((o) =>
- {
- ThreadManage.GetInstance().Start(new Action(() =>
- {
- Electromagnetism(o);
- }), "Electromagnetism");
- }), "Electromagnetism");
- ActionManage.GetInstance.Register(new Action(() =>
- {
- ThreadManage.GetInstance().Start(new Action(() =>
- {
- PawToPoint1();
- }), "PawToPoint1");
- }), "PawToPoint1");
- ActionManage.GetInstance.Register(new Action(() =>
- {
- ThreadManage.GetInstance().Start(new Action(() =>
- {
- PawToPoint2();
- }), "PawToPoint2");
- }), "PawToPoint2");
- ActionManage.GetInstance.Register(new Action(() =>
- {
- ThreadManage.GetInstance().Start(new Action(() =>
- {
- PawToPoint3();
- }), "PawToPoint3");
- }), "PawToPoint3");
- ActionManage.GetInstance.Register(new Action(() =>
- {
- ThreadManage.GetInstance().Start(new Action(() =>
- {
- GetDistance_1();
- }), "GetDistance_1");
- }), "GetDistance_1");
- ActionManage.GetInstance.Register(new Action(() =>
- {
- ThreadManage.GetInstance().Start(new Action(() =>
- {
- GetDistance_2();
- }), "GetDistance_2");
- }), "GetDistance_2");
- ActionManage.GetInstance.Register(new Action(() =>
- {
- ThreadManage.GetInstance().Start(new Action(() =>
- {
- GetDistance_3();
- }), "GetDistance_3");
- }), "GetDistance_3");
- ActionManage.GetInstance.Register(PawTurnFront, "PawTurnFront");
- ActionManage.GetInstance.Register(PawTurnBack, "PawTurnBack");
- ActionManage.GetInstance.Register(new Action<object>((list) =>
- {
- ThreadManage.GetInstance().Start(new Action(() =>
- {
- if (list is List<int> list_int)
- {
- if (list_int.Count == 2)
- {
- SetArmPosition(list_int[0], list_int[1]);
- }
- }
- }), "SetArmPosition");
- }), "SetArmPosition");
- ActionManage.GetInstance.Register(new Action<object>((list) =>
- {
- if (list is bool startOrStop)
- {
- ArmRunUp(startOrStop);
- }
- }), "ArmRunUp");
- ActionManage.GetInstance.Register(new Action<object>((list) =>
- {
- if (list is bool startOrStop)
- {
- ArmRunDown(startOrStop);
- }
- }), "ArmRunDown");
- ActionManage.GetInstance.Register(new Action<object>((list) =>
- {
- if (list is bool startOrStop)
- {
- ArmRunLeft(startOrStop);
- }
- }), "ArmRunLeft");
- ActionManage.GetInstance.Register(new Action<object>((list) =>
- {
- if (list is bool startOrStop)
- {
- ArmRunRight(startOrStop);
- }
- }), "ArmRunRight");
- ActionManage.GetInstance.Register(new Action<object>((list) =>
- {
- ThreadManage.GetInstance().Start(new Action(() =>
- {
- if (list is List<int> list_int)
- {
- if (list_int.Count == 2)
- {
- SetNowPosition(list_int[0], list_int[1]);
- }
- }
- }), "SetNowPosition");
- }), "SetNowPosition");
- #endregion
- //ActionManage.GetInstance.Register(PLCInite, "InitCommand");
- #region 配料控制
- ActionManage.GetInstance.Register(new Action<object>((o) =>
- {
- ThreadManage.GetInstance().Start(new Action(() =>
- {
- OutSeasoning(o, fryIndex);
- }), "OutMaterials");
- }), "OutMaterials");
- //ActionManage.GetInstance.Register(OutSeasoning, "OutMaterials");
- #endregion
-
- #region 炒锅
- ActionManage.GetInstance.Register(new Action(() =>
- {
- ThreadManage.GetInstance().Start(new Action(() =>
- {
- Plc1Reset(fryIndex);
- }), "Plc1Reset");
- }), "Plc1Reset");
-
- ActionManage.GetInstance.Register(new Action(() =>
- {
- ThreadManage.GetInstance().Start(new Action(() =>
- {
- AddOil();
- }), "AddOil");
- }), "AddOil");
-
- ActionManage.GetInstance.Register(new Action(() =>
- {
- ThreadManage.GetInstance().Start(new Action(() =>
- {
- StartFire(fryIndex);
- }), "StartFire");
- }), "StartFire");
-
- ActionManage.GetInstance.Register(new Action(() =>
- {
- ThreadManage.GetInstance().Start(new Action(() =>
- {
- StopFire(fryIndex);
- }), "StopFire");
- }), "StopFire");
-
- ActionManage.GetInstance.Register(new Action(() =>
- {
- ThreadManage.GetInstance().Start(new Action(() =>
- {
- StartStir(fryIndex);
- }), "StartStir");
- }), "StartStir");
-
- ActionManage.GetInstance.Register(new Action(() =>
- {
- ThreadManage.GetInstance().Start(new Action(() =>
- {
- StopStir(fryIndex);
- }), "StopStir");
- }), "StopStir");
-
- ActionManage.GetInstance.Register(new Action(() =>
- {
- ThreadManage.GetInstance().Start(new Action(() =>
- {
- OutFood(fryIndex);
- }), "OutFood");
- }), "OutFood");
-
- ActionManage.GetInstance.Register(new Action(() =>
- {
- ThreadManage.GetInstance().Start(new Action(() =>
- {
- StirArmGoOrigin(fryIndex);
- }), "StirArmGoOrigin");
- }), "StirArmGoOrigin");
-
- ActionManage.GetInstance.Register(new Action(() =>
- {
- ThreadManage.GetInstance().Start(new Action(() =>
- {
- StirArmGoWork(fryIndex);
- }), "StirArmGoWork");
- }), "StirArmGoWork");
-
- ActionManage.GetInstance.Register(new Action(() =>
- {
- ThreadManage.GetInstance().Start(new Action(() =>
- {
- HBOTGoWork(fryIndex);
- }), "HBOTGoWork");
- }), "HBOTGoWork");
-
- ActionManage.GetInstance.Register(new Action(() =>
- {
- ThreadManage.GetInstance().Start(new Action(() =>
- {
- OutMeal(fryIndex);
- }), "OutMeal");
- }), "OutMeal");
-
- ActionManage.GetInstance.Register(new Action<object>((o) =>
- {
- ThreadManage.GetInstance().Start(new Action(() =>
- {
- SetFire(o, fryIndex);
- }), "SetFire");
- }), "SetFire");
-
- ActionManage.GetInstance.Register(new Action<object>((o) =>
- {
- ThreadManage.GetInstance().Start(new Action(() =>
- {
- SetFry(o);
- }), "SetFry");
- }), "SetFry");
-
- ActionManage.GetInstance.Register(new Action<object>((o) =>
- {
- ThreadManage.GetInstance().Start(new Action(() =>
- {
- SetStir(o, fryIndex);
- }), "SetStir");
- }), "SetStir");
- #endregion
-
-
- }
-
- /// <summary>
- /// 本地菜单下单
- /// </summary>
- private void StartOrder(object o)
- {
- if (o == null) return;
- if (o is int goodId)
- {
- var res = LocalstirFryGoods?.FirstOrDefault(p => p.GoodsKey == goodId);//匹配订单对应制作流程
- if (res != null)
- {
- /* morkF.listStirBom.Add(res.StirFryBomInfo);*///添加订单制作流程
- if (StirFryGoodsQuenes.Count > 0) return;
- StirFryGoodsQuenes.Enqueue(new OrderLocInfo()
- {
- SuborderId = Guid.NewGuid().ToString(),
- StirPotActions = res.StirPotActions,
- GoodName = "本地菜品"
- });
- MessageLog.GetInstance.Show($"添加本地订单{res.GoodsKey}");
- }
- }
- }
-
- /// <summary>
- /// 本地菜单下单
- /// </summary>
- private void StartOrderMain(object o)
- {
- if (o == null) return;
- if (o is string goodId)
- {
- var res = LocalstirFryGoods?.FirstOrDefault(p => p.GoodsKey.ToString() == goodId);//匹配订单对应制作流程
- if (res != null)
- {
- /* morkF.listStirBom.Add(res.StirFryBomInfo);*///添加订单制作流程
- if (StirFryGoodsQuenes.Count > 0) return;
- StirFryGoodsQuenes.Enqueue(new OrderLocInfo()
- {
- SuborderId = Guid.NewGuid().ToString(),
- StirPotActions = res.StirPotActions,
- GoodName = "本地菜品"
- });
- MessageLog.GetInstance.Show($"添加本地订单{res.GoodsKey}");
- }
- }
- }
-
- private void StartLocalOrder()
- {
- if (StirFryGoodsQuenes.Count > 0) return;//只能一个一个做
- if (Json<LocalPotStep>.Data.LocalstirFryGoods.StirPotActions.Count > 0)
- {
- StirFryGoodsQuenes.Enqueue(new OrderLocInfo()
- {
- SuborderId = Guid.NewGuid().ToString(),
- StirPotActions = Json<LocalPotStep>.Data.LocalstirFryGoods.StirPotActions,
- GoodName = "本地菜品"
- });
- MessageLog.GetInstance.Show($"添加本地模拟的订单{Json<LocalPotStep>.Data.LocalstirFryGoods.GoodsKey}");
- }
- }
-
- /// <summary>
- /// 停止本地菜单炒制
- /// </summary>
- private void StopLocalOrder(int num = -1)
- {
- //判断当前是否有炒制菜品
- if (nowStirFryGood == null) return;
-
- //根据morkFs中是否有num执行不同的停止操作
- if (!morkFs.ContainsKey(num))
- {
- //根据调试界面的当前炒锅编号停止炒制线程
- ThreadManage.GetInstance().StopTask(String.Format(striConst, fryIndex.ToString(), nowStirFryGood[fryIndex].GoodName), new Action(() => { Plc1Reset(fryIndex); }));
- }
- else
- {
- //根据炒锅编号停止炒制线程
- ThreadManage.GetInstance().StopTask(String.Format(striConst, fryIndex.ToString(), nowStirFryGood[num].GoodName), new Action(() => { Plc1Reset(num); }));
- }
- }
-
- /// <summary>
- /// 重置程序
- /// </summary>
- public override void ResetProgram()
- {
- IsHealth = true;
- morkFs.Clear();
- morkFs = new Dictionary<int, GVL_MorkF>();
- ml_morkf = new ML_MorkF();
-
- //根据编号new炒锅实例对象
- for (int i = 0; i < count; i++)
- {
- morkFs.Add(i, new GVL_MorkF());
- }
- }
-
- #endregion
-
- #region 公用PLC方法
- /// <summary>
- /// 获取设备PLC的所有状态
- /// </summary>
- /// <param name="key"></param>
- /// <param name="action"></param>
- /// <param name="num">炒锅编号</param>
- private void GetStatus(string key, Action<object> action, int num)
- {
- if (dicPort2peripheralStatus.ContainsKey(num))
- {
- if (dicPort2peripheralStatus[num].ContainsKey(key))
- {
- action((object)dicPort2peripheralStatus[num][key]);//获取PLC指定地址的状态值
- }
- }
- }
-
- /// <summary>
- /// PLC数据读取
- /// </summary>
- public override void ReadData()
- {
-
- for (int i = 0; i < morkFs.Count; i++)
- {
- GetStatus("LB50", new Action<object>((objects) =>
- {
- if (!morkFs.ContainsKey(i))
- {
- return;
- }
- if (objects is bool[] bools)
- {
- morkFs[i].FryPot1_InitialComplete = bools[0];
- morkFs[i].FryPot1_HOBTPut = bools[1];
- morkFs[i].FryPot1_HOBTGet = bools[2];
- morkFs[i].FryPot1_MaterialIntoPot = bools[3];
- morkFs[i].OutFoodCompelete = bools[4];
- morkFs[i].CanOutFood = bools[5];
- morkFs[i].GetFoodCompelete = bools[6];
- morkFs[i].CanOutPotWashingWater = bools[7];
- morkFs[i].ArmOnOrigin = bools[8];
- morkFs[i].ArmOnWorking = bools[9];
- morkFs[i].PotOnOrigin = bools[10];
- }
-
- }), i);
- }
- for (int j = 0; j < morkFs.Count; j++)
- {
- GetStatus("LB74", new Action<object>((objects) =>
- {
- if (!morkFs.ContainsKey(j))
- {
- return;
- }
-
- if (objects is bool[] bools)
- {
- for (int i = 0; i < 14; i++)
- {
- morkFs[j].PassWay1_Compelete[i] = bools[i];
- }
- }
-
- }), j);
- }
- for (int i = 0; i < morkFs.Count; i++)
- {
- if (!morkFs.ContainsKey(i))
- {
- return;
- }
-
- GetStatus("LB90", new Action<object>((objects) =>
- {
- if (objects is bool[] bools)
- {
- morkFs[i].AutoMode = bools[0];
- }
-
- }), i);
- }
-
- }
-
- #endregion
-
- #region 菜品库PLC操作方法
- /// <summary>
- /// 菜品库数据写入
- /// </summary>
- /// <param name="address"></param>
- /// <param name="value"></param>
- private void MaterailLibrary_Write(string address, object value, int num = 2)
- {
- WriteControlExact(address, value, num);
- }
- /// <summary>
- /// 菜品库初始化
- /// </summary>
- public bool FoodLibInit()
- {
- MaterailLibrary_Write("M0.2", true);
-
- Thread.Sleep(500);
-
- for (int i = 0; i < materialSleepCount && !ml_morkf.InitialComplete; i++)
- {
- Thread.Sleep(materialSleepTime);
- if (i >= materialSleepCount - 1)
- {
- MessageLog.GetInstance.Show("菜品库初始化超时");
- return false;
- }
- }
-
- return true;
- }
- /// <summary>
- /// 电磁阀启停
- /// </summary>
- /// <param name="o">电磁阀启停:true:启动。false:停止。</param>
- public void Electromagnetism(object o)
- {
- if (!ml_morkf.InitialComplete)
- {
- return;
- }
-
- if (o == null) return;
- if (o is List<bool> bs && bs.Count == 1)
- {
- MaterailLibrary_Write("M1.3", bs[0]);
- }
- }
-
- public bool PawToPoint1()
- {
- if (!ml_morkf.InitialComplete)
- {
- return false;
- }
-
- MessageLog.GetInstance.Show("爪子去到1号位");
- MaterailLibrary_Write("M1.0", true);
- for (int i = 0; i < materialSleepCount && !ml_morkf.PawArrivePortOne; i++)
- {
- Thread.Sleep(materialSleepTime);
- if (i >= materialSleepCount - 1)
- {
- MessageLog.GetInstance.Show("爪子去到1号位超时");
- return false;
- }
- }
-
- Thread.Sleep(300);
-
- MessageLog.GetInstance.Show("爪子去到1号位完成");
- return true;
- }
-
- public bool PawToPoint2()
- {
- if (!ml_morkf.InitialComplete)
- {
- return false;
- }
-
- MessageLog.GetInstance.Show("爪子去到2号位");
- MaterailLibrary_Write("M1.1", true);
- for (int i = 0; i < materialSleepCount && !ml_morkf.PawArrivePortTwo; i++)
- {
- Thread.Sleep(materialSleepTime);
- if (i >= materialSleepCount - 1)
- {
- MessageLog.GetInstance.Show("爪子去到2号位超时");
- return false;
- }
- }
-
- Thread.Sleep(300);
-
- MessageLog.GetInstance.Show("爪子去到2号位完成");
- return true;
- }
-
- public bool PawToPoint3()
- {
- if (!ml_morkf.InitialComplete)
- {
- return false;
- }
-
- MessageLog.GetInstance.Show("爪子去到3号位");
- MaterailLibrary_Write("M1.2", true);
- for (int i = 0; i < materialSleepCount && !ml_morkf.PawArrivePortThree; i++)
- {
- Thread.Sleep(materialSleepTime);
- if (i >= materialSleepCount - 1)
- {
- MessageLog.GetInstance.Show("爪子去到3号位超时");
- return false;
- }
- }
-
- Thread.Sleep(300);
-
- MessageLog.GetInstance.Show("爪子去到3号位完成");
- return true;
- }
-
- public bool GetDistance_1()
- {
- if (!ml_morkf.InitialComplete)
- {
- return false;
- }
-
- MaterailLibrary_Write("M0.3", true);
- MaterailLibrary_Write("M0.3", false);
- Thread.Sleep(300);
-
- MessageLog.GetInstance.Show("1号测距开启完成");
- return true;
- }
- public bool GetDistance_2()
- {
- if (!ml_morkf.InitialComplete)
- {
- return false;
- }
-
- MaterailLibrary_Write("M0.4", true);
- MaterailLibrary_Write("M0.4", false);
- Thread.Sleep(300);
-
- MessageLog.GetInstance.Show("2号测距开启完成");
- return true;
- }
- public bool GetDistance_3()
- {
- if (!ml_morkf.InitialComplete)
- {
- return false;
- }
-
- MaterailLibrary_Write("M0.5", true);
- MaterailLibrary_Write("M0.5", false);
- Thread.Sleep(300);
-
- MessageLog.GetInstance.Show("3号测距开启完成");
- return true;
- }
-
- /// <summary>
- /// 根据x坐标获取传感器距离
- /// </summary>
- /// <param name="x"></param>
- /// <returns></returns>
- public bool GetDistance(int x)
- {
- bool ret = true;
- //是否在第一层菜品库
- if (x > 0 && x <= materialLaserHigh)
- {
- //获取传感器1的距离
- ret &= GetDistance_1();
-
- Thread.Sleep(400);
-
- //获取传感器1的距离
- ret &= GetDistance_1();
- }
- //是否在第二层菜品库
- else if (x > materialLaserHigh && x <= (2 * materialLaserHigh + clapboardLaserHigh))
- {
- //获取传感器2的距离
- ret &= GetDistance_2();
-
- Thread.Sleep(400);
-
- ret &= GetDistance_2();
- }
- //是否在第三层菜品库
- else if (x > (2 * materialLaserHigh + clapboardLaserHigh) && x <= (3 * materialLaserHigh + 2 * clapboardLaserHigh))
- {
- //获取传感器3的距离
- ret &= GetDistance_3();
-
- Thread.Sleep(400);
-
- ret &= GetDistance_3();
- }
- if (!ret)
- {
- MessageLog.GetInstance.Show($"错误:X轴:{x}获取传感器数据出错");
- }
- return ret;
- }
-
- public void PawTurnFront()
- {
- MaterailLibrary_Write("", true);
- }
- public void PawTurnBack()
- {
- MaterailLibrary_Write("", true);
- }
-
- /// <summary>
- /// 设定机械臂的位置
- /// </summary>
- /// <param name="x"></param>
- /// <param name="y"></param>
- public bool SetArmPosition(int x, int y)
- {
- if (!ml_morkf.InitialComplete)
- {
- return false;
- }
-
- //取反
- x -= 2 * x;
- y -= 2 * y;
- if (x > 0 || y > 0)
- {
- MessageLog.GetInstance.Show($"机械臂移动到[{x},{y}]非法");
- return false;
- }
- MaterailLibrary_Write("VD836", x);
- Thread.Sleep(100);
- MaterailLibrary_Write("VD840", y);
-
- Thread.Sleep(200);
-
- MessageLog.GetInstance.Show($"机械臂移动到[{x},{y}]");
-
- //到达置为false
- ml_morkf.ArriveComplete = false;
-
- //定位启动
- MaterailLibrary_Write("M0.0", true);
-
- Thread.Sleep(400);
-
- for (int i = 0; i < materialSleepCount && (!ml_morkf.ArriveComplete); i++)
- {
- Thread.Sleep(materialSleepTime);
- //若是目标点与当前坐标一致,跳出循环
- if((ml_morkf.ArmPositionX == x && ml_morkf.ArmPositionY == y))
- {
- MessageLog.GetInstance.Show($"机械臂已移动到[{ml_morkf.ArmPositionX},{ml_morkf.ArmPositionY}]");
- break;
- }
- if (i >= materialSleepCount - 1)
- {
- MessageLog.GetInstance.Show("机械臂移动操作超时");
- return false;
- }
- }
-
- MessageLog.GetInstance.Show("机械臂移动操作完成");
-
- Thread.Sleep(500);
-
- return true;
- }
-
- /// <summary>
- /// 设定机械臂的当前坐标
- /// </summary>
- /// <param name="x"></param>
- /// <param name="y"></param>
- public void SetNowPosition(int x, int y)
- {
- //取反
- x -= 2 * x;
- y -= 2 * y;
- MessageLog.GetInstance.Show($"设置机械臂当前坐标为[{x},{y}]");
- MaterailLibrary_Write("VD828", x);
- Thread.Sleep(200);
- MaterailLibrary_Write("VD832", y);
- Thread.Sleep(200);
- }
-
- /// <summary>
- /// 机械臂向上移动开关
- /// </summary>
- /// <param name="startOrStop">true:开启,false:关闭</param>
- public void ArmRunUp(bool startOrStop)
- {
- MaterailLibrary_Write("M5.3", startOrStop);
- Thread.Sleep(200);
- }
-
- /// <summary>
- /// 机械臂向下移动开关
- /// </summary>
- /// <param name="startOrStop">true:开启,false:关闭</param>
- public void ArmRunDown(bool startOrStop)
- {
- MaterailLibrary_Write("M5.4", startOrStop);
- Thread.Sleep(200);
- }
-
- /// <summary>
- /// 机械臂向左移动开关
- /// </summary>
- /// <param name="startOrStop">true:开启,false:关闭</param>
- public void ArmRunLeft(bool startOrStop)
- {
- MaterailLibrary_Write("M5.5", startOrStop);
- Thread.Sleep(200);
- }
-
- /// <summary>
- /// 机械臂向右移动开关
- /// </summary>
- /// <param name="startOrStop">true:开启,false:关闭</param>
- public void ArmRunRight(bool startOrStop)
- {
- MaterailLibrary_Write("M5.6", startOrStop);
- Thread.Sleep(200);
- }
-
- //菜品库锁
- object lock_Materail = new object();
-
- /// <summary>
- /// 取菜操作
- /// </summary>
- /// <param name="x">取菜x轴坐标</param>
- /// <param name="y">取菜y轴坐标</param>
- /// <param name="fryNum">炒锅编号</param>
- public bool GetMaterail(int x, int y, int fryNum)
- {
- //设置到抓菜处
- if (!SetArmPosition(x, y))
- {
- return false;
- }
-
- if (!GetDistance(x))
- {
- MessageLog.GetInstance.Show("错误:获取传感器数据出错");
- return false;
- }
-
- Thread.Sleep(1000);
-
- //获取传感器的距离,转换成坐标系的点
- int laserHigh = ml_morkf.LaserDistance;
-
- MessageLog.GetInstance.Show($"传感器的距离{laserHigh}");
-
- if (laserHigh > 29)
- {
- MessageLog.GetInstance.Show("错误:传感器获取数据大于隔板与传感器之间的距离");
- return false;
- }
-
- int moveX = 0;
- if (LaserDistance2Coordinate(laserHigh, out moveX) != Laser2CoordinateState.MaterialNormal)
- {
- MessageLog.GetInstance.Show("错误:传感器距离转坐标值失败");
- return false;
- }
-
- if(x < moveX)
- {
- MessageLog.GetInstance.Show("错误:传感器距离转坐标值大于当前x轴坐标");
- return false;
- }
-
- //设置到传感器感应的距离处
- if (!SetArmPosition(x - moveX, y))
- {
- return false;
- }
-
- ////关闭电磁阀
- //Electromagnetism(new List<bool> { true });
- //Thread.Sleep(500);
-
- #if true
- //爪子去1号位抓菜
- if (!PawToPoint1())
- {
- return false;
- }
- #endif
-
- //设置到该层菜品库最高处
- if (!SetArmPosition(x - materialHigh, y))
- {
- return false;
- }
-
- ////移动前开启电磁阀
- //Electromagnetism(new List<bool> { false });
- //Thread.Sleep(500);
-
- #if true
- //爪子去2号位放菜
- if (!PawToPoint2())
- {
- return false;
- }
- #endif
- ////判断炒锅是否可以放盒
- //for (int i = 0; materialSleepCount > i && !morkFs[fryIndex].FryPot1_HOBTPut; i++)
- //{
- // Thread.Sleep(materialSleepTime);
- // if (i >= materialSleepCount - 1)
- // {
- // MessageLog.GetInstance.Show("等待炒锅允许放盒超时");
- // return;
- // }
- //}
-
- //关闭启磁吸
- MagnetOff(fryNum);
-
- Thread.Sleep(200);
-
- //根据炒锅不同设置不同的出菜处
- if (fryNum % 2 == 0)
- {
- x = materialPutPositionXFry1;
- y = materialPutPositionYFry1;
- }
- else
- {
- x = materialPutPositionXFry2;
- y = materialPutPositionYFry2;
- }
-
- //设置到炒锅1出菜处
- if (!SetArmPosition(x, y))
- {
- return false;
- }
-
- #if true
- //爪子去3号位放菜
- if (!PawToPoint3())
- {
- return false;
- }
- #endif
-
- //开启磁吸
- MagnetOn(fryNum);
-
- Thread.Sleep(200);
-
- //设置到炒锅1出菜处
- if (!SetArmPosition(x + materialToFryDistance, y))
- {
- return false;
- }
-
- #if true
- //爪子回到2号位
- if (!PawToPoint2())
- {
- return false;
- }
- #endif
- ////关闭电磁阀
- //Electromagnetism(new List<bool> { false });
- //Thread.Sleep(300);
- if (morkFs.ContainsKey(fryNum))
- {
- morkFs[fryNum].GetMaterialComplete = true;
- }
- return true;
- }
-
- /// <summary>
- /// 更新菜品库存量
- /// </summary>
- /// <returns></returns>
- public bool UpdateMaterialLibraryStock()
- {
- try
- {
- //判断是否初始化
- if(!ml_morkf.InitialComplete)
- {
- return false;
- }
-
- foreach(var item in MaterialCoordinate.dicNoMaterialCoordinate)
- {
- //设置到抓菜处
- if (!SetArmPosition(item.Value.X, item.Value.Y))
- {
- return false;
- }
-
- //获取该处的距离
- if (!GetDistance(item.Value.X))
- {
- MessageLog.GetInstance.Show("错误:获取传感器数据出错");
- return false;
- }
-
- Thread.Sleep(1000);
-
- //获取传感器的距离
- int laserHigh = ml_morkf.LaserDistance;
- int surplus = 0;
- //根据距离算出菜品余量
- Laser2CoordinateState state = LaserDistance2MaterialCount(laserHigh, out surplus);
- if (state != Laser2CoordinateState.MaterialNormal || state != Laser2CoordinateState.MaterialEmpty)
- {
- return false;
- }
-
- //更新菜品余量
- if (!MaterialSurplusOperation.GetInstance().UpdateSurplus(item.Key, surplus))
- {
- return false;
- }
- }
- return true;
- }
- catch(Exception ex)
- {
- return false;
- }
- }
-
- /// <summary>
- /// 传感器距离转为x轴坐标系
- /// </summary>
- /// <param name="distance"></param>
- /// <param name="x"></param>
- /// <returns></returns>
- public Laser2CoordinateState LaserDistance2Coordinate(int distance,out int x)
- {
- x = 0;
- try
- {
- //菜盒放多了
- if (distance<6)
- {
- return Laser2CoordinateState.MaterialOverdo;
- }
- else if (distance >= 6 && distance < 10)
- {
- x = 24882;
- return Laser2CoordinateState.MaterialNormal;
- }
- else if(distance>=10 && distance < 14)
- {
- x = 18661;
- return Laser2CoordinateState.MaterialNormal;
- }
- else if(distance >= 14 && distance < 19)
- {
- x = 12441;
- return Laser2CoordinateState.MaterialNormal;
- }
- else if (distance >= 19 && distance < 23)
- {
- x = 6220;
- return Laser2CoordinateState.MaterialNormal;
- }
- else if (distance >= 23 && distance < 28)
- {
- x = 0;
- return Laser2CoordinateState.MaterialNormal;
- }
- else
- {
- return Laser2CoordinateState.MaterialEmpty;
- }
- }
- catch(Exception ex)
- {
- return Laser2CoordinateState.MaterialUnkown;
- }
- }
-
- /// <summary>
- /// 传感器距离转为菜品数量
- /// </summary>
- /// <param name="distance"></param>
- /// <param name="x"></param>
- /// <returns></returns>
- public Laser2CoordinateState LaserDistance2MaterialCount(int distance, out int count)
- {
- count = 0;
- try
- {
- //菜盒放多了
- if (distance < 6)
- {
- return Laser2CoordinateState.MaterialOverdo;
- }
- else if (distance >= 6 && distance < 10)
- {
- count = 5;
- return Laser2CoordinateState.MaterialNormal;
- }
- else if (distance >= 10 && distance < 14)
- {
- count = 4;
- return Laser2CoordinateState.MaterialNormal;
- }
- else if (distance >= 14 && distance < 19)
- {
- count = 3;
- return Laser2CoordinateState.MaterialNormal;
- }
- else if (distance >= 19 && distance < 23)
- {
- count = 2;
- return Laser2CoordinateState.MaterialNormal;
- }
- else if (distance >= 23 && distance < 28)
- {
- count = 1;
- return Laser2CoordinateState.MaterialNormal;
- }
- else
- {
- count = 0;
- return Laser2CoordinateState.MaterialEmpty;
- }
- }
- catch (Exception ex)
- {
- return Laser2CoordinateState.MaterialUnkown;
- }
- }
-
- /// <summary>
- /// 获取菜品测试
- /// </summary>
- /// <param name="isGet"></param>
- private void GetMaterailTest(MaterialOperation materialOperation)
- {
- ThreadManage.GetInstance().Start(new Action(() => {
- bool ret = false;
- try
- {
- Coordinate myCoordinate = new Coordinate();
- //获取坐标,根据菜品的位置信息
- if (!NoMaterial2Coordinate(materialOperation.materialInfo.Loc, out myCoordinate))
- {
- MessageLog.GetInstance.Show($"炒锅{materialOperation.fryNum}获取菜品[{materialOperation.materialInfo.Name}]失败,找不到对应位置");
- return;
- }
- lock (lock_Materail)
- {
- ml_morkf.MaterailIsWorking = true;
- }
- //取菜操作
- ret =GetMaterail(myCoordinate.X, myCoordinate.Y, materialOperation.fryNum);
- }
- catch (Exception ex)
- {
- ret = false;
- }
- finally
- {
- lock (lock_Materail)
- {
- ml_morkf.MaterailIsWorking = false;
- }
- if (ret == true)
- {
- //指定位置菜品减一
- MaterialSurplusOperation.GetInstance().ReduceSurplus(materialOperation.materialInfo.Loc);
- }
- }
-
- }), $"菜品库操作");
- }
- #endregion
-
- #region 炒锅PLC基本操作方法
- /// <summary>
- /// 炒锅写寄存器方法,num为炒锅对应编号,从0开始
- /// </summary>
- /// <param name="address">寄存器地址</param>
- /// <param name="value">值</param>
- /// <param name="num">炒锅编号</param>
- private void FirePot_Write(string address, object value, int num)
- {
- WriteControlExact(address, value, num);
- }
-
- /// <summary>
- /// 出调料
- /// </summary>
- /// <param name="o"></param>
- public void OutSeasoning(object o, int num)
- {
- if (o == null) return;
- if (o is List<int> ints && ints.Count == 2 && morkFs.ContainsKey(num))
- {
- FirePot_Write(morkFs[num].PassWayValue[ints[0]], (ushort)ints[1], num);//写入通道值
- Thread.Sleep(400);
- FirePot_Write(morkFs[num].StartPassWay[ints[0]], true, num);//开启通道
- Thread.Sleep(400);
- FirePot_Write(morkFs[num].StartPassWay[ints[0]], false, num);//开启通道
- }
- }
-
- /// <summary>
- /// 出多个调料
- /// </summary>
- public void OutSeasonings(List<SeasoningList> seasoningLists, int num)
- {
- //防止越界
- if (!morkFs.ContainsKey(num))
- {
- return;
- }
-
- foreach (SeasoningList seasoning in seasoningLists)
- {
- FirePot_Write(morkFs[num].PassWayValue[seasoning.Loc], (ushort)seasoning.Qty, num);
- Thread.Sleep(300);
- }
- foreach (SeasoningList seasoning in seasoningLists)
- {
- FirePot_Write(morkFs[num].StartPassWay[seasoning.Loc], true, num);
- Thread.Sleep(300);
- }
- foreach (SeasoningList seasoning in seasoningLists)
- {
- FirePot_Write(morkFs[num].StartPassWay[seasoning.Loc], false, num);
- Thread.Sleep(300);
- }
-
- foreach (SeasoningList seasoning in seasoningLists)
- {
- FirePot_Write(morkFs[num].StartPassWay[seasoning.Loc], false, num);
- Thread.Sleep(300);
- }
- }
-
- /// <summary>
- /// 复位
- /// </summary>
- public void Plc1Reset(int num)
- {
- ThreadManage.GetInstance().Start(new Action(() =>
- {
- StopFire(num);
- Thread.Sleep(200);
- StopStir(num);
- Thread.Sleep(200);
- FirePot_Write("LB5", false, num);
- Thread.Sleep(200);
- FirePot_Write("LB3", false, num);
- Thread.Sleep(200);
- FirePot_Write("LB6", false, num);
- Thread.Sleep(200);
- FirePot_Write("LB7", false, num);
- Thread.Sleep(200);
- FirePot_Write("LB4", false, num);
- Thread.Sleep(200);
- FirePot_Write("LB53", false, num);
- if (morkFs.ContainsKey(num))
- {
- foreach (var item in morkFs[num].StartPassWay.Values)
- {
- Thread.Sleep(200);
- FirePot_Write(item, false, num);
- }
- }
- }), "炒锅1初始化");
-
- }
-
- /// <summary>
- /// 炒锅初始化
- /// </summary>
- public void PLCInite()
- {
- for (int i = 0; i < morkFs.Count; i++)
- {
- FirePot_Write("LB0", true, i);
-
- for (int j = 0; j < sleepCount && !morkFs[i].FryPot1_InitialComplete; j++)
- {
- Thread.Sleep(sleepTime);
- if (j >= sleepCount - 1)
- {
- MessageLog.GetInstance.Show($"炒锅{j}初始化超时");
- }
- }
-
- //while (!morkFs[i].FryPot1_InitialComplete)
- //{
- // Thread.Sleep(500);
- //}
- FirePot_Write("LB0", false, i);
- }
- }
-
- //加油
- public void AddOil()
- {
-
- }
- //加热启动
- public void StartFire(int num)
- {
- FirePot_Write("LB1", true, num);
- Thread.Sleep(200);
- }
- //加热停止
- public void StopFire(int num)
- {
- FirePot_Write("LB1", false, num);
- Thread.Sleep(200);
- }
- //搅拌启动
- public void StartStir(int num)
- {
- FirePot_Write("LB2", true, num);
- Thread.Sleep(200);
- }
- //搅拌启停止
- public void StopStir(int num)
- {
- FirePot_Write("LB2", false, num);
- Thread.Sleep(200);
- }
- //倒菜
- public void OutFood(int num,bool isMaterial = false)
- {
- if (!morkFs.ContainsKey(num))
- {
- return;
- }
- int i = 0;
-
- if(isMaterial)
- {
- //判断是否完成取菜
- for (i = 0; i < materialSleepCount && !morkFs[num].GetMaterialComplete; i++)
- {
- if (i == 10)
- {
- //停止搅拌
- StopStir(num);
- //火力设置为2档
- SetFire(new List<int> { 2 }, num);
- }
- Thread.Sleep(materialSleepTime);
- if (i >= materialSleepCount - 1)
- {
- MessageLog.GetInstance.Show($"炒锅{num}倒菜超时:未能等到取菜完成");
- return;
- }
- }
-
- if (i > 9)
- {
- //开启搅拌
- StartStir(num);
- //火力设置回原本火力
- SetFire(new List<int> { morkFs[num].Fire }, num);
- }
- }
-
- FirePot_Write("LB3", true, num);
- MessageLog.GetInstance.Show("倒菜启动");
-
- Thread.Sleep(500);
-
- for (i = 0; i < materialSleepCount && !morkFs[num].FryPot1_MaterialIntoPot; i++)
- {
- Thread.Sleep(materialSleepTime);
- if (i >= materialSleepCount - 1)
- {
- MessageLog.GetInstance.Show($"炒锅{num}倒菜超时");
- }
- }
-
- FirePot_Write("LB3", false, num);
- Thread.Sleep(200);
- MessageLog.GetInstance.Show("倒菜完成");
- morkFs[num].GetMaterialComplete = false;
- }
- //搅拌臂去原点位
- public void StirArmGoOrigin(int num)
- {
- if (!morkFs.ContainsKey(num))
- {
- return;
- }
-
- FirePot_Write("LB5", true, num);
- MessageLog.GetInstance.Show("搅拌臂去原点位");
-
- for (int i = 0; i < sleepCount && !morkFs[num].ArmOnOrigin; i++)
- {
- Thread.Sleep(sleepTime);
- if (i >= sleepCount - 1)
- {
- MessageLog.GetInstance.Show($"炒锅{num}搅拌臂去原点位超时");
- }
- }
-
- FirePot_Write("LB5", false, num);
- Thread.Sleep(200);
- MessageLog.GetInstance.Show("搅拌臂到达原点位");
- }
-
- //搅拌臂去炒制位
- public void StirArmGoWork(int num)
- {
- if (!morkFs.ContainsKey(num))
- {
- return;
- }
-
- if (!morkFs[num].ArmOnWorking/* && morkFs[num].PotOnOrigin*/)
- {
- FirePot_Write("LB6", true, num);
- MessageLog.GetInstance.Show("搅拌臂去工作位");
-
- for (int i = 0; i < sleepCount && !morkFs[num].ArmOnWorking; i++)
- {
- Thread.Sleep(sleepTime);
- if (i >= sleepCount - 1)
- {
- MessageLog.GetInstance.Show($"炒锅{num}搅拌臂去炒制位超时");
- }
- }
-
- //while (!morkFs[num].ArmOnWorking)
- //{
- // Thread.Sleep(200);
- //}
-
- FirePot_Write("LB6", false, num);
- Thread.Sleep(200);
- MessageLog.GetInstance.Show("搅拌臂到达工作位");
- }
-
- }
- //HBOT放盒子到位
- public void HBOTGoWork(int num)
- {
- FirePot_Write("LB7", true, num);
- Thread.Sleep(400);
- FirePot_Write("LB7", false, num);
- }
- //出餐启动
- public void OutMeal(int num)
- {
- if (!morkFs[num].ArmOnOrigin /*&& morkFs[num].PotOnOrigin*/)
- {
- MessageLog.GetInstance.Show("搅拌臂不在原点位,无法完成出餐");
- return;
- }
- FirePot_Write("LB4", true, num);
- Thread.Sleep(200);
- FirePot_Write("LB4", false, num);
- }
- //加热挡位设定
- public void SetFire(object o, int num)
- {
- if (o == null) return;
- if (o is List<int> ints && ints.Count == 1)
- {
- FirePot_Write("LW14", (ushort)ints[0], num);
- Thread.Sleep(200);
-
- }
-
- }
-
- public void SetFry(object o)
- {
- if (o == null) return;
- if (o is List<int> ints && ints.Count == 1)
- {
- fryIndex = ints[0] - 1;
- }
- }
-
- /// <summary>
- /// 搅拌挡位设定
- /// </summary>
- /// <param name="o"></param>
- public void SetStir(object o, int num)
- {
- if (o == null) return;
- if (o is List<int> ints && ints.Count == 1)
- {
- FirePot_Write("LW15", (ushort)ints[0], num);
- Thread.Sleep(200);
-
- }
- }
-
- public void MagnetOn(int num)
- {
- FirePot_Write("LB8", false, num);
- Thread.Sleep(200);
- }
-
- public void MagnetOff(int num)
- {
- FirePot_Write("LB8", true, num);
- Thread.Sleep(200);
- }
- #endregion
-
- private static object saveMaterialLock = new object();
- private void SaveMaterialData()
- {
- lock(saveMaterialLock)
- {
- Json<MaterialSurplus>.Data = MaterialSurplusOperation.GetInstance().materialSurplus;
- Json<MaterialSurplus>.Save();
- }
- }
-
- bool isInitialArrive = false;
-
- bool isInitialPaw = false;
-
- #region 联网交互
- private void ServerInit()
- {
- //物料信息
- EventBus.EventBus.GetInstance().Subscribe<MaterialDeliveryEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
- {
- if (@event == null) return;
- if (@event is MaterialDeliveryEvent material)
- {
- orderMaterialDelivery = material.orderMaterialDelivery;
-
- }
- });
-
- //配方数据信息
- EventBus.EventBus.GetInstance().Subscribe<RecipeBomEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
- {
- if (@event == null) return;
- if (@event is RecipeBomEvent recipe)
- {
- recipeBoms = recipe.recipeBoms;
- }
- });
- //小炒流程信息
- EventBus.EventBus.GetInstance().Subscribe<StirFryGoodsEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callback)
- {
- if (@event == null) return;
- if (@event is StirFryGoodsEvent stirFry)
- {
- if(stirFry.stirFrymessage != null)
- {
- if (stirFry.stirFrymessage.stirFryGoods.Count > 0)
- {
- foreach (var item in stirFry.stirFrymessage.stirFryGoods)
- {
- LocalstirFryGoods.Add(new StirFryGoods
- {
- GoodsKey = item.GoodsKey,
- StirPotActions = OrderSort(item.StirPotActions),
- });
- GlobalFoodMenu.LocalFoodMenus.Add(new FoodMenuModel
- {
- GoodKey = item.GoodsKey.ToString(),
- GoodName = "",
- });
- }
- }
- if(stirFry.stirFrymessage.materials.Count > 0)
- {
- MaterialSurplusOperation.GetInstance().UpdateSurplusAll(stirFry.stirFrymessage.materials);
- //foreach (var item in stirFry.stirFrymessage.materials)
- //{
- // MaterialSurplusOperation.GetInstance().materialSurplus.dicSurplus.Add(new MaterialInfo
- // {
- // Id = item.Id,
- // Key = item.Key,
- // Name = item.Name,
- // Loc = item.Loc,
- // Qty = item.Qty,
- // });
- //}
- SaveMaterialData();
- }
- }
-
- MessageLog.GetInstance.Show("接收到小炒流程信息");
- //流程解析
- foreach (var item in LocalstirFryGoods)
- {
- MessageLog.GetInstance.Show($"添加菜谱{item.GoodsKey}");
- // morkF.listStirBom.Add(item.StirFryBomInfo);//添加订单制作流程
- string MenuStep = "菜单步骤:";
- foreach (var items in item.StirPotActions)
- {
- switch (items.Actions)
- {
- case nameof(StirFryPotActionEnum.加热开启):
- MenuStep += items.Actions + ",";
- break;
- case nameof(StirFryPotActionEnum.设置加热挡位1):
- MenuStep += items.Actions + ",";
- break;
- case nameof(StirFryPotActionEnum.设置加热挡位2):
- MenuStep += items.Actions + ",";
- break;
- case nameof(StirFryPotActionEnum.设置加热挡位3):
- MenuStep += items.Actions + ",";
- break;
- case nameof(StirFryPotActionEnum.设置加热挡位4):
- MenuStep += items.Actions + ",";
- break;
- case nameof(StirFryPotActionEnum.设置加热挡位5):
- MenuStep += items.Actions + ",";
- break;
- case nameof(StirFryPotActionEnum.设置加热挡位6):
- MenuStep += items.Actions + ",";
- break;
- case nameof(StirFryPotActionEnum.设置加热挡位7):
- MenuStep += items.Actions + ",";
- break;
- case nameof(StirFryPotActionEnum.设置加热挡位8):
- MenuStep += items.Actions + ",";
- break;
- case nameof(StirFryPotActionEnum.设置加热挡位9):
- MenuStep += items.Actions + ",";
- break;
- case nameof(StirFryPotActionEnum.设置加热挡位10):
- MenuStep += items.Actions + ",";
- break;
- case nameof(StirFryPotActionEnum.停止加热):
- MenuStep += items.Actions + ",";
- break;
- case nameof(StirFryPotActionEnum.加调料):
- MenuStep += items.Actions + ",";
- break;
- case nameof(StirFryPotActionEnum.取原料):
- MenuStep += items.Actions + ",";
- break;
- case nameof(StirFryPotActionEnum.开启搅拌):
- MenuStep += items.Actions + ",";
- break;
- case nameof(StirFryPotActionEnum.设置搅拌挡位1):
- MenuStep += items.Actions + ",";
- break;
- case nameof(StirFryPotActionEnum.设置搅拌挡位2):
- MenuStep += items.Actions + ",";
- break;
- case nameof(StirFryPotActionEnum.设置搅拌挡位3):
- MenuStep += items.Actions + ",";
- break;
- case nameof(StirFryPotActionEnum.关闭搅拌):
- MenuStep += items.Actions + ",";
- break;
- case nameof(StirFryPotActionEnum.出餐启动):
- MenuStep += items.Actions + ",";
- break;
- case nameof(StirFryPotActionEnum.道菜启动):
- MenuStep += items.Actions + ",";
- break;
- case nameof(StirFryPotActionEnum.炒制菜品):
- MenuStep += items.Actions + ",";
- break;
- case nameof(StirFryPotActionEnum.搅拌臂原点位):
- MenuStep += items.Actions + ",";
- break;
- case nameof(StirFryPotActionEnum.搅拌臂炒制位):
- MenuStep += items.Actions + ",";
- break;
- case nameof(StirFryPotActionEnum.洗锅):
- MenuStep += items.Actions + ",";
- break;
- default:
- break;
-
- }
-
-
- }
- MessageLog.GetInstance.Show(MenuStep);
-
-
-
- }
- }
- });
- }
-
- /// <summary>
- /// 订单排序
- /// </summary>
- /// <param name="potActions"></param>
- /// <returns></returns>
- private List<PotActions> OrderSort(List<PotActions> potActions)
- {
- if (potActions.Count > 1)
- {
- potActions.Sort(
- delegate (PotActions st1, PotActions st2)
- {
- //降序排列
- //return st2.FryTime.CompareTo(st1.FryTime);
-
- //升序版(颠倒 st1 和 st2 即可)
- return st1.FryTime.CompareTo(st2.FryTime);
-
- });
- }
- return potActions;
- }
-
- /// <summary>
- /// 订单状态发布
- /// </summary>
- /// <param name="subid"></param>
- /// <param name="oRDER_STATUS"></param>
- private void OrderChange(string subid, ORDER_STATUS oRDER_STATUS)
- {
- EventBus.EventBus.GetInstance().Publish(new OrderStatusChangedEvent() { Status = oRDER_STATUS, SubOrderId = subid });
- }
-
- /// <summary>
- /// 数据解析
- /// </summary>
- private void DataParse()
- {
- //if (!morkFs.ContainsKey(num))
- //{
- // return;
- //}
-
- EventBus.EventBus.GetInstance().Subscribe<DoOrderEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBackHandle)
- {
- if (@event == null) return;
- if (@event is DoOrderEvent order)
- {
- if (order.MorkOrder.GoodBatchings == null) return;
- OrderCount++;
- DeviceProcessLogShow($"接收到{OrderCount}次订单");
- Enum.GetNames(typeof(StirFryPotActionEnum));
-
- //var res = LocalstirFryGoods?.FirstOrDefault(p => p.GoodsKey == order.MorkOrder.RecipeId);//匹配订单对应制作流程
- if(order.MorkOrder.GoodBatchings.Count <= 0)
- {
- return;
- }
-
- if(order.MorkOrder.DeviceId != DeviceId)
- {
- return;
- }
-
- //var res = LocalstirFryGoods[0];
- //if (res != null)
- //{
- // /* morkF.listStirBom.Add(res.StirFryBomInfo);*///添加订单制作流程
- // //添加到带炒小炒队列
- // if (StirFryGoodsQuenes.FirstOrDefault(p => p.SuborderId == order.MorkOrder.SuborderId) == null)
- // {
- // lock (lock_MainProcessExcute)
- // {
- // StirFryGoodsQuenes.Enqueue(new OrderLocInfo()
- // {
- // SuborderId = order.MorkOrder.SuborderId,
- // StirPotActions = res.StirPotActions,
- // GoodName = order.MorkOrder.GoodsName
- // });
- // }
- // }
- //}
-
- //暂时使用本地菜单
- StartLocalOrder();
- }
-
- });
- }
- #endregion
-
- private object lock_MainProcessExcute = new object();
-
- /// <summary>
- /// 炒锅主流程
- /// </summary>
- private void MainProcessExcute()
- {
- //判断是否有订单信息
- if(StirFryGoodsQuenes.Count > 0)
- {
- //遍历炒锅,找到合适、空闲的炒锅
- for(int i = 0;i<morkFs.Count;i++)
- {
- if (/*morkFs[i].AutoMode && morkFs[i].FryPot1_InitialComplete &&*/
- !morkFs[i].FryWorking)//炒锅在自动状态&&初始化完成&&是否在炒菜中
- {
- lock(lock_MainProcessExcute)
- {
- //待炒小炒队列出队列
- if (StirFryGoodsQuenes.TryDequeue(out var res))
- {
- //设置当前炒制菜品
- nowStirFryGood[i] = res;
- //炒锅工作状态置为正在工作中
- morkFs[i].FryWorking = true;
- //空闲炒锅入队列
- morkFs[i].StirFryGoodsQuenes.Enqueue(res);
- //炒锅取菜状态置为未取到
- morkFs[i].GetMaterialComplete = false;
- //开启线程进行炒制
- ThreadManage.GetInstance().Start(new Action(() =>
- {
- try
- {
- MessageLog.GetInstance.Show($"炒锅{i}开始炒制菜品{res.GoodName}");
-
- Coordinate myCoordinate = new Coordinate();
- int getMaterailNum = 0;
- foreach (var potActions in res.StirPotActions)
- {
- if (ThreadManage.GetInstance().IsCanncel(String.Format(striConst, i.ToString(), nowStirFryGood[i].GoodName)))
- {
- return;
- }
-
- switch (potActions.Actions)
- {
- case nameof(StirFryPotActionEnum.加热开启):
- StartFire(i);
- MessageLog.GetInstance.Show(potActions.Actions);
- break;
- case nameof(StirFryPotActionEnum.设置加热挡位1):
- morkFs[i].Fire = 1;
- SetFire(new List<int> { 1 }, i);
- MessageLog.GetInstance.Show(potActions.Actions);
- break;
- case nameof(StirFryPotActionEnum.设置加热挡位2):
- morkFs[i].Fire = 2;
- SetFire(new List<int> { 2 }, i);
- MessageLog.GetInstance.Show(potActions.Actions);
- break;
- case nameof(StirFryPotActionEnum.设置加热挡位3):
- morkFs[i].Fire = 3;
- SetFire(new List<int> { 3 }, i);
- MessageLog.GetInstance.Show(potActions.Actions);
- break;
- case nameof(StirFryPotActionEnum.设置加热挡位4):
- morkFs[i].Fire = 4;
- SetFire(new List<int> { 4 }, i);
- MessageLog.GetInstance.Show(potActions.Actions);
- break;
- case nameof(StirFryPotActionEnum.设置加热挡位5):
- morkFs[i].Fire = 5;
- SetFire(new List<int> { 5 }, i);
- MessageLog.GetInstance.Show(potActions.Actions);
- break;
- case nameof(StirFryPotActionEnum.设置加热挡位6):
- morkFs[i].Fire = 6;
- SetFire(new List<int> { 6 }, i);
- MessageLog.GetInstance.Show(potActions.Actions);
- break;
- case nameof(StirFryPotActionEnum.设置加热挡位7):
- morkFs[i].Fire = 7;
- SetFire(new List<int> { 7 }, i);
- MessageLog.GetInstance.Show(potActions.Actions);
- break;
- case nameof(StirFryPotActionEnum.设置加热挡位8):
- morkFs[i].Fire = 8;
- SetFire(new List<int> { 8 }, i);
- MessageLog.GetInstance.Show(potActions.Actions);
- break;
- case nameof(StirFryPotActionEnum.设置加热挡位9):
- morkFs[i].Fire = 9;
- SetFire(new List<int> { 9 }, i);
- MessageLog.GetInstance.Show(potActions.Actions);
- break;
- case nameof(StirFryPotActionEnum.设置加热挡位10):
- morkFs[i].Fire = 10;
- SetFire(new List<int> { 10 }, i);
- MessageLog.GetInstance.Show(potActions.Actions);
- break;
- case nameof(StirFryPotActionEnum.停止加热):
- StopFire(i);
- MessageLog.GetInstance.Show(potActions.Actions);
- break;
- case nameof(StirFryPotActionEnum.加调料):
- OutSeasonings(potActions.SeasoningLists, i);
- MessageLog.GetInstance.Show(potActions.Actions);
- break;
- case nameof(StirFryPotActionEnum.取原料):
- //炒锅取菜状态置为未取到
- morkFs[i].GetMaterialComplete = false;
- if(potActions.MaterialLists != null)
- {
- foreach (var item in potActions.MaterialLists)
- {
- //通过ID获取有效的菜品对象
- MaterialInfo materialInfo = MaterialSurplusOperation.GetInstance().GetVailedMaterial(item.MaterialId);
- if (materialInfo == null)
- {
- MessageLog.GetInstance.Show($"炒锅{i}获取菜品[{materialInfo.Name}]失败,库存不足");
- continue;
- }
-
- materialOperationQuenes.Enqueue(new MaterialOperation { materialInfo = materialInfo, fryNum = i });
- }
- }
- else
- {
- MessageLog.GetInstance.Show($"炒锅{i}获取菜品失败,菜品列表为空");
- }
- //if(getMaterailNum == 0)
- //{
- // if (NoMaterial2Coordinate(MaterialCoordinate.MLNumberList[1], out myCoordinate))
- // {
- // materialOperationQuenes.Enqueue(new MaterialOperation() { fryNum = i, coordinate = myCoordinate });
- // getMaterailNum++;
- // }
- //}
- //else if(getMaterailNum == 1)
- //{
- // if (NoMaterial2Coordinate(MaterialCoordinate.MLNumberList[4], out myCoordinate))
- // {
- // materialOperationQuenes.Enqueue(new MaterialOperation() { fryNum = i, coordinate = myCoordinate });
- // getMaterailNum++;
- // }
- //}
- MessageLog.GetInstance.Show(potActions.Actions);
- break;
- case nameof(StirFryPotActionEnum.开启搅拌):
- StartStir(i);
- MessageLog.GetInstance.Show(potActions.Actions);
- break;
- case nameof(StirFryPotActionEnum.设置搅拌挡位1):
- SetStir(new List<int> { 1 }, i);
- MessageLog.GetInstance.Show(potActions.Actions);
- break;
- case nameof(StirFryPotActionEnum.设置搅拌挡位2):
- SetStir(new List<int> { 2 }, i);
- MessageLog.GetInstance.Show(potActions.Actions);
- break;
- case nameof(StirFryPotActionEnum.设置搅拌挡位3):
- SetStir(new List<int> { 3 }, i);
- MessageLog.GetInstance.Show(potActions.Actions);
- break;
- case nameof(StirFryPotActionEnum.关闭搅拌):
- StopStir(i);
- MessageLog.GetInstance.Show(potActions.Actions);
- break;
- case nameof(StirFryPotActionEnum.出餐启动):
- MessageLog.GetInstance.Show(potActions.Actions);
- break;
- case nameof(StirFryPotActionEnum.道菜启动):
- OutFood(i, true);
- break;
- case nameof(StirFryPotActionEnum.炒制菜品):
- Thread.Sleep(potActions.During * 1000);
- break;
- case nameof(StirFryPotActionEnum.搅拌臂原点位):
- StirArmGoOrigin(i);
- MessageLog.GetInstance.Show(potActions.Actions);
- break;
- case nameof(StirFryPotActionEnum.搅拌臂炒制位):
- StirArmGoWork(i);
- MessageLog.GetInstance.Show(potActions.Actions);
- break;
- case nameof(StirFryPotActionEnum.洗锅):
- MessageLog.GetInstance.Show(potActions.Actions);
- break;
- default:
- break;
-
- }
- }
-
- Plc1Reset(i);//复位
- Thread.Sleep(3000);
- //回原点位
- StirArmGoOrigin(i);
-
- MessageLog.GetInstance.Show($"菜品{res.GoodName}完成");
-
-
- }
- catch (Exception ex)
- {
- ThreadManage.GetInstance().StopTask($"炒锅{i}{res.GoodName}炒制线程");
- MessageLog.GetInstance.Show($"炒锅{i}炒制菜品{res.GoodName}出错,错误信息:" + ex.Message);
- }
- finally
- {
- nowStirFryGood[i] = null;
- //炒完后出队列
- morkFs[i].StirFryGoodsQuenes.TryDequeue(out var orderQueue);
- morkFs[i].FryWorking = false;
- }
- }), String.Format(striConst, i.ToString(), nowStirFryGood[i].GoodName)/*$"炒锅{i}炒制{res.GoodName}线程"*/);
- }
- }
- break;
- }
- }
- }
- }
-
- /// <summary>
- /// 菜品库主流程
- /// </summary>
- private void MainProcessMaterial()
- {
- if(materialOperationQuenes.Count > 0)
- {
- //判断是否初始化与是否在工作中
- if (ml_morkf.InitialComplete && !ml_morkf.MaterailIsWorking)
- {
- //待菜品库操作列出队列
- if (materialOperationQuenes.TryDequeue(out var res))
- {
-
- ml_morkf.MaterailIsWorking = true;
- MessageLog.GetInstance.Show("开始操作菜品库");
- //ThreadManage.GetInstance().Start(new Action(()=>{
- // GetMaterail(res.x, res.y, res.fryNum);
- //}),"菜品库操作");
- GetMaterailTest(res);
- }
- }
- }
- }
-
- /// <summary>
- /// 信号处理
- /// </summary>
- private void SingleProcess()
- {
-
- }
-
-
- private void ScreenDataServer()
- {
- LocalMqtt.GetInstance.Init(ScreenDeviceType.大炒);
- ThreadManage.GetInstance().StartLong(new Action(() =>
- {
- List<StatsModel> statsModels = new List<StatsModel>();
- statsModels.Add(new StatsModel() { Name = "帝王蟹", Count = 666 });
- ScreenModelMinWok maxWok = new ScreenModelMinWok
- {
- IsRun = new Random().Next(0, 2) == 0 ? IsRun.运行 : IsRun.停止, //设备是否运行
- WorkStatus_1 = (WorkStatus)new Random().Next(0, 3), //设备运行状态
- WorkStatus_2 = (WorkStatus)new Random().Next(0, 3), //设备运行状态
- RobotStatu = (WorkStatus)new Random().Next(0, 3), //机器人状态 待机 1 正常
- Alarm = new List<AlarmModel>(), //告警集合
- FailuresCount = 0, //故障数
- StatsCount = statsModels, //统计菜品数量
- MinWok_Dishes_1 = "满汉全席",//当前炒制菜品名
- MinWok_Dishes_2 = "海鲜大餐",
- MinWok_Task_1 = "炒制菜品",//当前任务
- MinWok_Task_2 = "热油", //当前任务
- MinWok_Process_1 = new List<ProcessModel>(), //炒锅当前任务流程
- MinWok_Process_2 = new List<ProcessModel>(), //炒锅当前任务流程
- MinWok_HeatGear_1 = new Random().Next(0, 5).ToString(), //炒锅加热档位
- MinWok_HeatGear_2 = new Random().Next(0, 5).ToString(), //炒锅加热档位
- MinWok_StirGear_1 = new Random().Next(0, 5).ToString(), //炒锅搅拌档位
- MinWok_StirGear_2 = new Random().Next(0, 5).ToString(), //炒锅搅拌档位
- MinWok_OrderCount_1 = 2, //订单数量
- MinWok_OrderCount_2 = 2, //订单数量
- MinWok_ErrorOrderCount_1 = 0, //异常订单数量
- MinWok_ErrorOrderCount_2 = 0, //异常订单数量
-
- };
- LocalMqtt.GetInstance.Publish(maxWok);
- Thread.Sleep(1000);
-
- }), "大屏数据上报");
-
- }
-
- public override void SimOrder()
- {
-
- }
-
- /// <summary>
- /// 菜品编号转换为坐标
- /// </summary>
- /// <param name="no"></param>
- /// <param name="coordinate"></param>
- /// <returns></returns>
- public bool NoMaterial2Coordinate(string no,out Coordinate coordinate)
- {
- //默认返回0,0
- coordinate = new Coordinate();
- coordinate.X = 0;
- coordinate.Y = 0;
-
- try
- {
- if(MaterialCoordinate.dicNoMaterialCoordinate.ContainsKey(no))
- {
- coordinate = MaterialCoordinate.dicNoMaterialCoordinate[no];
- return true;
- }
- else
- {
- return false;
- }
- }
- catch (Exception ex)
- {
- return false;
- }
- finally
- {
-
- }
- }
-
- public enum Laser2CoordinateState
- {
- MaterialOverdo = 0,//菜品过量
- MaterialNormal = 1,//菜品正常
- MaterialEmpty = 2,//菜品为空
- MaterialUnkown = 999,//菜品未知错误
- }
- }
- }
|