using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Threading; using BPA.Message; using HBLConsole.Communication; using HBLConsole.Factory; using HBLConsole.GVL; using HBLConsole.Interface; using HBLConsole.Model; using HBLConsole.Service; using HBLDevice.Coffee; using Robotc; using System.Collections.Concurrent; using System.Diagnostics; using BPA.Message.IOT; using System.Threading.Tasks; namespace HBLConsole.MORKJC2 { /* * 冰淇淋咖啡机组合套装 * 物料位置: * 1:冰淇料 * 2:冰淇淋杯 * 5:咖啡 * 6:咖啡杯 * 9: 茶 * 10: 茶杯 */ public class Control_MORKJC2 : IControl { private CoffeEndCook coffeEndCook = new CoffeEndCook();//模拟咖啡制作成功 //咖啡机主控程序 private CoffeeMachine coffeeMachine; //物料存放位置 private Dictionary batchings = new Dictionary(); //容器位置 private string holderLoc; /// /// 获取乐百机器人的数据 /// SignalResult lebai = new SignalResult(); //主料位置 private string mainMaterialLoc; //子订单ID private string subOrderId; private bool enableFunny = false; private DateTime lastRecvdOrder = DateTime.Now; private bool working = false; /// /// 果汁机做法,true:热饮,false:冷饮 /// private bool GuMake = false; private SerialPortClient commProxy; public void ConnectOk() { } ConcurrentQueue morkOrderPushes = new ConcurrentQueue(); public void Init() { ActionManage.GetInstance.Register(new Action((s) => { if (s is DrCoffeeDrinksCode cf) { DoCoffee(); } else if (s is Dictionary ms) { if (ms.ContainsKey("Button")) { switch (ms["Button"]) { case "启动示教": LebaiHelper.GetInstance.StartTeachMode(); break; case "停止示教": LebaiHelper.GetInstance.EndtTeachMode(); break; case "启动机器人": LebaiHelper.GetInstance.StartRobot(); break; case "急停": LebaiHelper.GetInstance.EStopRobot(); break; default: break; } } } }), "SimCoffee"); //构建所有商品物料信息 batchings = PolymerBatching.BuildAll(); EventBus.GetInstance().Subscribe(CoffeEndCookHandle); System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None); //一系列外围基础配置 var com_Coffee = config.AppSettings.Settings["COM_Coffee"].Value; var baud_Coffee = config.AppSettings.Settings["BAUD_Coffee"].Value; //咖啡机创建 coffeeMachine = new CoffeeMachine(com_Coffee, (BaudRates)Enum.Parse(typeof(BaudRates), baud_Coffee)); Main(); ReadData(); ThreadManage.GetInstance.StartLong(new Action(() => { while (GeneralConfig.Healthy && (morkOrderPushes.Count > 0)) { working = true; if (morkOrderPushes.TryDequeue(out MorkOrderPush order)) { MessageLog.GetInstance.Show($"开始制作订单[{order.SortNum}]"); //商品类型 GOODS_TYPE currentGoodsType = GOODS_TYPE.NEITHER; //子订单ID subOrderId = order.SuborderId; //遍历物料 foreach (var item in order.GoodBatchings) { var res = Json.Data.orderMaterialDelivery.BatchingInfo.FirstOrDefault(p => p.BatchingId == item.BatchingId); if (res != null) { //获取主料和容器位置 switch (batchings[res.BatchingLoc].BatchingClass) { case BATCHING_CLASS.HOLDER: holderLoc = res.BatchingLoc; break; case BATCHING_CLASS.MAIN_MATERIAL: // mainMaterialLoc ="1"; mainMaterialLoc = res.BatchingLoc; //验证商品是咖啡还是冰淇淋 if (ValidateGoodsByBatching(res.BatchingLoc) != GOODS_TYPE.NEITHER) { //获取当前物料所属商品类型 currentGoodsType = ValidateGoodsByBatching(res.BatchingLoc); } break; } } } //根据商品类型执行具体制作流程 switch (currentGoodsType) { case GOODS_TYPE.COFFEE: DoCoffee(); break; case GOODS_TYPE.JUICE: GuMake = order.MakeID == "2"; DoJuicer(); break; case GOODS_TYPE.TEA: DoTea(); break; case GOODS_TYPE.WATER: DoWater(); break; case GOODS_TYPE.NEITHER: MessageLog.GetInstance.Show("未知的商品类型"); break; } } working = false; lastRecvdOrder = DateTime.Now; } Thread.Sleep(1000); }), "订单制作"); } public void Main() { //咖啡机开启主线程 coffeeMachine.Start(); //开始心跳刷新,根据咖啡机及冰淇淋机来判断 ThreadManage.GetInstance.StartLong(new Action(() => { //GeneralConfig.Healthy = // LebaiHelper.GetInstance.IsConnected && // MorkCStatus.GetInstance().CanDo && // JuicerHelper.GetInstance.IsOpen && // MCUSerialHelper.GetInstance.IsOpen; GeneralConfig.Healthy = true; Thread.Sleep(100); }), "MORK-IC心跳刷新"); ThreadManage.GetInstance.Start(new Action(() => { while (!LebaiHelper.GetInstance.IsConnected) { Thread.Sleep(10); } //LebaiHelper.GetInstance.Scene(LebaiHelper.SENCE_欢迎); }), "MORK-JC欢迎"); } public void DataParse(T order) { if (order is MorkOrderPush morkOrderPush) { morkOrderPushes.Enqueue(morkOrderPush); } } /// /// 验证当前是做咖啡还是做冰淇淋 /// /// 物料位置 private GOODS_TYPE ValidateGoodsByBatching(string batchingLoc) { if (batchings.ContainsKey(batchingLoc)) return batchings[batchingLoc].GoodsType; return GOODS_TYPE.NEITHER; } private AutoResetEvent are = new AutoResetEvent(false); private void Wait(int value = 101) { while (!(lebai.Ok && lebai.Value == value)) { Thread.Sleep(5); } } int[] devStatusBy = new int[2] { 0, 0 }; bool outCupCheck = false;//放纸杯位置有无判断 /// /// 传感器的输入信号 0:无意义 1:有信号 2:无信号 3:信号不正确 /// int bSensorInput; /// /// 延迟的超时时间 /// DateTime delayTimeOut; /// /// 做咖啡 /// private void DoCoffee() { #region 接咖啡流程 are.Reset(); while (LebaiHelper.GetInstance.GetInput(1))//判断放杯位置是否有物品 { if (!outCupCheck) MessageLog.GetInstance.ShowEx("成品处有纸杯存在,请取走!!"); outCupCheck = true; } outCupCheck = false; SimpleFactory.GetInstance.OrderChanged(subOrderId, BPA.Message.Enum.ORDER_STATUS.COOKING); int resultTakeCup = takeCup(); if (resultTakeCup == 1) { MessageLog.GetInstance.Show("咖啡杯取杯完成"); LebaiHelper.GetInstance.SetValue(0); LebaiHelper.GetInstance.Scene(LebaiHelper.JUICE2_接咖啡); Wait(); new MakeCoffeeEvent() { DrinkCode = (DrCoffeeDrinksCode)int.Parse(mainMaterialLoc) }.Publish(); //接咖啡控制 //DrCoffeeDrinksCode.热水 //Task.Delay(10000).Wait();//模拟接咖啡 //coffeEndCook.Publish();//模拟咖啡制作完成 are.WaitOne(1000 * 120); while (LebaiHelper.GetInstance.GetInput(1))//判断放杯位置是否有物品 { if (!outCupCheck) MessageLog.GetInstance.ShowEx("成品处有纸杯存在,请取走!!"); outCupCheck = true; } outCupCheck = false; LebaiHelper.GetInstance.SetValue(0); LebaiHelper.GetInstance.Scene(LebaiHelper.JUICE2_放咖啡杯); Wait(); SimpleFactory.GetInstance.OrderChanged(subOrderId, BPA.Message.Enum.ORDER_STATUS.COMPLETED_TAKE); MessageLog.GetInstance.Show("咖啡制作完成"); } else { return; } #endregion } /// /// 做茶 /// private void DoTea() { #region 接茶流程 while (LebaiHelper.GetInstance.GetInput(1))//判断放杯位置是否有物品 { if (!outCupCheck) MessageLog.GetInstance.ShowEx("成品处有纸杯存在,请取走!!"); outCupCheck = true; } outCupCheck = false; SimpleFactory.GetInstance.OrderChanged(subOrderId, BPA.Message.Enum.ORDER_STATUS.COOKING); int resultTakeCup = takeCup(); if (resultTakeCup == 1) { MessageLog.GetInstance.Show("取茶杯完成"); LebaiHelper.GetInstance.SetValue(0); LebaiHelper.GetInstance.Scene(LebaiHelper.JUICE2_接茶叶); Wait(); // MCUSerialHelper.GetInstance.ServoControl(1, 105); Thread.Sleep(1000); // MCUSerialHelper.GetInstance.ServoControl(1, 130); Thread.Sleep(1000); // MCUSerialHelper.GetInstance.ServoControl(1, 105); Thread.Sleep(3000); LebaiHelper.GetInstance.SetValue(0); LebaiHelper.GetInstance.Scene(LebaiHelper.JUICE2_接茶水); Wait(); // MCUSerialHelper.GetInstance.OutputControl(4, false); LebaiHelper.GetInstance.SetOutput(false,1); // MCUSerialHelper.GetInstance.OutputControl(3, false); LebaiHelper.GetInstance.SetOutput(false, 0); Thread.Sleep(100); // MCUSerialHelper.GetInstance.OutputControl(3, true); LebaiHelper.GetInstance.SetOutput(true, 0); Thread.Sleep(3000); // MCUSerialHelper.GetInstance.OutputControl(3, false); LebaiHelper.GetInstance.SetOutput(false, 0); Thread.Sleep(100); LebaiHelper.GetInstance.SetOutput(false, 1); // MCUSerialHelper.GetInstance.OutputControl(4, false); Thread.Sleep(100); LebaiHelper.GetInstance.SetOutput(true, 1); // MCUSerialHelper.GetInstance.OutputControl(4, true); Thread.Sleep(500); // MCUSerialHelper.GetInstance.OutputControl(4, false); LebaiHelper.GetInstance.SetOutput(false, 1); Thread.Sleep(60000); while (LebaiHelper.GetInstance.GetInput(1))//判断放杯位置是否有物品 { if (!outCupCheck) MessageLog.GetInstance.ShowEx("成品处有纸杯存在,请取走!!"); outCupCheck = true; } outCupCheck = false; LebaiHelper.GetInstance.SetValue(0); LebaiHelper.GetInstance.Scene(LebaiHelper.JUICE2_放水杯); Wait(); SimpleFactory.GetInstance.OrderChanged(subOrderId, BPA.Message.Enum.ORDER_STATUS.COMPLETED_TAKE); MessageLog.GetInstance.Show("茶水制作完成"); } else { return; } #endregion } /// /// 接开水 /// private void DoWater() { #region 接水流程 while (LebaiHelper.GetInstance.GetInput(1))//判断放杯位置是否有物品 { if (!outCupCheck) MessageLog.GetInstance.ShowEx("成品处有纸杯存在,请取走!!"); outCupCheck = true; } outCupCheck = false; SimpleFactory.GetInstance.OrderChanged(subOrderId, BPA.Message.Enum.ORDER_STATUS.COOKING); int resultTakeCup = takeCup(); if (resultTakeCup == 1) { LebaiHelper.GetInstance.SetValue(0); LebaiHelper.GetInstance.Scene(LebaiHelper.JUICE2_接水); Wait(); // LebaiHelper.GetInstance.SetOutput(true,0); //// MCUSerialHelper.GetInstance.OutputControl(3, true); // Thread.Sleep(1000); // LebaiHelper.GetInstance.SetOutput(false, 0); // // MCUSerialHelper.GetInstance.OutputControl(3, false); // Thread.Sleep(10000); // LebaiHelper.GetInstance.SetOutput(true,1); // // MCUSerialHelper.GetInstance.OutputControl(4, true); // Thread.Sleep(1000); // LebaiHelper.GetInstance.SetOutput(false, 1); // // MCUSerialHelper.GetInstance.OutputControl(4, false); // Thread.Sleep(3000); LebaiHelper.GetInstance.SetOutput(false, 1); // MCUSerialHelper.GetInstance.OutputControl(3, false); LebaiHelper.GetInstance.SetOutput(false, 0); Thread.Sleep(100); // MCUSerialHelper.GetInstance.OutputControl(3, true); LebaiHelper.GetInstance.SetOutput(true, 0); Thread.Sleep(3000); // MCUSerialHelper.GetInstance.OutputControl(3, false); LebaiHelper.GetInstance.SetOutput(false, 0); Thread.Sleep(100); LebaiHelper.GetInstance.SetOutput(false, 1); // MCUSerialHelper.GetInstance.OutputControl(4, false); Thread.Sleep(100); LebaiHelper.GetInstance.SetOutput(true, 1); // MCUSerialHelper.GetInstance.OutputControl(4, true); Thread.Sleep(500); // MCUSerialHelper.GetInstance.OutputControl(4, false); LebaiHelper.GetInstance.SetOutput(false, 1); Thread.Sleep(60000); while (LebaiHelper.GetInstance.GetInput(1))//判断放杯位置是否有物品 { if (!outCupCheck) MessageLog.GetInstance.ShowEx("成品处有纸杯存在,请取走!!"); outCupCheck = true; } outCupCheck = false; //添加控制接水机构程序 LebaiHelper.GetInstance.SetValue(0); LebaiHelper.GetInstance.Scene(LebaiHelper.JUICE2_放水杯); Wait(); SimpleFactory.GetInstance.OrderChanged(subOrderId, BPA.Message.Enum.ORDER_STATUS.COMPLETED_TAKE); MessageLog.GetInstance.Show("纯净水制作完成"); } else { return; } #endregion } /// /// 果汁机控制信号 /// private byte JuicerNum; /// /// 做果汁 /// private void DoJuicer() { #region 接果汁流程 are.Reset(); while (LebaiHelper.GetInstance.GetInput(1))//判断放杯位置是否有物品 { if (!outCupCheck) MessageLog.GetInstance.ShowEx("成品处有纸杯存在,请取走!!"); outCupCheck = true; } SimpleFactory.GetInstance.OrderChanged(subOrderId, BPA.Message.Enum.ORDER_STATUS.COOKING); int resultTakeCup = takeCup(); if (resultTakeCup == 1) { int JuicerNum1 = int.Parse(mainMaterialLoc); switch (JuicerNum1) { case 52: if(GuMake) { JuicerNum = 0x00; } else { JuicerNum = 0x01; } LebaiHelper.GetInstance.SetValue(0); LebaiHelper.GetInstance.Scene(LebaiHelper.JUICE2_接果汁公共位); Wait(); LebaiHelper.GetInstance.SetValue(0); LebaiHelper.GetInstance.Scene(LebaiHelper.JUICE2_接1号果汁); Wait(); break; case 53: if (GuMake) { JuicerNum = 0x02; } else { JuicerNum = 0x03; } LebaiHelper.GetInstance.SetValue(0); LebaiHelper.GetInstance.Scene(LebaiHelper.JUICE2_接果汁公共位); Wait(); LebaiHelper.GetInstance.SetValue(0); LebaiHelper.GetInstance.Scene(LebaiHelper.JUICE2_接2号果汁); Wait(); break; case 54: if (GuMake) { JuicerNum = 0x04; } else { JuicerNum = 0x05; } LebaiHelper.GetInstance.SetValue(0); LebaiHelper.GetInstance.Scene(LebaiHelper.JUICE2_接果汁公共位); Wait(); LebaiHelper.GetInstance.SetValue(0); LebaiHelper.GetInstance.Scene(LebaiHelper.JUICE2_接3号果汁); Wait(); break; case 55: if (GuMake) { JuicerNum = 0x06; } else { JuicerNum = 0x07; } LebaiHelper.GetInstance.SetValue(0); LebaiHelper.GetInstance.Scene(LebaiHelper.JUICE2_接果汁公共位); Wait(); LebaiHelper.GetInstance.SetValue(0); LebaiHelper.GetInstance.Scene(LebaiHelper.JUICE2_接4号果汁); ; Wait(); break; default: JuicerNum = 0x00; LebaiHelper.GetInstance.SetValue(0); LebaiHelper.GetInstance.Scene(LebaiHelper.JUICE2_接果汁公共位); Wait(); LebaiHelper.GetInstance.SetValue(0); LebaiHelper.GetInstance.Scene(LebaiHelper.JUICE_接1号果汁); Wait(); break; } // var devStatus = JuicerHelper.GetInstance.GetDeviceStatus(); //var devStatus1 = Convert.ToString(devStatus[0], 2); //var devStatus2 = devStatus[1]; //if (devStatus1.IndexOf("0") == 1 && devStatus2 == 0) //{ // JuicerHelper.GetInstance.StartCook(JuicerNum); // Thread.Sleep(100); // devStatusBy = JuicerHelper.GetInstance.GetDeviceStatus(); // while (!(devStatusBy[1] == 0)) // { // Thread.Sleep(100); // devStatusBy = JuicerHelper.GetInstance.GetDeviceStatus(); // while (devStatusBy.Length != 2) // { // Thread.Sleep(100); // devStatusBy = JuicerHelper.GetInstance.GetDeviceStatus(); // } // } // devStatusBy = JuicerHelper.GetInstance.GetDeviceStatus(); // Thread.Sleep(5000); // LebaiHelper.GetInstance.SetValue(0); // LebaiHelper.GetInstance.Scene(LebaiHelper.JUICE2_放果汁杯); // Wait(); // SimpleFactory.GetInstance.OrderChanged(subOrderId, BPA.Message.Enum.ORDER_STATUS.COMPLETED_TAKE); // MessageLog.GetInstance.Show("果汁制作完成"); //} //模拟果汁 Thread.Sleep(15000); while (LebaiHelper.GetInstance.GetInput(1))//判断放杯位置是否有物品 { if (!outCupCheck) MessageLog.GetInstance.ShowEx("成品处有纸杯存在,请取走!!"); outCupCheck = true; } outCupCheck = false; LebaiHelper.GetInstance.SetValue(0); LebaiHelper.GetInstance.Scene(LebaiHelper.JUICE2_放果汁杯); Wait(); SimpleFactory.GetInstance.OrderChanged(subOrderId, BPA.Message.Enum.ORDER_STATUS.COMPLETED_TAKE); MessageLog.GetInstance.Show("果汁制作完成"); } else { return; } #endregion } /// /// 取杯的次数 /// private int nCnt; /// /// 取杯流程 /// /// 0:无意义,1:取杯成功 2:取杯失败 private int takeCup() { try { nCnt = 0; LebaiHelper.GetInstance.SetValue(0); LebaiHelper.GetInstance.Scene(LebaiHelper.JUICE2_初始位); Wait(); LebaiHelper.GetInstance.SetValue(0); LebaiHelper.GetInstance.Scene(LebaiHelper.JUICE2_取纸杯); Wait(); LebaiHelper.GetInstance.SetValue(0); LebaiHelper.GetInstance.Scene(LebaiHelper.JUICE2_纸杯检测); Wait(); nCnt++; while (!LebaiHelper.GetInstance.GetInput(0)) //读取传感器的值 { if (nCnt > 3) { nCnt = 0; MessageLog.GetInstance.ShowEx("三次取杯失败,请查看"); return 2; } else { nCnt++; LebaiHelper.GetInstance.SetValue(0); LebaiHelper.GetInstance.Scene(LebaiHelper.JUICE2_二次取杯); Wait(); LebaiHelper.GetInstance.SetValue(0); LebaiHelper.GetInstance.Scene(LebaiHelper.JUICE2_纸杯检测); Wait(); } Thread.Sleep(100); } } catch (Exception ex) { MessageLog.GetInstance.Show(ex.ToString()); } return 1; } /// /// 放杯 /// /// 0:无意义 1:放杯成功 2:执行失败(传感器还有信号) 3:放杯异常 private int putCup() { try { if (LebaiHelper.GetInstance.GetInput(1)) return 2; LebaiHelper.GetInstance.Scene(LebaiHelper.JUICE_放杯); Wait(); LebaiHelper.GetInstance.SetValue(1); LebaiHelper.GetInstance.Scene(LebaiHelper.JUICE_放杯检测); Wait(); LebaiHelper.GetInstance.SetValue(1); if (LebaiHelper.GetInstance.GetInput(2)) { return 1; } else { return 3; } } catch (Exception ex) { MessageLog.GetInstance.Show(ex.ToString()); return 0; } } /// /// 检测放杯位,是否有杯子 /// /// 0:无意义 1:没有杯子 2:有杯子 private int checkCup() { try { while (LebaiHelper.GetInstance.GetInput(1)) { Thread.Sleep(100); if (DateTime.Now.Subtract(delayTimeOut).TotalSeconds >= 1) return 2; } MessageLog.GetInstance.Show("放杯位有杯子未取走,等待取走,最多等待60s,即跳出流程"); return 1; } catch (Exception ex) { MessageLog.GetInstance.Show(ex.ToString()); return 0; } } private void CoffeEndCookHandle(IEvent @event, EventBus.EventCallBackHandle callBack) { are.Set(); } public void ReadData() { ThreadManage.GetInstance.StartLong(new Action(() => { lebai = LebaiHelper.GetInstance.GetValueAsync(); LebaiHelper.GetInstance.GetRobotModeStatus(); //LebaiHelper.GetInstance.GetInput(); Thread.Sleep(100); }), "乐百机器人数据读取", true); } public void SimOrder(T simOrder) { //if (morkOrderPushes.Count > 0) //{ // morkOrderPushes.Clear(); //} //morkOrderPushes.Enqueue(simOrder as MorkOrderPush); } /// /// IOT 广播消息命令 /// public void IotBroadcast(T broadcast) { if (broadcast != null && broadcast is IOTCommandModel iOTCommand) { switch (iOTCommand.CommandName) { case 0://控制类 if (iOTCommand.CommandValue != null) { if (iOTCommand.CommandValue.ContainsKey("SimOrder")) { //SimOrder(new SimOrderData { NoodleLoc = 1, BowlLoc = 10 }); } } break; case 1://设置属性 break; case 2://通知消息 break; default: break; } } } public class SimOrderData { public string id { get; set; } public int OrderStatus { get; set; } public string Loc { get; set; } public MorkOrderPush morkOrder { get; set; } public SimOrderData() { id = Guid.NewGuid().ToString(); OrderStatus = 0; } } } }