EquipmentController.cs
11.8 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
using HHECS.BllModel;
using HHECS.DAQServer.DataFlag;
using HHECS.DAQServer.Dto.Equipment;
using HHECS.DAQServer.Services;
using HHECS.DAQShared.Common.Enums;
using HHECS.DAQShared.Dto;
using HHECS.DAQShared.Models;
using Microsoft.AspNetCore.Mvc;
namespace HHECS.DAQServer.Controllers
{
/// <summary>
/// 设备数据
/// </summary>
[Route("api/[controller]/[action]")]
[ApiController]
public class EquipmentController : ControllerBase
{
private readonly IFreeSql<IOTCloundFlag> _iotCloundFreeSql;
private readonly EquipmentService _equipmentService;
public EquipmentController(IFreeSql<IOTCloundFlag> iotCloundFreeSql, EquipmentService equipmentService)
{
_iotCloundFreeSql = iotCloundFreeSql;
_equipmentService = equipmentService;
}
/// <summary>
/// 推送设备实时数据
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
[HttpPost]
public async Task<BllResult> SendEquipmentData(IEnumerable<EquipmentDataDto> data)
{
return await _equipmentService.SendEquipmentData(data);
}
/// <summary>
/// 推送某段时间的设备数据
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
[HttpPost]
public async Task<BllResult> SendEquipmentDataV2(IEnumerable<EquipmentDataV2Dto> data)
{
return await _equipmentService.SendEquipmentDataV2(data);
}
/// <summary>
/// 推送设备任务完成数
/// </summary>
/// <param name="taskInfos"></param>
/// <returns></returns>
[HttpPost]
public async Task<BllResult> EquipmentTask(List<EquipmentTaskDto> taskInfos)
{
try
{
if (taskInfos.Count == 0)
{
return BllResultFactory.Error($"数据不能为空!");
}
var equipmentCodes = taskInfos.Select(x => x.EquipmentCode).Distinct().ToList();
var equipments = await _iotCloundFreeSql.Queryable<Equipment>().Where(x => equipmentCodes.Contains(x.Code)).ToListAsync(x => new Equipment
{
Code = x.Code,
Name = x.Name,
EquipmentTypeId = x.EquipmentTypeId,
FactoryCode = x.FactoryCode,
ProjectCode = x.ProjectCode,
});
if (equipmentCodes.Count != equipments.Count)
{
var temps = equipmentCodes.Except(equipments.Select(x => x.Code));
return BllResultFactory.Error($"设备编码[{string.Join(',', temps)}]不存在,请检查数据并重试!");
}
var equipmentTypeIds = equipments.Select(x => x.EquipmentTypeId).Distinct().ToList();
var equipmentTypes = _iotCloundFreeSql.Queryable<EquipmentType>().Where(x => equipmentTypeIds.Contains(x.Id)).ToList(x => new EquipmentType
{
Id = x.Id,
Code = x.Code,
});
using var equipmentTaskRepository = _iotCloundFreeSql.GetRepository<EquipmentTaskInfo>();
var addTemps = new List<EquipmentTaskInfo>();
var updateTemps = new List<EquipmentTaskInfo>();
foreach (var item in taskInfos)
{
var record = equipmentTaskRepository.Where(x => x.EquipmentCode == item.EquipmentCode && x.Created.Value.Date == item.DateTime.Date).First();
//更新
if (record != null)
{
equipmentTaskRepository.Attach(record);
record.TotalTask = item.TotalTask;
record.Updated = item.DateTime;
updateTemps.Add(record);
continue;
}
//新增
var currentEquipment = equipments.Where(x => x.Code == item.EquipmentCode).First();
var currentEquipmentTypeCode = equipmentTypes.Where(x => x.Id == currentEquipment.EquipmentTypeId).Select(x => x.Code).FirstOrDefault();
addTemps.Add(new EquipmentTaskInfo
{
EquipmentCode = currentEquipment.Code,
EquipmentName = currentEquipment.Name,
EquipmentTypeCode = currentEquipmentTypeCode,
ProjectCode = currentEquipment.ProjectCode,
FactoryCode = currentEquipment.FactoryCode,
TotalTask = item.TotalTask,
Created = item.DateTime,
});
}
var updateTotal = equipmentTaskRepository.Update(updateTemps);
var addTotal = equipmentTaskRepository.Insert(addTemps);
return BllResultFactory.Success($"操作成功,新增{addTotal.Count}条记录,更新{updateTotal}条记录");
}
catch (Exception ex)
{
return BllResultFactory.Error($"程序异常:{ex.Message}");
}
}
/// <summary>
/// 更新客户端在线状态
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
[HttpPost]
public async Task<BllResult> UpdateClientStatus(ClientStatusDto data)
{
return await _equipmentService.UpdateClientStatus(data.ClientId);
}
/// <summary>
/// 更新AGV点位信息
/// </summary>
/// <param name="agvPoints"></param>
/// <returns></returns>
[HttpPost]
public async Task<BllResult> UpdateAGVPoint(List<AGVPointDto> agvPoints)
{
try
{
if (agvPoints.Count == 0)
{
return BllResultFactory.Error($"数据不能为空!");
}
if (agvPoints.Where(x => string.IsNullOrWhiteSpace(x.EquipmentSN)).Any())
{
return BllResultFactory.Error($"EquipmentSN数据不能为空");
}
var equipmentSNs = agvPoints.Select(x => x.EquipmentSN).ToList();
var equipments = await _iotCloundFreeSql.Queryable<Equipment>().Where(x => equipmentSNs.Contains(x.Code)).ToListAsync(x => new Equipment
{
Id = x.Id,
Code = x.Code,
ProjectCode = x.ProjectCode,
FactoryCode = x.FactoryCode,
});
var temp = equipmentSNs.Except(equipments.Select(x => x.Code)).Distinct();
if (temp.Any())
{
var str = string.Join(',', temp);
return BllResultFactory.Error($"设备SN[{str}]在系统中不存在,请检测数据是否正确,或联系管理员获取!");
}
var allPoints = _iotCloundFreeSql.Queryable<AGVPoint>().Where(x => equipmentSNs.Contains(x.EquipmentCode)).ToList();
foreach (var item in agvPoints)
{
//数据库现有数据
var points = allPoints.Where(x => x.EquipmentCode == item.EquipmentSN).ToList();
var oldBarCodes = points.Select(x => x.BarCode).ToList();
var newBarCodes = item.BarCodes;
var currentEquipment = equipments.Where(x => x.Code == item.EquipmentSN).FirstOrDefault();
if (currentEquipment == null)
{
return BllResultFactory.Error($"设备SN“{item.EquipmentSN}”不存在,请检查数据是否正确,或联系管理员处理!");
}
if (string.IsNullOrWhiteSpace(currentEquipment.ProjectCode))
{
return BllResultFactory.Error($"{nameof(item.EquipmentSN)}的“{nameof(currentEquipment.ProjectCode)}”字段数据未配置,请联系管理员处理!");
}
if (string.IsNullOrWhiteSpace(currentEquipment.FactoryCode))
{
return BllResultFactory.Error($"{nameof(item.EquipmentSN)}为的“{nameof(currentEquipment.FactoryCode)}”字段数据未配置,请联系管理员处理!");
}
var addTemps = newBarCodes.Except(oldBarCodes);
var removeTemps = oldBarCodes.Except(newBarCodes);
_iotCloundFreeSql.Delete<AGVPoint>().Where(x => x.EquipmentCode == item.EquipmentSN && removeTemps.Contains(x.BarCode)).ExecuteAffrows();
var temps = addTemps.Select(x => new AGVPoint
{
EquipmentCode = currentEquipment.Code,
BarCode = x,
ProjectCode = currentEquipment.ProjectCode,
FactoryCode = currentEquipment.FactoryCode,
Created = DateTime.Now
}).ToList();
_iotCloundFreeSql.Insert(temps).ExecuteAffrows();
}
return BllResultFactory.Success();
}
catch (Exception ex)
{
return BllResultFactory.Error(ex.Message);
}
}
/// <summary>
/// 获取客户端设备数据
/// </summary>
/// <param name="clientId"></param>
/// <remarks>用于客户端主动同步数据使用</remarks>
/// <returns></returns>
[HttpGet]
public BllResult<List<EquipmentType>> GetClientEquipmentData(Guid clientId)
{
try
{
var client = _iotCloundFreeSql.Queryable<ClientConfig>().Where(x => x.Id == clientId).First();
if (client == null)
{
return BllResultFactory.Error<List<EquipmentType>>($"未找到ClientId“{clientId}”的客户端信息!");
}
if (string.IsNullOrWhiteSpace(client.ProjectCode))
{
return BllResultFactory.Error<List<EquipmentType>>($"未配置客户端“{clientId}”的项目编号!");
}
//需过滤的设备类型
var filterEquipmentTypeCodes = new List<EquipmentTypeConst>
{
EquipmentTypeConst.WeldRobot,
EquipmentTypeConst.WeldRobotV2,
EquipmentTypeConst.AGVForklift,
}.Select(x => x.ToString()).ToList();
var filterEquipmentTypeIds = _iotCloundFreeSql.Queryable<EquipmentType>().Where(x => filterEquipmentTypeCodes.Contains(x.Code)).ToList(x => x.Id);
var equipments = _iotCloundFreeSql.Queryable<Equipment>().Where(x => x.ProjectCode == client.ProjectCode && !filterEquipmentTypeIds.Contains(x.EquipmentTypeId)).IncludeMany(x => x.EquipmentProps).ToList();
var equipmentTypeIds = equipments.Select(x => x.EquipmentTypeId).Distinct().ToList();
var equipmentTypes = _iotCloundFreeSql.Queryable<EquipmentType>().Where(x => equipmentTypeIds.Contains(x.Id)).IncludeMany(x => x.EquipmentTypePropTemplates).ToList();
foreach (var equipmentType in equipmentTypes)
{
equipmentType.Equipments = equipments.Where(x => x.EquipmentTypeId == equipmentType.Id).ToList();
}
return BllResultFactory.Success(equipmentTypes);
}
catch (Exception ex)
{
return BllResultFactory.Error<List<EquipmentType>>(ex.Message);
}
}
/// <summary>
/// 推送日志
/// </summary>
/// <param name="softLog"></param>
/// <returns></returns>
[HttpPost]
public async Task<BllResult> SendClientLog(SoftLogDto softLog)
{
return await _equipmentService.SendClientLog(softLog);
}
}
}