|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479 |
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Linq;
- using System.Threading;
- using System.Collections.Concurrent;
- using System.Diagnostics;
- using System.Threading.Tasks;
- using BPASmartClient.Device;
- using BPA.Message.Enum;
- using BPA.Message;
- using BPA.Helper;
- using BPASmartClient.Model.咖啡机.Enum;
- using BPASmartClient.Model;
-
-
-
- using BPASmartClient.Model.乐白机器人;
- using BPASmartClient.Model.单片机;
- using BPASmartClient.Model.PLC;
- using BPASmartClient.DRCoffee;
- using BPASmartClient.LebaiRobot;
- using System.Text.RegularExpressions;
- using static BPA.Helper.EventBus;
-
- namespace BPASmartClient.MorkT_Container
- {
- public class Control_MorkT_Container : BaseDevice
- {
- private Dictionary<string, PolymerBatching> batchings = new Dictionary<string, PolymerBatching>();
- //容器位置
- private string holderLoc;
- //主料位置
- private string mainMaterialLoc;
- public override global::BPA.Message.Enum.DeviceClientType DeviceType { get { return BPA.Message.Enum.DeviceClientType.MORKT; } }
-
- GVL_MorkT morkTLebaiJC =new GVL_MorkT();
- public override void DoMain()
- {
- if (Json<KeepDataBase>.Data.IsVerify)
- {
- IsHealth = true;
- }
- IsHealth = true;
- serverInit();
- DataParse();
- ActionManage.GetInstance.Register(new Action<object>((o) => { SimOrder(o); }), "SimOrder");//模拟订单委托注册
- }
-
- private void serverInit()
- {
- EventBus.GetInstance().Subscribe<MaterialDeliveryEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
- {
- if (@event == null) return;
- if (@event is MaterialDeliveryEvent material)
- {
- orderMaterialDelivery = material.orderMaterialDelivery;
- }
- });
- }
- private void DataParse()
- {
- 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}次订单");
- batchings = PolymerBatching.BuildAll();
- //商品类型
- GOODS_TYPE currentGoodsType = GOODS_TYPE.NEITHER;
- foreach (var item in order.MorkOrder.GoodBatchings)
- {
- var res = 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:
-
- if (morkTLebaiJC.morkOrderPushesCoffee.FirstOrDefault(p => p.SuborderId == order.MorkOrder.SuborderId) == null)
- {
- morkTLebaiJC.morkOrderPushesCoffee.Enqueue(new OrderLocInfo()
- {
- SuborderId = order.MorkOrder.SuborderId,
- BatchingId = res.BatchingId,
- Loc = ushort.Parse( mainMaterialLoc),
- GoodName = order.MorkOrder.GoodsName,
- });
- }
- break;
- case GOODS_TYPE.NEITHER:
- DeviceProcessLogShow("未知的商品类型");
- break;
- }
- }
- }
- }
- });
- }
- /// <summary>
- /// 将空杯放好到接饮料的地方的标志位
- /// </summary>
- private bool bFirstTrig_Coffee = false;
- /// <summary>
- /// 延迟的超时时间
- /// </summary>
- DateTime delayTimeOut_Coffee;
- public override void MainTask()
- {
- if (morkTLebaiJC.morkOrderPushesCoffee.Count > 0 && morkTLebaiJC.IsHaveCoffeeCup)
- {
- EventBus.GetInstance().Subscribe<DRCoffee_CoffeEndCookEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
- {
- if (morkTLebaiJC.IsHaveCoffeeCup && morkTLebaiJC.MakeCoffeeEnd != true)
- {
- morkTLebaiJC.MakeCoffeeEnd = true;
- bFirstTrig_Coffee = false;
- }
- });
- if (morkTLebaiJC.IsHaveCoffeeCup && morkTLebaiJC.MakeCoffeeEnd != true)
- {
- if (!bFirstTrig_Coffee)
- {
- bFirstTrig_Coffee = true;
- delayTimeOut_Coffee = DateTime.Now;
- }
- else if (DateTime.Now.Subtract(delayTimeOut_Coffee).TotalSeconds > 180 && bFirstTrig_Coffee == true)
- {
- DeviceProcessLogShow("接咖啡超时,接咖啡结束,等待取咖啡");
- bFirstTrig_Coffee = false;
- morkTLebaiJC.MakeCoffeeEnd = true;
- }
- }
- }
-
- DoCoffee();
- }
-
- /// <summary>
- /// 订单状态改变
- /// </summary>
- /// <param name="subid"></param>
- /// <param name="oRDER_STATUS"></param>
- private void OrderChange(string subid, ORDER_STATUS oRDER_STATUS)
- {
- EventBus.GetInstance().Publish(new OrderStatusChangedEvent() { Status = oRDER_STATUS, SubOrderId = subid, deviceClientType = DeviceType });
- }
- /// <summary>
- /// 验证商品是做的某种饮料
- /// </summary>
- /// <param name="batchingLoc">物料位置</param>
- private GOODS_TYPE ValidateGoodsByBatching(string batchingLoc)
- {
- if (batchings.ContainsKey(batchingLoc))
- return batchings[batchingLoc].GoodsType;
- return GOODS_TYPE.NEITHER;
- }
- /// <summary>
- /// 乐白的场景结束等待
- /// </summary>
- /// <param name="value"></param>
- private void Wait(int value = 101)
- {
- while (!((bool)peripheralStatus["RobotOK"] && (int)peripheralStatus["RobotValue"] == value))
- {
- Thread.Sleep(5);
- }
-
- EventBus.GetInstance().Publish(new LebaiRobot_SetValueEvent { DeviceId = DeviceId, RobotSetValue = 0 }, (o) => { });
- }
- /// <summary>
- /// 乐白的场景
- /// </summary>
- /// <param name="sen"></param>
- private void Sence(int sen)
- {
- new LebaiRobot_LebaiSenceEvent { DeviceId = DeviceId, LebaiSence = sen }.Publish();
- }
- /// <summary>
- /// 乐白的数字量输出
- /// </summary>
- /// <param name="value"></param>
- /// <param name="pin"></param>
- private void Output(bool value,int pin)
- {
- new LebaiRobot_SetOutPutEvent { DeviceId = DeviceId, Pin = pin,Value=value }.Publish();
- }
- bool outCupCheck = false;//放纸杯位置有无判断
-
- /// <summary>
- /// 判断是否有咖啡订单
- /// </summary>
- /// <returns></returns>
- private bool IsMakeCoffee()
- {
- bool bMake = (IsHealth && morkTLebaiJC.morkOrderPushesCoffee.Count > 0 && !morkTLebaiJC.IsHaveCoffeeCup) ? true : false;
- return bMake;
- }
- /// <summary>
- /// 做咖啡流程
- /// </summary>
- private void DoCoffee()
- {
- if (IsMakeCoffee())
- {
- PickUpCoffee();//接咖啡
- morkTLebaiJC.IsHaveCoffeeCup = true;
- }
- else if(morkTLebaiJC.MakeCoffeeEnd)
- {
- try
- {
- PutCoffeeCup();
- morkTLebaiJC.MakeCoffeeEnd = false;
- morkTLebaiJC.IsHaveCoffeeCup = false;
- morkTLebaiJC.morkOrderPushesCoffee.TryDequeue(out OrderLocInfo orderLoc);
- }
- catch (Exception ex)
- {
- MessageLog.GetInstance.ShowEx(ex.ToString());
- }
- }
- }
-
-
-
- #region 做咖啡流程
- /// <summary>
- /// 接咖啡
- /// </summary>
- private void PickUpCoffee()
- {
- if (!morkTLebaiJC.IsHaveCoffeeCup)
- {
- outCupCheck = false;
- OrderChange(morkTLebaiJC.morkOrderPushesCoffee.ElementAt(0).SuborderId, BPA.Message.Enum.ORDER_STATUS.COOKING);
- EventBus.GetInstance().Publish(new LebaiRobot_SetValueEvent { DeviceId = DeviceId, RobotSetValue = 0 }, (o) => { });
- int resultTakeCup = takeCup();
- if (resultTakeCup == 1)
- {
- Sence(GVL_MorkT.攀华_接咖啡);
- Wait();
- new DRCoffee_MakeCoffeeEvent() { DeviceId=DeviceId, DrinkCode = (DrCoffeeDrinksCode)(morkTLebaiJC.morkOrderPushesCoffee.ElementAt(0).Loc)}.Publish();
- }
- else
- {
- DeviceProcessLogShow("取杯失败 回到初始位,请及时处理!!");
- /*new LebaiRobot_SetValueEvent { DeviceId = DeviceId, RobotSetValue = 0 }.Publish();
- Sence(GVL_MorkT.攀华_初始位);
- Wait();*/
- }
- }
- }
- /// <summary>
- /// 咖啡杯接好,放咖啡杯
- /// </summary>
- private void PutCoffeeCup()
- {
- while (morkTLebaiJC.RobotGetInput0)//判断放杯位置是否有物品
- {
- if (!outCupCheck)
- DeviceProcessLogShow("成品处有纸杯存在,请取走!!");
- outCupCheck = true;
- }
- outCupCheck = false;
-
- Sence(GVL_MorkT.攀华_放杯);
- Wait();
- if (!morkTLebaiJC.RobotGetInput0)
- {
- DeviceProcessLogShow("咖啡杯未安全放置在放杯位");
- }
- Sence(GVL_MorkT.攀华_初始位);
- Wait();
- while (morkTLebaiJC.RobotGetTcpInput)
- {
- DeviceProcessLogShow("咖啡杯仍在夹爪上");
- }
- OrderChange(morkTLebaiJC.morkOrderPushesCoffee.ElementAt(0).SuborderId, BPA.Message.Enum.ORDER_STATUS.COMPLETED_TAKE);
- DeviceProcessLogShow($"-------------{Regex.Replace(morkTLebaiJC.morkOrderPushesCoffee.ElementAt(0).GoodName, @"[\r\n]", "")}制作完成------------");
- }
- #endregion
-
- /// <summary>
- /// 取杯的次数
- /// </summary>
- private int nCnt;
- /// <summary>
- /// 取杯流程
- /// </summary>
- /// <returns>0:无意义,1:取杯成功 2:取杯失败</returns>
- private int takeCup()
- {
- try
- {
- nCnt = 0;
-
- Sence(GVL_MorkT.攀华_初始位);
- Wait();
-
- Sence(GVL_MorkT.攀华_取杯);
- Wait();
-
- Sence(GVL_MorkT.攀华_落杯);
- Wait();//落杯使用机器人控制
- /*new LebaiRobot_SetOutPutEvent { DeviceId = DeviceId, Value = true, Pin = 1 }.Publish();//落杯器电机转动
-
- while (true)
- {
- if (GetStatus<bool>("LeibaiGetInput2"))
- {
- break;
- }
- }
- while (true)
- {
- if (!GetStatus<bool>("LeibaiGetInput2"))
- {
- break;
- }
- }
- new LebaiRobot_SetOutPutEvent { DeviceId = DeviceId, Value = true, Pin = 0 }.Publish();//落杯器电机转动*/
-
- nCnt++;
- Thread.Sleep(2000);
- while (!morkTLebaiJC.RobotGetTcpInput) //读取传感器的值
- {
- if (nCnt > 3)
- {
- nCnt = 0;
- DeviceProcessLogShow("三次取杯失败,回原点");
- Sence(GVL_MorkT.攀华_安全位);
- Wait();
- new LebaiRobot_SetValueEvent { DeviceId = DeviceId, RobotSetValue = 0 }.Publish();
- Sence(GVL_MorkT.攀华_初始位);
- Wait();
- new LebaiRobot_SetValueEvent { DeviceId = DeviceId, RobotSetValue = 0 }.Publish();
- return 2;
- }
- else
- {
- nCnt++;
- Sence(GVL_MorkT.攀华_二次取杯);
- Wait();
- EventBus.GetInstance().Publish(new LebaiRobot_SetValueEvent { DeviceId = DeviceId, RobotSetValue = 0 }, (o) => { });
-
- Sence(GVL_MorkT.攀华_落杯);
- Wait();
- EventBus.GetInstance().Publish(new LebaiRobot_SetValueEvent { DeviceId = DeviceId, RobotSetValue = 0 }, (o) => { });
- }
- Thread.Sleep(100);
- }
- return 1;
- }
- catch (Exception ex)
- {
- DeviceProcessLogShow(ex.ToString());
- }
- return 2;
- }
- public void SimOrder<T>(T simOrder)
- {
-
- }
- #region 流程控制
-
- private void GetStatus(string key, Action<object> action)
- {
- if (peripheralStatus.ContainsKey(key))
- {
- if (peripheralStatus[key] != null)
- {
- action?.Invoke(peripheralStatus[key]);
- }
- }
- }
-
- public override void ReadData()
- {
- #region 乐白机器人状态
- GetStatus("RobotIsConnected", new Action<object>((o) =>
- {
- if (o is bool b)
- {
- morkTLebaiJC.RobotIsConnected = b;
- }
- }));
- GetStatus("RobotMode", new Action<object>((o) =>
- {
- if (o is ELebaiRModel eLebaiRModel)
- {
- morkTLebaiJC.RobotMode = eLebaiRModel;
- }
- }));
- GetStatus("LeibaiGetTcpInput", new Action<object>((o) =>
- {
- if (o is bool b)
- {
- morkTLebaiJC.RobotGetTcpInput = b;
- }
- }));
- GetStatus("LeibaiGetInput1", new Action<object>((o) =>
- {
- if (o is bool b)
- {
- morkTLebaiJC.RobotGetInput0 = b;
- }
- }));
- GetStatus("LeibaiGetInput2", new Action<object>((o) =>
- {
- if (o is bool b)
- {
- morkTLebaiJC.RobotGetInput1 = b;
- }
- }));
- GetStatus("LeibaiGetInput3", new Action<object>((o) =>
- {
- if (o is bool b)
- {
- morkTLebaiJC.RobotGetInput2 = b;
- }
- }));
- GetStatus("LeibaiGetInput4", new Action<object>((o) =>
- {
- if (o is bool b)
- {
- morkTLebaiJC.RobotGetInput3 = b;
- }
- }));
-
- #endregion
- }
- #endregion
- public override void Stop()
- {
-
- }
-
- public override void ResetProgram()
- {
-
- }
-
- public override void SimOrder()
- {
- EventBus.GetInstance().Subscribe<MorkTSimOrder>(0, delegate (IEvent @event, EventCallBackHandle callBackHandle)
- {
- string guid = Guid.NewGuid().ToString();
- if (@event != null && @event is MorkTSimOrder msm)
- {
- DeviceProcessLogShow("----开始模拟订单----");
- if (msm.OrderNum == 1)
- {
- morkTLebaiJC.morkOrderPushesCoffee.Enqueue(new OrderLocInfo() { Loc =(ushort)msm.DrinkCode, SuborderId = guid, GoodName="模拟咖啡订单" });
- }
- }
- });
- }
- }
- }
|