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/Bro.Device.Common/DeviceBase/PLCBase.cs |   95 +++++++++++++----------------------------------
 1 files changed, 26 insertions(+), 69 deletions(-)

diff --git a/src/Bro.Device.Common/DeviceBase/PLCBase.cs b/src/Bro.Device.Common/DeviceBase/PLCBase.cs
index 10d42f1..9862511 100644
--- a/src/Bro.Device.Common/DeviceBase/PLCBase.cs
+++ b/src/Bro.Device.Common/DeviceBase/PLCBase.cs
@@ -58,8 +58,6 @@
                         if (newValues == null || newValues.Count == 0)
                             continue;
 
-                        //Stopwatch sw = new Stopwatch();
-                        //sw.Start();
                         if (oldValues.Count == newValues.Count)
                         {
                             var tempNew = new List<int>(newValues);
@@ -67,13 +65,6 @@
                             MonitorCheckAndInvoke(tempNew, tempOld);
                         }
                         oldValues = new List<int>(newValues);
-                        //sw.Stop();
-
-                        //if (sw.ElapsedMilliseconds > 10)
-                        //{
-                        //    LogAsync(DateTime.Now, $"杞鏃堕棿锛歿sw.ElapsedMilliseconds}", "");
-                        //    TimeRecordCSV(DateTime.Now, "杞鏃堕棿", (int)sw.ElapsedMilliseconds);
-                        //}
 
                         Thread.Sleep(PLCIConfig.ScanInterval);
                     }
@@ -88,42 +79,31 @@
         protected virtual void MonitorCheckAndInvoke(List<int> tempNew, List<int> tempOld)
         {
             #region PLC璀︽姤淇℃伅
-            //bool warningSignal = (m.TriggerIndex >= PLCIConfig.WarningStartIndex && m.TriggerIndex < PLCIConfig.WarningStartIndex + PLCIConfig.WarningLength);
+            PLCIConfig.WarningSetCollection.ForEach(w =>
+            {
+                if (w.WarningIndex_Word < 0 || w.WarningIndex_Word >= tempNew.Count)
+                    return;
 
-            //if (warningSignal)
-            //{
-            //    if (newValue != oldValue)
-            //    {
-            //        int warningIndex = m.TriggerIndex - PLCIConfig.WarningStartIndex;
-            //        for (int i = 0; i < 16; i++)
-            //        {
-            //            var ws = PLCIConfig.WarningSetCollection.FirstOrDefault(w => w.WaringIndex == (warningIndex * 16) + i);
+                if (w.WarningIndex_Bit < 0 || w.WarningIndex_Bit > 16)
+                    return;
 
-            //            if (ws != null)
-            //            {
-            //                int newAlarmValue = newValue >> i & 1;
-            //                int oldAlarmValue = oldValue >> i & 1;
+                bool isOn = ((tempNew[w.WarningIndex_Word] >> w.WarningIndex_Bit) & 1) == 1;
 
-            //                if (newAlarmValue != oldAlarmValue)
-            //                {
-            //                    OnMonitorAlarm?.BeginInvoke(ws, newAlarmValue == 1, null, null);
-
-            //                    //浠呬繚瀛樿鎶ヤ俊鎭紝涓嶄繚瀛樻彁绀轰俊鎭�
-            //                    //if (ws.WarningLvl == 0)
-            //                    //{
-            //                    //    SaveAlarm(Config.StationCode, ws, newValue);
-            //                    //}
-            //                }
-            //            }
-            //        }
-            //    }
-
-            //    return;
-            //}
+                if (w.CurrentStatus != isOn)
+                {
+                    w.CurrentStatus = isOn;
+                    OnMonitorAlarm?.BeginInvoke(DateTime.Now, this, w, null, null);
+                }
+            });
             #endregion
 
             PLCIConfig.MonitorSetCollection.ForEach(m =>
             {
+                if (m.TriggerIndex < 0 || m.TriggerIndex >= tempNew.Count)
+                {
+                    return;
+                }
+
                 int newValue = tempNew[m.TriggerIndex];
                 int oldValue = tempOld[m.TriggerIndex];
 
@@ -147,7 +127,7 @@
             });
         }
 
-        private void OnMethodInvoked(IAsyncResult ar)
+        protected virtual void OnMethodInvoked(IAsyncResult ar)
         {
             MonitorSet monitorSet = ar.AsyncState as MonitorSet;
 
@@ -186,11 +166,16 @@
 
                         if (repeatTime <= 0)
                         {
-                            new ProcessException("PLC鍙嶉鍐欏叆寮傚父", ex);
+                            new ProcessException("PLC鍙嶉鍐欏叆寮傚父", ExceptionLevel.Info, ex);
                         }
                     }
                 } while (repeatTime > 0);
             }
+        }
+
+        public virtual void ResetAlarm()
+        {
+            PLCIConfig.WarningSetCollection.ForEach(u => u.CurrentStatus = !u.TriggerValue);
         }
         #endregion
     }
@@ -212,36 +197,8 @@
     {
     }
 
-    public class PLCInitialConfigBase : InitialConfigBase
+    public class PLCInitialConfigBase : InitialMonitorConfigBase
     {
-        [Category("鐩戝惉璁剧疆")]
-        [Description("璀︽姤閰嶇疆鍒楄〃")]
-        [TypeConverter(typeof(CollectionCountConvert))]
-        [Editor(typeof(ComplexCollectionEditor<WarningSet>), typeof(UITypeEditor))]
-        public List<WarningSet> WarningSetCollection { get; set; } = new List<WarningSet>();
-
-        [Category("鐩戝惉璁剧疆")]
-        [Description("鐩戝惉鎿嶄綔閰嶇疆闆嗗悎")]
-        [TypeConverter(typeof(CollectionCountConvert))]
-        [Editor(typeof(ComplexCollectionEditor<MonitorSet>), typeof(UITypeEditor))]
-        public List<MonitorSet> MonitorSetCollection { get; set; } = new List<MonitorSet>();
-
-        [Category("鐩戝惉璁剧疆")]
-        [Description("鎵弿闂撮殧鏃堕棿锛屽崟浣嶏細ms")]
-        public int ScanInterval { get; set; } = 100;
-
-        [Category("鐩戝惉璁剧疆")]
-        [Description("瓒呮椂璁剧疆锛屽崟浣嶏細ms")]
-        public int Timeout { get; set; } = 500;
-
-        [Category("杈撳嚭璁剧疆")]
-        [Description("鏄惁鏃ュ織杈撳嚭")]
-        public bool IsEnabelLog { get; set; } = false;
-
-        [Category("杈撳嚭璁剧疆")]
-        [Description("杈撳嚭鏂囦欢璺緞")]
-        public string LogPath { get; set; } = @"D:\PLCLog.txt";
-
         #region 鍦板潃璁剧疆
         [Category("浜嬩欢鍦板潃璁剧疆")]
         [Description("浜嬩欢寮�濮嬪湴鍧�锛孭LC鐨勫疄闄呭瘎瀛樺櫒鍦板潃銆傚崄杩涘埗锛屼笉鍖呭惈鍔熻兘鐮併��")]

--
Gitblit v1.8.0