|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455 |
- 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;
- 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<object[]>((o) =>
- {
- if(o.Length > 0)
- {
- Dictionary<int, float> res = new Dictionary<int, float>();
- res.Add(Convert.ToInt32(o[0]), Convert.ToSingle(o[1]));
- SetMatertialWeight(res);
- Thread.Sleep(100);
- OpenUsePassageWay(Convert.ToInt32(o[0]));
- }
- }), "通道口出料");
-
- ActionManage.GetInstance.Register(new Action<object[]>((o) =>
- {
- if(o.Length > 0)
- {
- PosionTurnOnTest(Convert.ToInt32(o[0]));
- }
- }), "转盘转动");
-
- ActionManage.GetInstance.Register(new Action<object[]>((o) =>
- {
- if (o.Length > 0)
- {
- OpenPassway(Convert.ToInt32(o[0]));
- }
- }), "开启通道");
-
- ActionManage.GetInstance.Register(new Action<object[]>((o) =>
- {
- if (o.Length > 0)
- {
- CheckPassway(Convert.ToInt32(o[0]), Convert.ToInt32(o[1]));
- }
- }), "开始矫正");
- ActionManage.GetInstance.Register(new Action<object[]>((o) =>
- {
- if (o.Length > 0)
- {
- CheckMaterailWeight(Convert.ToInt32(o[0]), Convert.ToInt32(o[1]));
- }
- }), "矫正重量");
-
-
- ActionManage.GetInstance.Register(new Action<object>((o) =>
- {
- if (o != null && o is WritePar writePar) WriteData(writePar.Address, writePar.Value);
- }), "WriteVW");
-
- ActionManage.GetInstance.Register(new Action<object>((o) =>
- {
- if (o != null && o is WritePar writePar) WriteData(writePar.Address, writePar.Value);
- }), "WriteBools");
- morkTM.ReachPosions = new List<bool>() { morkTM.ReachPosion_1, morkTM.ReachPosion_2, morkTM.ReachPosion_3, morkTM.ReachPosion_4, morkTM.ReachPosion_5, morkTM.ReachPosion_6 , morkTM.ReachOutPosion_0, };
-
-
- DeviceProcessLogShow("设备初始化完成");
-
- }
- 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++;
- morkTM.doOrderEvents.Add(order);
- OrderChange(order.MorkOrder.SuborderId, ORDER_STATUS.WAIT);
- DeviceProcessLogShow($"接收到{OrderCount}次订单");
- Dictionary<int, float> OrderPushes = new Dictionary<int, float>();
- 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(string subid, ORDER_STATUS oRDER_STATUS)
- {
- var res = morkTM.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) morkTM.doOrderEvents.Remove(res);
- }
-
-
- public override void MainTask()
- {
- IsHealth = true;
- MakeTeaWithMilkProcess();
- }
-
- private void MakeTeaWithMilkProcess()
- {
- if (morkTM.morkOrderPushesTeaWithMilk.Count > 0)
- {
- if (morkTM.morkOrderPushesTeaWithMilk.TryDequeue(out OrderLocInfo orderLoc)) //&&原点位置是否有杯子)
- {
- DeviceProcessLogShow($"开始制作奶茶{orderLoc.GoodName}");
- morkTM.MakeCount = 0;
- SetMatertialWeight(orderLoc.GoodPushes);//设置物料出料量
- morkTM.RecipesPushes.Clear();
- morkTM.RecipesPushes = orderLoc.GoodPushes;
- MakeProcess(orderLoc.GoodName, morkTM.MakeCount, morkTM.RecipesPushes.Count);//制作进度
- OrderChange(orderLoc.SuborderId, ORDER_STATUS.COOKING);
- foreach (var item in morkTM.RecipesPushes)
- {
- morkTM.MakeCount++;
- PosionTurnOn(item.Key);//设定转盘位置并等待到位信号
- Thread.Sleep(1000);
- OpenUsePassageWay(item.Key);//打开通道并等待出料完成
- DeviceProcessLogShow($"奶茶{orderLoc.GoodName}:配料{item.Key}:添加量{item.Value}");
- MakeProcess(orderLoc.GoodName,morkTM.MakeCount,morkTM.RecipesPushes.Count);
- }
- TurnOutPosion();
- OrderChange(orderLoc.SuborderId, ORDER_STATUS.COMPLETED_COOK);
- MakeProcess(orderLoc.GoodName, 1, 1);
- DeviceProcessLogShow($"奶茶{orderLoc.GoodName}制作完成");
- }
- }
- }
- /// <summary>
- /// 打开出料通道
- /// </summary>
- private void OpenUsePassageWay(int pos)
- {
- string address = "";
- foreach (var item in polymer.OutPosionPLCs)//根据位置筛选物料plc点位
- {
- 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);
-
-
- }
-
- /// <summary>
- /// 转盘旋转位置设定
- /// </summary>
- /// <param name="posion"></param>
- 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-1])//等待转盘到达信号
- {
- Thread.Sleep(1000);
- }
- if(address != null) WriteData(address, false);//把信号置为0
-
-
- }
-
- #region 调试功能
- /// <summary>
- /// 转盘回原点
- /// </summary>
- private void TurnOutPosion()
- {
- WriteData("M4.6", true);
- while (!morkTM.ReachPosions[6])
- {
- Thread.Sleep(1000);
- }
- WriteData("M14.6", false);
- }
- /// <summary>
- /// 调试转盘
- /// </summary>
- /// <param name="posion"></param>
- private void PosionTurnOnTest(int posion)
- {
- if(posion==0) TurnOutPosion();
- else
- {
- string address = string.Empty;
- int i = 0;
- foreach (var item in polymer.TurnPosionPLCs)
- {
-
- if ((OutMaterialPosion)item.posion == (OutMaterialPosion)posion)
- {
- WriteData(item.SetPLCPosion, true);
- address = item.GetPLCPosion;
- i = Convert.ToInt32(item.posion);
- return ;
- }
- }
- while(morkTM.ReachPosions[posion - 1])
- {
- Thread.Sleep(1000);
- }
- WriteData(address,false);
- DeviceProcessLogShow($"转盘转动到位置{i}");
- }
- }
-
- /// <summary>
- /// 开启通道
- /// </summary>
- private void OpenPassway(int posion)
- {
- foreach(var item in polymer.OutPosionPLCs)
- {
- if((MaterialPosion)item.posion == (MaterialPosion)posion)
- {
- WriteData(item.SetPLCPosion,true);
- return ;
- }
- }
- }
- /// <summary>
- /// 开始校正
- /// </summary>
- /// <param name="passway"></param>
- /// <param name="time"></param>
- private void CheckPassway(int passway,int time)
- {
- WriteData(polymer.PasswayPosionList[(MaterialPosion)passway], Convert.ToInt32(time * expand));//写入出料时间
- Thread.Sleep(100);
- WriteData("M5.0", true);//开始校正
- }
-
- /// <summary>
- /// 确认校正重量
- /// </summary>
- private void CheckMaterailWeight(int passway,float weight)
- {
- WriteData(polymer.PasswayPosionList[(MaterialPosion)passway], weight*expand);
-
- }
-
- /// <summary>
- /// 奶茶制作进度
- /// </summary>
- private void MakeProcess(string goodName,int percent,int count)
- {
- int res = Convert.ToInt32(Math.Floor((double)percent / count)) * 100;
- ActionManage.GetInstance.Send("奶茶制作进度",new object[] {goodName, res });
- }
- #endregion
-
- /// <summary>
- /// 把每一个物料的用量写入plc
- /// </summary>
- /// <param name="materials"></param>
- private void SetMatertialWeight(Dictionary<int, float> materials)
- {
- foreach (var material in materials)
- {
- int value = Convert.ToInt32(material.Value*expand);
- WriteData(polymer.MaterialPosionList[(MaterialPosion)material.Key], value);
- DeviceProcessLogShow($"通道{material.Key}预设料{material.Value}g");
- Thread.Sleep(200);
- }
- }
-
-
- public override void ReadData()
- {
-
- //启用通道的地址1
- GetStatus("M0.0", new Action<object>((obj) =>
- {
- if (obj is bool[] bools && bools.Length > 0 && bools.Length <= 28)
- {
-
- }
- }));
-
-
-
- GetStatus("M10.0",new Action<object>((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<object>((obj) =>
- {
- if (obj is bool[] bools && bools.Length > 0 && bools.Length <= 7)
- {
-
- }
- }));
- //装盘移动到位的地址
- GetStatus("M14.0", new Action<object>((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<object> action)
- {
- if (peripheralStatus.ContainsKey(key))
- {
- if (peripheralStatus[key] != null)
- {
- action?.Invoke(peripheralStatus[key]);
- }
- }
- }
-
- public override void SimOrder()
- {
- ActionManage.GetInstance.Register(new Action<object>((o) =>
- {
- if (o is string goodName)
- {
-
- }
- }), "");
- }
-
- public override void Stop()
- {
- throw new NotImplementedException();
- }
- }
- }
|