BoardController.cs
46.6 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
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
using DataAcquisition.Common.Enums;
using DataAcquisition.DataAccess;
using DataAcquisition.Models;
using DataAcquisition.Services;
using DataAcquisition.ViewModels.Board;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
namespace DataAcquisition.ApiControllers
{
/// <summary>
/// 看板接口
/// </summary>
[Route("api/[controller]")]
[ApiController]
public class BoardController : ControllerBase
{
private readonly IDbContextFactory<DataContext> _dbContextFactory;
private readonly DataCacheService _dataCacheService;
private readonly IMemoryCache _memoryCache;
private readonly TimeSpan _absoluteExpirationRelativeToNow;
public BoardController(IDbContextFactory<DataContext> dbContextFactory, DataCacheService dataCacheService, IMemoryCache memoryCache)
{
_dbContextFactory = dbContextFactory;
_dataCacheService = dataCacheService;
_memoryCache = memoryCache;
_absoluteExpirationRelativeToNow = TimeSpan.FromSeconds(2);
}
/// <summary>
/// 焊接看板1
/// </summary>
/// <param name="robotNo">机器人编号</param>
/// <remarks>1号厂房</remarks>
/// <returns></returns>
[HttpGet]
[Route("WeldBoard/{robotNo:int}")]
public BoardResult<dynamic> GetWeldBoardData(int robotNo = 1)
{
var result = new BoardResult<dynamic>();
try
{
var key = $"{nameof(GetWeldBoardData)}_{robotNo}";
var cacheData = _memoryCache.Get<BoardResult<dynamic>>(key);
if (cacheData != null)
{
return cacheData;
}
//本月开始时间
var monthStartTime = DateTime.Today.AddDays(1 - DateTime.Today.Day).Date;
using var context = _dbContextFactory.CreateDbContext();
var robotEquipment = robotNo switch
{
1 => _dataCacheService.Equipments.Find(x => x.Code == EquipmentConst.KukaC4_1.ToString())!,
2 => _dataCacheService.Equipments.Find(x => x.Code == EquipmentConst.KukaC4_2.ToString())!,
3 => _dataCacheService.Equipments.Find(x => x.Code == EquipmentConst.KukaC2_1.ToString())!,
4 => _dataCacheService.Equipments.Find(x => x.Code == EquipmentConst.KukaC2_2.ToString())!,
5 => _dataCacheService.Equipments.Find(x => x.Code == EquipmentConst.Efort_1.ToString())!,
6 => _dataCacheService.Equipments.Find(x => x.Code == EquipmentConst.Fanuc_1.ToString())!,
_ => throw new NotImplementedException($"未找到编号为“{robotNo}”的设备数据!"),
};
var plc = robotNo switch
{
1 or 2 => _dataCacheService.Equipments.Find(x => x.Code == EquipmentConst.SiemensPLC_1.ToString())!,
3 => _dataCacheService.Equipments.Find(x => x.Code == EquipmentConst.SiemensPLC_2.ToString())!,
4 => _dataCacheService.Equipments.Find(x => x.Code == EquipmentConst.SiemensPLC_3.ToString())!,
5 => _dataCacheService.Equipments.Find(x => x.Code == EquipmentConst.CoTrust_PLC1.ToString())!,
6 => _dataCacheService.Equipments.Find(x => x.Code == EquipmentConst.SiemensPLC_4.ToString())!,
_ => throw new NotImplementedException($"未找到编号为“{robotNo}”的PLC设备数据!"),
};
_ = double.TryParse(robotEquipment[RobotProps.Pos_X.ToString()].Value, out var pos_X);
_ = double.TryParse(robotEquipment[RobotProps.Pos_Y.ToString()].Value, out var pos_Y);
_ = double.TryParse(robotEquipment[RobotProps.Pos_Z.ToString()].Value, out var pos_Z);
_ = double.TryParse(robotEquipment[RobotProps.Pos_A.ToString()].Value, out var pos_A);
_ = double.TryParse(robotEquipment[RobotProps.Pos_B.ToString()].Value, out var pos_B);
_ = double.TryParse(robotEquipment[RobotProps.Pos_C.ToString()].Value, out var pos_C);
_ = double.TryParse(robotEquipment[RobotProps.Pos_E1.ToString()].Value, out var pos_E1);
_ = double.TryParse(robotEquipment[RobotProps.Pos_E2.ToString()].Value, out var pos_E2);
_ = double.TryParse(robotEquipment[RobotProps.Pos_E3.ToString()].Value, out var pos_E3);
_ = double.TryParse(robotEquipment[RobotProps.Pos_E4.ToString()].Value, out var pos_E4);
_ = bool.TryParse(robotEquipment[RobotProps.Weld_CleanGun.ToString()].Value, out var weld_CleanGun);
_ = bool.TryParse(robotEquipment[RobotProps.WeldFlag.ToString()].Value, out var weldFlag);
_ = bool.TryParse(robotEquipment[RobotProps.Weld_Gas.ToString()].Value, out var weld_Gas);
_ = bool.TryParse(robotEquipment[RobotProps.WeldCompleteFlag.ToString()].Value, out var weldCompleteFlag);
_ = bool.TryParse(robotEquipment[RobotProps.BootFlag.ToString()].Value, out var bootFalgValue);
_ = bool.TryParse(robotEquipment[RobotProps.Alarm.ToString()].Value, out var alarmValue);
_ = bool.TryParse(robotEquipment[RobotProps.WorkFlag.ToString()].Value, out var workFlagValue);
_ = bool.TryParse(robotEquipment[RobotProps.Work_Time.ToString()].Value, out var workTime);
_ = float.TryParse(robotEquipment[RobotProps.Weld_V.ToString()].Value, out var weld_V);
_ = float.TryParse(robotEquipment[RobotProps.Weld_I.ToString()].Value, out var weld_I);
_ = float.TryParse(robotEquipment[RobotProps.Weld_Speed.ToString()].Value, out var weld_Speed);
_ = int.TryParse(robotEquipment[RobotProps.Work_Mode.ToString()].Value, out var work_Mode);
_ = int.TryParse(robotEquipment[RobotProps.Program_No.ToString()].Value, out var program_no);
_ = int.TryParse(robotEquipment[RobotProps.Type.ToString()].Value, out var type);
var equipmentState = "在线";
if (!bootFalgValue)
{
equipmentState = "离线";
}
//仅自动模式显示
else if (alarmValue && work_Mode >= 3)
{
equipmentState = "报警";
}
else if (workFlagValue)
{
equipmentState = "工作";
}
//加工状态
var productStateName = "空闲";
var timeSum = 0d;
if (workTime)
{
productStateName = weldFlag ? "焊接" : "加工";
timeSum = Math.Round(TimeSpan.FromTicks(context.WorkpieceProductions.Where(x => x.EquipmentCode == robotEquipment.Code && x.ProgramNo == type).OrderByDescending(x => x.CreateTime).Select(x => x.UpdateTime.Ticks - x.CreateTime.Ticks).FirstOrDefault()).TotalMinutes, 2);
}
//功率
_ = double.TryParse(plc[RobotPlcProps.P_KW.ToString()].Value, out var kw);
var lastProductionId = context.WorkpieceProductions.Where(x => x.EquipmentCode == robotEquipment.Code).OrderBy(x => x.IsEnd).ThenByDescending(x => x.CreateTime).Select(x => x.Id).FirstOrDefault();
var days = 7;//显示最近N天的数据
var xAxisData = Enumerable.Range(0, days).Select(x => DateTime.Today.AddDays(-x)).OrderBy(x => x).ToList();
var bootFalgTicks = context.EquipmentPropertyRecords.Where(x => x.EquipmentCode == robotEquipment.Code && x.EquipmentPropertyCode == RobotProps.BootFlag.ToString()).Sum(x => x.UpdateTime.Ticks - x.CreateTime.Ticks);
var vm = new Board1VM
{
NowDayCountLeft = new NowDayCountLeft
{
WorkTime = Math.Round(TimeSpan.FromTicks(context.EquipmentPropertyRecords.Where(x => x.EquipmentCode == robotEquipment.Code && x.EquipmentPropertyCode == RobotProps.WorkFlag.ToString() && x.CreateTime >= DateTime.Today).Sum(x => x.UpdateTime.Ticks - x.CreateTime.Ticks)).TotalHours, 2),
ArcingTime = Math.Round(TimeSpan.FromTicks(context.EquipmentPropertyRecords.Where(x => x.EquipmentCode == robotEquipment.Code && x.EquipmentPropertyCode == RobotProps.WeldFlag.ToString() && x.CreateTime >= DateTime.Today).Sum(x => x.UpdateTime.Ticks - x.CreateTime.Ticks)).TotalHours, 2),
FinishCount = context.WorkpieceProductions.Where(x => x.EquipmentCode == robotEquipment.Code && x.CreateTime >= DateTime.Today && x.IsEnd).Count(),
PowerTime = Math.Round(TimeSpan.FromTicks(context.EquipmentPropertyRecords.Where(x => x.EquipmentCode == robotEquipment.Code && x.EquipmentPropertyCode == RobotProps.BootFlag.ToString() && x.CreateTime >= DateTime.Today).Sum(x => x.UpdateTime.Ticks - x.CreateTime.Ticks)).TotalHours, 2)
},
EqCenter = new EquipmentStatusVM
{
EqCode = robotEquipment.Code,
EqName = robotEquipment.Name,
EqState = equipmentState,
Eqmode = work_Mode switch
{
1 => "T1模式",
2 => "T2模式",
3 => "自动模式",
4 => "外部自动模式",
_ => "无数据"
},
MonthDaoDian = context.WorkpieceProductions.Where(x => x.EquipmentCode == robotEquipment.Code && x.CreateTime >= monthStartTime && x.IsEnd).Count(),
MonthWeld = Math.Round(context.EquipmentPropertyRecords.Where(x => x.EquipmentCode == robotEquipment.Code && x.EquipmentPropertyCode == RobotProps.Weld_Speed.ToString() && x.CreateTime >= monthStartTime).Select(x => x.Value).AsEnumerable().Sum(Convert.ToSingle), 2),
MonthVoltage = Math.Round(context.EquipmentPropertyRecords.Where(x => x.EquipmentCode == plc.Code && x.EquipmentPropertyCode == RobotPlcProps.Electricity.ToString() && x.CreateTime >= monthStartTime).Select(x => x.Value).AsEnumerable().Sum(Convert.ToSingle), 2),
NowDayVoltage = Math.Round(context.EquipmentPropertyRecords.Where(x => x.EquipmentCode == plc.Code && x.EquipmentPropertyCode == RobotPlcProps.Electricity.ToString() && x.CreateTime >= DateTime.Today).Select(x => x.Value).AsEnumerable().Sum(Convert.ToSingle), 2),
MonthWeldVoltage = Math.Round(context.EquipmentPropertyRecords.Where(x => x.EquipmentCode == robotEquipment.Code && x.EquipmentPropertyCode == $"{RobotProps.WeldFlag}_{RobotPlcProps.Electricity}" && x.CreateTime >= monthStartTime).Select(x => x
.Value).AsEnumerable().Sum(Convert.ToSingle), 2),
TodayWeldVoltage = Math.Round(context.EquipmentPropertyRecords.Where(x => x.EquipmentCode == robotEquipment.Code && x.EquipmentPropertyCode == $"{RobotProps.WeldFlag}_{RobotPlcProps.Electricity}" && x.CreateTime >= DateTime.Today).Select(x => x
.Value).AsEnumerable().Sum(Convert.ToSingle), 2),
MonthGas = $"{context.EquipmentPropertyRecords.Where(x => x.EquipmentCode == robotEquipment.Code && x.EquipmentPropertyCode == RobotProps.Gas_Flow.ToString() && x.CreateTime >= monthStartTime).Select(x => x.Value).AsEnumerable().Sum(Convert.ToSingle):N2}/L",
P_KW = kw,
},
MonthCountRight = new MonthCountRight
{
WorkTime = Math.Round(TimeSpan.FromTicks(context.EquipmentPropertyRecords.Where(x => x.EquipmentCode == robotEquipment.Code && x.EquipmentPropertyCode == RobotProps.WorkFlag.ToString() && x.CreateTime >= monthStartTime).Sum(x => x.UpdateTime.Ticks - x.CreateTime.Ticks)).TotalHours, 2),
ArcingTime = Math.Round(TimeSpan.FromTicks(context.EquipmentPropertyRecords.Where(x => x.EquipmentCode == robotEquipment.Code && x.EquipmentPropertyCode == RobotProps.WeldFlag.ToString() && x.CreateTime >= monthStartTime).Sum(x => x.UpdateTime.Ticks - x.CreateTime.Ticks)).TotalHours, 2),
PowerTime = Math.Round(TimeSpan.FromTicks(context.EquipmentPropertyRecords.Where(x => x.EquipmentCode == robotEquipment.Code && x.EquipmentPropertyCode == RobotProps.BootFlag.ToString() && x.CreateTime >= monthStartTime).Sum(x => x.UpdateTime.Ticks - x.CreateTime.Ticks)).TotalHours, 2),
AlarmTime = Math.Round(TimeSpan.FromTicks(context.EquipmentPropertyRecords.Where(x => x.EquipmentCode == robotEquipment.Code && x.EquipmentPropertyCode == RobotProps.Alarm.ToString() && x.CreateTime >= monthStartTime).Sum(x => x.UpdateTime.Ticks - x.CreateTime.Ticks)).TotalHours, 2),
},
EqInfoLeft = new EquipmentInfoLeftVM
{
ClearSpearState = weld_CleanGun,
ArcingState = weldFlag,
AspiratedState = weld_Gas,
NowProject = type == 0 ? "无" : $"程序{type}",
ProjectRow = program_no,
X = Math.Round(pos_X, 1),
Y = Math.Round(pos_Y, 1),
Z = Math.Round(pos_Z, 1),
A = Math.Round(pos_A, 1),
B = Math.Round(pos_B, 1),
C = Math.Round(pos_C, 1),
E1 = Math.Round(pos_E1, 1),
E2 = Math.Round(pos_E2, 1),
E3 = Math.Round(pos_E3, 1),
E4 = Math.Round(pos_E4, 1),
SsSpeed = Math.Round(weld_Speed, 1),
Current = Math.Round(weld_I, 1),
Voltage = GetVoltageVal(weld_I, weld_V, robotEquipment),
},
EqInfoRight = new EquipmentInfoRight
{
EfficiencyList = new Efficiency
{
Arcing = xAxisData.Select(x => Convert.ToInt32(TimeSpan.FromTicks(context.EquipmentPropertyRecords.Where(r => r.EquipmentCode == robotEquipment.Code && r.EquipmentPropertyCode == RobotProps.WeldFlag.ToString() && r.CreateTime.Date == x).Sum(r => r.UpdateTime.Ticks - r.CreateTime.Ticks)).TotalMinutes)).ToList(),
PowerOn = xAxisData.Select(x => Convert.ToInt32(TimeSpan.FromTicks(context.EquipmentPropertyRecords.Where(r => r.EquipmentCode == robotEquipment.Code && r.EquipmentPropertyCode == RobotProps.BootFlag.ToString() && r.CreateTime.Date == x).Sum(r => r.UpdateTime.Ticks - r.CreateTime.Ticks)).TotalMinutes)).ToList(),
Work = xAxisData.Select(x => Convert.ToInt32(TimeSpan.FromTicks(context.EquipmentPropertyRecords.Where(r => r.EquipmentCode == robotEquipment.Code && r.EquipmentPropertyCode == RobotProps.Work_Time.ToString() && r.CreateTime.Date == x).Sum(r => r.UpdateTime.Ticks - r.CreateTime.Ticks)).TotalMinutes)).ToList(),
XAxisData = xAxisData.Select(x => x.ToString("M")).ToList(),
},
WeldFlagData = xAxisData.Select(dateTime => new BoardChart
{
Name = dateTime.ToString("M"),
Value = GetWeldFlagRate(context, robotEquipment, dateTime),
WorkTime = Math.Round(TimeSpan.FromTicks(context.EquipmentPropertyRecords.Where(x => x.EquipmentCode == robotEquipment.Code && x.EquipmentPropertyCode == RobotProps.WorkFlag.ToString() && x.CreateTime.Date == dateTime.Date).Sum(x => x.UpdateTime.Ticks - x.CreateTime.Ticks)).TotalHours, 2),
ArcingTime = Math.Round(TimeSpan.FromTicks(context.EquipmentPropertyRecords.Where(x => x.EquipmentCode == robotEquipment.Code && x.EquipmentPropertyCode == RobotProps.WeldFlag.ToString() && x.CreateTime.Date == dateTime.Date).Sum(x => x.UpdateTime.Ticks - x.CreateTime.Ticks)).TotalHours, 2),
}).ToList(),
ProductInfo = robotNo switch
{
1 => "立柱",
2 => "固定座",
3 => "立柱",
4 => "横梁",
_ => type.ToString(),
},
ProductState = productStateName,
TimeSum = timeSum,
YieldList = new YieldListVM
{
XAxisData = xAxisData.Select(x => x.ToString("M")).ToList(),
Yield = xAxisData.Select(x => context.WorkpieceProductions.Where(w => w.EquipmentCode == robotEquipment.Code && w.CreateTime.Date == x.Date && w.IsEnd).Count()).ToList(),
}
},
CurrentList = context.WeldProcessRecords.Where(x => x.ProductionId == lastProductionId && x.Code == RobotProps.Weld_I.ToString()).OrderBy(x => x.CreateTime).Select(x => x.Value).AsEnumerable().Select(x =>
{
_ = double.TryParse(x, out var val);
return Math.Round(val, 1);
}).ToList(),
EqEfficiencyCenter = new EquipmentEfficiency
{
AlarmRate = GetAlarmRate(context, robotEquipment),
AutoRate = GetAutoRate(context, robotEquipment),
OnLineRate = GetOnLineRate(context, robotEquipment),
UtilizeRate = UtilizeRate(context, robotEquipment),
},
VoltagetList = context.WeldProcessRecords.Where(x => x.ProductionId == lastProductionId && x.Code == RobotProps.Weld_V.ToString()).OrderBy(x => x.CreateTime).Select(x => x.Value).AsEnumerable().Select(x =>
{
_ = double.TryParse(x, out var val);
return Math.Round(val, 1);
}).ToList(),
};
//获取电压值
double GetVoltageVal(float weld_I, float weld_V, Equipment robot)
{
//Kuka C2设备数值有误差,电流值为0时,电压应显示0
var codes = new List<EquipmentConst>
{
EquipmentConst.KukaC2_1,
EquipmentConst.KukaC2_2,
}.Select(x => x.ToString()).ToList();
if (codes.Contains(robot.Code) && weld_I <= 0)
{
return 0;
}
return Math.Round(weld_V, 1);
}
//故障率
int GetAlarmRate(DataContext context, Equipment robotEquipment)
{
if (bootFalgTicks == 0) return 0;
return Convert.ToInt32(context.EquipmentPropertyRecords.Where(x => x.EquipmentCode == robotEquipment.Code && x.EquipmentPropertyCode == RobotProps.Alarm.ToString()).Sum(x => x.UpdateTime.Ticks - x.CreateTime.Ticks) * 100 / bootFalgTicks);
}
int UtilizeRate(DataContext context, Equipment robotEquipment)
{
if (bootFalgTicks == 0) return 0;
return Convert.ToInt32(context.EquipmentPropertyRecords.Where(x => x.EquipmentCode == robotEquipment.Code && x.EquipmentPropertyCode == RobotProps.WorkFlag.ToString()).Sum(x => x.UpdateTime.Ticks - x.CreateTime.Ticks) * 100 / bootFalgTicks);
}
int GetAutoRate(DataContext context, Equipment robotEquipment)
{
if (bootFalgTicks == 0) return 0;
return Convert.ToInt32(context.EquipmentPropertyRecords.Where(x => x.EquipmentCode == robotEquipment.Code && x.EquipmentPropertyCode == RobotProps.WeldFlag.ToString()).Sum(x => x.UpdateTime.Ticks - x.CreateTime.Ticks) * 100 / bootFalgTicks);
}
//在线率
int GetOnLineRate(DataContext context, Equipment robotEquipment)
{
var bootFlagStartDay = context.EquipmentPropertyRecords.Where(x => x.EquipmentCode == robotEquipment.Code && x.EquipmentPropertyCode == RobotProps.BootFlag.ToString()).OrderBy(x => x.CreateTime).Select(x => x.CreateTime).FirstOrDefault();
if (bootFlagStartDay == default) return 0;
if (bootFlagStartDay <= DateTime.Today.AddMonths(-1))
{
bootFlagStartDay = DateTime.Today.AddMonths(-1);
}
return Convert.ToInt32(context.EquipmentPropertyRecords.Where(x => x.EquipmentCode == robotEquipment.Code && x.EquipmentPropertyCode == RobotProps.BootFlag.ToString()).Sum(x => x.UpdateTime.Ticks - x.CreateTime.Ticks) * 100 / (DateTime.Now - bootFlagStartDay).Ticks);
}
result.Data = new
{
Head = new
{
vm.NowDayCountLeft,
vm.EqCenter,
vm.MonthCountRight,
},
Center = new
{
vm.EqInfoLeft,
vm.EqInfoRight,
},
Footer = new
{
vm.CurrentList,
vm.EqEfficiencyCenter,
vm.VoltagetList,
}
};
_memoryCache.Set(key, result, _absoluteExpirationRelativeToNow);
}
catch (Exception ex)
{
result.Code = 500;
result.Message = ex.Message;
}
return result;
}
/// <summary>
/// 焊接看板2
/// </summary>
/// <remarks>2号厂房</remarks>
/// <returns></returns>
[HttpGet]
[Route("WeldBoard")]
public BoardResult<Board2VM> GetWeldBoardData()
{
try
{
var key = $"{nameof(GetWeldBoardData)}";
var cacheData = _memoryCache.Get<BoardResult<Board2VM>>(key);
if (cacheData != null)
{
return cacheData;
}
using var context = _dbContextFactory.CreateDbContext();
//本月开始时间
var monthStartTime = DateTime.Today.AddDays(1 - DateTime.Today.Day).Date;
var days = 7;//显示最近N天的数据
var xAxisData = Enumerable.Range(0, days).Select(x => DateTime.Today.AddDays(-x)).OrderBy(x => x).ToList();
var vm = new Board2VM
{
Robot1 = GetRobotInfo(EquipmentConst.KukaC4_1),
Robot2 = GetRobotInfo(EquipmentConst.KukaC4_2),
Robot3 = GetRobotInfo(EquipmentConst.KukaC2_1),
Robot4 = GetRobotInfo(EquipmentConst.KukaC2_2),
Robot5 = GetRobotInfo(EquipmentConst.Efort_1),
Robot6 = GetRobotInfo(EquipmentConst.Fanuc_1),
};
var result = new BoardResult<Board2VM>
{
Code = 200,
Data = vm,
Message = "成功"
};
_memoryCache.Set(key, result, _absoluteExpirationRelativeToNow);
return result;
RobotDto GetRobotInfo(EquipmentConst equipmentCode)
{
var robot = context.Equipments.Where(x => x.Code == equipmentCode.ToString()).FirstOrDefault();
if (robot == null)
{
return new RobotDto();
}
var result = new RobotDto
{
RobotCode = robot.Code,
RobotName = robot.Name,
CompleteCount = context.WorkpieceProductions.Where(x => x.EquipmentCode == robot.Code && x.CreateTime >= monthStartTime).Count(),
WorkTime = Math.Round(TimeSpan.FromTicks(context.EquipmentPropertyRecords.Where(x => x.EquipmentCode == robot.Code && x.EquipmentPropertyCode == RobotProps.WorkFlag.ToString() && x.CreateTime >= monthStartTime).Sum(x => x.UpdateTime.Ticks - x.CreateTime.Ticks)).TotalHours, 2),
MonthWeld = Math.Round(context.EquipmentPropertyRecords.Where(x => x.EquipmentCode == robot.Code && x.EquipmentPropertyCode == RobotProps.Weld_Speed.ToString() && x.CreateTime >= monthStartTime).Select(x => x.Value).AsEnumerable().Sum(Convert.ToSingle)),
Charts = xAxisData.Select(dateTime => new BoardChart
{
Name = dateTime.ToString("M"),
Value = GetWeldFlagRate(context, robot, dateTime),
WorkTime = Math.Round(TimeSpan.FromTicks(context.EquipmentPropertyRecords.Where(x => x.EquipmentCode == robot.Code && x.EquipmentPropertyCode == RobotProps.WorkFlag.ToString() && x.CreateTime.Date == dateTime.Date).Sum(x => x.UpdateTime.Ticks - x.CreateTime.Ticks)).TotalHours, 2),
ArcingTime = Math.Round(TimeSpan.FromTicks(context.EquipmentPropertyRecords.Where(x => x.EquipmentCode == robot.Code && x.EquipmentPropertyCode == RobotProps.WeldFlag.ToString() && x.CreateTime.Date == dateTime.Date).Sum(x => x.UpdateTime.Ticks - x.CreateTime.Ticks)).TotalHours, 2),
}).ToList()
};
return result;
}
}
catch (Exception ex)
{
return new BoardResult<Board2VM>
{
Code = 500,
Message = ex.Message
};
}
}
/// <summary>
/// 焊接总看板
/// </summary>
/// <remarks>1号厂房</remarks>
/// <returns></returns>
[HttpGet]
[Route("MainWeldBoard")]
public BoardResult<Board3VM> GetMainWeldBoard()
{
var result = new BoardResult<Board3VM>();
try
{
var key = $"{nameof(GetMainWeldBoard)}";
var cacheData = _memoryCache.Get<BoardResult<Board3VM>>(key);
if (cacheData != null)
{
return cacheData;
}
var monthStartTime = DateTime.Today.AddDays(1 - DateTime.Today.Day).Date;
using var context = _dbContextFactory.CreateDbContext();
var robotCodes = new List<EquipmentConst>
{
EquipmentConst.KukaC4_1,
EquipmentConst.KukaC4_2,
EquipmentConst.KukaC2_1,
EquipmentConst.KukaC2_2,
EquipmentConst.Efort_1,
EquipmentConst.Fanuc_1,
}.Select(x => x.ToString()).ToList();
var days = 7;//显示最近N天的数据
var xAxisData = Enumerable.Range(0, days).Select(x => DateTime.Today.AddDays(-x)).OrderBy(x => x).ToList();
var robots = _dataCacheService.Equipments.Where(x => robotCodes.Contains(x.Code)).OrderBy(x => x.Code).ToList();
var totalTicks = (DateTime.Now - DateTime.Today).Ticks;
var vm = new Board3VM
{
RobotStateMonitor = robots.Select(robot =>
{
_ = bool.TryParse(robot[RobotProps.BootFlag.ToString()].Value, out var bootFalgValue);
_ = bool.TryParse(robot[RobotProps.Alarm.ToString()].Value, out var alarmValue);
_ = bool.TryParse(robot[RobotProps.WorkFlag.ToString()].Value, out var workFlagValue);
_ = int.TryParse(robot[RobotProps.Work_Mode.ToString()].Value, out var work_Mode);
var state = 2;//待机
if (!bootFalgValue)
{
state = 0;//离线
}
else if (workFlagValue)
{
state = 1;//工作
}
//仅自动模式显示
else if (alarmValue && work_Mode >= 3)
{
state = 3;//报警
}
var startTime = DateTime.Now.AddDays(-6).Date;
var data = new RobotState
{
Code = robot.Code,
Name = robot.Name,
State = state,
StateName = state switch
{
1 => "运行",
2 => "待机",
3 => "报警",
_ => "离线",
},
PowerTime = Math.Round(TimeSpan.FromTicks(context.EquipmentPropertyRecords.Where(x => x.EquipmentCode == robot.Code && x.EquipmentPropertyCode == RobotProps.BootFlag.ToString() && x.CreateTime >= startTime).Sum(x => x.UpdateTime.Ticks - x.CreateTime.Ticks)).TotalHours, 2),
WorkTime = Math.Round(TimeSpan.FromTicks(context.EquipmentPropertyRecords.Where(x => x.EquipmentCode == robot.Code && x.EquipmentPropertyCode == RobotProps.WorkFlag.ToString() && x.CreateTime >= startTime).Sum(x => x.UpdateTime.Ticks - x.CreateTime.Ticks)).TotalHours, 2),
ArcingTime = Math.Round(TimeSpan.FromTicks(context.EquipmentPropertyRecords.Where(x => x.EquipmentCode == robot.Code && x.EquipmentPropertyCode == RobotProps.WeldFlag.ToString() && x.CreateTime >= startTime).Sum(x => x.UpdateTime.Ticks - x.CreateTime.Ticks)).TotalHours, 2)
};
return data;
}).ToList(),
RobotConditions = robots.Select(x =>
{
_ = int.TryParse(x[RobotProps.Work_Mode.ToString()].Value, out var mode);
_ = bool.TryParse(x[RobotProps.Weld_CleanGun.ToString()].Value, out var weld_CleanGun);
_ = float.TryParse(x[RobotProps.Weld_I.ToString()].Value, out var weld_I);
_ = float.TryParse(x[RobotProps.Weld_V.ToString()].Value, out var weld_V);
_ = float.TryParse(x[RobotProps.Weld_Speed.ToString()].Value, out var weld_Speed);
var data = new RobotCondition
{
EquipmentCode = x.Code,
Weld_CleanGun = weld_CleanGun,
Weld_I = Math.Round(weld_I, 1),
Weld_V = weld_I == 0 ? 0 : Math.Round(weld_V, 1),
Weld_Speed = Math.Round(weld_Speed, 1),
WorkMode = mode switch
{
1 => "T1模式",
2 => "T2模式",
3 => "自动模式",
4 => "外部自动模式",
_ => "无数据",
}
};
return data;
}).ToList(),
RobotStateStatistics = robots.Select(robot =>
{
var bootFlagTicks = context.EquipmentPropertyRecords.Where(x => x.EquipmentCode == robot.Code && x.EquipmentPropertyCode == RobotProps.BootFlag.ToString() && x.CreateTime >= DateTime.Today).Sum(x => x.UpdateTime.Ticks - x.CreateTime.Ticks);
var run = 0d;
var free = 0d;
var alarm = 0d;
var offLine = 0d;
if (totalTicks == 0 || bootFlagTicks == 0)
{
offLine = 100;
}
else
{
var workFlagRecords = context.EquipmentPropertyRecords.Where(x => x.EquipmentCode == robot.Code && x.EquipmentPropertyCode == RobotProps.WorkFlag.ToString() && x.CreateTime >= DateTime.Today).Select(x => new
{
x.CreateTime,
x.UpdateTime,
}).ToList();
var alarmFlagRecords = context.EquipmentPropertyRecords.Where(x => x.EquipmentCode == robot.Code && x.EquipmentPropertyCode == RobotProps.Alarm.ToString() && x.CreateTime >= DateTime.Today).Select(x => new
{
x.CreateTime,
x.UpdateTime,
}).ToList();
var workFlagTicks = workFlagRecords.Sum(workRecord =>
{
var alarmRecord = alarmFlagRecords.Where(x => x.CreateTime >= workRecord.CreateTime && x.CreateTime <= workRecord.UpdateTime).FirstOrDefault();
if (alarmRecord == null)
{
return (workRecord.UpdateTime - workRecord.CreateTime).Ticks;
}
if (alarmRecord.UpdateTime > workRecord.UpdateTime)
{
return (alarmRecord.CreateTime - workRecord.CreateTime).Ticks;
}
return (workRecord.UpdateTime - workRecord.CreateTime).Ticks - (alarmRecord.UpdateTime - alarmRecord.CreateTime).Ticks;
});
var alarmFlagTicks = alarmFlagRecords.Sum(x => x.UpdateTime.Ticks - x.CreateTime.Ticks);
var freeTicks = bootFlagTicks - workFlagTicks - alarmFlagTicks;
var offLineTicks = totalTicks - bootFlagTicks;
run = Math.Round(workFlagTicks * 100d / totalTicks, 1);
alarm = Math.Round(alarmFlagTicks * 100d / totalTicks, 1);
free = Math.Round(freeTicks * 100d / totalTicks, 1);
offLine = Math.Round(offLineTicks * 100d / totalTicks, 1);
}
var data = new ChartBar
{
Code = robot.Code,
Name = robot.Name,
Run = run,
Free = free,
Alarm = alarm,
OffLine = offLine,
};
return data;
}).ToList(),
ProcessingStatistics = new ProcessingStatistics
{
XAxisDate = xAxisData.Select(x => x.ToString("M")).ToList(),
ChartLines = robots.Select(robot => new ChartLine
{
Code = robot.Code,
Name = robot.Name,
Values = xAxisData.Select(date => context.WorkpieceProductions.Where(x => x.EquipmentCode == robot.Code && x.CreateTime.Date == date && x.IsEnd).Count()).ToList()
}).ToList()
},
RobotStatistics = new RobotStatistics
{
TodayUtilizationRate = GetTodayUtilizationRate(),
TodayArcingRate = GetTodayArcingRate(),
TodayGasConsumption = Math.Round(context.EquipmentPropertyRecords.Where(x => x.EquipmentPropertyCode == RobotProps.Gas_Flow.ToString() && x.CreateTime >= DateTime.Today).Select(x => x.Value).AsEnumerable().Sum(Convert.ToSingle), 2),
MonthGasConsumption = Math.Round(context.EquipmentPropertyRecords.Where(x => x.EquipmentPropertyCode == RobotProps.Gas_Flow.ToString() && x.CreateTime >= monthStartTime).Select(x => x.Value).AsEnumerable().Sum(Convert.ToSingle), 2),
TodayWireConsumption = Math.Round(context.EquipmentPropertyRecords.Where(x => x.EquipmentPropertyCode == RobotProps.Weld_Speed.ToString() && x.CreateTime >= DateTime.Today).Select(x => x.Value).AsEnumerable().Sum(Convert.ToSingle), 2),
MonthWireConsumption = Math.Round(context.EquipmentPropertyRecords.Where(x => x.EquipmentPropertyCode == RobotProps.Weld_Speed.ToString() && x.CreateTime >= monthStartTime).Select(x => x.Value).AsEnumerable().Sum(Convert.ToSingle), 2),
TodayProductionCapacity = context.WorkpieceProductions.Where(x => x.CreateTime >= DateTime.Today && x.IsEnd).Count(),
MonthProductionCapacity = context.WorkpieceProductions.Where(x => x.CreateTime >= monthStartTime && x.IsEnd).Count(),
TodayPowerConsumption = Math.Round(context.EquipmentPropertyRecords.Where(x => x.EquipmentPropertyCode == RobotPlcProps.Electricity.ToString() && x.CreateTime >= DateTime.Today).Select(x => x.Value).AsEnumerable().Sum(Convert.ToSingle), 2),
MonthPowerConsumption = Math.Round(context.EquipmentPropertyRecords.Where(x => x.EquipmentPropertyCode == RobotPlcProps.Electricity.ToString() && x.CreateTime >= monthStartTime).Select(x => x.Value).AsEnumerable().Sum(Convert.ToSingle), 2),
TodayWeldPowerConsumption = Math.Round(context.EquipmentPropertyRecords.Where(x => x.EquipmentPropertyCode == $"{RobotProps.WeldFlag}_{RobotPlcProps.Electricity}" && x.CreateTime >= DateTime.Today).Select(x => x
.Value).AsEnumerable().Sum(Convert.ToSingle), 2),
MonthWeldPowerConsumption = Math.Round(context.EquipmentPropertyRecords.Where(x => x.EquipmentPropertyCode == $"{RobotProps.WeldFlag}_{RobotPlcProps.Electricity}" && x.CreateTime >= monthStartTime).Select(x => x
.Value).AsEnumerable().Sum(Convert.ToSingle), 2),
}
};
result.Data = vm;
_memoryCache.Set(key, result, _absoluteExpirationRelativeToNow);
return result;
//获取当天利用率
double GetTodayUtilizationRate()
{
var bootFalgTicks = context.EquipmentPropertyRecords.Where(x => x.EquipmentPropertyCode == RobotProps.BootFlag.ToString()).Sum(x => x.UpdateTime.Ticks - x.CreateTime.Ticks);
if (bootFalgTicks == 0) return 0;
return Convert.ToInt32(context.EquipmentPropertyRecords.Where(x => x.EquipmentPropertyCode == RobotProps.WorkFlag.ToString()).Sum(x => x.UpdateTime.Ticks - x.CreateTime.Ticks) * 100 / bootFalgTicks);
}
//获取当天燃弧率
double GetTodayArcingRate()
{
var runTicks = context.EquipmentPropertyRecords.Where(p => p.EquipmentPropertyCode == RobotProps.BootFlag.ToString() && p.CreateTime.Date == DateTime.Today.Date).Sum(s => s.UpdateTime.Ticks - s.CreateTime.Ticks);
if (runTicks == 0) return 0;
var weldTicks = context.EquipmentPropertyRecords.Where(p => p.EquipmentPropertyCode == RobotProps.WeldFlag.ToString() && p.CreateTime.Date == DateTime.Today.Date).Sum(s => s.UpdateTime.Ticks - s.CreateTime.Ticks);
return Convert.ToInt32(weldTicks * 100 / runTicks);
}
}
catch (Exception ex)
{
result.Code = 500;
result.Message = ex.Message;
}
return result;
}
/// <summary>
/// 焊接机器人数字孪生看板
/// </summary>
/// <remarks>机器人实时数据</remarks>
/// <returns></returns>
[HttpGet]
[Route("RobotMonitorData")]
public BoardResult<DigitalTwinVM1> GetRobotMonitorData()
{
var result = new BoardResult<DigitalTwinVM1>();
try
{
var key = $"{nameof(GetRobotMonitorData)}";
var cacheData = _memoryCache.Get<BoardResult<DigitalTwinVM1>>(key);
if (cacheData != null)
{
return cacheData;
}
using var context = _dbContextFactory.CreateDbContext();
var robot1 = _dataCacheService.Equipments.Find(x => x.Code == EquipmentConst.KukaC4_1.ToString())!;
var robot2 = _dataCacheService.Equipments.Find(x => x.Code == EquipmentConst.KukaC4_2.ToString())!;
var vm = new DigitalTwinVM1
{
RobotMonitor1 = GetRobotMonitor(robot1),
RobotMonitor2 = GetRobotMonitor(robot2),
};
result.Data = vm;
_memoryCache.Set(key, result, _absoluteExpirationRelativeToNow);
}
catch (Exception ex)
{
result.Code = 500;
result.Message = ex.Message;
}
return result;
}
private static RobotMonitor GetRobotMonitor(Equipment robotEquipment)
{
_ = double.TryParse(robotEquipment[RobotProps.Pos_X.ToString()].Value, out var pos_X);
_ = double.TryParse(robotEquipment[RobotProps.Pos_Y.ToString()].Value, out var pos_Y);
_ = double.TryParse(robotEquipment[RobotProps.Pos_Z.ToString()].Value, out var pos_Z);
_ = double.TryParse(robotEquipment[RobotProps.Pos_A.ToString()].Value, out var pos_A);
_ = double.TryParse(robotEquipment[RobotProps.Pos_B.ToString()].Value, out var pos_B);
_ = double.TryParse(robotEquipment[RobotProps.Pos_C.ToString()].Value, out var pos_C);
_ = double.TryParse(robotEquipment[RobotProps.Pos_E1.ToString()].Value, out var pos_E1);
_ = double.TryParse(robotEquipment[RobotProps.Pos_E2.ToString()].Value, out var pos_E2);
_ = double.TryParse(robotEquipment[RobotProps.Pos_E3.ToString()].Value, out var pos_E3);
_ = double.TryParse(robotEquipment[RobotProps.Pos_E4.ToString()].Value, out var pos_E4);
_ = bool.TryParse(robotEquipment[RobotProps.Weld_CleanGun.ToString()].Value, out var weld_CleanGun);
_ = bool.TryParse(robotEquipment[RobotProps.WeldFlag.ToString()].Value, out var weldFlag);
_ = bool.TryParse(robotEquipment[RobotProps.Weld_Gas.ToString()].Value, out var weld_Gas);
_ = bool.TryParse(robotEquipment[RobotProps.WeldCompleteFlag.ToString()].Value, out var weldCompleteFlag);
_ = bool.TryParse(robotEquipment[RobotProps.BootFlag.ToString()].Value, out var bootFalgValue);
_ = bool.TryParse(robotEquipment[RobotProps.Alarm.ToString()].Value, out var alarmValue);
_ = bool.TryParse(robotEquipment[RobotProps.WorkFlag.ToString()].Value, out var workFlagValue);
//_ = bool.TryParse(robotEquipment[RobotProps.Work_Time.ToString()].Value, out var workTime);
_ = float.TryParse(robotEquipment[RobotProps.Weld_V.ToString()].Value, out var weld_V);
_ = float.TryParse(robotEquipment[RobotProps.Weld_I.ToString()].Value, out var weld_I);
_ = float.TryParse(robotEquipment[RobotProps.Weld_Speed.ToString()].Value, out var weld_Speed);
_ = int.TryParse(robotEquipment[RobotProps.Work_Mode.ToString()].Value, out var work_Mode);
//_ = int.TryParse(robotEquipment[RobotProps.Program_No.ToString()].Value, out var program_no);
//_ = int.TryParse(robotEquipment[RobotProps.Type.ToString()].Value, out var type);
var equipmentState = "在线";
if (!bootFalgValue)
{
equipmentState = "离线";
}
//仅自动模式显示
else if (alarmValue && work_Mode >= 3)
{
equipmentState = "报警";
}
else if (workFlagValue)
{
equipmentState = "工作";
}
var result = new RobotMonitor
{
EquipmentCode = robotEquipment.Code,
EquipmentName = robotEquipment.Name,
Status = equipmentState,
Weld_V = Math.Round(weld_V, 1),
Weld_I = Math.Round(weld_I, 1),
Weld_Speed = Math.Round(weld_Speed, 1),
Work_Mode = work_Mode switch
{
1 => "T1模式",
2 => "T2模式",
3 => "自动模式",
4 => "外部自动模式",
_ => "无数据",
},
A1 = Math.Round(pos_X, 1),
A2 = Math.Round(pos_Y, 1),
A3 = Math.Round(pos_Z, 1),
A4 = Math.Round(pos_A, 1),
A5 = Math.Round(pos_B, 1),
A6 = Math.Round(pos_C, 1),
E1 = Math.Round(pos_E1 * 100 / 8200, 1),
E2 = Math.Round(pos_E2, 1),
E3 = Math.Round(pos_E3, 1),
E4 = Math.Round(pos_E4, 1),
BootFlag = bootFalgValue,
WorkFlag = workFlagValue,
WeldFlag = weldFlag,
WeldCompleteFlag = weldCompleteFlag,
Weld_Gas = weld_Gas,
Weld_CleanGun = weld_CleanGun,
Alarm = alarmValue,
RedLight = false,
GreenLight = false,
YellowLight = false,
//Work_Time = workTime,
};
return result;
}
//燃弧率
private static int GetWeldFlagRate(DataContext context, Equipment robotEquipment, DateTime date)
{
var runTicks = context.EquipmentPropertyRecords.Where(p => p.EquipmentCode == robotEquipment.Code && p.EquipmentPropertyCode == RobotProps.BootFlag.ToString() && p.CreateTime.Date == date).Sum(s => s.UpdateTime.Ticks - s.CreateTime.Ticks);
if (runTicks == 0) return 0;
var weldTicks = context.EquipmentPropertyRecords.Where(p => p.EquipmentCode == robotEquipment.Code && p.EquipmentPropertyCode == RobotProps.WeldFlag.ToString() && p.CreateTime.Date == date).Sum(s => s.UpdateTime.Ticks - s.CreateTime.Ticks);
return Convert.ToInt32(weldTicks * 100 / runTicks);
}
}
}