From b0a4c47dd74bc41c5df3bab6ddd8de7bcc6a52b0 Mon Sep 17 00:00:00 2001
From: patrick <patrick.xu@broconcentric.com>
Date: 星期五, 06 十二月 2019 18:35:42 +0800
Subject: [PATCH] 1. 重新整理项目,按照A034模式,将设备异步操作修改为类同步操作。使用任务队列来存储和分配任务。

---
 src/A032.Process/ProcessControl.cs |  182 ++++++++++++---------------------------------
 1 files changed, 49 insertions(+), 133 deletions(-)

diff --git a/src/A032.Process/ProcessControl.cs b/src/A032.Process/ProcessControl.cs
index 08cba1d..1c21d00 100644
--- a/src/A032.Process/ProcessControl.cs
+++ b/src/A032.Process/ProcessControl.cs
@@ -5,7 +5,6 @@
 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 +13,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;
@@ -152,6 +147,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 +168,7 @@
                 return;
 
             InitialProcessMethods();
+            TrayTaskCollection.OnItemChangedWithItemInfo = OnTaskListChanged;
 
             OpenDevices(RobotDict.Values.ToList());
             OpenDevices(AGVDict.Values.ToList());
@@ -177,14 +178,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", "");
         }
@@ -291,27 +293,27 @@
             #region 涓埆閰嶇疆鐨勭壒鍒鐞�
             #endregion
 
-            _warningRemains.CollectionChanged -= _warningRemains_CollectionChanged;
-            _warningRemains.CollectionChanged += _warningRemains_CollectionChanged;
+            WarningRemains.CollectionChanged -= _warningRemains_CollectionChanged;
+            WarningRemains.CollectionChanged += _warningRemains_CollectionChanged;
 
             InitialPLCs();
             InitialAGVs();
             InitialRobots();
             InitialCameras();
             InitialAGVBindUnit();
-            InitialMachineTrayNums();
+            //InitialMachineTrayNums();
 
             AutoFacRegister();
 
             LogAsync(DateTime.Now, "Process Initialized", "");
         }
 
-        private void InitialMachineTrayNums()
-        {
-            machineEmptyTrayDict = Config.PositionCollection.Where(u => u.Description == PathPositionDefinition.UnloadEmptyTray).ToDictionary(p => p.PositionNo, p => 0);
+        //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);
-        }
+        //    machineFullTrayDict = Config.PositionCollection.Where(u => u.Description == PathPositionDefinition.LoadFullTray).ToDictionary(p => p.PositionNo, p => 0);
+        //}
 
         private void InitialAGVBindUnit()
         {
@@ -390,6 +392,9 @@
                 agv.InitialConfig = c;
                 AGVDict[agv.InitialConfig.ID] = agv;
 
+                agv.OnMonitorAlarm -= OnMonitorAlarm;
+                agv.OnMonitorInvoke -= OnMonitorInvoke;
+
                 agv.OnLog = OnDeviceLog;
                 agv.OnAGVPositoinChanged = OnAGVPositionChanged;
                 agv.OnAGVTaskStatusChanged = OnAGVTaskStatusChanged;
@@ -448,7 +453,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))
@@ -505,71 +510,36 @@
                 if (attr != null)
                 {
                     _processMethodDict[attr.MethodCode] = m;
-
-                    #region 鍒濆鍖朒alconTool 鏍规嵁processMethod鐨勭壒鎬ф潵閰嶇疆
-                    //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 鏍规嵁閰嶇疆鐨勬帴鍙g被鍨嬫潵閰嶇疆
-            _halconToolDict = new Dictionary<string, HDevEngineTool>();
-            Config.PLCConfigCollection.SelectMany(plcConfig => plcConfig.MonitorSetCollection).Select(ms => ms.OpConfig).ToList().ForEach(c =>
-            {
-                InitialHalconTool(c as IHalconToolPath);
-            });
-
-            Config.VisionConfigCollection.ForEach(c =>
-            {
-                InitialHalconTool(c as IHalconToolPath);
-            });
-
-            Config.ProcessOpConfigDict.Values.ToList().ForEach(c =>
-            {
-                InitialHalconTool(c as IHalconToolPath);
-            });
+            InitialHalconTool();
             #endregion
         }
 
-        private void InitialHalconTool(IHalconToolPath toolPath)
+        private void InitialHalconTool()
         {
-            //IHalconToolPath toolPath = c as IHalconToolPath;
-            if (toolPath != null)
+            foreach (HDevEngineTool tool in _halconToolDict.Values)
             {
-                toolPath.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;
-                    }
-                });
+                tool?.Dispose();
             }
+
+            _halconToolDict = new Dictionary<string, HDevEngineTool>();
+
+            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;
+                }
+            });
         }
 
         public List<IDevice> GetDeviceList()
@@ -621,7 +591,7 @@
                                     res = new ProcessResponse((int)ReturnValue.EXCEPTIONVALUE);
                                 }
 
-                                var newEx = new ProcessException("鍑芥暟" + methodCode + "鎵ц寮傚父", ex);
+                                var newEx = new ProcessException("鍑芥暟" + methodCode + "鎵ц寮傚父", ExceptionLevel.Warning, ex);
                             }
                             else
                             {
@@ -658,60 +628,6 @@
             {
                 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
         }
 
@@ -1176,7 +1092,7 @@
                     else
                     {
                         //MessageBox.Show("鏈兘鑾峰彇绂荤嚎鍥剧墖锛�");
-                        throw new ProcessException("鏈兘鑾峰彇绂荤嚎鍥剧墖锛�", null);
+                        throw new ProcessException("鏈兘鑾峰彇绂荤嚎鍥剧墖锛�");
                     }
                 }
             }
@@ -1440,7 +1356,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