From 6bcafc823e836ecd5790f87178a906333699f868 Mon Sep 17 00:00:00 2001
From: alielie <alielie@git.com>
Date: 星期四, 10 四月 2025 10:46:47 +0800
Subject: [PATCH] 优化插件结构;

---
 HMI.LogViewer/LogViewer.xaml.cs |  141 +++++++++++++++++++++++++++++------------------
 1 files changed, 87 insertions(+), 54 deletions(-)

diff --git a/HMI.LogViewer/LogViewer.xaml.cs b/HMI.LogViewer/LogViewer.xaml.cs
index 272d886..508aecf 100644
--- a/HMI.LogViewer/LogViewer.xaml.cs
+++ b/HMI.LogViewer/LogViewer.xaml.cs
@@ -5,6 +5,7 @@
 using System.Collections.Generic;
 using System.Data;
 using System.IO;
+using System.Linq;
 using System.Timers;
 using System.Windows;
 using System.Windows.Controls;
@@ -16,8 +17,6 @@
     /// </summary>
     public partial class LogViewer : PluginControl
     {
-        private string logDir;
-        private string todayLogFile;
 
         private bool autoRfh;
 
@@ -36,42 +35,34 @@
 
         public LogViewer()
         {
-            string logDir = Path.Combine(PathHelper.AppFolder, "Log");
-            todayLogFile = Path.Combine(logDir, $"log{DateTime.Now.ToString("yyyy-MM-dd")}.db");
-
-            if (!Directory.Exists(logDir))
-            {
-                Directory.CreateDirectory(logDir);
-            }
-            this.logDir = logDir;
             DataContext = this;
             InitializeComponent();
         }
 
         public override void InitFinish()
         {
-            base.InitFinish();
-            string baseFile = Path.Combine(logDir, "log.db");
+            //base.InitFinish();
+            //string baseFile = Path.Combine(logDir, "log.db");
 
-            try
-            {
-                if (!File.Exists(baseFile))
-                {
-                    SqliteHelper.CreateDatabase(baseFile);
-                }
-                SqliteHelper sqliteHelper = new SqliteHelper(baseFile);
-                sqliteHelper.ExecuteSql(@"CREATE TABLE IF NOT EXISTS log (
-                    id INTEGER PRIMARY KEY AUTOINCREMENT,
-                    type TEXT NOT NULL,
-                    text TEXT NOT NULL,
-                    source TEXT,
-                    time TIME
-                    );");
-            }
-            catch (Exception ex)
-            {
-                log.Error("Create Log File Failed.", ex);
-            }
+            //try
+            //{
+            //    if (!File.Exists(baseFile))
+            //    {
+            //        SqliteHelper.CreateDatabase(baseFile);
+            //    }
+            //    SqliteHelper sqliteHelper = new SqliteHelper(baseFile);
+            //    sqliteHelper.ExecuteSql(@"CREATE TABLE IF NOT EXISTS log (
+            //        id INTEGER PRIMARY KEY AUTOINCREMENT,
+            //        level TEXT NOT NULL,
+            //        text TEXT NOT NULL,
+            //        source TEXT,
+            //        time TIME
+            //        );");
+            //}
+            //catch (Exception ex)
+            //{
+            //    log.Error("Create Log File Failed.", ex);
+            //}
         }
 
 
@@ -113,16 +104,32 @@
         {
             try
             {
+                if (startTimeInput.SelectedDateTime == null && endTimeInput.SelectedDateTime == null)
+                {
+                    ToastHelper.Error(this.lang("log.timeCantBeNull"));
+                    return;
+                }
+
+                if (endTimeInput.SelectedDateTime != null && startTimeInput.SelectedDateTime != null &&
+                endTimeInput.SelectedDateTime < startTimeInput.SelectedDateTime)
+                {
+                    ToastHelper.Error(this.lang("log.timeRangeError"));
+                    return;
+                }
+
                 queryBtn.IsEnabled = false;
                 autoRfhInput.IsEnabled = false;
                 Filtrate();
-                queryBtn.IsEnabled = true;
-                autoRfhInput.IsEnabled = true;
             }
             catch (Exception ex)
             {
                 log.Error("Log Query Failed.", ex);
                 this.Error($"鏌ヨ澶辫触锛亄ex.Message}");
+            }
+            finally
+            {
+                queryBtn.IsEnabled = true;
+                autoRfhInput.IsEnabled = true;
             }
         }
 
@@ -139,16 +146,43 @@
         {
             string str = LoadSql();
             DataTable dt = new DataTable();
-            var files = Directory.GetFiles(logDir, "*.db");
+            var files = GetOldDbFiles(PathHelper.LogViewerDir, new DateTime?[] { startTimeInput.SelectedDateTime,
+                endTimeInput.SelectedDateTime });
             foreach (string file in files)
             {
                 SqliteHelper sql = new SqliteHelper(file);
                 dt.Merge(sql.GetDataTable(str));
                 sql.Close();
             }
-
             dt.DefaultView.Sort = "time Desc";
             table.ItemsSource = dt.DefaultView;
+        }
+
+        public static string[] GetOldDbFiles(string folderPath, DateTime?[] range)
+        {
+            if (!Directory.Exists(folderPath))
+            {
+                return new string[0];
+            }
+            // 鑾峰彇鎵�鏈�.db鏂囦欢骞剁瓫閫夊嚭7澶╁墠鏇存柊鐨勬枃浠�
+            var oldFiles = Directory.GetFiles(folderPath, "*.db");
+            try
+            {
+                oldFiles = oldFiles.Where(file =>
+                {
+                    var lastWriteTime = File.GetLastWriteTime(file);
+                    bool flag = true;
+                    if (range[0] != null) flag &= lastWriteTime >= range[0];
+                    if (range[1] != null) flag &= lastWriteTime <= range[1];
+                    return flag;
+                }).ToArray();
+
+                return oldFiles;
+            }
+            catch (Exception ex)
+            {
+                return new string[0];
+            }
         }
 
         /// <summary>
@@ -157,12 +191,11 @@
         /// <returns> str鏁版嵁搴撶殑璇彞 </returns>
         private string LoadSql()
         {
-            string sql = " select type,time,source,text from log";
-
+            string sql = " select level,time,source,text from log";
             List<string> datas = new List<string>();
             if (!string.IsNullOrEmpty(levelInput.Text))
             {
-                datas.Add($"type='{levelInput.Text}'");
+                datas.Add($"level='{levelInput.Text}'");
             }
             if (!string.IsNullOrEmpty(startTimeInput.Text))
             {
@@ -189,22 +222,22 @@
         /// </summary>
         private void Refresh100items()
         {
-            try
-            {
-                if (!File.Exists(todayLogFile)) return;
-                string str = "select type,time,source,text from log order by time desc limit 100";
-                SqliteHelper sql = new SqliteHelper(todayLogFile);
-                var dt = sql.GetDataTable(str);
-                this.Dispatcher.BeginInvoke((Action)delegate ()
-                {
-                    table.ItemsSource = dt.DefaultView;
-                });
-            }
-            catch (Exception ex)
-            {
-                log.Error("Log Auto Refresh Failed.", ex);
-                this.Error($"鑷姩鍒锋柊澶辫触锛亄ex.Message}");
-            }
+            //try
+            //{
+            //    if (!File.Exists(todayLogFile)) return;
+            //    string str = "select type,time,source,text from log order by time desc limit 100";
+            //    SqliteHelper sql = new SqliteHelper(todayLogFile);
+            //    var dt = sql.GetDataTable(str);
+            //    this.Dispatcher.BeginInvoke((Action)delegate ()
+            //    {
+            //        table.ItemsSource = dt.DefaultView;
+            //    });
+            //}
+            //catch (Exception ex)
+            //{
+            //    log.Error("Log Auto Refresh Failed.", ex);
+            //    this.Error($"鑷姩鍒锋柊澶辫触锛亄ex.Message}");
+            //}
         }
     }
 }

--
Gitblit v1.9.1