Blame view

HHECS.DAQClient/ViewModel/EquipmentPropVM/EquipmentPropMonitorVM.cs 5.61 KB
唐召明 authored
1
2
3
4
5
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HandyControl.Data;
using HHECS.BllModel;
using HHECS.DAQClient.DataAccess;
唐召明 authored
6
using HHECS.DAQClient.Services;
唐召明 authored
7
8
9
10
11
using HHECS.EquipmentModel;
using LinqKit;
using System.Collections.ObjectModel;
using System.Linq.Expressions;
using System.Windows;
唐召明 authored
12
using System.Windows.Threading;
唐召明 authored
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
using MessageBox = HandyControl.Controls.MessageBox;

namespace HHECS.DAQClient.ViewModel.EquipmentPropVM
{
    public partial class EquipmentPropMonitorVM : ObservableObject
    {
        [ObservableProperty]
        private string equipmentPropCode;

        [ObservableProperty]
        private string equipmentPropName;

        [ObservableProperty]
        private Dictionary<string, EquipmentPropType?> equipmentPropTypes = new Dictionary<string, EquipmentPropType?>();

        [ObservableProperty]
        private EquipmentPropType? equipmentPropType;

        [ObservableProperty]
        private ObservableCollection<EquipmentProp> equipmentPropList = new ObservableCollection<EquipmentProp>();

        [ObservableProperty]
        private int pageIndex = 1;

        [ObservableProperty]
        private long maxPage;

        [ObservableProperty]
        private int pageSize = 30;

        public Window Owner { get; set; }

        /// <summary>
        /// 设备Id
        /// </summary>
唐召明 authored
48
        private int EquipmentId { get; set; }
唐召明 authored
49
50

        private readonly DataContext _context;
唐召明 authored
51
        private readonly CenterService _centerService;
唐召明 authored
52
唐召明 authored
53
        public EquipmentPropMonitorVM(DataContext context, CenterService centerService)
唐召明 authored
54
55
        {
            _context = context;
唐召明 authored
56
            _centerService = centerService;
唐召明 authored
57
58
        }
唐召明 authored
59
        public void InitialData(Equipment equipment, CancellationTokenSource cancellationTokenSource)
唐召明 authored
60
61
62
63
64
65
66
67
68
69
        {
            var keyValuePairs = new Dictionary<string, EquipmentPropType?>
            {
                { "全部",null }
            };
            foreach (var item in Enum.GetNames<EquipmentPropType>())
            {
                keyValuePairs.Add(item, Enum.Parse<EquipmentPropType>(item));
            }
            EquipmentPropTypes = keyValuePairs;
唐召明 authored
70
71
            Owner.Title = $"{equipment.Name}-属性数据";
            EquipmentId = equipment.Id;
72
            LoadData();
唐召明 authored
73
            RefreshValues(cancellationTokenSource);
唐召明 authored
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
        }

        [RelayCommand]
        public void Serach()
        {
            var result = LoadData();
            if (!result.Success)
            {
                MessageBox.Error($"加载数据失败.{result.Msg}");
            }
        }

        /// <summary>
        /// 页码改变
        /// </summary>
        [RelayCommand]
        private void PageUpdated(FunctionEventArgs<int> info)
        {
            PageIndex = info.Info;
            LoadData();
        }

        private BllResult LoadData()
        {
            try
            {
100
                var query = _context.EquipmentProp.Where(GetFilter()).Include(x => x.EquipmentTypePropTemplate);
唐召明 authored
101
102
103
104
105
106
107
108
109
110
111
112
113
114
                var total = query.Count();
                MaxPage = total / PageSize + Convert.ToInt32(total % PageSize != 0);
                var result = query.Page(PageIndex, PageSize).ToList();
                EquipmentPropList = new ObservableCollection<EquipmentProp>(result);
                return BllResultFactory.Success();
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error(ex.Message);
            }
        }

        private Expression<Func<EquipmentProp, bool>> GetFilter()
        {
唐召明 authored
115
            var filter = PredicateBuilder.New<EquipmentProp>(x => x.EquipmentId.Equals(EquipmentId));
唐召明 authored
116
117
118
119
120
121
122
123
124
125
            if (!string.IsNullOrWhiteSpace(EquipmentPropCode))
            {
                filter = filter.And(x => x.EquipmentTypePropTemplateCode.Contains(EquipmentPropCode));
            }
            if (!string.IsNullOrWhiteSpace(EquipmentPropName))
            {
                filter = filter.And(x => x.Remark.Contains(EquipmentPropName));
            }
            if (EquipmentPropType != null)
            {
唐召明 authored
126
                filter = filter.And(x => x.EquipmentTypePropTemplate.PropType == EquipmentPropType);
唐召明 authored
127
128
129
            }
            return filter;
        }
唐召明 authored
130
131
132
133
134
135
136
137
138
139
140
141
142

        /// <summary>
        /// 定时刷新Value数据
        /// </summary>
        private void RefreshValues(CancellationTokenSource cancellationTokenSource)
        {
            Task.Run(() =>
            {
                Application.Current.Dispatcher.Invoke(async () =>
                {
                    while (!cancellationTokenSource.IsCancellationRequested)
                    {
                        await Task.Delay(1000);
唐召明 authored
143
144
145
146
147
148
149
                        if (!_centerService.Equipments.Where(x => x.Id == EquipmentId).Any())
                        {
                            continue;
                        }

                        var temps = EquipmentPropList.ToList();
                        foreach (var item in temps)
唐召明 authored
150
151
152
153
154
155
156
157
158
                        {
                            var prop = _centerService.Equipments.SelectMany(x => x.EquipmentProps).Where(x => x.Id == item.Id).FirstOrDefault();
                            if (prop == null)
                            {
                                continue;
                            }
                            item.Value = prop.Value;
                            item.Updated = prop.Updated;
                        }
唐召明 authored
159
160
                        EquipmentPropList = null;
                        EquipmentPropList = new ObservableCollection<EquipmentProp>(temps);
唐召明 authored
161
162
163
164
                    }
                }, DispatcherPriority.Background);
            }, cancellationTokenSource.Token);
        }
唐召明 authored
165
166
    }
}