ProjectController.cs
18.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
using HHECS.BllModel;
using HHECS.DAQServer.DataFlag;
using HHECS.DAQServer.Dto.Project;
using HHECS.DAQServer.Model.HuaHengLicence;
using HHECS.DAQShared.Models;
using HHECS.DAQShared.Models.IOTCloud;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using System.Text.Json;
namespace HHECS.DAQServer.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class ProjectController : ControllerBase
{
private readonly IFreeSql<IOTCloundFlag> _iotCloundFreeSql;
private readonly IFreeSql<HuaHengLicenceFlag> _licenceFreeSql;
public ProjectController(IFreeSql<IOTCloundFlag> iotCloundFreeSql, IFreeSql<HuaHengLicenceFlag> licenceFreeSql)
{
_iotCloundFreeSql = iotCloundFreeSql;
_licenceFreeSql = licenceFreeSql;
}
/// <summary>
/// 注册项目、设备
/// </summary>
/// <param name="model"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
[HttpPost]
public async Task<BllResult> RegisterEquipment(RegisterEquipmentDto model, CancellationToken cancellationToken = default)
{
var stopwatch = new Stopwatch();
using var uw = _iotCloundFreeSql.CreateUnitOfWork();
using var sysInterfaceLogRepository = _iotCloundFreeSql.GetRepository<SysInterfaceLog>();
var sysInterfaceLog = new SysInterfaceLog
{
Type = "上位机SN注册项目和设备",
System = "HHECS.DAQServer",
Method = "POST",
Server = $"{HttpContext.Request.Host}",
Path = HttpContext.Request.Path,
Request = JsonSerializer.Serialize(model),
CreateBy = "HHECS.DAQServer",
CreateTime = DateTime.Now,
};
try
{
using var busSnProjectInfoWmsHeadRepository = uw.GetRepository<BusSnProjectInfoWmsHead>();
using var sysRoleProjectRelRepository = uw.GetRepository<SysRoleProjectRel>();
using var baseProjectRepository = uw.GetRepository<BaseProject>();
using var baseFactoryRepository = uw.GetRepository<BaseFactory>();
using var baseEquipmentRepository = uw.GetRepository<BaseEquipment>();
using var equipmentRepository = uw.GetRepository<Equipment>();
using var equipmentTypeRepository = uw.GetRepository<EquipmentType>();
using var equipmentPropRepository = uw.GetRepository<EquipmentProp>();
using var equipmentTypePropTemplateRepository = uw.GetRepository<EquipmentTypePropTemplate>();
var result = BllResultFactory.Success();
var busSnProjectInfoWmsHead = await busSnProjectInfoWmsHeadRepository.Where(x => x.SN == model.SN).FirstAsync(cancellationToken);
if (busSnProjectInfoWmsHead == null)
{
//设备SN不在WMS推送的表中,需要校验SN有效性,若SN有效,则使用默认项目和仓库
var snInfo = await _licenceFreeSql.Queryable<SnInfo>().Where(x => x.Sn == model.SN).FirstAsync(cancellationToken);
if (snInfo == null)
{
result = BllResultFactory.Error($"当前设备SN“{model.SN}”无效,请联系管理员获取有效SN后再试!");
sysInterfaceLog.Response = JsonSerializer.Serialize(result);
sysInterfaceLog.TotalMilliseconds = stopwatch.ElapsedMilliseconds;
await sysInterfaceLogRepository.InsertAsync(sysInterfaceLog);
return result;
}
busSnProjectInfoWmsHead = new BusSnProjectInfoWmsHead
{
CorrelatedCode = "DefaultProject",
OrdeName = "默认项目",
CreateBy = snInfo.CreatedBy,
CreateTime = snInfo.Created,
};
}
if (string.IsNullOrWhiteSpace(busSnProjectInfoWmsHead.OrdeName))
{
result = BllResultFactory.Error($"当前SN“{model.SN}”对应的项目编号“{busSnProjectInfoWmsHead.CorrelatedCode}”项目名称为空,请联系管理员处理!");
sysInterfaceLog.Response = JsonSerializer.Serialize(result);
sysInterfaceLog.TotalMilliseconds = stopwatch.ElapsedMilliseconds;
await sysInterfaceLogRepository.InsertAsync(sysInterfaceLog);
return result;
}
var equipmentType = await equipmentTypeRepository.Where(x => x.Code == model.EquipmentTypeCode).FirstAsync(cancellationToken);
if (equipmentType == null)
{
result = BllResultFactory.Error($"IOT不存在编号为“{model.EquipmentTypeCode}”的设备类型数据,请联系管理员处理!");
sysInterfaceLog.Response = JsonSerializer.Serialize(result);
sysInterfaceLog.TotalMilliseconds = stopwatch.ElapsedMilliseconds;
await sysInterfaceLogRepository.InsertAsync(sysInterfaceLog);
return result;
}
var baseProject = await baseProjectRepository.Where(x => x.ProjectCode == busSnProjectInfoWmsHead.CorrelatedCode).FirstAsync(cancellationToken);
if (baseProject == null)
{
//项目不存在,需要创建项目
baseProject = new BaseProject
{
Keys = Guid.NewGuid(),
ProjectCode = busSnProjectInfoWmsHead.CorrelatedCode,
ProjectName = busSnProjectInfoWmsHead.OrdeName,
ProjectManager = model.ProjectManager,
ProjectStartTime = busSnProjectInfoWmsHead.CreateTime,
CreateTime = busSnProjectInfoWmsHead.CreateTime,
CreateBy = busSnProjectInfoWmsHead.CreateBy,
};
await baseProjectRepository.InsertAsync(baseProject, cancellationToken);
var sysRoleProjectRel = new SysRoleProjectRel
{
ProjectRolesKey = Guid.Parse("9D0DABCE-A0C5-4048-8CA0-421DC3959D09"),//默认管理员角色
ProjectKey = baseProject.Keys,
CreateTime = DateTime.Now,
CreateBy = busSnProjectInfoWmsHead.CreateBy,
};
await sysRoleProjectRelRepository.InsertAsync(sysRoleProjectRel, cancellationToken);
}
var baseFactory = await baseFactoryRepository.Where(x => x.FactoryCode == $"{busSnProjectInfoWmsHead.CorrelatedCode}-01").FirstAsync(cancellationToken);
if (baseFactory == null)
{
//仓库不存在,需要创建仓库
baseFactory = new BaseFactory
{
Keys = Guid.NewGuid(),
ProjectKeys = baseProject.Keys,
FactoryCode = $"{busSnProjectInfoWmsHead.CorrelatedCode}-01",
FactoryName = $"默认仓库",
CreateTime = DateTime.Now,
CreateBy = busSnProjectInfoWmsHead.CreateBy,
};
await baseFactoryRepository.InsertAsync(baseFactory, cancellationToken);
}
var baseEquipment = await baseEquipmentRepository.Where(x => x.EquipmentCode == model.SN).FirstAsync(cancellationToken);
if (baseEquipment == null)
{
//设备不存在,需要创建设备
baseEquipment = new BaseEquipment
{
EquipmentCode = model.SN,
EquipmentName = model.EquipmentName,
ProjectKeys = baseProject.Keys,
DestinationArea = "1",
IsEnable = true,
FactoryCode = baseFactory.FactoryCode,
EquipmentTypeCode = equipmentType.Code,
OtherCode = model.SN,
StartTime = busSnProjectInfoWmsHead.CreateTime,
CreateTime = DateTime.Now,
CreateBy = busSnProjectInfoWmsHead.CreateBy,
};
await baseEquipmentRepository.InsertAsync(baseEquipment, cancellationToken);
}
var daqEquipment = await equipmentRepository.Where(x => x.Code == baseEquipment.OtherCode).FirstAsync(cancellationToken);
if (daqEquipment == null)
{
//设备不存在,需要创建设备
daqEquipment = new Equipment
{
Code = baseEquipment.OtherCode,
Name = baseEquipment.EquipmentName,
FactoryCode = baseFactory.FactoryCode,
ProjectCode = baseProject.ProjectCode,
DestinationArea = baseEquipment.DestinationArea,
Disable = false,
ConnectName = $"{baseFactory.FactoryCode}-{baseEquipment.OtherCode}",
EquipmentTypeId = equipmentType.Id,
Created = DateTime.Now,
CreatedBy = busSnProjectInfoWmsHead.CreateBy,
};
await equipmentRepository.InsertAsync(daqEquipment, cancellationToken);
daqEquipment.Id = await equipmentRepository.Where(x => x.Code == baseEquipment.OtherCode).FirstAsync(x => x.Id, cancellationToken);
var equipmentProps = await equipmentTypePropTemplateRepository.Where(x => x.EquipmentTypeId == equipmentType.Id).ToListAsync(x => new EquipmentProp
{
Id = 0,
EquipmentId = daqEquipment.Id,
Code = x.Code,
Name = x.Name,
Address = x.Address,
DataType = x.DataType,
PropType = x.PropType,
MonitorFailure = x.MonitorFailure,
MonitorCompareValue = x.MonitorCompareValue,
MonitorNormal = x.MonitorNormal,
Remark = x.Description,
Created = DateTime.Now,
CreatedBy = busSnProjectInfoWmsHead.CreateBy,
}, cancellationToken);
await equipmentPropRepository.InsertAsync(equipmentProps, cancellationToken);
}
uw.Commit();
sysInterfaceLog.Response = JsonSerializer.Serialize(result);
sysInterfaceLog.TotalMilliseconds = stopwatch.ElapsedMilliseconds;
await sysInterfaceLogRepository.InsertAsync(sysInterfaceLog);
return BllResultFactory.Success();
}
catch (Exception ex)
{
uw.Rollback();
var result = BllResultFactory.Error(ex.Message);
sysInterfaceLog.Response = JsonSerializer.Serialize(result);
sysInterfaceLog.TotalMilliseconds = stopwatch.ElapsedMilliseconds;
await sysInterfaceLogRepository.InsertAsync(sysInterfaceLog);
return result;
}
}
/// <summary>
/// 注册客户端
/// </summary>
/// <param name="model"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
[HttpPost]
public async Task<BllResult<Guid>> RegisterClient(RegisterClientDto model, CancellationToken cancellationToken = default)
{
try
{
var project = await _iotCloundFreeSql.Queryable<BaseProject>().Where(p => p.ProjectCode == model.ProjectCode).FirstAsync(cancellationToken);
if (project == null)
{
return BllResultFactory.Error<Guid>($"IOT中不存在“{model.ProjectCode}”项目编号,请检查数据后重试!");
}
BaseFactory factory = null;
//仓库编号为空,默认取第一个仓库编号
if (string.IsNullOrWhiteSpace(model.FactoryCode))
{
factory = await _iotCloundFreeSql.Queryable<BaseFactory>().Where(f => f.ProjectKeys == project.Keys).FirstAsync(cancellationToken);
if (factory == null)
{
return BllResultFactory.Error<Guid>($"项目编号“{model.ProjectCode}”不存在仓房信息,请联系管理员处理!");
}
}
else
{
factory = await _iotCloundFreeSql.Queryable<BaseFactory>().Where(f => f.ProjectKeys == project.Keys && f.FactoryCode == model.FactoryCode)
.FirstAsync(cancellationToken);
if (factory == null)
{
return BllResultFactory.Error<Guid>($"项目编号“{model.ProjectCode}”不存在仓房“{model.FactoryCode}”信息,请检查数据后重试!");
}
}
var clientId = Guid.NewGuid();
var clientName = model.Name;
if (string.IsNullOrWhiteSpace(model.Name))
{
clientName = $"{project.ProjectName}-{model.Type}";
}
var clientConfig = new ClientConfig
{
Id = clientId,
ProjectCode = project.ProjectCode,
FactoryCode = factory.FactoryCode,
Name = clientName,
Type = model.Type,
LastSeenDate = DateTime.Now,
Created = DateTime.Now,
};
await _iotCloundFreeSql.Insert(clientConfig).ExecuteAffrowsAsync(cancellationToken);
return BllResultFactory.Success(clientId);
}
catch (Exception ex)
{
return BllResultFactory.Error<Guid>(ex.Message);
}
}
/// <summary>
/// 注册客户端
/// </summary>
/// <param name="model"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
[HttpPost]
public async Task<BllResult<Guid>> RegisterClient2(RegisterClientDto2 model, CancellationToken cancellationToken = default)
{
try
{
var busSnProjectInfoWmsHead = await _iotCloundFreeSql.Queryable<BusSnProjectInfoWmsHead>().Where(x => x.SN == model.SN).FirstAsync(cancellationToken);
var projectCode = string.Empty;
if (busSnProjectInfoWmsHead == null)
{
//设备SN不在WMS推送的表中,需要校验SN有效性,若SN有效,则使用默认项目和仓库
var snInfo = await _licenceFreeSql.Queryable<SnInfo>().Where(x => x.Sn == model.SN).FirstAsync(cancellationToken);
if (snInfo == null)
{
return BllResultFactory.Error<Guid>($"当前设备SN“{model.SN}”无效,请联系管理员获取有效SN后再试!");
}
busSnProjectInfoWmsHead = new BusSnProjectInfoWmsHead
{
CorrelatedCode = "DefaultProject",
OrdeName = "默认项目",
CreateBy = snInfo.CreatedBy,
CreateTime = snInfo.Created,
};
}
var project = await _iotCloundFreeSql.Queryable<BaseProject>().Where(x => x.ProjectCode == busSnProjectInfoWmsHead.CorrelatedCode).FirstAsync(cancellationToken);
if (project == null)
{
return BllResultFactory.Error<Guid>($"IOT中不存在项目编号“{busSnProjectInfoWmsHead.CorrelatedCode}”,请先注册项目数据之后再申请客户端ID");
}
var factory = await _iotCloundFreeSql.Queryable<BaseFactory>().Where(f => f.ProjectKeys == project.Keys).FirstAsync(cancellationToken);
if (factory == null)
{
return BllResultFactory.Error<Guid>($"项目编号“{project.ProjectCode}”不存在仓房信息,请联系管理员处理!");
}
var clientId = Guid.NewGuid();
var clientConfig = new ClientConfig
{
Id = clientId,
ProjectCode = project.ProjectCode,
FactoryCode = factory.FactoryCode,
Name = model.Name,
Type = model.Type,
LastSeenDate = DateTime.Now,
Created = DateTime.Now,
};
await _iotCloundFreeSql.Insert(clientConfig).ExecuteAffrowsAsync(cancellationToken);
return BllResultFactory.Success(clientId);
}
catch (Exception ex)
{
return BllResultFactory.Error<Guid>(ex.Message);
}
}
/// <summary>
/// SN是否绑定项目
/// </summary>
/// <param name="sn">设备SN</param>
/// <returns></returns>
[HttpPost]
public async Task<BllResult<bool>> SNBindingProjectVerification(string sn)
{
try
{
var snVerification = await _licenceFreeSql.Queryable<SnInfo>().Where(x => x.Sn == sn).AnyAsync();
if (!snVerification)
{
return BllResultFactory.Error<bool>($"当前SN“{sn}”无效,请联系管理员获取有效SN后再试!");
}
var baseEquipment = await _iotCloundFreeSql.Queryable<BaseEquipment>().Where(x => x.EquipmentCode == sn).FirstAsync();
if (baseEquipment == null)
{
return BllResultFactory.Error<bool>($"当前SN“{sn}”未绑定项目");
}
var project = await _iotCloundFreeSql.Queryable<BaseProject>().Where(x => x.Keys == baseEquipment.ProjectKeys).FirstAsync();
return BllResultFactory.Success(true, $"当前SN“{sn}”绑定了“{project.ProjectName}”项目,项目编号为“{project.ProjectCode}”");
}
catch (Exception ex)
{
return BllResultFactory.Error<bool>(ex.Message);
}
}
}
}