MainVM.cs
2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System.Collections.ObjectModel;
using System.Windows;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HHECS.DAQClient.Common;
using HHECS.DAQClient.Services;
namespace HHECS.DAQClient.ViewModel
{
internal partial class MainVM : ObservableObject
{
private readonly SystemLog _log = SystemLog.GetInstance();
private readonly CenterService _centerService;
[ObservableProperty]
private bool btnStartEnabled = true;
[ObservableProperty]
private bool btnStopEnabled = false;
[ObservableProperty]
private ObservableCollection<LogModel> logModels = new();
public MainVM(CenterService centerService)
{
_centerService = centerService;
InitialLogComponent();
}
[RelayCommand]
public void Start()
{
BtnStartEnabled = false;
BtnStopEnabled = true;
_centerService.Start();
}
[RelayCommand]
public void Stop()
{
BtnStartEnabled = true;
BtnStopEnabled = false;
_centerService.Stop();
}
private void InitialLogComponent()
{
Task.Run(() =>
{
Application.Current.Dispatcher.Invoke(async () =>
{
do
{
while (!_log.IsEmpty)
{
var log = _log.GetLog();
if (log != null)
{
var oldItem = LogModels.Where(x => x.Messages.Equals(log.Messages)).FirstOrDefault();
if (oldItem != null)
{
oldItem.CreateTime = log.CreateTime;
LogModels = new ObservableCollection<LogModel>(LogModels.OrderByDescending(x => x.CreateTime));
}
else
{
LogModels.Insert(0, new LogModel()
{
LogType = log.LogType,
Messages = log.Messages,
CreateTime = log.CreateTime
});
}
if (LogModels.Count > 50)
{
LogModels.Remove(LogModels.Last());
}
}
}
await Task.Delay(100);
} while (true);
});
});
}
}
}