DigitalTwinController.cs
38.7 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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
using HHECS.BllModel;
using HHECS.DAQServer.Dto.DigitalTwin;
using HHECS.DAQServer.Services;
using HHECS.DAQShared.Common.Enums;
using HHECS.DAQShared.Dto;
using HHECS.DAQShared.Models;
using LinqKit;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Distributed;
using System.Data;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
namespace HHECS.DAQServer.Controllers
{
/// <summary>
/// 数字孪生接口
/// </summary>
[Route("api/[controller]/[action]")]
[ApiController]
public class DigitalTwinController : ControllerBase
{
private readonly IFreeSql _freeSql;
private readonly CommonService _commonService;
private readonly DigitalTwinService _digitalTwinService;
private readonly IDistributedCache _cache;
public DigitalTwinController(IFreeSql freeSql, CommonService commonService, DigitalTwinService digitalTwinService, IDistributedCache cache)
{
_freeSql = freeSql;
_commonService = commonService;
_digitalTwinService = digitalTwinService;
_cache = cache;
}
/// <summary>
/// 获取设备实时数据(Version 1)
/// </summary>
/// <param name="projectCode">项目编号</param>
/// <param name="factoryCode">仓库编号</param>
/// <param name="area">区域</param>
/// <param name="displayAnnotation">显示注释</param>
/// <remarks>通用设备数据查询</remarks>
/// <returns></returns>
[HttpGet("{projectCode}")]
public async Task<BllResult<Dictionary<string, Dictionary<string, Dictionary<string, string>>>>> GetEquipmentInfoV1(string projectCode, string factoryCode, string area = null, bool displayAnnotation = false)
{
try
{
if (string.IsNullOrWhiteSpace(projectCode))
{
return BllResultFactory.Error<Dictionary<string, Dictionary<string, Dictionary<string, string>>>>($"项目编号[{nameof(projectCode)}]不能为空!");
}
if (string.IsNullOrWhiteSpace(factoryCode))
{
return BllResultFactory.Error<Dictionary<string, Dictionary<string, Dictionary<string, string>>>>($"仓库编号[{nameof(factoryCode)}]不能为空!");
}
//数据缓存1秒,避免前端重复请求
var lastRequestCacheKey = $"{nameof(GetEquipmentInfoV1)}_{projectCode}_{factoryCode}_{area}_{displayAnnotation}";
var lastRequestCacheOptions = new DistributedCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromSeconds(1));
var lastRequestCacheDataBytes = await _cache.GetAsync(lastRequestCacheKey);
if (lastRequestCacheDataBytes != null)
{
var lastRequestCacheData = JsonSerializer.Deserialize<Dictionary<string, Dictionary<string, Dictionary<string, string>>>>(Encoding.Default.GetString(lastRequestCacheDataBytes));
return BllResultFactory.Success(lastRequestCacheData);
}
var filter = PredicateBuilder.New<Equipment>(true);
filter = filter.And(x => x.ProjectCode == projectCode);
filter = filter.And(x => x.FactoryCode == factoryCode);
if (!string.IsNullOrWhiteSpace(area))
{
filter = filter.And(x => x.DestinationArea == area);
}
InitialEquipments(filter, lastRequestCacheKey, out var equipments, out var equipmentTypes);
var data = equipmentTypes.ToDictionary(a => a.Code, b =>
{
var equipmentTemps = equipments.Where(x => x.EquipmentTypeId == b.Id).ToList();
return equipmentTemps.ToDictionary(c => c.Code, d => d.EquipmentProps.ToDictionary(e => string.Concat(e.Code[..1].ToLower(), e.Code.AsSpan(1)), f => displayAnnotation ? f.Name : f.Value));
});
await _cache.SetAsync(lastRequestCacheKey, JsonSerializer.SerializeToUtf8Bytes(data), lastRequestCacheOptions);
return BllResultFactory.Success(data);
}
catch (Exception ex)
{
return BllResultFactory.Error<Dictionary<string, Dictionary<string, Dictionary<string, string>>>>(ex.Message);
}
}
/// <summary>
/// 获取设备实时数据(Version 2)
/// </summary>
/// <param name="projectCode">项目编号</param>
/// <param name="factoryCode">仓库编号</param>
/// <param name="area">区域</param>
/// <remarks>定制化接口,新增设备类型需要单独开发</remarks>
/// <returns></returns>
[HttpGet("{projectCode}")]
public async Task<BllResult<EquipmentInfoResponseV2Dto>> GetEquipmentInfoV2(string projectCode, string factoryCode, string area = null)
{
try
{
if (string.IsNullOrWhiteSpace(projectCode))
{
return BllResultFactory.Error<EquipmentInfoResponseV2Dto>($"项目编号[{nameof(projectCode)}]不能为空!");
}
if (string.IsNullOrWhiteSpace(factoryCode))
{
return BllResultFactory.Error<EquipmentInfoResponseV2Dto>($"仓库编号[{nameof(factoryCode)}]不能为空!");
}
//数据缓存1秒,避免前端重复请求
var lastRequestCacheKey = $"{nameof(GetEquipmentInfoV2)}_{projectCode}_{factoryCode}_{area}";
var lastRequestCacheOptions = new DistributedCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromSeconds(1));
var lastRequestCacheDataBytes = await _cache.GetAsync(lastRequestCacheKey);
if (lastRequestCacheDataBytes != null)
{
var lastRequestCacheData = JsonSerializer.Deserialize<EquipmentInfoResponseV2Dto>(Encoding.Default.GetString(lastRequestCacheDataBytes));
return BllResultFactory.Success(lastRequestCacheData);
}
var filter = PredicateBuilder.New<Equipment>(true);
filter = filter.And(x => x.ProjectCode == projectCode);
filter = filter.And(x => x.FactoryCode == factoryCode);
if (!string.IsNullOrWhiteSpace(area))
{
filter = filter.And(x => x.DestinationArea == area);
}
InitialEquipments(filter, lastRequestCacheKey, out var equipments, out var equipmentTypes);
if (equipments.Count == 0)
{
return BllResultFactory.Error<EquipmentInfoResponseV2Dto>($"设备数据为空!");
}
var srmTypeCodes = new List<EquipmentTypeConst>
{
EquipmentTypeConst.SingleForkSRM,
EquipmentTypeConst.SingleForkSRMV2,
EquipmentTypeConst.DoubleForkSRM,
EquipmentTypeConst.DoubleForkSRMV2,
EquipmentTypeConst.SingleForkSSRM,
EquipmentTypeConst.SingleForkSSRMV2,
EquipmentTypeConst.SingleForkSSRMV132,
}.Select(x => x.ToString()).ToList();
var stackers = equipments.Where(x => srmTypeCodes.Contains(x.EquipmentType.Code)).ToDictionary(x => x.Code, srm =>
{
var (mainAlarm, detailAlarms) = _commonService.GetEquipmentAlarmInfo(srm);
_ = Enum.TryParse<SRMOperationModel>(srm[SRMProps.OperationModel.ToString()].Value, out var operationModel);
_ = int.TryParse(srm[SRMProps.HorizontalDistance.ToString()].Value, out var horizontalDistance);
_ = int.TryParse(srm[SRMProps.VerticalDistance.ToString()].Value, out var verticalDistance);
_ = int.TryParse(srm[SRMProps.ManageSmallColumn.ToString()].Value, out var manageSmallColumn);
_ = int.TryParse(srm[SRMProps.ManageMaxColumn.ToString()].Value, out var manageMaxColumn);
_ = int.TryParse(srm[SRMProps.CurrentColumn.ToString()].Value, out var currentColumn);
//货叉1
_ = int.TryParse(srm[SRMProps.Fork1Distance.ToString()].Value, out var fork1Distance);
_ = int.TryParse(srm[SRMProps.Fork1XPosition.ToString()].Value, out var fork1XPosition);
_ = int.TryParse(srm[SRMProps.Fork1YPosition.ToString()].Value, out var fork1YPosition);
_ = int.TryParse(srm[SRMProps.Fork1ZPosition.ToString()].Value, out var fork1ZPosition);
_ = bool.TryParse(srm[SRMProps.Fork1HasPallet.ToString()].Value, out var fork1HasPallet);
_ = bool.TryParse(srm[SRMProps.Fork1Center.ToString()].Value, out var fork1Center);
_ = bool.TryParse(srm[SRMProps.Fork1Block.ToString()].Value, out var fork1Block);
_ = int.TryParse(srm[SRMProps.Fork1TaskNo.ToString()].Value, out var fork1TaskNo);
_ = int.TryParse(srm[SRMProps.Fork1TaskExcuteStatus.ToString()].Value, out var fork1TaskExcuteStatus);
_ = int.TryParse(srm[SRMProps.Fork1TaskType.ToString()].Value, out var fork1TaskType);
_ = int.TryParse(srm[SRMProps.Fork1PickXPosition.ToString()].Value, out var fork1PickXPosition);
_ = int.TryParse(srm[SRMProps.Fork1PickYPosition.ToString()].Value, out var fork1PickYPosition);
_ = int.TryParse(srm[SRMProps.Fork1PickZPosition.ToString()].Value, out var fork1PickZPosition);
_ = int.TryParse(srm[SRMProps.Fork1PutXPosition.ToString()].Value, out var fork1PutXPosition);
_ = int.TryParse(srm[SRMProps.Fork1PutYPosition.ToString()].Value, out var fork1PutYPosition);
_ = int.TryParse(srm[SRMProps.Fork1PutZPosition.ToString()].Value, out var fork1PutZPosition);
var fork1GoodsBarcode = srm[SRMProps.Fork1GoodsBarcode.ToString()].Value;
var stackerDto = new StackerDto
{
EquipmentName = srm.Name,
EquipmentTypeCode = srm.EquipmentType.Code,
Number = srm[SRMProps.Number.ToString()].Value,
Status = _commonService.GetEquipmentStatus(srm).ToString(),
OperationModel = operationModel,
HorizontalDistance = horizontalDistance,
VerticalDistance = verticalDistance,
ManageSmallColumn = manageSmallColumn,
ManageMaxColumn = manageMaxColumn,
CurrentColumn = currentColumn,
Fork1Distance = fork1Distance,
Fork1XPosition = fork1XPosition,
Fork1YPosition = fork1YPosition,
Fork1ZPosition = fork1ZPosition,
Fork1HasPallet = fork1HasPallet,
//数据有延迟,这里手动转换
Fork1Center = fork1Center || fork1Distance == 0,
Fork1Block = fork1Block,
Fork1TaskNo = fork1TaskNo,
Fork1TaskExcuteStatus = fork1TaskExcuteStatus,
Fork1TaskType = fork1TaskType,
Fork1PickXPosition = fork1PickXPosition,
Fork1PickYPosition = fork1PickYPosition,
Fork1PickZPosition = fork1PickZPosition,
Fork1PutXPosition = fork1PutXPosition,
Fork1PutYPosition = fork1PutYPosition,
Fork1PutZPosition = fork1PutZPosition,
Fork1GoodsBarcode = fork1GoodsBarcode,
MainAlarm = mainAlarm,
DetailAlarms = detailAlarms,
};
//堆垛机货叉1有货状态转换
var fork1SpareProp = srm.EquipmentProps.Where(x => x.Code == "Fork1Spare").FirstOrDefault();
if (fork1SpareProp != null)
{
//伸叉标志为1,缩叉标志为2,其他条件为0
_ = int.TryParse(fork1SpareProp.Value, out var fork1SpareValue);
//取货
if (!fork1Center && fork1TaskType == 2 && fork1SpareValue == 2)
{
stackerDto.Fork1HasPallet = true;
//有货,堆垛机货叉1无条码,则使用WCS给的Fork1条码
if (string.IsNullOrWhiteSpace(stackerDto.Fork1GoodsBarcode))
{
stackerDto.Fork1GoodsBarcode = srm[SRMProps.WCSFork1GoodsBarcode.ToString()].Value;
}
}
//放货
if (fork1TaskType == 3 && fork1SpareValue == 2)
{
//清空条码
stackerDto.Fork1HasPallet = false;
stackerDto.Fork1GoodsBarcode = string.Empty;
}
}
//堆垛机货叉1任务目标位置
if (fork1TaskType == (int)SRMForkAction.Walk || fork1TaskType == (int)SRMForkAction.Get)
{
stackerDto.Fork1NextXPosition = stackerDto.Fork1PickXPosition;
stackerDto.Fork1NextYPosition = stackerDto.Fork1PickYPosition;
stackerDto.Fork1NextZPosition = stackerDto.Fork1PickZPosition;
}
else if (fork1TaskType == (int)SRMForkAction.Put)
{
stackerDto.Fork1NextXPosition = stackerDto.Fork1PutXPosition;
stackerDto.Fork1NextYPosition = stackerDto.Fork1PutYPosition;
stackerDto.Fork1NextZPosition = stackerDto.Fork1PutZPosition;
}
else
{
stackerDto.Fork1NextXPosition = stackerDto.Fork1XPosition;
stackerDto.Fork1NextYPosition = stackerDto.Fork1YPosition;
stackerDto.Fork1NextZPosition = stackerDto.Fork1ZPosition;
}
var doubleForkSRMTypes = new List<EquipmentTypeConst>
{
EquipmentTypeConst.DoubleForkSRM,
EquipmentTypeConst.DoubleForkSRMV2,
}.Select(x => x.ToString()).ToList();
//双叉堆垛机
if (doubleForkSRMTypes.Contains(srm.EquipmentType.Code))
{
_ = int.TryParse(srm[SRMProps.Fork2Distance.ToString()].Value, out var fork2Distance);
_ = int.TryParse(srm[SRMProps.Fork2XPosition.ToString()].Value, out var fork2XPosition);
_ = int.TryParse(srm[SRMProps.Fork2YPosition.ToString()].Value, out var fork2YPosition);
_ = int.TryParse(srm[SRMProps.Fork2ZPosition.ToString()].Value, out var fork2ZPosition);
_ = bool.TryParse(srm[SRMProps.Fork2HasPallet.ToString()].Value, out var fork2HasPallet);
_ = bool.TryParse(srm[SRMProps.Fork2Center.ToString()].Value, out var fork2Center);
_ = bool.TryParse(srm[SRMProps.Fork2Block.ToString()].Value, out var fork2Block);
_ = int.TryParse(srm[SRMProps.Fork2TaskExcuteStatus.ToString()].Value, out var fork2TaskExcuteStatus);
_ = int.TryParse(srm[SRMProps.Fork2TaskType.ToString()].Value, out var fork2TaskType);
_ = int.TryParse(srm[SRMProps.Fork2PickXPosition.ToString()].Value, out var fork2PickXPosition);
_ = int.TryParse(srm[SRMProps.Fork2PickYPosition.ToString()].Value, out var fork2PickYPosition);
_ = int.TryParse(srm[SRMProps.Fork2PickZPosition.ToString()].Value, out var fork2PickZPosition);
_ = int.TryParse(srm[SRMProps.Fork2PutXPosition.ToString()].Value, out var fork2PutXPosition);
_ = int.TryParse(srm[SRMProps.Fork2PutYPosition.ToString()].Value, out var fork2PutYPosition);
_ = int.TryParse(srm[SRMProps.Fork2PutZPosition.ToString()].Value, out var fork2PutZPosition);
_ = int.TryParse(srm[SRMProps.Fork2TaskNo.ToString()].Value, out var fork2TaskNo);
var fork2GoodsBarcode = srm[SRMProps.Fork2GoodsBarcode.ToString()].Value;
stackerDto.Fork2Distance = fork2Distance;
stackerDto.Fork2XPosition = fork2XPosition;
stackerDto.Fork2YPosition = fork2YPosition;
stackerDto.Fork2ZPosition = fork2ZPosition;
stackerDto.Fork2HasPallet = fork2HasPallet;
//数据有延迟,这里手动转换
stackerDto.Fork2Center = fork2Center || fork2Distance == 0;
stackerDto.Fork2Block = fork2Block;
stackerDto.Fork2TaskNo = fork2TaskNo;
stackerDto.Fork2TaskExcuteStatus = fork2TaskExcuteStatus;
stackerDto.Fork2TaskType = fork2TaskType;
stackerDto.Fork2PickXPosition = fork2PickXPosition;
stackerDto.Fork2PickYPosition = fork2PickYPosition;
stackerDto.Fork2PickZPosition = fork2PickZPosition;
stackerDto.Fork2PutXPosition = fork2PutXPosition;
stackerDto.Fork2PutYPosition = fork2PutYPosition;
stackerDto.Fork2PutZPosition = fork2PutZPosition;
stackerDto.Fork2GoodsBarcode = fork2GoodsBarcode;
//堆垛机货叉2任务目标位置
if (fork2TaskType == (int)SRMForkAction.Walk || fork2TaskType == (int)SRMForkAction.Get)
{
stackerDto.Fork2NextXPosition = stackerDto.Fork2PickXPosition;
stackerDto.Fork2NextYPosition = stackerDto.Fork2PickYPosition;
stackerDto.Fork2NextZPosition = stackerDto.Fork2PickZPosition;
}
else if (fork2TaskType == (int)SRMForkAction.Put)
{
stackerDto.Fork2NextXPosition = stackerDto.Fork2PutXPosition;
stackerDto.Fork2NextYPosition = stackerDto.Fork2PutYPosition;
stackerDto.Fork2NextZPosition = stackerDto.Fork2PutZPosition;
}
else
{
stackerDto.Fork2NextXPosition = stackerDto.Fork2XPosition;
stackerDto.Fork2NextYPosition = stackerDto.Fork2YPosition;
stackerDto.Fork2NextZPosition = stackerDto.Fork2ZPosition;
}
//堆垛机货2有货状态转换
var fork2SpareProp = srm.EquipmentProps.Where(x => x.Code == "Fork2Spare").FirstOrDefault();
if (fork2SpareProp != null)
{
//伸叉标志为1,缩叉标志为2,其他条件为0
_ = int.TryParse(fork2SpareProp.Value, out var fork2SpareValue);
if (!fork2Center && fork2TaskType == 2 && fork2SpareValue == 2)
{
//取货
stackerDto.Fork2HasPallet = true;
//有货,堆垛机货叉2无条码,则使用WCS给的Fork2条码
if (string.IsNullOrWhiteSpace(stackerDto.Fork2GoodsBarcode))
{
stackerDto.Fork2GoodsBarcode = srm[SRMProps.WCSFork2GoodsBarcode.ToString()].Value;
}
}
//放货,收叉
if (fork2TaskType == 3 && fork2SpareValue == 2)
{
//清空条码和占位
stackerDto.Fork2HasPallet = false;
stackerDto.Fork2GoodsBarcode = string.Empty;
}
}
}
return stackerDto;
});
var rgvStations = equipments.Where(x => x.EquipmentType.Code == EquipmentTypeConst.RGVStation.ToString()).ToDictionary(x => x.Code, y =>
{
var (mainAlarm, detailAlarms) = _commonService.GetEquipmentAlarmInfo(y);
_ = int.TryParse(y[RGVStationProps.StationRGVPosition.ToString()].Value, out var stationRGVPosition);
_ = int.TryParse(y[RGVStationProps.StationRGVSmall.ToString()].Value, out var stationRGVSmall);
_ = int.TryParse(y[RGVStationProps.StationRGVMax.ToString()].Value, out var stationRGVMax);
_ = int.TryParse(y[RGVStationProps.StationRGVDestination.ToString()].Value, out var stationRGVDestination);
_ = int.TryParse(y[RGVStationProps.StationRGVActualPositionID.ToString()].Value, out var StationRGVActualPositionID);
var rgvStation = new RGVStationDto
{
Status = _commonService.GetEquipmentStatus(y).ToString(),
StationRGVPosition = stationRGVDestination,
StationRGVSmall = stationRGVSmall,
StationRGVMax = stationRGVMax,
StationRGVDestination = stationRGVDestination,
StationRGVActualPositionID = StationRGVActualPositionID,
MainAlarm = mainAlarm,
DetailAlarms = detailAlarms
};
return rgvStation;
});
var stations = equipments.Where(x => x.EquipmentType.Code == EquipmentTypeConst.StationMonitor.ToString()).ToDictionary(x => x.Code, y =>
{
_ = int.TryParse(y[StationMonitorProp.StationDestination.ToString()].Value, out var stationDestination);
var stationMonitorOccupied = y[StationMonitorProp.StationMonitorOccupied.ToString()].Value == "1";
var stationCorotation = false;
var stationReverse = false;
var stationMonitorBarcode = y[StationMonitorProp.StationMonitorBarcode.ToString()].Value;
if (y.EquipmentProps.Exists(x => x.Code == StationMonitorProp.StationCorotation.ToString()))
{
_ = bool.TryParse(y[StationMonitorProp.StationCorotation.ToString()].Value, out stationCorotation);
}
if (y.EquipmentProps.Exists(x => x.Code == StationMonitorProp.StationReverse.ToString()))
{
_ = bool.TryParse(y[StationMonitorProp.StationReverse.ToString()].Value, out stationReverse);
}
foreach (var srm in stackers.Values)
{
if (string.IsNullOrWhiteSpace(stationMonitorBarcode))
{
continue;
}
//同一条码货物,堆垛机货叉1与输送线上只能出现一个
if (srm.Fork1HasPallet && stationMonitorBarcode == srm.Fork1GoodsBarcode)
{
stationMonitorOccupied = false;
stationMonitorBarcode = string.Empty;
}
//同一条码货物,堆垛机货叉2与输送线上只能出现一个
if (srm.Fork2HasPallet && stationMonitorBarcode == srm.Fork2GoodsBarcode)
{
stationMonitorOccupied = false;
stationMonitorBarcode = string.Empty;
}
}
var station = new StationDto
{
EquipmentName = y.Name,
EquipmentTypeCode = y.EquipmentType.Code,
Status = _commonService.GetEquipmentStatus(y).ToString(),
StationMonitorBarcode = stationMonitorBarcode,
StationMonitorOccupied = stationMonitorOccupied,
StationDestination = stationDestination,
StationCorotation = stationCorotation,
StationReverse = stationReverse
};
return station;
});
foreach (var item in rgvStations)
{
var regex = new Regex("[0-9]+");
var rgvCodeNumber = regex.Match(item.Key.Split('-').LastOrDefault()).Value;
var rgvStation = stations.Where(e => e.Key.EndsWith(rgvCodeNumber)).Select(x => x.Value).FirstOrDefault();
if (rgvStation == null)
{
continue;
}
var rgvMonitorBarcode = rgvStation.StationMonitorBarcode;
var rgvStationMonitorOccupied = rgvStation.StationMonitorOccupied;
if (string.IsNullOrWhiteSpace(rgvMonitorBarcode))
{
//条码为空,则不做处理
rgvStation.StationMonitorOccupied = false;
continue;
}
//获取与RGV站台条码一致的输送线
var otherStation = stations.Where(x => x.Value.EquipmentName != rgvStation.EquipmentName && x.Value.StationMonitorBarcode == rgvMonitorBarcode).Select(x => x.Value).FirstOrDefault();
if (otherStation == null)
{
continue;
}
//RGV站台有条码,无占位,是输送线往RGV送货
if (rgvStationMonitorOccupied == false)
{
//清空输送线的条码和占位,数据赋值给RGV
otherStation.StationMonitorBarcode = string.Empty;
otherStation.StationMonitorOccupied = false;
rgvStation.StationMonitorOccupied = true;
continue;
}
// RGV有条码,有占位,且输送线有条码,无占位,是RGV往输送线送货
if (rgvStationMonitorOccupied)
{
//清空RGV的条码和占位,数据赋值给输送线
rgvStation.StationMonitorBarcode = string.Empty;
rgvStation.StationMonitorOccupied = false;
otherStation.StationMonitorOccupied = true;
}
}
var data = new EquipmentInfoResponseV2Dto
{
ProjectCode = projectCode,
FactoryCode = factoryCode,
Stacker = stackers,
RgvStations = rgvStations,
Agv = equipments.Where(x => x.EquipmentType.Code == EquipmentTypeConst.AGVForklift.ToString()).ToDictionary(x => x.Code, y =>
{
_ = int.TryParse(y[AGVForkliftProp.AgvNo.ToString()].Value, out var agvNo);
_ = int.TryParse(y[AGVForkliftProp.Type.ToString()].Value, out var type);
_ = int.TryParse(y[AGVForkliftProp.Speed.ToString()].Value, out var speed);
_ = int.TryParse(y[AGVForkliftProp.CurrentBarcode.ToString()].Value, out var currentBarcode);
_ = int.TryParse(y[AGVForkliftProp.AgvHeight.ToString()].Value, out var agvHeight);
_ = int.TryParse(y[AGVForkliftProp.InStock.ToString()].Value, out var inStock);
_ = int.TryParse(y[AGVForkliftProp.Direction.ToString()].Value, out var direction);
_ = int.TryParse(y[AGVForkliftProp.DialDirection.ToString()].Value, out var dialDirection);
_ = int.TryParse(y[AGVForkliftProp.Status.ToString()].Value, out var agvStatus);
_ = int.TryParse(y[AGVForkliftProp.RadarStatus.ToString()].Value, out var radarStatus);
_ = int.TryParse(y[AGVForkliftProp.Battery.ToString()].Value, out var battery);
_ = int.TryParse(y[AGVForkliftProp.RadarArea.ToString()].Value, out var radarArea);
_ = int.TryParse(y[AGVForkliftProp.IsOnline.ToString()].Value, out var isOnline);
_ = long.TryParse(y[AGVForkliftProp.CoordX.ToString()].Value, out var coordX);
_ = long.TryParse(y[AGVForkliftProp.CoordY.ToString()].Value, out var coordY);
_ = int.TryParse(y[AGVForkliftProp.RollerState.ToString()].Value, out var rollerState);
_ = int.TryParse(y[AGVForkliftProp.AGVAngle.ToString()].Value, out var agvAngle);
_ = int.TryParse(y[AGVForkliftProp.ActionType.ToString()].Value, out var actionType);
var error = y[AGVForkliftProp.Error.ToString()].Value?.Split(';').ToList() ?? new List<string>();
return new AGVDto
{
EquipmentName = y.Name,
EquipmentTypeCode = y.EquipmentType.Code,
Status = _commonService.GetEquipmentStatus(y).ToString(),
MainAlarm = error.FirstOrDefault(),
DetailAlarms = error,
AgvNo = agvNo,
Type = type,
Speed = speed,
CurrentBarcode = currentBarcode,
AgvHeight = agvHeight,
InStock = inStock,
Direction = direction,
DialDirection = dialDirection,
AgvStatus = agvStatus,
RadarStatus = radarStatus,
Battery = battery,
RadarArea = radarArea,
IsOnline = isOnline,
CoordX = coordX,
CoordY = coordY,
RollerState = rollerState,
FrontBarcode = y[AGVForkliftProp.FrontBarcode.ToString()].Value,
AfterBarcode = y[AGVForkliftProp.AfterBarcode.ToString()].Value,
AGVAngle = agvAngle,
ActionType = actionType,
PalletBarcode = y[AGVForkliftProp.PalletBarcode.ToString()].Value,
};
}),
Stations = stations,
WeldRobots = equipments.Where(x => x.EquipmentType.Code == EquipmentTypeConst.WeldRobot.ToString()).ToDictionary(x => x.Code, y =>
{
var weldRobotDto = new WeldRobotDto
{
EquipmentName = y.Name,
EquipmentTypeCode = y.EquipmentType.Code,
Status = _commonService.GetEquipmentStatus(y).ToString(),
};
return weldRobotDto;
}),
Hoists = equipments.Where(x => x.EquipmentType.Code == EquipmentTypeConst.Hoist.ToString()).ToDictionary(x => x.Code, y =>
{
_ = int.TryParse(y[HoistProps.HoistStatus.ToString()].Value, out var hoistStatus);
_ = int.TryParse(y[HoistProps.P1002ActualPositionID.ToString()].Value, out var actualPositionID);
var (mainAlarm, detailAlarms) = _commonService.GetEquipmentAlarmInfo(y);
var hoistDto = new HoistDto
{
EquipmentName = y.Name,
EquipmentTypeCode = y.EquipmentType.Code,
Status = _commonService.GetEquipmentStatus(y).ToString(),
HoistStatus = hoistStatus,
ActualPositionID = actualPositionID,
MainAlarm = mainAlarm,
DetailAlarms = detailAlarms
};
return hoistDto;
})
};
await _cache.SetAsync(lastRequestCacheKey, JsonSerializer.SerializeToUtf8Bytes(data), lastRequestCacheOptions);
return BllResultFactory.Success(data);
}
catch (Exception ex)
{
return BllResultFactory.Error<EquipmentInfoResponseV2Dto>(ex.Message);
}
}
/// <summary>
/// 初始化设备数据
/// </summary>
/// <param name="filter">数据过滤条件</param>
/// <param name="cacheKey">数据过滤条件</param>
/// <param name="equipments">设备数据</param>
/// <param name="equipmentTypes">设备类型</param>
/// <returns></returns>
private void InitialEquipments(ExpressionStarter<Equipment> filter, string cacheKey, out List<Equipment> equipments, out List<EquipmentType> equipmentTypes)
{
var jsonSerializerOptions = new JsonSerializerOptions
{
ReferenceHandler = ReferenceHandler.Preserve,
WriteIndented = true
};
var equipmentCacheKey = $"Equipment_{cacheKey}";
var equipmentTypeCacheKey = $"EquipmentType_{cacheKey}";
//缓存1分钟,这里的缓存时间不能超过设备数据接收接口的缓存时间
var cacheOptions = new DistributedCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromMinutes(1));
var equipmentCacheDataBytes = _cache.Get(equipmentCacheKey);
var equipmentTypeCacheDataBytes = _cache.Get(equipmentTypeCacheKey);
if (equipmentCacheDataBytes != null && equipmentTypeCacheDataBytes != null)
{
equipments = JsonSerializer.Deserialize<List<Equipment>>(Encoding.Default.GetString(equipmentCacheDataBytes), jsonSerializerOptions);
equipmentTypes = JsonSerializer.Deserialize<List<EquipmentType>>(Encoding.Default.GetString(equipmentTypeCacheDataBytes), jsonSerializerOptions);
LoadCacheData(ref equipments);
return;
}
using var equipmentTypeRepository = _freeSql.GetRepository<EquipmentType>();
using var equipmentRepository = _freeSql.GetRepository<Equipment>();
using var equipmentPropRepository = _freeSql.GetRepository<EquipmentProp>();
using var equipmentTypePropTemplateRepository = _freeSql.GetRepository<EquipmentTypePropTemplate>();
equipments = equipmentRepository.Where(filter).WithLock(SqlServerLock.NoLock).ToList(x => new Equipment
{
Id = x.Id,
Code = x.Code,
Name = x.Name,
EquipmentTypeId = x.EquipmentTypeId,
ConnectName = x.ConnectName,
});
var equipmentIds = equipments.Select(x => x.Id).ToList();
var equipmentProps = equipmentPropRepository.Where(x => equipmentIds.Contains(x.EquipmentId)).WithLock(SqlServerLock.NoLock).ToList(x => new EquipmentProp
{
Id = x.Id,
Code = x.Code,
Name = x.Name,
EquipmentId = x.EquipmentId,
Address = x.Address,
Value = x.Value,
Updated = x.Updated,
});
var equipmentTypeIds = equipments.Select(x => x.EquipmentTypeId).Distinct().ToList();
equipmentTypes = equipmentTypeRepository.Where(x => equipmentTypeIds.Contains(x.Id))
.WithLock(SqlServerLock.NoLock).ToList(x => new EquipmentType
{
Id = x.Id,
Code = x.Code,
Name = x.Name,
});
foreach (var item in equipments)
{
item.EquipmentType = equipmentTypes.Find(x => x.Id == item.EquipmentTypeId);
item.EquipmentProps = equipmentProps.Where(x => x.EquipmentId == item.Id).ToList();
foreach (var propItem in item.EquipmentProps)
{
propItem.Equipment = item;
//propItem.EquipmentTypePropTemplate = equipmentTypePropTemplates.Find(x => x.Id == propItem.EquipmentTypePropTemplateId);
}
}
LoadCacheData(ref equipments);
_cache.Set(equipmentCacheKey, JsonSerializer.SerializeToUtf8Bytes(equipments, jsonSerializerOptions), cacheOptions);
_cache.Set(equipmentTypeCacheKey, JsonSerializer.SerializeToUtf8Bytes(equipmentTypes, jsonSerializerOptions), cacheOptions);
}
/// <summary>
/// 加载缓存数据并组装
/// </summary>
/// <param name="equipments">设备数据</param>
private void LoadCacheData(ref List<Equipment> equipments)
{
//从缓存取值
foreach (var item in equipments)
{
var cacheDataBytes = _cache.Get(item.Code);
if (cacheDataBytes == null)
{
continue;
}
var cacheData = JsonSerializer.Deserialize<EquipmentDataDto>(Encoding.Default.GetString(cacheDataBytes));
var cacheDataSourceTime = _commonService.TimestampConvertToLocalDateTime(cacheData.Timestamp);
foreach (var tagItem in cacheData.Reported)
{
var propItem = item.EquipmentProps.Where(x => x.Code == tagItem.Tag || x.Address == tagItem.Tag).FirstOrDefault();
if (propItem != null && cacheDataSourceTime > propItem.Updated)
{
propItem.Value = tagItem.Value;
propItem.Updated = cacheDataSourceTime;
}
}
}
}
}
}