From 1c4426810c71eead57084be8a18ade8d314dd8c4 Mon Sep 17 00:00:00 2001 From: patrick <patrick.xu@broconcentric.com> Date: 星期二, 10 十二月 2019 14:24:31 +0800 Subject: [PATCH] 1. 重构项目 --- src/A032.Process/ProcessControl.cs | 289 +++++++++++++++++++++++++++++---------------------------- 1 files changed, 147 insertions(+), 142 deletions(-) diff --git a/src/A032.Process/ProcessControl.cs b/src/A032.Process/ProcessControl.cs index 7d26329..f823dc3 100644 --- a/src/A032.Process/ProcessControl.cs +++ b/src/A032.Process/ProcessControl.cs @@ -3,9 +3,7 @@ using Bro.Common.Helper; using Bro.Common.Interface; using Bro.Common.Model; -using Bro.Common.Model.Interface; using Bro.Common.PubSub; -using Bro.Common.UI; using Bro.Device.AuboRobot; using Bro.Device.OmronFins; using Bro.Device.SeerAGV; @@ -14,18 +12,14 @@ using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; -using System.Collections.Concurrent; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Collections.Specialized; -using System.Configuration; using System.Diagnostics; using System.Drawing; -using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Reflection; -using System.Runtime.ExceptionServices; using System.Threading; using System.Threading.Tasks; using static Bro.Common.Helper.EnumHelper; @@ -40,7 +34,7 @@ { #region AutoFac娉ㄥ唽 builder.RegisterInstance<ProcessConfig>(StationConfig as ProcessConfig); - builder.RegisterInstance<List<IDevice>>(GetDeviceList()); + builder.RegisterInstance<List<IDevice>>(DeviceList); builder.RegisterInstance<List<ProcessMethodAttribute>>(CollectProcessMethods()); if (isBuild) @@ -125,6 +119,8 @@ Dictionary<string, SeerAGVDriver> AGVDict = new Dictionary<string, SeerAGVDriver>(); Dictionary<string, CameraBase> CameraDict = new Dictionary<string, CameraBase>(); + public List<IDevice> DeviceList { get; set; } = new List<IDevice>(); + private ProcessConfig Config { get => StationConfig as ProcessConfig; } string _configBackupStr = ""; @@ -152,6 +148,11 @@ if (ProcessState == DeviceState.DSClose) return; + List<string> currentTaskIds = Config.AGVBindCollection.Select(u => u.CurrentTaskId).Where(s => !string.IsNullOrWhiteSpace(s)).ToList(); + TrayTaskCollection.RemoveAll(t => !currentTaskIds.Contains(t.TaskId)); + + _bindTaskDoneHandleDict.Values.ToList().ForEach(h => h.WaitOne()); + CloseDevice(PLCDict.Values.ToList()); CloseDevice(RobotDict.Values.ToList()); CloseDevice(AGVDict.Values.ToList()); @@ -168,6 +169,7 @@ return; InitialProcessMethods(); + TrayTaskCollection.OnItemChangedWithItemInfo = OnTaskListChanged; OpenDevices(RobotDict.Values.ToList()); OpenDevices(AGVDict.Values.ToList()); @@ -177,14 +179,15 @@ OpenDevices(PLCDict.Values.ToList()); + //鍔犺浇AGVUnit鐘舵�佷簨浠� + Config.AGVBindCollection.ForEach(b => + { + b.OnUnitStateChanged = OnUnitStateChanged; + + _bindTaskDoneHandleDict[b.Id] = new AutoResetEvent(true); + }); + ProcessState = DeviceState.DSOpen; - - QueryRobotIO(); - - //Task.Run(() => - //{ - // //PLCMonitor(); - //}); LogAsync(DateTime.Now, "Process Opened", ""); } @@ -263,6 +266,7 @@ }); } + //Dictionary<string, MethodInfo> InvokeMethodDict = new Dictionary<string, MethodInfo>(); public List<ProcessMethodAttribute> CollectProcessMethods() { List<ProcessMethodAttribute> resultList = new List<ProcessMethodAttribute>(); @@ -274,6 +278,7 @@ if (attr != null) { resultList.Add(attr); + //InvokeMethodDict[attr.MethodCode] = m; } }); @@ -287,20 +292,55 @@ StationConfig = LoadStationConfig(configPath); #region 涓埆閰嶇疆鐨勭壒鍒鐞� - #endregion - _warningRemains.CollectionChanged -= _warningRemains_CollectionChanged; - _warningRemains.CollectionChanged += _warningRemains_CollectionChanged; + WarningRemains.CollectionChanged -= _warningRemains_CollectionChanged; + WarningRemains.CollectionChanged += _warningRemains_CollectionChanged; + + #region 璁惧鍒濆鍖� + DeviceList = new List<IDevice>(); InitialPLCs(); - InitialRobots(); InitialAGVs(); + InitialRobots(); InitialCameras(); + InitialAGVBindUnit(); + //InitialMachineTrayNums(); + #endregion AutoFacRegister(); LogAsync(DateTime.Now, "Process Initialized", ""); + } + + //private void InitialMachineTrayNums() + //{ + // machineEmptyTrayDict = Config.PositionCollection.Where(u => u.Description == PathPositionDefinition.UnloadEmptyTray).ToDictionary(p => p.PositionNo, p => 0); + + // machineFullTrayDict = Config.PositionCollection.Where(u => u.Description == PathPositionDefinition.LoadFullTray).ToDictionary(p => p.PositionNo, p => 0); + //} + + private void InitialAGVBindUnit() + { + Config.AGVBindCollection.ForEach(u => + { + if (AGVDict.ContainsKey(u.AGVId)) + { + u.AGV = AGVDict[u.AGVId]; + } + + if (RobotDict.ContainsKey(u.RobotId)) + { + u.Robot = RobotDict[u.RobotId]; + } + + if (CameraDict.ContainsKey(u.CameraId)) + { + u.Camera = CameraDict[u.CameraId]; + } + + //u.OnMethodInvoke = OnBindUnitTaskInvoke; + }); } private void InitialCameras() @@ -310,6 +350,8 @@ CameraBase camera = CameraHelper.GetCameraInstance(c.DriverType); camera.InitialConfig = c; CameraDict[camera.InitialConfig.ID] = camera; + + DeviceList.Add(camera); }); } @@ -321,11 +363,13 @@ plc.InitialConfig = c; PLCDict[plc.InitialConfig.ID] = plc; - plc.OnMonitorAlarm -= Plc_OnMonitorAlarm; - plc.OnMonitorInvoke -= Plc_OnMonitorInvoke; + plc.OnMonitorAlarm -= OnMonitorAlarm; + plc.OnMonitorInvoke -= OnMonitorInvoke; - plc.OnMonitorAlarm += Plc_OnMonitorAlarm; - plc.OnMonitorInvoke += Plc_OnMonitorInvoke; + plc.OnMonitorAlarm += OnMonitorAlarm; + plc.OnMonitorInvoke += OnMonitorInvoke; + + DeviceList.Add(plc); }); } @@ -336,6 +380,18 @@ AuboRobotDriver robot = new AuboRobotDriver(); robot.InitialConfig = c; RobotDict[robot.InitialConfig.ID] = robot; + + robot.OnLog = OnDeviceLog; + + robot.OnMsgReceived = OnRobotMsgReceived; + + robot.OnMonitorAlarm -= OnMonitorAlarm; + robot.OnMonitorInvoke -= OnMonitorInvoke; + + robot.OnMonitorAlarm += OnMonitorAlarm; + robot.OnMonitorInvoke += OnMonitorInvoke; + + DeviceList.Add(robot); }); } @@ -346,6 +402,19 @@ SeerAGVDriver agv = new SeerAGVDriver(); agv.InitialConfig = c; AGVDict[agv.InitialConfig.ID] = agv; + + agv.OnMonitorAlarm -= OnMonitorAlarm; + agv.OnMonitorInvoke -= OnMonitorInvoke; + + agv.OnMonitorAlarm += OnMonitorAlarm; + agv.OnMonitorInvoke += OnMonitorInvoke; + + agv.OnLog = OnDeviceLog; + agv.OnAGVPositoinChanged = OnAGVPositionChanged; + agv.OnAGVTaskStatusChanged = OnAGVTaskStatusChanged; + agv.OnAGVBatteryLvlChanged = OnAGVBatteryLvlChanged; + + DeviceList.Add(agv); }); } @@ -400,7 +469,7 @@ ProcessConfig pConfig = config as ProcessConfig; if (pConfig == null) - throw new ProcessException("鐩墠鍙敮鎸丳rocessConfig绫诲瀷鐨勯潪绌哄唴瀹逛繚瀛�", null); + throw new ProcessException("鐩墠鍙敮鎸丳rocessConfig绫诲瀷鐨勯潪绌哄唴瀹逛繚瀛�"); string newConfig = JsonConvert.SerializeObject(pConfig, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Auto }); using (StreamWriter writer = new StreamWriter(CONFIG_PATH, false, System.Text.Encoding.UTF8)) @@ -447,7 +516,7 @@ /// </summary> protected Dictionary<string, HDevEngineTool> _halconToolDict = new Dictionary<string, HDevEngineTool>(); - private void InitialProcessMethods() + public virtual void InitialProcessMethods() { _processMethodDict = new Dictionary<string, MethodInfo>(); var methods = this.GetType().GetMethods().ToList(); @@ -457,73 +526,52 @@ if (attr != null) { _processMethodDict[attr.MethodCode] = m; - - #region 鍒濆鍖朒alconTool - //if (attr.DeviceType.EndsWith("Camera")) - //{ - // if (StationConfig.ProcessOpConfigDict.Keys.Contains(attr.MethodCode)) - // { - // var opConfig = StationConfig.ProcessOpConfigDict[attr.MethodCode] as HalconRelatedCameraOprerationConfigBase; - - // if (opConfig != null) - // { - // if (!string.IsNullOrWhiteSpace(opConfig.AlgorithemPath)) - // { - // string directoryPath = Path.GetDirectoryName(opConfig.AlgorithemPath); - // string fileName = Path.GetFileNameWithoutExtension(opConfig.AlgorithemPath); - - // HDevEngineTool tool = new HDevEngineTool(directoryPath); - // tool.LoadProcedure(fileName); - - // _halconToolDict[attr.MethodCode] = tool; - // } - // } - // } - //} - #endregion } }); - #region 鍒濆鍖朒alconTool - _halconToolDict = new Dictionary<string, HDevEngineTool>(); - Config.PLCConfigCollection.SelectMany(plcConfig => plcConfig.MonitorSetCollection).Select(ms => ms.OpConfig).ToList().ForEach(c => - { - IHalconToolPath toolPath = c as IHalconToolPath; - if (toolPath != null) - { - toolPath.GetHalconToolPathList().ForEach(path => - { - if (!string.IsNullOrWhiteSpace(path)) - { - string directoryPath = Path.GetDirectoryName(path); - string fileName = Path.GetFileNameWithoutExtension(path); - - HDevEngineTool tool = new HDevEngineTool(directoryPath); - tool.LoadProcedure(fileName); - - _halconToolDict[path] = tool; - } - }); - } - }); + #region 鍒濆鍖朒alconTool 鏍规嵁閰嶇疆鐨勬帴鍙g被鍨嬫潵閰嶇疆 + InitialHalconTool(); #endregion } - public List<IDevice> GetDeviceList() + private void InitialHalconTool() { - List<IDevice> list = new List<IDevice>(); + foreach (HDevEngineTool tool in _halconToolDict.Values) + { + tool?.Dispose(); + } - list.AddRange(PLCDict.Values); - list.AddRange(RobotDict.Values); - list.AddRange(AGVDict.Values); - list.AddRange(CameraDict.Values); + _halconToolDict = new Dictionary<string, HDevEngineTool>(); - return list; + Config.GetHalconToolPathList().ForEach(path => + { + if (!string.IsNullOrWhiteSpace(path) && !_halconToolDict.ContainsKey(path)) + { + string directoryPath = Path.GetDirectoryName(path); + string fileName = Path.GetFileNameWithoutExtension(path); + + HDevEngineTool tool = new HDevEngineTool(directoryPath); + tool.LoadProcedure(fileName); + + _halconToolDict[path] = tool; + } + }); } - #region PLC鐩戝惉 + //public List<IDevice> GetDeviceList() + //{ + // List<IDevice> list = new List<IDevice>(); - private void Plc_OnMonitorInvoke(DateTime dt, MonitorSet monitorSet) + // list.AddRange(PLCDict.Values); + // list.AddRange(RobotDict.Values); + // list.AddRange(AGVDict.Values); + // list.AddRange(CameraDict.Values); + + // return list; + //} + + #region IMonitor鐩戝惉 + private void OnMonitorInvoke(DateTime dt, IDevice device, MonitorSet monitorSet) { IOperationConfig config = monitorSet.OpConfig; string methodCode = monitorSet.MethodCode; @@ -535,7 +583,7 @@ try { //鏈塈OperationConfig鍙傛暟鐨勮皟鐢� - res = _processMethodDict[methodCode].Invoke(this, new object[] { config }); + res = _processMethodDict[methodCode].Invoke(this, new object[] { config, device }); reTryTimes = -1; } catch (Exception invokeEX) //娴佺▼鍔ㄤ綔寮傚父澶辫触 @@ -559,7 +607,7 @@ res = new ProcessResponse((int)ReturnValue.EXCEPTIONVALUE); } - var newEx = new ProcessException("鍑芥暟" + methodCode + "鎵ц寮傚父", ex); + var newEx = new ProcessException("鍑芥暟" + methodCode + "鎵ц寮傚父", ExceptionLevel.Warning, ex); } else { @@ -597,64 +645,24 @@ monitorSet.Response.ResultValue = (int)ReturnValue.OKVALUE; } #endregion - - //sw.Stop(); - //LogAsync(DateTime.Now, methodCode + " 璋冪敤鑰楁椂: " + sw.ElapsedMilliseconds.ToString() + "ms", ""); - //TimeRecordCSV(DateTime.Now, methodCode + "璋冪敤", (int)sw.ElapsedMilliseconds); - //sw.Start(); - - #region 鍘熸湁PLC鍐欏叆缁撴灉鎿嶄綔锛岀幇杞埌寮傛璋冪敤鍚庡洖璋冨幓鎵ц - //ProcessResponse resValues = res as ProcessResponse; - - //if (resValues.ResultValue == (int)PLCReplyValue.IGNORE) - //{ - // return; - //} - - //if (monitorSet.ReplyDataAddress != -1 && resValues.DataList.Count > 0) - //{ - // PLC_ITEM item = new PLC_ITEM(); - // item.OP_TYPE = 2; - // item.ITEM_LENGTH = resValues.DataList.Count; - // item.ADDRESS = monitorSet.ReplyDataAddress.ToString(); - // item.ITEM_VALUE = String.Join(",", resValues.DataList); - // PLC.WriteItem(item, false); - //} - - //if (monitorSet.NoticeAddress != -1) - //{ - // //娴嬭瘯妯″紡涓嬪缁堝弽棣圤K淇″彿 - // if (StationConfig.IsDemoMode && resValues.ResultValue <= 0) - // { - // resValues.ResultValue = (int)ReturnValue.OKVALUE; - // } - - // int repeatTime = 5; - - // //LogAsync(DateTime.Now, methodCode + "寮�濮嬪弽棣�", ""); - // do - // { - // try - // { - // PLC.WriteSingleAddress(set.NoticeAddress, resValues.ResultValue, false); - // repeatTime = 0; - // } - // catch (Exception ex) - // { - // repeatTime--; - - // if (repeatTime <= 0) - // { - // new ProcessException("PLC鍙嶉鍐欏叆寮傚父", ex); - // } - // } - // } while (repeatTime > 0); - //} - #endregion } - private void Plc_OnMonitorAlarm(DateTime dt, WarningSet warning, bool isAlarmRaised) + private void OnMonitorAlarm(DateTime dt, IDevice device, WarningSet warning) { + var bind = Config.AGVBindCollection.FirstOrDefault(u => u.CameraId == device.Id || u.RobotId == device.Id || u.AGVId == device.Id); + + if (bind == null) + { + throw new ProcessException($"{device.Name}鐨勫紓甯镐俊鎭湭鑳借幏鍙栫浉鍏崇粦瀹氳澶囦俊鎭�"); + } + + if (warning.CurrentStatus == warning.TriggerValue) + { + bind.WarningMsg.Add(warning.WarningDescription); + bind.UnitState = AGVState.Warning; + } + + //鏆傛椂涓嶆墽琛岃嚜鍔ㄥ浣嶏紝闇�瑕佹墜宸ュ浣� } //List<int> _monitorList = new List<int>(); @@ -1099,7 +1107,7 @@ protected Dictionary<string, Queue<string>> CameraBitmapDict = new Dictionary<string, Queue<string>>(); //protected Dictionary<string, Bitmap> CameraBitmapDict = new Dictionary<string, Bitmap>(); - protected HObject CollectHImage(CameraBase camera, IOperationConfig opConfig, string cameraId, string methodCode) + protected HObject CollectHImage(CameraBase camera, IOperationConfig opConfig, string methodCode) { HObject hImage = null; @@ -1114,7 +1122,7 @@ else { //MessageBox.Show("鏈兘鑾峰彇绂荤嚎鍥剧墖锛�"); - throw new ProcessException("鏈兘鑾峰彇绂荤嚎鍥剧墖锛�", null); + throw new ProcessException("鏈兘鑾峰彇绂荤嚎鍥剧墖锛�"); } } } @@ -1131,9 +1139,6 @@ camera.UploadOperationConfig(opConfig); camera.Snapshot(opConfig, out hImage); - - //SaveTempImage(cameraName, camera.ImageFilePath); - //SaveTempImage(camera, cameraId); if (cameraConifg.DelayAfter > 0) { @@ -1381,7 +1386,7 @@ #endregion #region 鎶ヨ鍜孌ownTime - public ObservableCollection<string> _warningRemains = new ObservableCollection<string>(); + public ObservableCollection<string> WarningRemains { get; set; } = new ObservableCollection<string>(); bool warningRemainFlag = false; bool WarningRemainFlag -- Gitblit v1.8.0