using BPA.Message.Enum; using BPASmartClient.Device; using BPA.Helper; using BPASmartClient.Model; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using static BPA.Helper.EventBus; namespace BPASmartClient.MorkMOC { public class Control_MorkMOC : BaseDevice { public override global::BPA.Message.Enum.DeviceClientType DeviceType { get { return BPA.Message.Enum.DeviceClientType.TMC_MT; } } GVL_MorkMOC morkMoc = new GVL_MorkMOC(); PolymerBatching polymerBatching = new PolymerBatching(); //放大倍数 const int expand = 10; public override void DoMain() { ServerInit(); DataParse(); } private void ServerInit() { //物料信息 EventBus.GetInstance.Subscribe(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack) { if (@event == null) return; if (@event is MaterialDeliveryEvent material) { orderMaterialDelivery = material.orderMaterialDelivery; } }); //配方数据信息 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.GetInstance.Subscribe(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack) { if (@event == null) return; if (@event is DoOrderEvent order) { if (order.MorkOrder.GoodBatchings == null) return; OrderCount++; morkMoc.doOrderEvents.Add(order); OrderChange(order.MorkOrder.SuborderId, 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); } } morkMoc.morkOrderPushes.Enqueue(new OrderLocInfo() { GoodName = order.MorkOrder.GoodsName, SuborderId = order.MorkOrder.SuborderId, GoodPushes = OrderPushes }); } }); } /// /// 订单改变通知 /// /// /// private void OrderChange(string subid, ORDER_STATUS oRDER_STATUS) { var res = morkMoc.doOrderEvents.FirstOrDefault(p => p.MorkOrder.SuborderId == subid); string goodName = string.Empty; string SortNum = string.Empty; EventBus.GetInstance.Publish(new OrderStatusChangedEvent() { SortNum = res.MorkOrder.SortNum.ToString(), GoodName = res.MorkOrder.GoodsName, Status = oRDER_STATUS, SubOrderId = res.MorkOrder.SuborderId, deviceClientType = DeviceType }); if (oRDER_STATUS == ORDER_STATUS.COMPLETED_COOK) morkMoc.doOrderEvents.Remove(res); } public override void MainTask() { MakeProcess(); } /// /// 制作流程 /// private void MakeProcess() { if (morkMoc.morkOrderPushes.Count > 0) { if (morkMoc.morkOrderPushes.TryDequeue(out OrderLocInfo orderLoc)) { // 第一步:设置每个仓的出料量 SetMaterialsWeight(orderLoc.GoodPushes); DeviceProcessLogShow("设置出料量完成"); //第二步:循环出料 OutMaterials(orderLoc.GoodPushes); DeviceProcessLogShow($"{orderLoc.GoodName}制作完成"); } } } /// /// 设定出料重量 /// /// private void SetMaterialsWeight(Dictionary goodPushes) { foreach (var good in goodPushes) { WriteControl(polymerBatching.plcData[good.Key - 1].WeightVar, good.Value * expand); DeviceProcessLogShow($"{good.Key}号通道出料量:{good.Value}g"); Thread.Sleep(300); } } /// /// 循环出料 /// private void OutMaterials(Dictionary goodPushes) { foreach (var good in goodPushes) { WriteControl(polymerBatching.plcData[good.Key - 1].StartUpVar, true); Thread.Sleep(3000);//写入PLC开启通道后 PLC会先把完成信号置0,等待出料完成后置1 while (!morkMoc.OutMaterailCompelete)//接收到完成信号后退出循环 { Thread.Sleep(1000); //需要加入超时提示或处理 } DeviceProcessLogShow($"{good.Key}号通道出料完成"); } } public override void ReadData() { GetStatus("M10.0", new Action((obj) => { if (obj is bool[] bools && bools.Length > 0) { morkMoc.OutMaterailCompelete = bools[0]; } })); } public override void ResetProgram() { throw new NotImplementedException(); } public override void SimOrder() { ActionManage.GetInstance.Register(new Action((o) => { if (o == null) return; if (o is LocalRecipe recipe) { Dictionary OrderPushes = new Dictionary(); foreach (var item in recipe.localMaterails) { OrderPushes.Add(Convert.ToInt32(item.MaterialPosition), Convert.ToInt32(item.MaterialWeight)); } morkMoc.morkOrderPushes.Enqueue(new OrderLocInfo() { GoodName = recipe.RecipeName, SuborderId = Guid.NewGuid().ToString(), GoodPushes = OrderPushes }); } }), "MakeGoods"); } public override void Stop() { throw new NotImplementedException(); } private void GetStatus(string key, Action action) { if (peripheralStatus.ContainsKey(key)) { if (peripheralStatus[key] != null) { action?.Invoke(peripheralStatus[key]); } } } } }