From 771a0e7b6b1276087515bbe94de28358cb92963b Mon Sep 17 00:00:00 2001
From: alielie <alielie@git.com>
Date: 星期四, 04 六月 2026 10:32:27 +0800
Subject: [PATCH] 优化老化;

---
 HMI.AlermInfoManage/AlarmInfoManage.xaml.cs |  446 +++++--------------------------------------------------
 1 files changed, 41 insertions(+), 405 deletions(-)

diff --git a/HMI.AlermInfoManage/AlarmInfoManage.xaml.cs b/HMI.AlermInfoManage/AlarmInfoManage.xaml.cs
index 1cf4db1..02cb527 100644
--- a/HMI.AlermInfoManage/AlarmInfoManage.xaml.cs
+++ b/HMI.AlermInfoManage/AlarmInfoManage.xaml.cs
@@ -1,22 +1,15 @@
-锘縰sing ConnectionUtil;
-using HMI.AlarmInfoManage.Entity;
-using HMI.Base;
-using HMI.Core.Api;
-using HMI.Core.Setting;
+锘縰sing HMI.Base;
+using HMI.Core.Entity;
 using HMI.Core.Util;
 using HMI.UI.Util;
 using HMI.Util;
+using HMI.Util.Alarm;
 using Microsoft.Win32;
-using OPCConnection;
-using Sharp7;
-using SimensDriver;
 using System;
 using System.Collections.Generic;
 using System.Collections.ObjectModel;
 using System.Data;
 using System.IO;
-using System.Linq;
-using System.Timers;
 using System.Windows;
 using System.Windows.Controls;
 
@@ -27,118 +20,17 @@
     /// </summary>
     public partial class AlarmInfoManage : PluginControl
     {
-        private string blockPath;
-        private string markPath;
         private string alarmTextPath;
-
-        private string opcIP = "127.0.0.1";
-        private int opcPort = 6543;
-        private int localPort = 18888;
-        private volatile bool isClosing = false;
 
 
         private string dbPath;
-        private Simens plc = new Simens();
-
-        private bool isHaveAlarm;
-        private bool isNewAlarm;
 
         private SqliteHelper sqliteHelper;
 
-        private Timer MonitorTimer;
-        private Timer PcTimer;
-
-        /// <summary>
-        /// pc璀﹀憡鏁板瓧淇″彿
-        /// </summary>
-        private int isPcAlarmnumber = 0;
-
-        /// <summary>
-        /// 褰撳墠鎶ヨ淇℃伅
-        /// </summary>
-        private string currentInfo = null;
-
-        /// <summary>
-        /// 鎺ユ敹鎶ヨ鐨勯槦鍒�
-        /// </summary>
-        private MSMQHelper alarmQueue = new MSMQHelper("HMI_Alarm");
-        /// <summary>
-        /// 鏃ュ織鐢熸垚鍣�
-        /// </summary>
-        private MSMQHelper loggeralarm = new MSMQHelper("LTHMI_Logger");
-        /// <summary>
-        /// 鍒锋柊鎶ヨ鐨勯槦鍒�
-        /// </summary>
-        private MSMQHelper alarmRefershToMain = new MSMQHelper("LTHMI_Alarm_Rrfresh");
-
-        private SCADAApi lTSCADAApi = new OPCClient();
-
-        /// <summary>
-        /// 鎶ヨ
-        /// </summary>
-        private byte[] alarmCodeBytes;
-        /// <summary>
-        /// 鎶ヨ淇℃伅缂撳瓨
-        /// </summary>
-        private List<AlarmText> currentAlarmCachePlc = new List<AlarmText>();
-        /// <summary>
-        /// PLC鎶ヨ淇℃伅缂撳瓨
-        /// </summary>
-        private List<AlarmText> preAlarmCachePlc = new List<AlarmText>();
-        /// <summary>
-        /// 鎶ヨ淇℃伅缂撳瓨
-        /// </summary>
-        private List<AlarmText> currentAlarmCachePc = new List<AlarmText>();
-
-        /// <summary>
-        /// 浠g爜绠$悊
-        /// </summary>
-        private ObservableCollection<AlarmSetting> blockAlarmSettings = new ObservableCollection<AlarmSetting>();
-
-        public ObservableCollection<AlarmSetting> BlockAlarmSettings
-        {
-            get => blockAlarmSettings;
-            set
-            {
-                blockAlarmSettings = value;
-                RefreshBlockCache();
-            }
-        }
-        private Dictionary<string, Dictionary<string, string>> BlockMap = new Dictionary<string, Dictionary<string, string>>();
-        private Dictionary<string, string> BlockReadCache = new Dictionary<string, string>();
-
-        private void RefreshBlockCache()
-        {
-            BlockMap.Clear();
-            BlockReadCache.Clear();
-            if (BlockAlarmSettings == null || BlockAlarmSettings.Count <= 0)
-            {
-                return;
-            }
-            foreach (var setting in BlockAlarmSettings)
-            {
-                string key = $"{setting.DBAddress}.{setting.StartAddress}";
-                if (!BlockMap.ContainsKey(key))
-                {
-                    BlockMap[key] = new Dictionary<string, string>();
-                }
-                var codes = BlockMap[key];
-                codes[setting.AlarmCode] = setting.Description;
-            }
-        }
-
-        // <summary>
-        /// 鏍囧織浣嶆姤璀︾鐞�
-        /// </summary>
-        private ObservableCollection<AlarmSetting> markAlarmSetting = new ObservableCollection<AlarmSetting>();
-
-        /// <summary>
-        /// 浼犺嚦涓荤獥浣撶殑鎶ヨ淇℃伅
-        /// </summary>
-        private List<AlarmText> currentAlarmCachePlcToMain = new List<AlarmText>();
-
-        //private AlarmText alarm;
-        private SystemSetting systemSetting;
+        //private string opcIP = "127.0.0.1";
+        //private int opcPort = 6543;
+        //private int localPort = 18888;
+        //private SCADAApi lTSCADAApi = new OPCClient();
 
         private ObservableCollection<AlarmText> datasource = new ObservableCollection<AlarmText>();
 
@@ -157,8 +49,6 @@
             get { return alarmTexts; }
             set { Set(ref alarmTexts, value); }
         }
