From f0c94cdfd82e8db9145e72d4fe27cd6952067873 Mon Sep 17 00:00:00 2001 From: fyf Date: Mon, 16 May 2022 13:29:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=91=8A=E8=AD=A6=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E6=9F=A5=E8=AF=A2=E7=95=8C=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BPASmartClient.Model/Book.cs | 43 ++++ .../BPASmartClient.ViewModel.csproj | 1 + .../LogOrAlarmViewModel.cs | 209 ++++++++++++++++-- BPASmartClient.ViewModel/LogViewModel.cs | 1 + BPASmartClient.ViewModel/MainViewModel.cs | 6 +- BPASmartClient/Control/LogOrAlarmView.xaml | 94 ++------ BPASmartClient/Control/LogOrAlarmView.xaml.cs | 10 + BPASmartClient/MainWindow.xaml | 4 +- BPASmartClient/MainWindow.xaml.cs | 5 +- 9 files changed, 281 insertions(+), 92 deletions(-) create mode 100644 BPASmartClient.Model/Book.cs diff --git a/BPASmartClient.Model/Book.cs b/BPASmartClient.Model/Book.cs new file mode 100644 index 00000000..e6e5bbda --- /dev/null +++ b/BPASmartClient.Model/Book.cs @@ -0,0 +1,43 @@ +using Microsoft.Toolkit.Mvvm.ComponentModel; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BPASmartClient.Model +{ + public class Book + { + public string Name { get; set; } + public string Tag { get; set; } + } + public class BookEx : ObservableObject + { + public Book BookN { get; private set; } + + private bool _isChecked=false; + + public bool IsChecked + { + get + { + return _isChecked; + } + set + { + if (_isChecked != value) + { + _isChecked = value; + + OnPropertyChanged("IsChecked"); + } + } + } + + public BookEx(Book book) + { + BookN = book; + } + } +} diff --git a/BPASmartClient.ViewModel/BPASmartClient.ViewModel.csproj b/BPASmartClient.ViewModel/BPASmartClient.ViewModel.csproj index 41b62ed4..b99aa4f8 100644 --- a/BPASmartClient.ViewModel/BPASmartClient.ViewModel.csproj +++ b/BPASmartClient.ViewModel/BPASmartClient.ViewModel.csproj @@ -12,6 +12,7 @@ + diff --git a/BPASmartClient.ViewModel/LogOrAlarmViewModel.cs b/BPASmartClient.ViewModel/LogOrAlarmViewModel.cs index bc48b94a..4e097ed9 100644 --- a/BPASmartClient.ViewModel/LogOrAlarmViewModel.cs +++ b/BPASmartClient.ViewModel/LogOrAlarmViewModel.cs @@ -1,11 +1,20 @@ -using BPASmartClient.Helper; +using BPASmartClient.CustomResource.UserControls; +using BPASmartClient.CustomResource.UserControls.MessageShow; +using BPASmartClient.Helper; +using BPASmartClient.Model; using Microsoft.Toolkit.Mvvm.ComponentModel; using Microsoft.Toolkit.Mvvm.Input; using System; using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.ComponentModel; +using System.Data; +using System.IO; using System.Linq; using System.Text; +using System.Text.RegularExpressions; using System.Threading.Tasks; +using System.Windows.Media; namespace BPASmartClient.ViewModel { @@ -24,25 +33,22 @@ namespace BPASmartClient.ViewModel #endregion #region 变量 - /// - /// 选中变量 - /// - private string _SelectCombox; - public string SelectCombox + string path = $"{System.AppDomain.CurrentDomain.BaseDirectory}LogDir\\HBL.LogDir2022_5_13.log"; + private string _SelectedText = ""; + public string SelectedText { get { - return _SelectCombox; + return _SelectedText; } set { - if (_SelectCombox == value) + if (_SelectedText == value) return; - _SelectCombox = value; - OnPropertyChanged("SelectCombox"); + _SelectedText = value; + OnPropertyChanged("SelectedText"); } } - /// /// 时间条件 /// @@ -61,6 +67,54 @@ namespace BPASmartClient.ViewModel OnPropertyChanged("DateTimeStr"); } } + public ObservableCollection SelectBookExs { set; get; } + private ObservableCollection _books; + public ObservableCollection BookExs + { + get + { + if (_books == null) + { + _books = new ObservableCollection(); + + _books.CollectionChanged += (sender, e) => + { + if (e.OldItems != null) + { + foreach (BookEx bookEx in e.OldItems) + { + bookEx.PropertyChanged -= ItemPropertyChanged; + } + } + + if (e.NewItems != null) + { + foreach (BookEx bookEx in e.NewItems) + { + bookEx.PropertyChanged += ItemPropertyChanged; + } + } + }; + } + + return _books; + } + } + + private ObservableCollection _LogModels; public ObservableCollection LogDataGrid + { + get + { + return _LogModels; + } + set + { + if (_LogModels == value) + return; + _LogModels = value; + OnPropertyChanged("LogDataGrid"); + } + } #endregion #region Command @@ -71,23 +125,148 @@ namespace BPASmartClient.ViewModel #region 函数 public void Init() { + LogDataGrid = new ObservableCollection(); DateTimeStr = DateTime.Now; + BookExs.Add(new BookEx(new Book() { Name = "一般日志条件", Tag = "Info" }) { IsChecked = true }); + BookExs.Add(new BookEx(new Book() { Name = "设备日志条件", Tag = "DeviceLog" })); + BookExs.Add(new BookEx(new Book() { Name = "错误日志条件", Tag = "Error" })); + BookExs.Add(new BookEx(new Book() { Name = "设备告警条件", Tag = "DeviceAlarm" })); + SelectedText = "一般日志条件"; + SelectBookExs = new ObservableCollection(); + SelectBookExs.Add(new BookEx(new Book() { Name = "一般日志条件", Tag = "Info" }) { IsChecked = true }); + + //查询 QueryCommand = new RelayCommand(() => { - + string sql = string.Empty; + if (SelectBookExs.Count <= 0) + { + NoticeDemoViewModel.OpenMsg(EnumPromptType.Info, MainViewModel.GetInstance().window, "提示", $"至少选中一个条件!"); + return; + } + SelectBookExs?.ToList().ForEach(par => + { + if (string.IsNullOrEmpty(sql)) + sql += $"LOGGER='{par.BookN.Tag}'"; + else + sql += $" OR LOGGER='{par.BookN.Tag}'"; + }); + //1.找到某天的文件 + string path = $"{System.AppDomain.CurrentDomain.BaseDirectory}LogDir\\HBL.LogDir{DateTimeStr.ToString("yyyy_M_d")}.log"; + if (File.Exists(path)) + { + LogDataGrid.Clear(); + //2.根据选中查询日志 + DataTable dataTable = ReadFile(path); + DataRow[] datas = dataTable.Select(sql); + if (datas.Count() <= 0) + { + NoticeDemoViewModel.OpenMsg(EnumPromptType.Info, MainViewModel.GetInstance().window, "提示", $"查询结果为空!"); + return; + } + foreach (DataRow item in datas) + { + LogDataGrid.Add(new LogModel + { + time = item["TIME"].ToString(), + type = item["LOGGER"].ToString(), + message = item["MESSAGE"].ToString(), + foreground = (item["LOGGER"].ToString() == "Error" || item["LOGGER"].ToString() == "DeviceAlarm") ? new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#ed0032")) : new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#21bb2e")) + }); + } + } + else + { + NoticeDemoViewModel.OpenMsg(EnumPromptType.Info, MainViewModel.GetInstance().window, "提示", $"文件不存在,{path}!"); + } }); - + //打开文件目录 OpenCommand = new RelayCommand(() => { System.Diagnostics.Process.Start("Explorer", "/select," + logHelper.GetLogConfigInstance().directRollfileAppender.File); - //logHelper.GetLogConfigInstance().OpenFile(logHelper.GetLogConfigInstance().directRollfileAppender.File); + logHelper.GetLogConfigInstance().OpenFile(logHelper.GetLogConfigInstance().directRollfileAppender.File); }); } - #endregion + /// + /// 查询告警或日志 + /// + /// + /// + public void QueryLogOrAlarm(DateTime time, string info) + { + } + /// + /// 读取文件 + /// + /// + public DataTable ReadFile(string path) + { + DataTable dt = new DataTable(); + dt.Columns.AddRange(new[] + { + new DataColumn("TIME"), + new DataColumn("LOGGER"), + new DataColumn("NAME"), + new DataColumn("PRIORITY"), + new DataColumn("MESSAGE"), + }); + //读取文件 + string s = ""; + using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) + { + using (StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default)) + { + s = sr.ReadToEnd(); + } + } + + //正则表达式匹配,注意RegexOptions.RightToLeft + var matches = Regex.Matches(s, @"(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) ([A-Z, ]{0,15}) ([A-Z,a-z]{0,20}) (\[\d{1}\]) (.*)", RegexOptions.RightToLeft); + foreach (Match m in matches) + { + var time = m.Groups[1].Value; + var logger = m.Groups[2].Value; + var name = m.Groups[3].Value; + var priority = m.Groups[4].Value; + var msg = m.Groups[5].Value; + var dr = dt.NewRow(); + dr.ItemArray = new[] { time, logger, name, priority, msg }; + dt.Rows.InsertAt(dr, 0); + } + return dt; + } + + /// + /// 选中改变 + /// + /// + /// + private void ItemPropertyChanged(object sender, PropertyChangedEventArgs e) + { + if (e.PropertyName == "IsChecked") + { + BookEx bookEx = sender as BookEx; + + if (bookEx != null) + { + IEnumerable bookExs = BookExs.Where(b => b.IsChecked == true); + + StringBuilder builder = new StringBuilder(); + SelectBookExs.Clear(); + foreach (BookEx item in bookExs) + { + builder.Append(item.BookN.Name + " "); + SelectBookExs.Add((BookEx)item); + } + SelectedText = builder == null ? string.Empty : builder.ToString(); + } + } + } + #endregion } diff --git a/BPASmartClient.ViewModel/LogViewModel.cs b/BPASmartClient.ViewModel/LogViewModel.cs index 17a11b4e..0088abaa 100644 --- a/BPASmartClient.ViewModel/LogViewModel.cs +++ b/BPASmartClient.ViewModel/LogViewModel.cs @@ -236,6 +236,7 @@ namespace BPASmartClient.ViewModel if (_type == "Error") foreground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#ed0032")); OnPropertyChanged("type"); + OnPropertyChanged("foreground"); } } public string message { get; set; } diff --git a/BPASmartClient.ViewModel/MainViewModel.cs b/BPASmartClient.ViewModel/MainViewModel.cs index 595671c8..60990cc7 100644 --- a/BPASmartClient.ViewModel/MainViewModel.cs +++ b/BPASmartClient.ViewModel/MainViewModel.cs @@ -42,9 +42,13 @@ namespace BPASmartClient.ViewModel /// 是否告警 /// public AlarmModel IsAlarm { get; set; } - + public System.Windows.Window window; public DispatcherTimer dispatcherTimer; + #region 单一 + private volatile static MainViewModel _Instance; + public static MainViewModel GetInstance() => _Instance ?? (_Instance = new MainViewModel()); + #endregion public MainViewModel() { diff --git a/BPASmartClient/Control/LogOrAlarmView.xaml b/BPASmartClient/Control/LogOrAlarmView.xaml index 809221bf..4a690f85 100644 --- a/BPASmartClient/Control/LogOrAlarmView.xaml +++ b/BPASmartClient/Control/LogOrAlarmView.xaml @@ -4,6 +4,7 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:BPASmartClient.Control" + xmlns:k="clr-namespace:BPASmartClient.Model;assembly=BPASmartClient.Model" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignHeight="450" d:DesignWidth="800" @@ -25,11 +26,14 @@ - - 一般日志 - 设备日志 - 错误日志 - 设备告警 + + + + + + + + @@ -38,95 +42,41 @@ - + - + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + - + - + - + + diff --git a/BPASmartClient/Control/LogOrAlarmView.xaml.cs b/BPASmartClient/Control/LogOrAlarmView.xaml.cs index fb589b30..cae385b9 100644 --- a/BPASmartClient/Control/LogOrAlarmView.xaml.cs +++ b/BPASmartClient/Control/LogOrAlarmView.xaml.cs @@ -26,5 +26,15 @@ namespace BPASmartClient.Control InitializeComponent(); this.DataContext = LogOrAlarmViewModel.GetInstance(); } + + private void CheckBox_Checked(object sender, RoutedEventArgs e) + { + + } + + private void CheckBox_Unchecked(object sender, RoutedEventArgs e) + { + + } } } diff --git a/BPASmartClient/MainWindow.xaml b/BPASmartClient/MainWindow.xaml index dc110869..79af5cea 100644 --- a/BPASmartClient/MainWindow.xaml +++ b/BPASmartClient/MainWindow.xaml @@ -25,9 +25,7 @@ - - - + diff --git a/BPASmartClient/MainWindow.xaml.cs b/BPASmartClient/MainWindow.xaml.cs index 3720f443..ac58be4b 100644 --- a/BPASmartClient/MainWindow.xaml.cs +++ b/BPASmartClient/MainWindow.xaml.cs @@ -10,6 +10,7 @@ using BPASmartClient.Message; using BPASmartClient.Model; using BPASmartClient.Model.冰淇淋.Enum; using BPASmartClient.Model.咖啡机.Enum; +using BPASmartClient.ViewModel; using System; using System.Collections.Generic; using System.Linq; @@ -35,7 +36,9 @@ namespace BPASmartClient { public MainWindow() { - InitializeComponent(); + InitializeComponent(); + MainViewModel.GetInstance().window = this; + this.DataContext = MainViewModel.GetInstance(); Initialize(); }