WeldMonitorVM.cs
8.97 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HHECS.BllModel;
using HHECS.RobotTool.Dto;
using HHECS.RobotTool.Dto.WeldMonitor;
using HHECS.RobotTool.Model;
using HHECS.RobotTool.Service;
using MessageBox = HandyControl.Controls.MessageBox;
namespace HHECS.RobotTool.ViewModel
{
public partial class WeldMonitorVM : ObservableObject
{
[ObservableProperty]
private EquipmentExtend currentEquipment = new EquipmentExtend();
[ObservableProperty]
private string currentEquipmentParameterCode = string.Empty;
[ObservableProperty]
private RobotInputParameter inputParameter = new RobotInputParameter();
[ObservableProperty]
private List<WeldMonitorDataGrid> weldMonitorDataGrids = new List<WeldMonitorDataGrid>();
[ObservableProperty]
private string monitorSource = string.Empty;
[ObservableProperty]
private WeldMonitorModel weldMonitorModel = new WeldMonitorModel();
[ObservableProperty]
private List<EquipmentTreeViewDto> equipmentTreeViews = new List<EquipmentTreeViewDto>();
/// <summary>
/// 唯一标识
/// </summary>
private readonly Guid currentIdentifier;
private readonly IFreeSql _freeSql;
private readonly HttpService _httpService;
private readonly GrooveService _grooveService;
private List<WeldMonitorModel> _monitorCache = new List<WeldMonitorModel>();
public WeldMonitorVM(IFreeSql freeSql, HttpService httpService, TcpListenerService tcpListenerService, GrooveService grooveService)
{
currentIdentifier = Guid.NewGuid();
_freeSql = freeSql;
_httpService = httpService;
_grooveService = grooveService;
tcpListenerService.WeldMonitor += TcpListenerService_WeldMonitor; ;
MonitorSource = $"http://172.16.29.87:6002/index.html#/Home?key={currentIdentifier}&w=700&h=500&font=10";
InitialTreeView();
}
private void TcpListenerService_WeldMonitor(WeldMonitorModel monitorModel)
{
try
{
var old = _monitorCache.Where(x => x.EquipmentIPAddress == monitorModel.EquipmentIPAddress).FirstOrDefault();
if (old != null)
{
_monitorCache.Remove(old);
}
_monitorCache.Add(monitorModel);
//刷新页面
if (CurrentEquipment.IP == monitorModel.EquipmentIPAddress)
{
CurrentEquipmentParameterCode = _freeSql.Queryable<RobotConfig>().Where(x => x.Id == CurrentEquipment.RobotConfigId).First(x => x.Code);
ReloadMonitor(currentIdentifier, monitorModel);
}
}
catch (Exception ex)
{
MessageBox.Error($"{nameof(WeldMonitorVM)}的“{TcpListenerService_WeldMonitor}”方法异常:{ex.Message}");
}
}
[RelayCommand]
private void InitialTreeView()
{
try
{
var equipmentTypes = _freeSql.Queryable<EquipmentTypeExtend>().ToList();
var equipments = _freeSql.Queryable<EquipmentExtend>().ToList();
var temps = new List<EquipmentTreeViewDto>();
foreach (var item in equipmentTypes)
{
var temp = new EquipmentTreeViewDto
{
Code = item.Code,
Name = item.Name,
Childrens = equipments.Where(x => x.EquipmentTypeId == item.Id).Select(x => new EquipmentTreeViewDto
{
Id = x.Id,
Code = x.Code,
Name = x.Name,
Type = 1
}).ToList()
};
temps.Add(temp);
}
EquipmentTreeViews = new List<EquipmentTreeViewDto>(temps);
}
catch (Exception ex)
{
MessageBox.Error($"“{nameof(WeldMonitorVM)}”初始化数据失败:{ex.Message}");
}
}
[RelayCommand]
private void SelectedItemChanged(object parameter)
{
if (parameter is EquipmentTreeViewDto dto)
{
CurrentEquipment = _freeSql.Queryable<EquipmentExtend>().Where(x => x.Id == dto.Id).First();
if (CurrentEquipment == null)
{
MessageBox.Error($"设备“{dto.Name}”信息不存在,请刷新数据后重试!");
return;
}
CurrentEquipmentParameterCode = _freeSql.Queryable<RobotConfig>().Where(x => x.Id == CurrentEquipment.RobotConfigId).First(x => x.Code);
var monitorModel = _monitorCache.Where(x => x.EquipmentIPAddress == CurrentEquipment.IP).FirstOrDefault();
if (monitorModel == null)
{
var defaultParameterResult = _grooveService.GetInputParameterDefault(CurrentEquipment.IP);
if (!defaultParameterResult.Success)
{
MessageBox.Error($"{defaultParameterResult.Msg}");
return;
}
monitorModel = new WeldMonitorModel
{
RobotInputParameter = defaultParameterResult.Data
};
}
ReloadMonitor(currentIdentifier, monitorModel);
}
}
/// <summary>
/// 刷新监控
/// </summary>
/// <param name="identifier">页面唯一标识</param>
/// <param name="monitorModel">焊接数据</param>
/// <returns></returns>
private BllResult ReloadMonitor(Guid identifier, WeldMonitorModel monitorModel)
{
try
{
WeldMonitorModel = monitorModel;
InputParameter = WeldMonitorModel.RobotInputParameter;
var outputParameter = WeldMonitorModel.RobotOutputParameter;
WeldMonitorDataGrids = outputParameter.LayerModels.SelectMany(layer => layer.ChannelModels.Select(channel => new WeldMonitorDataGrid
{
Layer = layer.CurrentLayer,
LayerHeight = layer.LayerHeight,
SwingFrequency = layer.WeldSwingFrequency,
LeftAndRightDwellTime = layer.LeftAndRightDwellTime,
Channel = channel.CurrentChannel,
StartingPoint = channel.StartingPoint,
EndingPoint = channel.EndingPoint,
WeldSpeed = Math.Round(channel.WeldSpeed, 3),
Amplitude = channel.WeldAmplitude,
})).ToList();
Task.Run(() =>
{
var monitor = new WeldMonitorDto
{
Key = identifier,
Height = outputParameter.LayerModels.Sum(x => x.LayerHeight),
Data = outputParameter.LayerModels.Select(l => new LayerItem
{
Layer = l.CurrentLayer,
Height = l.LayerHeight,
Done = l.ChannelModels.Select(c => new DoneItem
{
State = GetState(l.CurrentLayer, c.CurrentChannel, monitorModel),
Position = c.CurrentChannel == l.ChannelModels.Count ? $"Y:{Math.Round(c.StartingPoint.Y, 1)},Z:{Math.Round(c.StartingPoint.Z, 1)}" : $"Y:{Math.Round(c.StartingPoint.Y, 1)}"
}).ToList()
}).ToList(),
};
_httpService.SetWeldMonitorData(monitor);
});
return BllResultFactory.Success();
}
catch (Exception ex)
{
return BllResultFactory.Error(ex.Message);
}
}
/// <summary>
/// 获取焊接状态
/// </summary>
/// <param name="layer">层</param>
/// <param name="channel">道</param>
/// <param name="monitorModel">当前焊接数据</param>
/// <remarks>0:未焊接,1:焊接中,2:已完成</remarks>
/// <returns></returns>
private static int GetState(int layer, int channel, WeldMonitorModel monitorModel)
{
if (monitorModel.CurrentLayer > layer)
{
return 2;
}
if (monitorModel.CurrentLayer == layer)
{
if (monitorModel.CurrentChannel > channel)
{
return 2;
}
if (monitorModel.CurrentChannel == channel)
{
return 1;
}
}
return 0;
}
}
}