|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485 |
- using BPA.Helper;
- using BPA.Message.Enum;
- using BPA.Models;
- using BPASmartClient.Device;
-
- using BPASmartClient.Modbus;
- using BPASmartClient.Model;
- using BPASmartClient.Model.PLC;
- using BPASmartClient.Model.;
- using BPASmartClient.Model.ƻ;
- using BPASmartClient.MorkMW.Model;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Net.Http.Json;
- using System.Reflection;
- using System.Threading;
- using System.Windows.Documents;
- using static BPA.Helper.EventBus;
-
- namespace BPASmartClient.MorkMW
- {
- public class Control_MorkMW : BaseDevice
- {
- int id = 0;
- ModbusTcp modbus;
- public override DeviceClientType DeviceType => DeviceClientType.MORKMW;
- GVL_MorkMW morkMW = new GVL_MorkMW();
- public override void DoMain()
- {
- Json<KeepDataBase>.Read();
- VaribleMonitorDis();
- ConnectKlpRobot("192.168.0.100", 8001);
- ServerInit();
- DataParse();
- TaskManage.GetInstance.StartLong(new Action(() => { VariableMonitor(); Thread.Sleep(5); }), "˱״̬߳", true);
- }
-
- private void VaribleMonitorDis()
- {
- PropertyInfo[] pros = morkMW.GetType().GetProperties();
- foreach (var item in pros)
- {
- var res = item.GetCustomAttribute<VariblesAttribute>();
- if (res != null)
- {
- GVL_MorkMW.varibleInfos.Add(new VaribleModel { Id = id + 1, VaribleName = res.VarName, RobotAddress = res.RobotAddress, ModbusAddress = res.ModbusTcpAddress, Notes = res.Notes });
- id++;
- }
- }
- }
-
- private void ServerInit()
- {
- //䷽Ϣ
- EventBus.GetInstance.Subscribe<MorkMWGoodsEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBack)
- {
- if (@event == null) return;
- if (@event is MorkMWGoodsEvent recipe)
- {
- listMorkMWGoods = recipe.morkMWPushMessage?.MorkMWGoods;
- DeviceProcessLogShow($"յƻ{listMorkMWGoods?.Count}Ʒ䷽");
- }
- });
- }
-
- public override void MainTask()
- {
- if (Json<KeepDataBase>.Data.IsVerify)
- IsHealth = modbus.Connected;
- else
- IsHealth = true;//ϱ
- ToGetWink();
- ToMixWink();
- ToPourWink();
- SignalDetect();
- }
- /// <summary>
- /// Ӿƹ
- /// </summary>
- public void ToGetWink()
- {
- if (!GVL_MorkMW.AllowLocalSimOrder)
- {
- if (morkMW.RobotIdle && orderLocInfos.Count > 0 && !morkMW.TaskLock && !morkMW.PourWinkComplete && morkMW.CupSignal && modbus.Connected)
- {
- DeviceProcessLogShow("ʼ");
- morkMW.TaskLock = true;
- if (orderLocInfos.TryDequeue(out BPASmartClient.MorkMW.Model.OrderLocInfo res))
- {
- morkMW.CurrentSuborderId = res.SuborderId;
- OrderChange(res.SuborderId, ORDER_STATUS.COOKING);
- foreach (var item in res.mixWink)
- {
- switch (item.Loc)
- {
- // case 7: ToMixWink(); break;
- case 1: ToSpecifiedLocTakeWink(item.Loc, item.Qty); break;
- case 2: ToSpecifiedLocTakeWink(item.Loc, item.Qty); break;
- case 3: ToSpecifiedLocTakeWink(item.Loc, item.Qty); break;
- case 4: ToSpecifiedLocTakeWink(item.Loc, item.Qty); break;
- case 5: ToSpecifiedLocTakeWink(item.Loc, item.Qty); break;
- case 6: ToSpecifiedLocTakeWink(item.Loc, item.Qty); break;
- }
- switch (item.Loc)
- {
- //case 7: while (!morkMW.MixWinkComplte) { Thread.Sleep(5); } break;
- case 1: while (!morkMW.TakeWinkOneComplete) { Thread.Sleep(5); } break;
- case 2: while (!morkMW.TakeWinkTwoComplete) { Thread.Sleep(5); } break;
- case 3: while (!morkMW.TakeWinkThreeComplete) { Thread.Sleep(5); } break;
- case 4: while (!morkMW.TakeWinkFourComplete) { Thread.Sleep(5); } break;
- case 5: while (!morkMW.TakeWinkFiveComplete) { Thread.Sleep(5); } break;
- case 6: while (!morkMW.TakeWinkSixComplete) { Thread.Sleep(5); } break;
-
- }
-
-
- }
-
- morkMW.AllowMixWink = true;//Ӿɣ
- // morkMW.AllowPourWink = true;//ɣ
- }
- }
- }
- else
- {
- if (morkMW.RobotIdle && simOrderLocInfos.Count > 0 && !morkMW.TaskLock && !morkMW.PourWinkComplete && morkMW.CupSignal)
- {
- DeviceProcessLogShow("ʼ");
- morkMW.TaskLock = true;
- if (simOrderLocInfos.TryDequeue(out SimOrderLocInfo res))
- {
- foreach (var item in res.mixWink)
- {
- switch (item.Loc)
- {
- // case 7: ToMixWink(); break;
- case 1: ToSpecifiedLocTakeWink(item.Loc, item.Time); break;
- case 2: ToSpecifiedLocTakeWink(item.Loc, item.Time); break;
- case 3: ToSpecifiedLocTakeWink(item.Loc, item.Time); break;
- case 4: ToSpecifiedLocTakeWink(item.Loc, item.Time); break;
- case 5: ToSpecifiedLocTakeWink(item.Loc, item.Time); break;
- case 6: ToSpecifiedLocTakeWink(item.Loc, item.Time); break;
-
- }
- switch (item.Loc)
- {
- //case 7: while (!morkMW.MixWinkComplte) { Thread.Sleep(5); } break;
- case 1: while (!morkMW.TakeWinkOneComplete) { Thread.Sleep(5); } break;
- case 2: while (!morkMW.TakeWinkTwoComplete) { Thread.Sleep(5); } break;
- case 3: while (!morkMW.TakeWinkThreeComplete) { Thread.Sleep(5); } break;
- case 4: while (!morkMW.TakeWinkFourComplete) { Thread.Sleep(5); } break;
- case 5: while (!morkMW.TakeWinkFiveComplete) { Thread.Sleep(5); } break;
- case 6: while (!morkMW.TakeWinkSixComplete) { Thread.Sleep(5); } break;
-
- }
-
-
- }
- DeviceProcessLogShow($"λýӾ");
- morkMW.AllowMixWink = true;//Ӿɣ
- // morkMW.AllowPourWink = true;//ɣ
- }
- }
-
- }
- }
- /// <summary>
- /// ƹ
- /// </summary>
- public void ToMixWink()
- {
- // modbus.WriteSingleRegister(0000, 7);//˵
-
- if (morkMW.AllowMixWink && modbus.Connected)
- {
- DeviceProcessLogShow("ʼ");
- modbus.WriteSingleRegister(0000, 7);//˵
- morkMW.AllowMixWink = false;
- }
- }
- /// <summary>
- /// ȥӦλýӾ
- /// </summary>
- /// <param name="loc"></param>
- /// <param name="time"></param>
- public void ToSpecifiedLocTakeWink(int loc, int time)
- {
- //ͽӾź
- switch (loc)
- {
- case 1: modbus.WriteSingleRegister(0000, 1); DeviceProcessLogShow($"ʼӡ{loc}λԭ"); break;
- case 2: modbus.WriteSingleRegister(0000, 2); DeviceProcessLogShow($"ʼӡ{loc}λԭ"); break;
- case 3: modbus.WriteSingleRegister(0000, 3); DeviceProcessLogShow($"ʼӡ{loc}λԭ"); break;
- case 4: modbus.WriteSingleRegister(0000, 4); DeviceProcessLogShow($"ʼӡ{loc}λԭ"); break;
- case 5: modbus.WriteSingleRegister(0000, 5); DeviceProcessLogShow($"ʼӡ{loc}λԭ"); break;
- case 6: modbus.WriteSingleRegister(0000, 6); DeviceProcessLogShow($"ʼӡ{loc}λԭ"); break;
-
- }
- //ȴ˵Ӿλ
- switch (loc)
- {
- case 1: while (!morkMW.ArriveWinkOneLoc) { Thread.Sleep(5); } DeviceProcessLogShow($"е۵{loc}ŽӾλ"); break;
- case 2: while (!morkMW.ArriveWinkTwoLoc) { Thread.Sleep(5); } DeviceProcessLogShow($"е۵{loc}ŽӾλ"); break;
- case 3: while (!morkMW.ArriveWinkThreeLoc) { Thread.Sleep(5); } DeviceProcessLogShow($"е۵{loc}ŽӾλ"); break;
- case 4: while (!morkMW.ArriveWinkFourLoc) { Thread.Sleep(5); } DeviceProcessLogShow($"е۵{loc}ŽӾλ"); break;
- case 5: while (!morkMW.ArriveWinkFiveLoc) { Thread.Sleep(5); } DeviceProcessLogShow($"е۵{loc}ŽӾλ"); break;
- case 6: while (!morkMW.ArriveWinkSixLoc) { Thread.Sleep(5); } DeviceProcessLogShow($"е۵{loc}ŽӾλ"); break;
-
- }
- Thread.Sleep(time * 1000);//ӾƵȴʱ
- //ͽӾź
- switch (loc)
- {
- case 1: modbus.WriteSingleCoil(4596, true); DeviceProcessLogShow($"{loc}λýӾ"); break;
- case 2: modbus.WriteSingleCoil(4597, true); DeviceProcessLogShow($"{loc}λýӾ"); break;
- case 3: modbus.WriteSingleCoil(4598, true); DeviceProcessLogShow($"{loc}λýӾ"); break;
- case 4: modbus.WriteSingleCoil(4599, true); DeviceProcessLogShow($"{loc}λýӾ"); break;
- case 5: modbus.WriteSingleCoil(4600, true); DeviceProcessLogShow($"{loc}λýӾ"); break;
- case 6: modbus.WriteSingleCoil(4601, true); DeviceProcessLogShow($"{loc}λýӾ"); break;
-
- }
-
- }
- /// <summary>
- /// ƹ
- /// </summary>
- public void ToPourWink()
- {
- //if (morkMW.AllowPourWink && morkMW.CupSignal)
- //{
- // morkMW.AllowPourWink = false;
- // modbus.WriteSingleRegister(0000, 8);//
-
- //}
- if (morkMW.CupSignal && RTrig.GetInstance("AllowOut").Start(morkMW.MixWinkComplte) && modbus.Connected)
- {
- DeviceProcessLogShow("ʼ");
- modbus.WriteSingleRegister(0000, 8);//
-
- }
-
- }
- /// <summary>
- /// źż
- /// </summary>
- public void SignalDetect()
- {
- if (RTrig.GetInstance("AllComplete").Start(morkMW.ProcessComplete))
- {
- OrderChange(morkMW.CurrentSuborderId, ORDER_STATUS.COMPLETED_COOK);
- morkMW.PourWinkComplete = true;
- DeviceProcessLogShow("ɣȡߵþƱ");
- }
- if (morkMW.PourWinkComplete)
- {
- if (TTrig.GetInstance("PoutWinkCom").Start(morkMW.CupSignal))
- {
- OrderChange(morkMW.CurrentSuborderId, ORDER_STATUS.COMPLETED_TAKE);
- DeviceProcessLogShow("ͻȡߵþƱ,ÿվƱԴ´ζ");
- }
- if (RTrig.GetInstance("PoutWinkCom").Start(morkMW.CupSignal))
- {
- DeviceProcessLogShow("վƱλִһ");
- morkMW.PourWinkComplete = false;
- morkMW.TaskLock = false;
- Thread.Sleep(2000);
- }
- }
- }
-
-
- /// <summary>
- /// ջ״̬
- /// </summary>
- public override void ReadData()
- {
- //GetStatus("M230.0", new Action<object>((obj) =>
- //{
-
- //}));
- if (modbus.Connected)
- {
- morkMW.ArriveWinkOneLoc = modbus.ReadCoils(4616);
- morkMW.ArriveWinkTwoLoc = modbus.ReadCoils(4617);
- morkMW.ArriveWinkThreeLoc = modbus.ReadCoils(4618);
- morkMW.ArriveWinkFourLoc = modbus.ReadCoils(4619);
- morkMW.ArriveWinkFiveLoc = modbus.ReadCoils(4620);
- morkMW.ArriveWinkSixLoc = modbus.ReadCoils(4621);
-
- morkMW.RobotIdle = modbus.ReadCoils(4201);
-
-
- morkMW.CupSignal = modbus.ReadInputs(0);
-
-
- morkMW.TakeWinkOneComplete = modbus.ReadCoils(4606);
- morkMW.TakeWinkTwoComplete = modbus.ReadCoils(4607);
- morkMW.TakeWinkThreeComplete = modbus.ReadCoils(4608);
- morkMW.TakeWinkFourComplete = modbus.ReadCoils(4609);
- morkMW.TakeWinkFiveComplete = modbus.ReadCoils(4610);
- morkMW.TakeWinkSixComplete = modbus.ReadCoils(4611);
- morkMW.MixWinkComplte = modbus.ReadCoils(4612);
- morkMW.ProcessComplete = modbus.ReadCoils(4613);
-
-
- }
- else
- {
- modbus.ModbusTcpConnect("192.168.0.100", 8001);
- }
- }
-
- public void VariableMonitor()
- {
- GVL_MorkMW.varibleInfos.FirstOrDefault(p => p.VaribleName == "ArriveWinkOneLoc").CurrentValue = morkMW.ArriveWinkOneLoc;
- GVL_MorkMW.varibleInfos.FirstOrDefault(p => p.VaribleName == "ArriveWinkTwoLoc").CurrentValue = morkMW.ArriveWinkTwoLoc;
- GVL_MorkMW.varibleInfos.FirstOrDefault(p => p.VaribleName == "ArriveWinkThreeLoc").CurrentValue = morkMW.ArriveWinkThreeLoc;
- GVL_MorkMW.varibleInfos.FirstOrDefault(p => p.VaribleName == "ArriveWinkFourLoc").CurrentValue = morkMW.ArriveWinkFourLoc;
- GVL_MorkMW.varibleInfos.FirstOrDefault(p => p.VaribleName == "ArriveWinkFiveLoc").CurrentValue = morkMW.ArriveWinkFiveLoc;
- GVL_MorkMW.varibleInfos.FirstOrDefault(p => p.VaribleName == "ArriveWinkSixLoc").CurrentValue = morkMW.ArriveWinkSixLoc;
-
- GVL_MorkMW.varibleInfos.FirstOrDefault(p => p.VaribleName == "RobotIdle").CurrentValue = morkMW.RobotIdle;
- GVL_MorkMW.varibleInfos.FirstOrDefault(p => p.VaribleName == "CupSignal").CurrentValue = morkMW.CupSignal;
-
- GVL_MorkMW.varibleInfos.FirstOrDefault(p => p.VaribleName == "TakeWinkOneComplete").CurrentValue = morkMW.TakeWinkOneComplete;
- GVL_MorkMW.varibleInfos.FirstOrDefault(p => p.VaribleName == "TakeWinkTwoComplete").CurrentValue = morkMW.TakeWinkTwoComplete;
- GVL_MorkMW.varibleInfos.FirstOrDefault(p => p.VaribleName == "TakeWinkThreeComplete").CurrentValue = morkMW.TakeWinkThreeComplete;
- GVL_MorkMW.varibleInfos.FirstOrDefault(p => p.VaribleName == "TakeWinkFourComplete").CurrentValue = morkMW.TakeWinkFourComplete;
- GVL_MorkMW.varibleInfos.FirstOrDefault(p => p.VaribleName == "TakeWinkFiveComplete").CurrentValue = morkMW.TakeWinkFiveComplete;
- GVL_MorkMW.varibleInfos.FirstOrDefault(p => p.VaribleName == "TakeWinkSixComplete").CurrentValue = morkMW.TakeWinkSixComplete;
-
- GVL_MorkMW.varibleInfos.FirstOrDefault(p => p.VaribleName == "MixWinkComplte").CurrentValue = morkMW.MixWinkComplte;
- GVL_MorkMW.varibleInfos.FirstOrDefault(p => p.VaribleName == "ProcessComplete").CurrentValue = morkMW.ProcessComplete;
-
- }
-
- public override void ResetProgram()
- {
- morkMW = null;
- morkMW = new GVL_MorkMW();
- }
-
-
- public override void SimOrder()
- {
- EventBus.GetInstance.Subscribe<MorkMWSimOrder>(0, delegate (IEvent @event, EventCallBackHandle callBackHandle)
- {
-
- if (@event != null && @event is MorkMWSimOrder order)
- {
- if (order.mixWink.Count > 0)
- {
-
- string guid = Guid.NewGuid().ToString();
- simOrderLocInfos.Enqueue(new SimOrderLocInfo { mixWink = order.mixWink, SuborderId = guid });
- DeviceProcessLogShow("յģⶩ");
- }
- }
-
- });
- }
-
- public override void Stop()
- {
-
- }
- private void GetStatus(string key, Action<object> action)
- {
- if (peripheralStatus.ContainsKey(key))
- {
- if (peripheralStatus[key] != null)
- {
- action?.Invoke(peripheralStatus[key]);
- }
- }
- }
- /// <summary>
- /// д
- /// </summary>
- /// <param name="address"></param>
- /// <param name="value"></param>
- private void WriteData(string address, object value)
- {
- EventBus.GetInstance.Publish(new WriteModel() { DeviceId = DeviceId, Address = address, Value = value });
- }
- /// <summary>
- /// ݽ
- /// </summary>
- public void DataParse()
- {
- EventBus.GetInstance.Subscribe<DoOrderEvent>(DeviceId, delegate (IEvent @event, EventCallBackHandle callBackHandle)
- {
-
- if (@event != null && @event is DoOrderEvent order)
- {
- morkMW.doOrderEvents.Add(order);
- if (listMorkMWGoods.Count == 0) return;
- if (morkMW.historySuborderId.FirstOrDefault(p => p == order.MorkOrder.SuborderId) != null) return;
- OrderCount++;
- var res = listMorkMWGoods?.FirstOrDefault(p => p.GoodsKey == order.MorkOrder.GoodsKey);
- if (res != null)
- {
- string guid = Guid.NewGuid().ToString();
- orderLocInfos.Enqueue(new BPASmartClient.MorkMW.Model.OrderLocInfo { mixWink = res.MorkMWBoms, SuborderId = order.MorkOrder.SuborderId });
- morkMW.historySuborderId.Add(order.MorkOrder.SuborderId);
- OrderChange(order.MorkOrder.SuborderId, ORDER_STATUS.WAIT);
- DeviceProcessLogShow($"յ{OrderCount}Сǰ{order.MorkOrder.GoodsName},š{order.MorkOrder.SuborderId}");
- }
- else
- {
- DeviceProcessLogShow($"δҵӦƷš{order.MorkOrder.SuborderId}");
- }
- }
-
- });
- }
- /// <summary>
- /// ״̬ϱ
- /// </summary>
- /// <param name="subid"></param>
- /// <param name="oRDER_STATUS"></param>
- private void OrderChange(string subid, ORDER_STATUS oRDER_STATUS)
- {
- try
- {
- var res = morkMW.doOrderEvents.FirstOrDefault(p => p.MorkOrder.SuborderId == subid);
- string goodName = string.Empty;
- string SortNum = string.Empty;
- if (res != null)
- {
- goodName = res.MorkOrder.GoodsName;
- SortNum = res.MorkOrder.SortNum.ToString();
- }
-
- EventBus.GetInstance.Publish(new OrderStatusChangedEvent() { SortNum = SortNum, GoodName = goodName, Status = oRDER_STATUS, SubOrderId = subid, deviceClientType = DeviceType });
- }
- catch (Exception)
- {
- DeviceProcessLogShow($"״̬ϱʧܣš{morkMW.CurrentSuborderId}");
- //throw;
- }
- }
- /// <summary>
- /// ӿջ
- /// </summary>
- /// <param name="ip"></param>
- /// <param name="port"></param>
- public void ConnectKlpRobot(string ip, int port)
- {
- modbus = new ModbusTcp();
- modbus.Show += new Action<string>((s) =>
- {
- if (s != null) DeviceProcessLogShow(s);
- });
- modbus.ShowEx += new Action<string>((s) =>
- {
- if ((s != null)) DeviceProcessLogShow(s);
- });
- modbus.ModbusTcpConnect(ip, port);
-
- }
-
- /// <summary>
- /// С
- /// </summary>
- public ConcurrentQueue<BPASmartClient.MorkMW.Model.OrderLocInfo> orderLocInfos = new ConcurrentQueue<BPASmartClient.MorkMW.Model.OrderLocInfo>();
-
- /// <summary>
- /// ģⶩ
- /// </summary>
- public ConcurrentQueue<SimOrderLocInfo> simOrderLocInfos = new ConcurrentQueue<SimOrderLocInfo>();
-
- /// <summary>
- /// ƻ䷽
- /// </summary>
- public List<MORKMWGoods> listMorkMWGoods = new List<MORKMWGoods>();
-
-
-
- }
- }
|