using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Threading; using BPA.Message; using BPA.Utility; using HBLConsole.Communication; using HBLConsole.Factory; using HBLConsole.GVL; using HBLConsole.Interface; using HBLConsole.Model; using HBLConsole.Service; using HBLDevice.Coffee; using HBLDevice.IceCream; using Robotc; using System.Collections.Concurrent; using System.Diagnostics; using BPA.Message.IOT; using HBLDevice.ICChip; namespace HBLConsole.MORKIC { /* * 冰淇淋咖啡机组合套装 * 物料位置: * 1:冰淇料 * 2:冰淇淋杯 * 5:咖啡 * 6:咖啡杯 */ public class Control_MORKIC : IControl { GVL_MORIC mORKD = new GVL_MORIC(); //咖啡机主控程序 private CoffeeMachine coffeeMachine; //单片机主控程序 private ICChipMachine icchipMachine; //物料存放位置 private Dictionary batchings = new Dictionary(); //容器位置 private string holderLoc; //主料位置 private string mainMaterialLoc; //子订单ID private string subOrderId; /// /// 获取乐百机器人的数据 /// SignalResult lebai = new SignalResult(); private bool enableFunny = false; private DateTime lastRecvdOrder = DateTime.Now; private bool working = false; public void ConnectOk() { } ConcurrentQueue morkOrderPushes = new ConcurrentQueue(); public void Init() { ActionManage.GetInstance.Register(new Action((s) => { if (s is DrCoffeeDrinksCode cf) { mainMaterialLoc = ((int)cf).ToString(); DoCoffee(); } }), "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; var com_IceCream = config.AppSettings.Settings["COM_IceCream"].Value; var baud_IceCream = config.AppSettings.Settings["BAUD_IceCream"].Value; var iceCreamCXBThreshold = int.Parse(config.AppSettings.Settings["IceCream_CXB_Threshold"].Value); var com_ICChip = config.AppSettings.Settings["COM_ICChip"].Value; var baud_ICChip = config.AppSettings.Settings["BAUD_IChip"].Value; if (iceCreamCXBThreshold > 0) { //设置冰淇淋成型比 MorkIStatus.GetInstance().CXB_Threshold = (byte)iceCreamCXBThreshold; } //咖啡机创建 coffeeMachine = new CoffeeMachine(com_Coffee, (BaudRates)Enum.Parse(typeof(BaudRates), baud_Coffee)); //单片机机创建 icchipMachine = new ICChipMachine(com_ICChip, (BaudRates)Enum.Parse(typeof(BaudRates), baud_ICChip)); Main(); ReadData(); ThreadManage.GetInstance.StartLong(new Action(() => { while (morkOrderPushes.Count > 0) { while (enableFunny) { Thread.Sleep(10); } MessageLog.GetInstance.Show("当前非自嗨模式,允许开工"); working = true; if (morkOrderPushes.TryDequeue(out MorkOrderPush order)) { MessageLog.GetInstance.Show($"开始制作订单[{order.SortNum}]"); //Thread.Sleep(5000); //SimpleFactory.GetInstance.OrderChanged(subOrderId, BPA.Message.Enum.ORDER_STATUS.COMPLETED_COOK); //商品类型 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) { //验证商品是咖啡还是冰淇淋 if (ValidateGoodsByBatching(res.BatchingLoc) != GOODS_TYPE.NEITHER) { //获取当前物料所属商品类型 currentGoodsType = ValidateGoodsByBatching(res.BatchingLoc); } //获取主料和容器位置 switch (batchings[res.BatchingLoc].BatchingClass) { case BATCHING_CLASS.HOLDER: holderLoc = res.BatchingLoc; break; case BATCHING_CLASS.MAIN_MATERIAL: mainMaterialLoc = res.BatchingLoc; break; } } } //根据商品类型执行具体制作流程 switch (currentGoodsType) { case GOODS_TYPE.COFFEE: DoCoffee(); break; case GOODS_TYPE.ICECREAM: DoIceCream(); break; } } working = false; lastRecvdOrder = DateTime.Now; } Thread.Sleep(1000); }), "订单制作"); } public void Main() { //咖啡机开启主线程 coffeeMachine.Start(); //单片机开启主线程 icchipMachine.Start(); new ModeSetEvent() { Mode = MORKI_MODE.制冷模式 }.Publish(); //开始心跳刷新,根据咖啡机及冰淇淋机来判断 ThreadManage.GetInstance.StartLong(new Action(() => { GeneralConfig.Healthy = LebaiHelper.GetInstance.IsConnected && MorkCStatus.GetInstance().CanDo && ChipStatus.GetInstance().CanDo; //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-IC欢迎"); //ThreadManage.GetInstance.StartLong(new Action(() => //{ // if (null != lebai) // { // if (!working) // { // if (DateTime.Now.Subtract(lastRecvdOrder).TotalSeconds >= 30) // { // MessageLog.GetInstance.Show("进入自嗨模式"); // enableFunny = true; // //ThreadManage.GetInstance.Start(new Action(() => // //{ // //}), "调用乐百机器人做咖啡场景"); // LebaiHelper.GetInstance.Scene(LebaiHelper.SENCE_自嗨); // Thread.Sleep(2000); // while (lebai.Ok && lebai.Value != 10) // { // Thread.Sleep(5); // } // enableFunny = false; // MessageLog.GetInstance.Show("退出自嗨模式"); // Thread.Sleep(60*1000); // } // } // } // Thread.Sleep(100); //}), "MORK-IC自嗨"); } 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 DoCoffee() { //are.Reset(); //LebaiHelper.GetInstance.SetValue(0); ////订单状态改变:开始制作 //SimpleFactory.GetInstance.OrderChanged(subOrderId, BPA.Message.Enum.ORDER_STATUS.COOKING); ////todo:先调用机器人 //LebaiHelper.GetInstance.Scene(LebaiHelper.SENCE_咖啡); ////ThreadManage.GetInstance.Start(new Action(() => { LebaiHelper.GetInstance.Scene(LebaiHelper.SENCE_咖啡); }), "调用乐百机器人做咖啡场景"); //Thread.Sleep(2000); //while (!(lebai.Ok && lebai.Value == 1)) //{ // Thread.Sleep(5); //} ////todo:咖啡杯下杯 //new TakeCupEvent() { Cup = IC_CUP.CUP_COFFEE }.Publish(); //Thread.Sleep(500); ////while (!ChipStatus.GetInstance().CompletedTake_CPU_CUP_COFFEE) { Thread.Sleep(5); } //MessageLog.GetInstance.Show("咖啡杯取杯完成"); //LebaiHelper.GetInstance.SetValue(100); //while (!(lebai.Ok && lebai.Value == 2)) //{ // Thread.Sleep(5); //} //MessageLog.GetInstance.Show("机器人到达接咖啡口位置"); //new MakeCoffeeEvent() { DrinkCode = (DrCoffeeDrinksCode)int.Parse(mainMaterialLoc) }.Publish(); //are.WaitOne(1000 * 180); ////are.WaitOne(1 * 180); //MessageLog.GetInstance.Show("咖啡机制作咖啡完成"); //LebaiHelper.GetInstance.SetValue(101); //while (!(lebai.Ok && lebai.Value == 3)) //{ // Thread.Sleep(5); //} //MessageLog.GetInstance.Show("机器人到达接咖啡口位置"); ////订单状态改变:完成 //SimpleFactory.GetInstance.OrderChanged(subOrderId, BPA.Message.Enum.ORDER_STATUS.COMPLETED_COOK); //---------------------------------------------------------含咖啡杯检测-----------------------------------------// #region 含咖啡杯检测的多场景 int checkeNum = 0; are.Reset(); LebaiHelper.GetInstance.SetValue(0); //订单状态改变:开始制作 SimpleFactory.GetInstance.OrderChanged(subOrderId, BPA.Message.Enum.ORDER_STATUS.COOKING); //todo:先调用机器人 LebaiHelper.GetInstance.Scene(LebaiHelper.SENCE_取咖啡杯); //ThreadManage.GetInstance.Start(new Action(() => { LebaiHelper.GetInstance.Scene(LebaiHelper.SENCE_咖啡); }), "调用乐百机器人做咖啡场景"); Thread.Sleep(2000); while (!(lebai.Ok && lebai.Value == 1)) { Thread.Sleep(5); } //todo:咖啡杯下杯 new TakeCupEvent() { Cup = IC_CUP.CUP_COFFEE }.Publish(); Thread.Sleep(500); //while (!ChipStatus.GetInstance().CompletedTake_CPU_CUP_COFFEE) { Thread.Sleep(5); } MessageLog.GetInstance.Show("咖啡杯取杯完成"); LebaiHelper.GetInstance.SetValue(100); //咖啡杯检测场景 while (!ChipStatus.GetInstance().ArticleExits) { checkeNum++; if (checkeNum > 1) { LebaiHelper.GetInstance.Scene(LebaiHelper.SENCE_再次取杯); while (!(lebai.Ok && lebai.Value ==3)) { Thread.Sleep(5); } new TakeCupEvent() { Cup = IC_CUP.CUP_COFFEE }.Publish(); Thread.Sleep(500); } LebaiHelper.GetInstance.Scene(LebaiHelper.SENCE_咖啡杯检测); while (!(lebai.Ok && lebai.Value == 2)) { Thread.Sleep(5); } new ArticleExitsEvent() { }.Publish(); Thread.Sleep(1000); } checkeNum = 0; LebaiHelper.GetInstance.Scene(LebaiHelper.SENCE_接咖啡); while (!(lebai.Ok && lebai.Value == 4)) { Thread.Sleep(5); } MessageLog.GetInstance.Show("机器人到达接咖啡口位置"); new MakeCoffeeEvent() { DrinkCode = (DrCoffeeDrinksCode)int.Parse(mainMaterialLoc) }.Publish(); are.WaitOne(1000 * 180); //are.WaitOne(1 * 180); MessageLog.GetInstance.Show("咖啡机制作咖啡完成"); LebaiHelper.GetInstance.SetValue(101); LebaiHelper.GetInstance.Scene(LebaiHelper.SENCE_送咖啡); while (!(lebai.Ok && lebai.Value == 5)) { Thread.Sleep(5); } MessageLog.GetInstance.Show("机器人到达顾客取咖啡位置"); //订单状态改变:完成 SimpleFactory.GetInstance.OrderChanged(subOrderId, BPA.Message.Enum.ORDER_STATUS.COMPLETED_COOK); #endregion } /// /// 做冰淇淋 /// private void DoIceCream() { LebaiHelper.GetInstance.SetValue(0); new RotorSwitchEvent() { TurnOn = true }.Publish(); int scene = LebaiHelper.SENCE_接冰淇淋2; var se = IC_SE.SE_2; se = PolymerBatching.GetIceCreamSE(mainMaterialLoc, out scene); //订单状态改变:开始制作 SimpleFactory.GetInstance.OrderChanged(subOrderId, BPA.Message.Enum.ORDER_STATUS.COOKING); //todo:先调用机器人 LebaiHelper.GetInstance.Scene(scene); MessageLog.GetInstance.Show("调用乐百机器人做冰淇淋场景"); Thread.Sleep(2000); while (!(lebai.Ok && lebai.Value == 1)) { Thread.Sleep(5); } new TakeCupEvent() { Cup = IC_CUP.CUP_ICECREAM }.Publish(); Thread.Sleep(500); //while (!ChipStatus.GetInstance().CompletedTake_CPU_CUP_ICECREAM) { Thread.Sleep(5); } LebaiHelper.GetInstance.SetValue(100); while (!(lebai.Ok && lebai.Value == 2)) { Thread.Sleep(5); } new MakeIceCreamEvent() { SteeringEngine = se }.Publish(); Thread.Sleep(1000); LebaiHelper.GetInstance.SetValue(101); new RotorSwitchEvent() { TurnOn = false }.Publish(); while (!(lebai.Ok && lebai.Value == 3)) { Thread.Sleep(5); } SimpleFactory.GetInstance.OrderChanged(subOrderId, BPA.Message.Enum.ORDER_STATUS.COMPLETED_COOK); } private void CoffeEndCookHandle(IEvent @event, EventBus.EventCallBackHandle callBack) { are.Set(); } //public void Main() //{ // //咖啡机开启主线程 // coffeeMachine.Start(); // //冰淇淋机开启主线程 // iceCreamMachine.Start(); // icchipMachine.Start(); // new ModeSetEvent() { Mode = MORKI_MODE.制冷模式 }.Publish(); // //开始心跳刷新,根据咖啡机及冰淇淋机来判断 // ThreadManage.GetInstance.StartLong(new Action(() => // { // //GeneralConfig.Healthy = true; // //GeneralConfig.Healthy = // // LebaiHelper.GetInstance.IsConnected && // // MorkIStatus.GetInstance().CanDo && // // MorkCStatus.GetInstance().CanDo; // GeneralConfig.Healthy = // LebaiHelper.GetInstance.IsConnected && // MorkCStatus.GetInstance().CanDo; // GeneralConfig.Healthy = true; // Thread.Sleep(100); // }), "MORK-IC心跳刷新"); //} public void ReadData() { ThreadManage.GetInstance.StartLong(new Action(() => { lebai = LebaiHelper.GetInstance.GetValueAsync(); LebaiHelper.GetInstance.GetRobotModeStatus(); Thread.Sleep(100); }), "乐百机器人数据读取"); } public void SimOrder(T simOrder) { //ThreadManage.GetInstance.Start(new Action(() => //{ // DoIceCream(); // //DoCoffee(); //}), "aaa"); } /// /// 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; } } } } }