using BPA.Message; using BPA.Message.Enum; using BPASmartClient.Device; using BPASmartClient.EventBus; using BPASmartClient.Helper; using BPASmartClient.Message; using BPASmartClient.Model; using BPASmartClient.Model.PLC; using BPASmartClient.MorkTM.Model; using System.Collections.Concurrent; using static BPASmartClient.EventBus.EventBus; namespace BPASmartClient.MorkTM { public class Control_MorkTM : BaseDevice { public override global::BPA.Message.Enum.DeviceClientType DeviceType { get { return BPA.Message.Enum.DeviceClientType.TMC_MT; } } GVL_MorkTM morkTM = new GVL_MorkTM(); PolymerBatching polymer = new PolymerBatching(); //浮点数放大倍数 const int expand = 100; public override void DoMain() { ServerInit(); DataParse(); polymer.GetMaterialInfo(); ActionManage.GetInstance.Register(new Action((o) => { if (o != null && o is WritePar writePar) WriteData(writePar.Address, writePar.Value); }), "WriteVW"); ActionManage.GetInstance.Register(new Action((o) => { if (o != null && o is WritePar writePar) WriteData(writePar.Address, writePar.Value); }), "WriteBools"); morkTM.ReachPosions = new List() { morkTM.ReachOutPosion_0, morkTM.ReachPosion_1, morkTM.ReachPosion_2, morkTM.ReachPosion_3, morkTM.ReachPosion_4, morkTM.ReachPosion_5, morkTM.ReachPosion_6 }; DeviceProcessLogShow("设备初始化完成"); } 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; } }); //配方数据信息 EventBus.EventBus.GetInstance().Subscribe(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack) { if (@event == null) return; if (@event is RecipeBomEvent recipe) { recipeBoms = recipe.recipeBoms; } }); } private void DataParse() { EventBus.EventBus.GetInstance().Subscribe(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack) { if(@event == null) return; if(@event is DoOrderEvent order) { if (order.MorkOrder.GoodBatchings == null) return; OrderCount++; OrderChange(order.MorkOrder, ORDER_STATUS.WAIT); DeviceProcessLogShow($"接收到{OrderCount}次订单"); Dictionary OrderPushes = new Dictionary(); foreach (var item in order.MorkOrder.GoodBatchings) { var res = orderMaterialDelivery?.BatchingInfo?.FirstOrDefault(p => p.BatchingId == item.BatchingId); if (res != null) { OrderPushes.TryAdd(int.Parse(res.BatchingLoc), item.BatchingCount); } } morkTM.morkOrderPushesTeaWithMilk.Enqueue(new OrderLocInfo() { GoodName = order.MorkOrder.GoodsName, SuborderId = order.MorkOrder.SuborderId, GoodPushes = OrderPushes }); } }); } private void OrderChange(MorkOrderPush orderPush, ORDER_STATUS oRDER_STATUS) { EventBus.EventBus.GetInstance().Publish(new OrderStatusChangedEvent() { SortNum = orderPush.SortNum.ToString(), GoodName = orderPush.GoodsName, Status = oRDER_STATUS, SubOrderId = orderPush.SuborderId, deviceClientType = DeviceType }); } public override void MainTask() { IsHealth = true; MakeTeaWithMilkProcess(); } private void MakeTeaWithMilkProcess() { if (morkTM.morkOrderPushesTeaWithMilk.Count > 0) { if (morkTM.morkOrderPushesTeaWithMilk.TryDequeue(out OrderLocInfo orderLoc)) //&&原点位置是否有杯子) { SetMatertialWeight(orderLoc.GoodPushes);//设置物料出料量 morkTM.RecipesPushes.Clear(); morkTM.RecipesPushes = orderLoc.GoodPushes; foreach (var item in morkTM.RecipesPushes) { PosionTurnOn(item.Key);//设定转盘位置并等待到位信号 Thread.Sleep(1000); OpenUsePassageWay(item.Key);//打开通道并等待出料完成 DeviceProcessLogShow($"奶茶{orderLoc.GoodName}:配料{item.Key}:添加量{item.Value}"); } } } } /// /// 打开出料通道 /// private void OpenUsePassageWay(int pos) { string address = ""; foreach (var item in polymer.OutPosionPLCs) { if(item.posion is MaterialPosion posion) { if(posion == (MaterialPosion)pos) { WriteData(item.SetPLCPosion,true); address = item.GetPLCPosion; return; } } } while(!morkTM.outMaterialCompletes[pos]) //等待出料完成 { Thread.Sleep(1000); } WriteData(address, false); } /// /// 转盘旋转位置设定 /// /// private void PosionTurnOn(int posion) { int i = 0; string address = ""; foreach (var item in polymer.GoodsMaterialPosion) { if (item.Key.Contains((MaterialPosion)posion)) { WriteData(item.Value.SetPLCPosion,true); i = Convert.ToInt32(item.Value.posion); address = item.Value.GetPLCPosion; return; } } while(!morkTM.ReachPosions[i])//等待转盘到达信号 { Thread.Sleep(1000); } if(address != null) WriteData(address, false);//把信号置为0 } /// /// 把每一个物料的用量写入plc /// /// private void SetMatertialWeight(Dictionary materials) { foreach (var material in materials) { int value = Convert.ToInt32(material.Value*expand); WriteData(polymer.MaterialPosionList[(MaterialPosion)material.Key], value); Thread.Sleep(100); } } public override void ReadData() { //启用通道的地址1 GetStatus("M0.0", new Action((obj) => { if (obj is bool[] bools && bools.Length > 0 && bools.Length <= 8) { } })); //启用通道的地址2 GetStatus("M1.0", new Action((obj) => { if (obj is bool[] bools && bools.Length > 0 && bools.Length <= 8) { } })); //启用通道的地址3 GetStatus("M2.0", new Action((obj) => { if (obj is bool[] bools && bools.Length > 0 && bools.Length <= 8) { } })); //打开通道的地址1 GetStatus("M10.0", new Action((obj) => { if (obj is bool[] bools && bools.Length > 0 && bools.Length <= 8) { morkTM.UsePassageWay_1 = bools[0]; morkTM.UsePassageWay_2 = bools[1]; morkTM.UsePassageWay_3 = bools[2]; morkTM.UsePassageWay_4 = bools[3]; morkTM.UsePassageWay_5 = bools[4]; morkTM.UsePassageWay_6 = bools[5]; morkTM.UsePassageWay_7 = bools[6]; morkTM.UsePassageWay_8 = bools[7]; } })); //打开通道的地址2 GetStatus("M11.0", new Action((obj) => { if (obj is bool[] bools && bools.Length > 0 && bools.Length <= 8) { morkTM.UsePassageWay_9 = bools[0]; morkTM.UsePassageWay_10 = bools[1]; morkTM.UsePassageWay_11 = bools[2]; morkTM.UsePassageWay_12 = bools[3]; morkTM.UsePassageWay_13 = bools[4]; morkTM.UsePassageWay_14 = bools[5]; morkTM.UsePassageWay_15 = bools[6]; morkTM.UsePassageWay_16 = bools[7]; } })); //打开通道的地址3 GetStatus("M12.0", new Action((obj) => { if (obj is bool[] bools && bools.Length > 0 && bools.Length <= 8) { morkTM.UsePassageWay_17 = bools[0]; morkTM.UsePassageWay_18 = bools[1]; morkTM.UsePassageWay_19 = bools[2]; morkTM.UsePassageWay_20 = bools[3]; morkTM.UsePassageWay_21 = bools[4]; morkTM.UsePassageWay_22 = bools[5]; morkTM.UsePassageWay_23 = bools[6]; morkTM.UsePassageWay_24 = bools[7]; } })); //打开通道的地址4 GetStatus("M13.0", new Action((obj) => { if (obj is bool[] bools && bools.Length > 0 && bools.Length <= 4) { morkTM.UsePassageWay_25 = bools[0]; morkTM.UsePassageWay_26 = bools[1]; morkTM.UsePassageWay_27 = bools[2]; morkTM.UsePassageWay_28 = bools[3]; } })); GetStatus("M50.0",new Action((obj) => { if (obj is bool[] bools && bools.Length > 0 && bools.Length <= 28) { for (int i = 0; i < 28; i++) { if (morkTM.outMaterialCompletes.ContainsKey(i+1)) { morkTM.outMaterialCompletes[i+1] = bools[i]; } else { morkTM.outMaterialCompletes.Add(i+1, bools[i]); } } } })); //转盘移动的地址 GetStatus("M4.0", new Action((obj) => { if (obj is bool[] bools && bools.Length > 0 && bools.Length <= 7) { } })); //装盘移动到位的地址 GetStatus("M14.0", new Action((obj) => { if (obj is bool[] bools && bools.Length > 0 && bools.Length <= 7) { morkTM.ReachPosion_1 = bools[0]; morkTM.ReachPosion_2 = bools[1]; morkTM.ReachPosion_3 = bools[2]; morkTM.ReachPosion_4 = bools[3]; morkTM.ReachPosion_5 = bools[4]; morkTM.ReachPosion_6 = bools[5]; morkTM.ReachOutPosion_0 = bools[6]; for (int i = 0; i < 7; i++) { morkTM.ReachPosions[i] = bools[i]; } } })); } public override void ResetProgram() { morkTM = null; morkTM = new GVL_MorkTM(); } private void WriteData(string address, object value) { EventBus.EventBus.GetInstance().Publish(new WriteModel() { DeviceId = DeviceId, Address = address, Value = value }); } private void GetStatus(string key, Action action) { if (peripheralStatus.ContainsKey(key)) { if (peripheralStatus[key] != null) { action?.Invoke(peripheralStatus[key]); } } } public override void SimOrder() { ActionManage.GetInstance.Register(new Action((o) => { if (o is string goodName) { } }), ""); } public override void Stop() { throw new NotImplementedException(); } } }