-
-        public Dictionary<string, AlarmText> textMap = new Dictionary<string, AlarmText>();
 
         private double size = 30;
 
@@ -186,7 +76,6 @@
         public AlarmInfoManage()
         {
             DataContext = this;
-            systemSetting = SystemSetting.Instance;
             InitializeComponent();
         }
 
@@ -195,9 +84,6 @@
             base.SetBaseFolder(folder);
             dbPath = Path.Combine(BaseFolder, "alarm.db");
             alarmTextPath = Path.Combine(BaseFolder, "AlarmText.data");
-
-            blockPath = Path.Combine(BaseFolder, "BlockSetting.xml");
-            markPath = Path.Combine(BaseFolder, "MarkSetting.xml");
         }
 
         public override void InitFinish()
@@ -214,40 +100,36 @@
                     alarminfo TEXT,
                     time DATETIME);");
 
-            LoadAlarmSetting();
             LoadAlarmText();
 
-            MonitorTimer = new Timer
-            {
-                Interval = 1000,
-            };
-            MonitorTimer.Elapsed += MonitorTimer_Elapsed;
-            if (!lTSCADAApi.Connect2(opcIP, opcPort, localPort, out string errMsg))
-            {
-                //MsgBoxHelper.Error(this, errMsg);
-            }
-            else
-            {
-                this.MonitorTimer.Start();
-            }
-
-            PcTimer = new Timer
-            {
-                Interval = 1000,
-                AutoReset = false
-            };
-            PcTimer.Elapsed += PcTimer_Elapsed;
-            PcTimer.Enabled = true;
+            AlarmUtil.OnAlarm += AlarmUtil_OnAlarm;
+            //if (!lTSCADAApi.Connect2(opcIP, opcPort, localPort, out string errMsg))
+            //{
+            //}
+            //else
+            //{
+            //    this.MonitorTimer.Start();
+            //}
         }
 
