|
- using BPA.Message.Enum;
- using BPASmartClient.Device;
- using BPASmartClient.EventBus;
- using BPASmartClient.Helper;
- using BPASmartClient.Model;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using static BPASmartClient.EventBus.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.EventBus.GetInstance().Subscribe<MaterialDeliveryEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
- {
- if (@event == null) return;
- if (@event is MaterialDeliveryEvent material)
- {
- orderMaterialDelivery = material.orderMaterialDelivery;
- }
- });
-
- //配方数据信息
- EventBus.EventBus.GetInstance().Subscribe<RecipeBomEvent>(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<DoOrderEvent>(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<int, int> OrderPushes = new Dictionary<int, int>();
- 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
- });
- }
- });
- }
-
- /// <summary>
- /// 订单改变通知
- /// </summary>
- /// <param name="subid"></param>
- /// <param name="oRDER_STATUS"></param>
- 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.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();
- }
- /// <summary>
- /// 制作流程
- /// </summary>
- 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}制作完成");
- }
- }
-
- }
- /// <summary>
- /// 设定出料重量
- /// </summary>
- /// <param name="goodPushes"></param>
- private void SetMaterialsWeight(Dictionary<int,int> 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);
- }
- }
-
- /// <summary>
- /// 循环出料
- /// </summary>
- private void OutMaterials(Dictionary<int, int> 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<object>((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<object>((o) =>
- {
- if (o == null) return;
- if(o is LocalRecipe recipe)
- {
- Dictionary<int, int> OrderPushes = new Dictionary<int, int>();
- 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<object> action)
- {
- if (peripheralStatus.ContainsKey(key))
- {
- if (peripheralStatus[key] != null)
- {
- action?.Invoke(peripheralStatus[key]);
- }
- }
- }
- }
- }
|