EquipmentPropMonitorVM.cs
5.73 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HandyControl.Data;
using HHECS.BllModel;
using HHECS.DAQClient.Model;
using HHECS.DAQClient.Services;
using HHECS.DAQClient.ViewModel.EquipmentVM;
using HHECS.EquipmentModel;
using LinqKit;
using System.Collections.ObjectModel;
using System.Linq.Expressions;
using System.Windows;
using System.Windows.Threading;
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<EquipmentPropExtend> equipmentPropList = new ObservableCollection<EquipmentPropExtend>();
[ObservableProperty]
private int pageIndex = 1;
[ObservableProperty]
private long maxPage;
[ObservableProperty]
private int pageSize = 30;
public Window Owner { get; set; }
/// <summary>
/// 设备Id
/// </summary>
private int EquipmentId { get; set; }
private readonly IFreeSql _freeSql;
private readonly CenterService _centerService;
public EquipmentPropMonitorVM(IFreeSql freeSql, CenterService centerService)
{
_freeSql = freeSql;
_centerService = centerService;
}
public void InitialData(EquipmentExtend equipment, CancellationTokenSource cancellationTokenSource)
{
var keyValuePairs = new Dictionary<string, EquipmentPropType?>
{
{ "全部",null }
};
foreach (var item in Enum.GetNames<EquipmentPropType>())
{
keyValuePairs.Add(item, Enum.Parse<EquipmentPropType>(item));
}
EquipmentPropTypes = keyValuePairs;
Owner.Title = $"{equipment.Name}-属性数据";
EquipmentId = equipment.Id;
LoadData();
RefreshValues(cancellationTokenSource);
}
[RelayCommand]
public void Serach()
{
var result = LoadData();
if (!result.Success)
{
MessageBox.Error($"[{nameof(EquipmentPropMonitorVM)}]加载数据失败.{result.Msg}");
}
}
/// <summary>
/// 页码改变
/// </summary>
[RelayCommand]
private void PageUpdated(FunctionEventArgs<int> info)
{
PageIndex = info.Info;
LoadData();
}
private BllResult LoadData()
{
try
{
var query = _freeSql.Queryable<EquipmentPropExtend>().Where(GetFilter()).Include(x => x.EquipmentTypePropTemplate);
var total = query.Count();
MaxPage = total / PageSize + Convert.ToInt32(total % PageSize != 0);
var result = query.Page(PageIndex, PageSize).ToList();
EquipmentPropList = new ObservableCollection<EquipmentPropExtend>(result);
return BllResultFactory.Success();
}
catch (Exception ex)
{
return BllResultFactory.Error(ex.Message);
}
}
private Expression<Func<EquipmentPropExtend, bool>> GetFilter()
{
var filter = PredicateBuilder.New<EquipmentPropExtend>(x => x.EquipmentId.Equals(EquipmentId));
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)
{
filter = filter.And(x => x.EquipmentTypePropTemplate.PropType == EquipmentPropType);
}
return filter;
}
/// <summary>
/// 定时刷新Value数据
/// </summary>
private void RefreshValues(CancellationTokenSource cancellationTokenSource)
{
Task.Run(() =>
{
Application.Current.Dispatcher.Invoke(async () =>
{
while (!cancellationTokenSource.IsCancellationRequested)
{
await Task.Delay(1000);
if (!_centerService.Equipments.Where(x => x.Id == EquipmentId).Any())
{
continue;
}
var temps = EquipmentPropList.ToList();
foreach (var item in temps)
{
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;
}
EquipmentPropList = null;
EquipmentPropList = new ObservableCollection<EquipmentPropExtend>(temps);
}
}, DispatcherPriority.Background);
}, cancellationTokenSource.Token);
}
}
}