diff --git a/BPASmartClient.ViewModel/LogOrAlarmViewModel.cs b/BPASmartClient.ViewModel/LogOrAlarmViewModel.cs index 7cff7fb1..6ed1ed0a 100644 --- a/BPASmartClient.ViewModel/LogOrAlarmViewModel.cs +++ b/BPASmartClient.ViewModel/LogOrAlarmViewModel.cs @@ -132,7 +132,7 @@ namespace BPASmartClient.ViewModel BookExs.Add(new BookEx(new Book() { Name = "错误日志条件", Tag = "Error" }) { IsChecked = true }); BookExs.Add(new BookEx(new Book() { Name = "设备告警条件", Tag = "DeviceAlarm" }) { IsChecked = true }); SelectBookExs = new ObservableCollection(); - ItemPropertyChanged(new BookEx(new Book() { Name = "一般日志条件", Tag = "Info" }) { IsChecked = true },new PropertyChangedEventArgs("IsChecked")); + ItemPropertyChanged(new BookEx(new Book()) { IsChecked = true },new PropertyChangedEventArgs("IsChecked")); //查询 QueryCommand = new RelayCommand(() => { diff --git a/BPASmartClient.ViewModel/LogViewModel.cs b/BPASmartClient.ViewModel/LogViewModel.cs index 5c5e2bb8..09088d00 100644 --- a/BPASmartClient.ViewModel/LogViewModel.cs +++ b/BPASmartClient.ViewModel/LogViewModel.cs @@ -1,12 +1,14 @@ using BPASmartClient.Helper; using BPASmartClient.IoT; using BPASmartClient.Message; +using BPASmartClient.Model; using Microsoft.Toolkit.Mvvm.ComponentModel; using Microsoft.Toolkit.Mvvm.Input; using Microsoft.Win32; using System; using System.Collections.Generic; using System.Collections.ObjectModel; +using System.ComponentModel; using System.Diagnostics; using System.IO; using System.Linq; @@ -26,6 +28,7 @@ namespace BPASmartClient.ViewModel #region 变量 public DispatcherTimer dispatcherTimer; public string ClientId = System.Configuration.ConfigurationManager.AppSettings["ClientId"].ToString(); + public ObservableCollection LogDataGridData { get; set; } private ObservableCollection _LogModels;public ObservableCollection LogDataGrid { get @@ -68,6 +71,54 @@ namespace BPASmartClient.ViewModel OnPropertyChanged("TimedClear"); } } + private string _SelectedText = ""; + public string SelectedText + { + get + { + return _SelectedText; + } + set + { + if (_SelectedText == value) + return; + _SelectedText = value; + OnPropertyChanged("SelectedText"); + } + } + 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; + } + } #endregion #region 单一 @@ -85,14 +136,23 @@ namespace BPASmartClient.ViewModel /// public void Init() { - if (LogDataGrid == null) LogDataGrid = new ObservableCollection(); logHelper.Fun_InitLog(System.AppDomain.CurrentDomain.BaseDirectory); + if (LogDataGrid == null) LogDataGrid = new ObservableCollection(); + if (LogDataGridData == null) LogDataGridData = new ObservableCollection(); + BookExs.Add(new BookEx(new Book() { Name = "一般日志", Tag = "Info" }) { IsChecked = true }); + BookExs.Add(new BookEx(new Book() { Name = "设备日志", Tag = "DeviceLog" }) { IsChecked = true }); + BookExs.Add(new BookEx(new Book() { Name = "错误日志", Tag = "Error" }) { IsChecked = true }); + BookExs.Add(new BookEx(new Book() { Name = "设备告警", Tag = "DeviceAlarm" }) { IsChecked = true }); + SelectBookExs = new ObservableCollection(); + ItemPropertyChanged(new BookEx(new Book()) { IsChecked = true }, new PropertyChangedEventArgs("IsChecked")); //一般日志 MessageLog.GetInstance.InfoNotify = new Action((s) => { System.Windows.Application.Current?.Dispatcher.Invoke((Action)(() => { - LogDataGrid.Insert(0, new LogModel { message = s, type = "Info" }); + LogModel logModel = new LogModel { message = s, type = "Info" }; + LogDataGridData.Insert(0, logModel); + AddLog(logModel); logHelper.GetLogConfigInstance().WriteLog(LogLevel.INFO, s); })); }); @@ -101,7 +161,9 @@ namespace BPASmartClient.ViewModel { System.Windows.Application.Current?.Dispatcher.Invoke((Action)(() => { - LogDataGrid.Insert(0, new LogModel { message = s, type = "DeviceLog" }); + LogModel logModel = new LogModel { message = s, type = "DeviceLog" }; + LogDataGridData.Insert(0, logModel); + AddLog(logModel); logHelper.GetLogConfigInstance().WriteLog(LogLevel.DEBUG, s); })); }); @@ -110,7 +172,9 @@ namespace BPASmartClient.ViewModel { System.Windows.Application.Current?.Dispatcher.Invoke((Action)(() => { - LogDataGrid.Insert(0, new LogModel { message = id, type = "DeviceAlarm" }); + LogModel logModel = new LogModel { message = id, type = "DeviceAlarm" }; + LogDataGridData.Insert(0, logModel); + AddLog(logModel); logHelper.GetLogConfigInstance().WriteLog(LogLevel.WARN, id); })); }); @@ -119,7 +183,9 @@ namespace BPASmartClient.ViewModel { System.Windows.Application.Current?.Dispatcher.Invoke((Action)(() => { - LogDataGrid.Insert(0, new LogModel { message = s, type = "Error" }); + LogModel logModel = new LogModel { message = s, type = "Error" }; + LogDataGridData.Insert(0, logModel); + AddLog(logModel); logHelper.GetLogConfigInstance().WriteLog(LogLevel.ERROR, s); DataVClient.GetInstance().HttpAddLog(new LogTable { @@ -154,9 +220,11 @@ namespace BPASmartClient.ViewModel { System.Windows.Application.Current?.Dispatcher.Invoke((Action)(() => { - if (LogDataGrid.Count > 100) + if (LogDataGridData.Count > 200) { - LogDataGrid.RemoveAt(LogDataGrid.Count - 1); + LogModel logModel= LogDataGridData.Last(); + DeleteLog(logModel); + LogDataGridData.Remove(logModel); } })); @@ -164,6 +232,43 @@ namespace BPASmartClient.ViewModel dispatcherTimer.Interval = TimeSpan.FromSeconds(10); dispatcherTimer.Start(); } + /// + /// 增加日志 + /// + public void AddLog(LogModel logModel) + { + BookEx book= SelectBookExs?.ToList().Find(par => par.IsChecked && par.BookN.Tag == logModel.type); + if (book != null) + { + LogDataGrid.Insert(0, logModel); + } + } + /// + /// 刷新日志 + /// + /// + public void RefreshLog() + { + LogDataGrid.Clear(); + LogDataGridData?.ToList().ForEach(b => { + + BookEx book = SelectBookExs?.ToList().Find(par => par.IsChecked && par.BookN.Tag == b.type); + if (book != null) + { + LogDataGrid.Add(b); + } + }); + + } + /// + /// 删除日志 + /// + public void DeleteLog(LogModel logModel) + { + LogModel log= LogDataGrid?.ToList().Find(par => par == logModel); + if(log!=null) LogDataGrid.Remove(log); + } + /// /// 导出数据 /// @@ -210,6 +315,33 @@ namespace BPASmartClient.ViewModel System.Windows.MessageBox.Show("无数据!"); } + /// + /// 选中改变 + /// + /// + /// + 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(); + RefreshLog(); + } + } + } #endregion #region Command diff --git a/BPASmartClient/Control/IOTView.xaml b/BPASmartClient/Control/IOTView.xaml new file mode 100644 index 00000000..a5c067cc --- /dev/null +++ b/BPASmartClient/Control/IOTView.xaml @@ -0,0 +1,16 @@ + + + + + diff --git a/BPASmartClient/Control/IOTView.xaml.cs b/BPASmartClient/Control/IOTView.xaml.cs new file mode 100644 index 00000000..96e3e884 --- /dev/null +++ b/BPASmartClient/Control/IOTView.xaml.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; + +namespace BPASmartClient.Control +{ + /// + /// IOTView.xaml 的交互逻辑 + /// + public partial class IOTView : UserControl + { + public IOTView() + { + InitializeComponent(); + webView.Source = new Uri("http://iot.black-pa.com"); + } + } +} diff --git a/BPASmartClient/Control/LogView.xaml b/BPASmartClient/Control/LogView.xaml index dfa3fc63..28b1a173 100644 --- a/BPASmartClient/Control/LogView.xaml +++ b/BPASmartClient/Control/LogView.xaml @@ -3,6 +3,7 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" + xmlns:k="clr-namespace:BPASmartClient.Model;assembly=BPASmartClient.Model" xmlns:vm="clr-namespace:BPASmartClient.ViewModel;assembly=BPASmartClient.ViewModel" xmlns:local="clr-namespace:BPASmartClient.Control" mc:Ignorable="d" @@ -25,8 +26,13 @@ - + + + + + + + @@ -54,7 +60,7 @@ - + diff --git a/BPASmartClient/MainWindow.xaml b/BPASmartClient/MainWindow.xaml index 52bd20cc..91442a31 100644 --- a/BPASmartClient/MainWindow.xaml +++ b/BPASmartClient/MainWindow.xaml @@ -102,17 +102,11 @@ Header="设备监视" Tag="DeviceMonitorView" /> - - + Tag="IOTView" />