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 BPASmartClient.Helper; using BPASmartClient.Model.咖啡机.Enum; using BPASmartClient.Model; using BPASmartClient.EventBus; using static BPASmartClient.EventBus.EventBus; using BPASmartClient.Model.PLC; using BPASmartClient.Model.单片机; using BPASmartClient.Business; using BPASmartClient.KLMCoffee.Protocal; using BPASmartClient.Message; namespace BPASmartClient.MorkTSingle { /* * 冰淇淋咖啡机组合套装 * 物料位置: * 1:冰淇料 * 2:冰淇淋杯 * 5:咖啡 * 6:咖啡杯 * 9: 茶 * 10: 茶杯 */ public class Control_MORKJC : BaseDevice { public override global::BPA.Message.Enum.DeviceClientType DeviceType { get { return BPA.Message.Enum.DeviceClientType.MORKT; } } GVL_MORKJC morkT = new GVL_MORKJC(); //物料存放位置 private Dictionary batchings = new Dictionary(); //容器位置 private string holderLoc; //主料位置 private string mainMaterialLoc; /// /// 果汁机做法,true:热饮,false:冷饮 /// private bool GuMake = false; private void OrderChange(string subid, ORDER_STATUS oRDER_STATUS) { EventBus.EventBus.GetInstance().Publish(new OrderStatusChangedEvent() { Status = oRDER_STATUS, SubOrderId = subid, deviceClientType = DeviceType }); } //private SerialPortClient commProxy; public void ConnectOk() { } ConcurrentQueue morkOrderPushes = new ConcurrentQueue(); /// /// /// /// 物料位置 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 GetStatus(string key, Action action) { if (peripheralStatus.ContainsKey(key)) { if (peripheralStatus[key] != null) { action?.Invoke(peripheralStatus[key]); } } } DateTime delayTimeOut_Coffee; private void DoCoffee() { if (IsHealth && morkT.morkOrderPushesCoffee.Count > 0 && !morkT.IsCoffeeMake /*&& morkT.coffeeState== K95SysTemStatus.空闲状态*/) { Thread.Sleep(1000); OrderChange(morkT.morkOrderPushesCoffee.ElementAt(0).SuborderId, BPA.Message.Enum.ORDER_STATUS.COOKING); if (!GVL_MORKJC.CoffeeKind) { new KLMCoffee_MakeCoffeeEvent() { DeviceId = DeviceId, KLMDrinkFaultCode = (KLMDrinkFaultType)(morkT.morkOrderPushesCoffee.ElementAt(0).Loc) }.Publish(); //接咖啡控制 //DrCoffeeDrinksCode.热水 } else { new DRCoffee_MakeCoffeeEvent() { DeviceId = DeviceId, DrinkCode = (DrCoffeeDrinksCode)int.Parse(mainMaterialLoc) }.Publish(); } morkT.IsCoffeeMake = true; } else if (morkT.MakeCoffeeFinish && IsHealth && morkT.morkOrderPushesCoffee.Count > 0 && morkT.IsCoffeeMake) { OrderChange(morkT.morkOrderPushesCoffee.ElementAt(0).SuborderId, BPA.Message.Enum.ORDER_STATUS.COMPLETED_TAKE); DeviceProcessLogShow($"{morkT.morkOrderPushesCoffee.ElementAt(0).GoodName}制作完成"); if (morkT.morkOrderPushesCoffee.TryDequeue(out OrderLocInfo orderloc)) { morkT.IsCoffeeMake = false; morkT.MakeCoffeeFinish = false; Thread.Sleep(1000); }; } } private T McuRead(string tagName, object par) { new ReadMcu() { DeviceId = DeviceId, TagName = tagName, ReadPar = par }; if (peripheralStatus.ContainsKey(tagName)) { if (peripheralStatus[tagName] != null) { return (T)peripheralStatus[tagName]; } } return default; } public void SimOrder(T simOrder) { } public override void DoMain() { if (Json.Data.IsVerify) { IsHealth = true; } IsHealth = true; serverInit(); DataParse(); } private void serverInit() { EventBus.EventBus.GetInstance().Subscribe(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack) { if (@event == null) return; if (@event is MaterialDeliveryEvent material) { orderMaterialDelivery = material.orderMaterialDelivery; } }); } private void DataParse() { EventBus.EventBus.GetInstance().Subscribe(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 (morkT.morkOrderPushesCoffee.FirstOrDefault(p => p.SuborderId == order.MorkOrder.SuborderId) == null) { morkT.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; } } } } }); } public override void Stop() { } public override void ReadData() { } private bool bFirstTrig_Coffee = false; public override void MainTask() { GetStatus("CoffeeStatus", new Action((o) => { if (o is int b) { morkT.coffeeState = (K95SysTemStatus)b; } })); GetStatus("CoffeeIsConnected", new Action((o) => { if (o is bool b) { morkT.KLMCoffeeIsConnected = b; } })); if ( morkT.morkOrderPushesCoffee.Count > 0) { if (morkT.KLMCoffeeIsConnected) { if (morkT.IsCoffeeMake) { if (!GVL_MORKJC.CoffeeKind) { EventBus.EventBus.GetInstance().Subscribe(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack) { bFirstTrig_Coffee = false; morkT.MakeCoffeeFinish = true; }); } else { EventBus.EventBus.GetInstance().Subscribe(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack) { bFirstTrig_Coffee = false; morkT.MakeCoffeeFinish = true; }); } if (morkT.MakeCoffeeFinish != 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; morkT.MakeCoffeeFinish = true; } } } DoCoffee(); } else { MessageLog.GetInstance.ShowEx("未读取到咖啡机设备心跳"); } } } public override void ResetProgram() { } public override void SimOrder() { EventBus.EventBus.GetInstance().Subscribe(0, delegate (IEvent @event, EventCallBackHandle callBackHandle) { string guid = Guid.NewGuid().ToString(); if (@event != null && @event is MorkTSimOrder msm) { DeviceProcessLogShow("----开始模拟订单----"); morkT.morkOrderPushesCoffee.Enqueue(new OrderLocInfo() { Loc = (ushort)msm.KLMDrinkFaultCode, SuborderId = guid, GoodName = "模拟咖啡订单" }); } }); /*string aa = calLrcCommon("01" + "01" + "0001"); string bb = ":01010001" + aa + "\r\n";*/ } public static string calLrcCommon(string data) { try { if (data.Length % 2 != 0) { data = data + "0"; } int total = 0; int len = data.Length; int num = 0; while (num < len) { string s = data.Substring(num, 2); total += Convert.ToInt32(s, 16); num += 2; } total = ~total + 1; string checkSum = (total & 255).ToString("x").ToUpper(); while (checkSum.Length < 2) { checkSum = "0" + checkSum; } return checkSum; } catch (Exception) { } return ""; } } }