|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- 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<string, PolymerBatching> batchings = new Dictionary<string, PolymerBatching>();
- //容器位置
- private string holderLoc;
- //主料位置
- private string mainMaterialLoc;
- /// <summary>
- /// 果汁机做法,true:热饮,false:冷饮
- /// </summary>
- 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<MorkOrderPush> morkOrderPushes = new ConcurrentQueue<MorkOrderPush>();
-
- /// <summary>
- ///
- /// </summary>
- /// <param name="batchingLoc">物料位置</param>
- 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<object> 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<T>(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>(T simOrder)
- {
-
- }
- public override void DoMain()
- {
- if (Json<KeepDataBase>.Data.IsVerify)
- {
- IsHealth = true;
- }
- IsHealth = true;
- serverInit();
- DataParse();
- }
- private void serverInit()
- {
- EventBus.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.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 (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<object>((o) =>
- {
- if (o is int b)
- {
- morkT.coffeeState = (K95SysTemStatus)b;
- }
- }));
- GetStatus("CoffeeIsConnected", new Action<object>((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<KLMCoffee_CoffeEndCookEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
- {
- bFirstTrig_Coffee = false;
- morkT.MakeCoffeeFinish = true;
- });
- }
- else
- {
- EventBus.EventBus.GetInstance().Subscribe<DRCoffee_CoffeEndCookEvent>(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<MorkTSimOrder>(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 "";
- }
- }
- }
|