-        public override void DoClose(object sender, EventArgs e)
+        private void AlarmUtil_OnAlarm(object sender, List<AlarmText> e)
         {
-            isClosing = true;
-            PcTimer.Enabled = false;
-            PcTimer?.Dispose();
-
-            MonitorTimer.Enabled = false;
-            MonitorTimer?.Dispose();
+            foreach (AlarmText alarm in e)
+            {
+                var sql = string.Empty;
+                if (alarm.IsPLCAlarm)
+                {
+                    var currentInfo = alarm.AlarmInfo;
+                    sql = string.Format("INSERT INTO Alarm (source, alarmcode, alarminfo, time) VALUES('{0}','{1}','{2}','{3}');",
+                        alarm.Source, alarm.AlarmCode, currentInfo, alarm.Time);
+                }
+                else
+                {
+                    sql = string.Format("INSERT INTO Alarm (source, alarmcode, alarminfo, time) VALUES('{0}','{1}','{2}','{3}');",
+                    alarm.Source, alarm.AlarmCode, alarm.AlarmInfo, alarm.Time);
+                }
+                sqliteHelper.ExecuteSql(sql);
+            }
         }
 
         private void Button_Click(object sender, RoutedEventArgs e)
@@ -272,13 +154,11 @@
 
         private void OpenSettingWindow()
         {
-            AlarmSettingList listModal = new AlarmSettingList(blockPath, markPath, BlockAlarmSettings, markAlarmSetting, log)
+            AlarmSettingList listModal = new AlarmSettingList(log)
             {
                 Owner = Window.GetWindow(this)
             };
             listModal.ShowDialog();
-            BlockAlarmSettings = listModal.Datasource;
-            markAlarmSetting = listModal.MarkDatasource;
         }
 
         private void RefreshPage(int? page = null)
@@ -435,40 +315,19 @@
         {
             try
             {
-                textMap.Clear();
-                AlarmTexts = XmlHelper.DeserializeFromXml<ObservableCollection<AlarmText>>(alarmTextPath);
-                foreach (var text in AlarmTexts)
+                if (string.IsNullOrEmpty(alarmTextPath) || !File.Exists(alarmTextPath))
                 {
-                    textMap[text.AlarmCode] = text;
+                    AlarmTexts = new ObservableCollection<AlarmText>();
+                }
+                else
+                {
+                    AlarmTexts = XmlHelper.DeserializeFromXml<ObservableCollection<AlarmText>>(alarmTextPath);
                 }
             }
             catch (Exception ex)
             {
                 log.Error("LoadAlarmText Error.", ex);
                 AlarmTexts = new ObservableCollection<AlarmText>();
-            }
-        }
-
-        private void LoadAlarmSetting()
-        {
-            try
-            {
-                BlockAlarmSettings = XmlHelper.DeserializeFromXml<ObservableCollection<AlarmSetting>>(blockPath);
-            }
-            catch (Exception ex)
-            {
-                log.Error("LoadAlarmSetting Error.", ex);
-                BlockAlarmSettings = new ObservableCollection<AlarmSetting>();
-            }
-
-            try
-            {
-                markAlarmSetting = XmlHelper.DeserializeFromXml<ObservableCollection<AlarmSetting>>(markPath);
-            }
-            catch (Exception ex)
-            {
-                log.Error("LoadAlarmText Error.", ex);
-                markAlarmSetting = new ObservableCollection<AlarmSetting>();
             }
         }
 
@@ -485,77 +344,9 @@
             Loaded -= UserControl_Loaded;
         }
 
