using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Threading; using BPA.Message; //using HBLDevice.ICChip; using HBLConsole.Communication; using HBLConsole.Factory; using HBLConsole.GVL; using HBLConsole.Interface; using HBLConsole.Model; using HBLConsole.Service; using HBLDevice.Coffee; //using HBLDevice.Juicer; using Robotc; using System.Collections.Concurrent; using System.Diagnostics; using BPA.Message.IOT; using System.Threading.Tasks; namespace HBLConsole.MORKJC { /* * 冰淇淋咖啡机组合套装 * 物料位置: * 1:冰淇料 * 2:冰淇淋杯 * 5:咖啡 * 6:咖啡杯 * 9: 茶 * 10: 茶杯 */ public class Control_MORKJC : IControl { GVL_MORKJC mORKD = new GVL_MORKJC(); //咖啡机主控程序 private CoffeeMachine coffeeMachine; ////果汁机主控程序 //private JuicerMachine juicerMachine; ////单片机主控程序 //private ICChipMachine icchipMachine; //物料存放位置 private Dictionary batchings = new Dictionary(); //容器位置 private string holderLoc; //主料位置 private string mainMaterialLoc; //子订单ID private string subOrderId; private bool enableFunny = false; private DateTime lastRecvdOrder = DateTime.Now; private bool working = 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) { 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_Juicer = config.AppSettings.Settings["COM_Juicer"].Value; //var baud_Juicer = config.AppSettings.Settings["BAUD_Juicer"].Value; //var com_ICChip = config.AppSettings.Settings["COM_ICChip"].Value; //var baud_ICChip = config.AppSettings.Settings["BAUD_IChip"].Value; //咖啡机创建 coffeeMachine = new CoffeeMachine(com_Coffee, (BaudRates)Enum.Parse(typeof(BaudRates), baud_Coffee)); ////单片机机创建 //icchipMachine = new ICChipMachine(com_ICChip, (BaudRates)Enum.Parse(typeof(BaudRates), baud_ICChip)); ////果汁机创建 //juicerMachine = new JuicerMachine(com_Juicer, (BaudRates)Enum.Parse(typeof(BaudRates), baud_Juicer)); Main(); ReadData(); ThreadManage.GetInstance.StartLong(new Action(() => { while (morkOrderPushes.Count > 0) { if (!LebaiHelper.GetInstance.GetInput()) { 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.JUICE: 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 = JaKaHelper.GetInstance.IsConnected && MorkCStatus.GetInstance().CanDo; //GeneralConfig.Healthy = true; Thread.Sleep(100); }), "MORK-IC心跳刷新"); ThreadManage.GetInstance.Start(new Action(() => { while (!JaKaHelper.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) { while (!((JaKaHelper.GetInstance.Get_RobotAO1() == value) && JaKaHelper.GetInstance.GetProgramStatus() == 0))//判断文件是否已经执行结束 且 文件末端变量值==文件名 { Thread.Sleep(5); } } /// /// 做咖啡 /// private void DoCoffee() { #region 且时且多入场设备程序 int checkeNum = 0; are.Reset(); SimpleFactory.GetInstance.OrderChanged(subOrderId, BPA.Message.Enum.ORDER_STATUS.COOKING); JaKaHelper.GetInstance.Set_RobotAO1(0); JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_取杯); Wait(int.Parse(JaKaHelper.SENCE_取杯)); Thread.Sleep(500); MessageLog.GetInstance.Show("咖啡杯取杯完成"); p: JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_取杯检测); Wait(int.Parse(JaKaHelper.SENCE_取杯检测)); if (!LebaiHelper.GetInstance.GetTcpInput()) { MessageLog.GetInstance.Show("执行二次取咖啡杯"); JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_取杯); Wait(int.Parse(JaKaHelper.SENCE_取杯)); goto p; } JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_接咖啡); Wait(int.Parse(JaKaHelper.SENCE_接咖啡)); new MakeCoffeeEvent() { DrinkCode = (DrCoffeeDrinksCode)int.Parse(mainMaterialLoc) }.Publish(); //接咖啡控制 are.WaitOne(1000 * 180); Thread.Sleep(3000); JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_放咖啡杯); Wait(int.Parse(JaKaHelper.SENCE_放咖啡杯)); JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_放杯检测); Wait(int.Parse(JaKaHelper.SENCE_放杯检测)); JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_初始位); Wait(int.Parse(JaKaHelper.SENCE_初始位)); //订单状态改变:完成 SimpleFactory.GetInstance.OrderChanged(subOrderId, BPA.Message.Enum.ORDER_STATUS.COMPLETED_TAKE); #endregion } /// /// 做茶 /// private void DoTea() { if (JaKaHelper.GetInstance.Get_RobotDI(1)) return; #region 流程 are.Reset(); JaKaHelper.GetInstance.Set_RobotAO1(0); SimpleFactory.GetInstance.OrderChanged(subOrderId, BPA.Message.Enum.ORDER_STATUS.COOKING); JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_取杯); Wait(int.Parse(JaKaHelper.SENCE_取杯)); Thread.Sleep(500); MessageLog.GetInstance.Show("茶水杯取杯完成"); p: JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_取杯检测); Wait(int.Parse(JaKaHelper.SENCE_取杯检测)); if (!LebaiHelper.GetInstance.GetTcpInput()) { MessageLog.GetInstance.Show("执行二次取茶水杯"); JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_取杯); Wait(int.Parse(JaKaHelper.SENCE_取杯)); goto p; } JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_接茶); Wait(int.Parse(JaKaHelper.SENCE_接茶)); MCUSerialHelper.GetInstance.ServoControl(1, 105); Thread.Sleep(1000); MCUSerialHelper.GetInstance.ServoControl(1, 130); Thread.Sleep(1000); MCUSerialHelper.GetInstance.ServoControl(1, 105); JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_接茶_接水); Wait(int.Parse(JaKaHelper.SENCE_接茶_接水)); JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_接水); Wait(int.Parse(JaKaHelper.SENCE_接水)); MCUSerialHelper.GetInstance.OutputControl(2, true); Thread.Sleep(1000); MCUSerialHelper.GetInstance.OutputControl(2, false); Thread.Sleep(1000); MCUSerialHelper.GetInstance.OutputControl(3, true); Thread.Sleep(1000); MCUSerialHelper.GetInstance.OutputControl(3, false); Thread.Sleep(3000); are.WaitOne(1000 * 180); //添加控制接水机构程序 JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_放茶水杯); Wait(int.Parse(JaKaHelper.SENCE_放茶水杯)); JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_放杯检测); Wait(int.Parse(JaKaHelper.SENCE_放杯检测)); JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_初始位); Wait(int.Parse(JaKaHelper.SENCE_初始位)); SimpleFactory.GetInstance.OrderChanged(subOrderId, BPA.Message.Enum.ORDER_STATUS.COMPLETED_TAKE); #endregion } /// /// 接开水 /// private void DoWater() { if (JaKaHelper.GetInstance.Get_RobotDI(1)) return; #region 流程 are.Reset(); JaKaHelper.GetInstance.Set_RobotAO1(0); SimpleFactory.GetInstance.OrderChanged(subOrderId, BPA.Message.Enum.ORDER_STATUS.COOKING); JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_取杯); Wait(int.Parse(JaKaHelper.SENCE_取杯)); Thread.Sleep(500); MessageLog.GetInstance.Show("水杯取杯完成"); p: JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_取杯检测); Wait(int.Parse(JaKaHelper.SENCE_取杯检测)); if (!LebaiHelper.GetInstance.GetTcpInput()) { MessageLog.GetInstance.Show("执行二次取水杯"); JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_取杯); Wait(int.Parse(JaKaHelper.SENCE_取杯)); goto p; } JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_接水); Wait(int.Parse(JaKaHelper.SENCE_接水)); are.WaitOne(1000 * 180); MCUSerialHelper.GetInstance.OutputControl(2, true); Thread.Sleep(1000); MCUSerialHelper.GetInstance.OutputControl(2, false); Thread.Sleep(1000); MCUSerialHelper.GetInstance.OutputControl(3, true); Thread.Sleep(1000); MCUSerialHelper.GetInstance.OutputControl(3, false); Thread.Sleep(3000); //添加控制接水机构程序 JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_放水杯); Wait(int.Parse(JaKaHelper.SENCE_放水杯)); JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_放杯检测); Wait(int.Parse(JaKaHelper.SENCE_放杯检测)); JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_初始位); Wait(int.Parse(JaKaHelper.SENCE_初始位)); SimpleFactory.GetInstance.OrderChanged(subOrderId, BPA.Message.Enum.ORDER_STATUS.COMPLETED_TAKE); #endregion } /// /// 做果汁 /// private void DoJuicer() { if (JaKaHelper.GetInstance.Get_RobotDI(1)) return; #region 入场设备程序 are.Reset(); JaKaHelper.GetInstance.Set_RobotAO1(0); SimpleFactory.GetInstance.OrderChanged(subOrderId, BPA.Message.Enum.ORDER_STATUS.COOKING); JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_取杯); Wait(int.Parse(JaKaHelper.SENCE_取杯)); MessageLog.GetInstance.Show("果汁杯取杯完成"); //new TakeCupEvent() { Cup = IC_CUP.CUP_ICECREAM }.Publish();//落碗控制 Thread.Sleep(10); p: JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_取杯检测); Wait(int.Parse(JaKaHelper.SENCE_取杯检测)); if (!JaKaHelper.GetInstance.Get_RobotDI(2)) { MessageLog.GetInstance.Show("执行二次取果汁杯"); JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_取杯); Wait(int.Parse(JaKaHelper.SENCE_取杯)); MessageLog.GetInstance.Show("二次果汁杯取杯完成"); goto p; } JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_接果汁1); Wait(int.Parse(JaKaHelper.SENCE_接果汁1)); are.WaitOne(1000 * 180); Thread.Sleep(3000);//等待几秒,饮料口延迟 防止有饮料滴在杯口 JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_放果汁杯1); Wait(int.Parse(JaKaHelper.SENCE_放果汁杯1)); //订单状态改变:完成 JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_放杯检测); Wait(int.Parse(JaKaHelper.SENCE_放杯检测)); JaKaHelper.GetInstance.JaKaProgramName(JaKaHelper.SENCE_初始位); Wait(int.Parse(JaKaHelper.SENCE_初始位)); SimpleFactory.GetInstance.OrderChanged(subOrderId, BPA.Message.Enum.ORDER_STATUS.COMPLETED_TAKE); #endregion } private void CoffeEndCookHandle(IEvent @event, EventBus.EventCallBackHandle callBack) { are.Set(); } public void ReadData() { ThreadManage.GetInstance.StartLong(new Action(() => { Thread.Sleep(100); }), "机器人数据读取", true); } public void SimOrder(T simOrder) { } /// /// 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; } } } } }