Blame view

HHECS.DAQClient/ViewModel/MainVM.cs 3.22 KB
唐召明 authored
1
2
using System.Collections.ObjectModel;
using System.Windows;
唐召明 authored
3
using System.Windows.Controls;
唐召明 authored
4
5
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
唐召明 authored
6
7
using HHECS.DAQClient.Common;
using HHECS.DAQClient.Services;
唐召明 authored
8
9
using HHECS.DAQClient.View.CommunicationsView;
using HHECS.DAQClient.View.EquipmentView;
唐召明 authored
10
唐召明 authored
11
namespace HHECS.DAQClient.ViewModel
唐召明 authored
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
{
    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();
唐召明 authored
27
28
29
30
31
32
33
34
35
        [ObservableProperty]
        private Page communicationPage = new CommunicationPage();

        [ObservableProperty]
        private Page equipmentPage = new EquipmentPage();

        [ObservableProperty]
        private Page equipmentDataQueuePage = new EquipmentDataQueuePage();
唐召明 authored
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
        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)
                            {
唐召明 authored
72
73
                                var oldItem = LogModels.Where(x => x.Messages.Equals(log.Messages)).FirstOrDefault();
                                if (oldItem != null)
唐召明 authored
74
                                {
唐召明 authored
75
76
77
78
79
80
81
82
83
84
85
86
87
                                    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)
唐召明 authored
88
89
90
91
92
93
94
95
96
97
98
99
                                {
                                    LogModels.Remove(LogModels.Last());
                                }
                            }
                        }
                        await Task.Delay(100);
                    } while (true);
                });
            });
        }
    }
}