-        private void PcTimer_Elapsed(object sender, ElapsedEventArgs e)
+        public override void DoClose(object sender, EventArgs e)
         {
-            try
-            {
-                PcTimer.Enabled = false;
-                this.alarmQueue.Receive(100, out string MessageID, out string MsgStr);
-                if (MessageID != string.Empty)
-                {
-                    switch (MessageID)
-                    {
-                        case "Clear":
-                            this.currentAlarmCachePc.Clear();
-                            this.RefershAlarm();
-                            break;
-                        case "AlarmCode":
-                            AlarmText aalarm = AlarmTexts.FirstOrDefault(alarm => alarm.AlarmCode == MsgStr);
-                            if (aalarm == null) break;
-                            var data = new AlarmText
-                            {
-                                Source = "PC",
-                                AlarmCode = aalarm.AlarmCode,
-                                AlarmInfo = aalarm.MAlarmText_CN,
-                                Time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
-                            };
-                            //aalarm.Source = "PC";
-                            //aalarm.Time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
-                            if (this.currentAlarmCachePc.Find(alarm => alarm.AlarmCode == aalarm.AlarmCode) == null)
-                            {
-                                this.currentAlarmCachePc.Add(data);
-                                this.RefershAlarm();
-                                this.isPcAlarmnumber = this.currentAlarmCachePc.Count;
-                            }
-                            break;
-                        default:
-                            string[] arr = MsgStr.Split(',');
-                            string source = "PC";
-                            string info = string.Empty;
-                            if (arr.Length > 1)
-                            {
-                                source = arr[0];
-                                info = arr[1];
-                            }
-                            else
-                            {
-                                info = MsgStr;
-                            }
-                            AlarmText newAlarm = new AlarmText
-                            {
-                                Source = source,
-                                AlarmInfo = info,
-                                Time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
-                            };
-                            if (this.currentAlarmCachePc
-                                .Find(alarm => alarm.Source == newAlarm.Source && alarm.AlarmInfo == newAlarm.AlarmInfo) == null)
-                            {
-                                this.currentAlarmCachePc.Add(newAlarm);
-                                this.RefershAlarm();
-                                this.isPcAlarmnumber = this.currentAlarmCachePc.Count;
-                            }
-                            break;
-                    }
-                }
-            }
-            catch (Exception ex)
-            {
-                log.Error("PcTimer Execute Failed", ex);
-            }
-            finally
-            {
-                if (!isClosing) PcTimer.Enabled = true;
-            }
+            // AlarmUtil.OnAlarm -= AlarmUtil_OnAlarm;
         }
 
         //private void MonitorTimer_Elapsed_bak(object sender, ElapsedEventArgs e)
@@ -629,160 +420,5 @@
         //        this.MonitorTimer.Enabled = true;
         //    }
         //}
-
-        /// <summary>
-        /// PLC鎶ヨ璇诲彇
-        /// </summary>
-        /// <param name="sender"></param>
-        /// <param name="e"></param>
-        private void MonitorTimer_Elapsed(object sender, ElapsedEventArgs e)
-        {
-            try
-            {
-                this.MonitorTimer.Enabled = false;
-
-                if (!lTSCADAApi.IsConnected(out string errMsg))
-                {
-                    log.Warn($"OPCServer Connection Error. {errMsg}");
-                    if (!lTSCADAApi.Connect2(opcIP, opcPort, localPort, out errMsg))
-                    {
-                        log.Warn($"OPCServer Try ReConnection Error. {errMsg}");
-                        return;
-                    }
-                }
-
-                isHaveAlarm = false;
-                List<AlarmText> cache = new List<AlarmText>();
-                //foreach (var item in BlockAlarmSettings)
-                //{
-                //    var name = $"{item.DBAddress}.{item.StartAddress}";
-                //    if (lTSCADAApi.DoFctReadSingle(name, out string alarmCode, out errMsg) && alarmCode != "0")
-                //    {
-                //        isHaveAlarm = true;
-                //        if (!textMap.TryGetValue(alarmCode, out alarm)) {
-                //            continue;
-                //        }
-                //        var newAlarm = new AlarmText
-                //        {
-                //            Source = "PLC",
-                //            AlarmCode = alarm.AlarmCode,
-                //            ModuleName_CN = alarm.ModuleName_CN,
-                //            MAlarmText_CN = alarm.MAlarmText_CN,
-                //            Time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
-                //            AlarmInfo = $"{alarm.ModuleName_CN} {alarm.MAlarmText_CN}"
-                //        };
-                //        if (this.currentAlarmCachePlc
-                //                .Find(alarm => alarm.AlarmCode == newAlarm.AlarmCode) == null)
-                //        {
-                //            this.currentAlarmCachePlc.Add(newAlarm);
-                //        }
-                //    }
-                //}
-
-                foreach (var key in BlockMap.Keys)
-                {
-                    if (lTSCADAApi.DoFctReadSingle(key, out string alarmCode, out errMsg) && BlockMap[key].ContainsKey(alarmCode))
-                    {
-                        //if(BlockReadCache.ContainsKey(key) && alarmCode)
-                        isHaveAlarm = true;
-                        var text = BlockMap[key][alarmCode];
-                        var newAlarm = new AlarmText
-                        {
-                            PLC_DB_BLOCK = key,
-                            Source = "PLC",
-                            AlarmCode = alarmCode,
-                            ModuleName_CN = text,
-                            MAlarmText_CN = text,
-                            Time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
-                            AlarmInfo = $"{alarmCode} {text}"
-                        };
-                        if (this.currentAlarmCachePlc
-                                .Find(alarm => alarm.AlarmCode == newAlarm.AlarmCode && alarm.PLC_DB_BLOCK == newAlarm.PLC_DB_BLOCK) == null)
-                        {
-                            this.currentAlarmCachePlc.Add(newAlarm);
-                        }
-                    }
-                }
-
-                foreach (var item in markAlarmSetting)
-                {
-                    var name = $"{item.DBAddress}.{item.StartAddress}";
-                    if (lTSCADAApi.DoFctReadSingle(name, out string alarmCode, out errMsg) && alarmCode != "0")
-                    {
-                        isHaveAlarm = true;
-                        var newAlarm = new AlarmText
-                        {
-                            Source = "PLC",
-                            AlarmCode = alarmCode,
-                            Time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
-                            AlarmInfo = item.Description
-                        };
-                        if (this.currentAlarmCachePlc
-                                .Find(d => d.AlarmInfo == newAlarm.AlarmInfo) == null)
-                        {
-                            this.currentAlarmCachePlc.Add(newAlarm);
-                        }
-                    }
-                }
-                this.RefershAlarm();
-                this.isPcAlarmnumber = this.currentAlarmCachePc.Count;
-
-                if (!isHaveAlarm) currentAlarmCachePlc.Clear();
-            }
-            catch (Exception ex)
-            {
-                log.Error("Read OPCServer Data Error.", ex);
-            }
-            finally
-            {
-                if (!isClosing) this.MonitorTimer.Enabled = true;
-            }
-        }
-
-        /// <summary>
-        /// 鍒锋柊鎶ヨ鍧椾俊鎭�
-        /// </summary>
-        private void RefreshDBSetting()
-        {
-            List<byte> bytes = new List<byte>();
-            foreach (var item in BlockAlarmSettings)
-            {
-                //byte[] itemalarmCodeBytes = this.plc.Simens_S7_ReadBytes_DB("DB", item.DBAddress, item.StartAddress, item.Length);
-                //bytes.AddRange(itemalarmCodeBytes.ToList());
-            }
-            alarmCodeBytes = bytes.ToArray();
-        }
-
-
-
-        /// <summary>
-        /// 鍒锋柊鎶ヨ淇℃伅
-        /// </summary>
-        private void RefershAlarm()
-        {
-            this.alarmRefershToMain.Send("AlarmRefersh", string.Empty);
-
-            foreach (AlarmText alarm in this.currentAlarmCachePlc)
-            {
-                var currentInfo = alarm.AlarmInfo;
-                string sqladd = string.Format("INSERT INTO Alarm (source, alarmcode, alarminfo, time) VALUES('{0}','{1}','{2}','{3}');",
-                    alarm.Source, alarm.AlarmCode, currentInfo, alarm.Time);
-                if (AlarmShowHelper.ShowAlarm(currentInfo))
-                {
-                    sqliteHelper.ExecuteSql(sqladd);
-                }
-            }
-            //this.currentAlarmCachePlc.Clear();
-
-            foreach (AlarmText alarm in this.currentAlarmCachePc)
-            {
-                string sqladd2 = string.Format("INSERT INTO Alarm (source, alarmcode, alarminfo, time) VALUES('{0}','{1}','{2}','{3}');", alarm.Source, alarm.AlarmCode, alarm.AlarmInfo, alarm.Time);
-                if (AlarmShowHelper.ShowAlarm(alarm.AlarmInfo))
-                {
-                    sqliteHelper.ExecuteSql(sqladd2);
-                }
-            }
-            this.currentAlarmCachePc.Clear();
-        }
     }
 }

--
Gitblit v1.9.1