RobotDataAnalysis.cs
15.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
using DataAcquisition.Common.Communications;
using DataAcquisition.Common.Enums;
using DataAcquisition.Common.Utils;
using DataAcquisition.DataAccess;
using DataAcquisition.Models;
using Microsoft.EntityFrameworkCore;
namespace DataAcquisition.Services.DataAnalysis
{
public class RobotDataAnalysis(IDbContextFactory<DataContext> dbContextFactory) : IDataAnalysis
{
private readonly IDbContextFactory<DataContext> dbContextFactory = dbContextFactory;
private readonly SystemLog log = SystemLog.Instance;
public void Analysis(IEnumerable<ICommunication> communications, IEnumerable<Equipment> equipments)
{
using var context = dbContextFactory.CreateDbContext();
try
{
//加工记录超过15分钟未更新,则结束记录
context.WorkpieceProductions.Where(x => !x.IsEnd && x.UpdateTime <= DateTime.Now.AddMinutes(-15)).ExecuteUpdate(x => x.SetProperty(p => p.IsEnd, true));
var equipmentTypes = new List<EquipmentTypeConst>
{
EquipmentTypeConst.Fanuc,
};
var robotEquipments = equipments.Where(x => equipmentTypes.Contains(x.EquipmentType) && x.EquipmentProperties.Any()).ToList();
foreach (var item in robotEquipments)
{
//超时未更新,则跳过
if (item == null || !item.EquipmentProperties.Any(x => x.UpdateTime > DateTime.Now.AddSeconds(-30)))
{
continue;
}
//设备未开机则跳过
if (item[RobotProps.BootFlag.ToString()].Value != bool.TrueString)
{
//结束当前设备所有记录
context.EquipmentPropertyRecords.Where(x => x.EquipmentCode == item.Code && !x.IsEnd).ExecuteUpdate(x => x.SetProperty(p => p.IsEnd, true));
continue;
}
var propCodes = new List<RobotProps>
{
RobotProps.BootFlag,//开机标志
RobotProps.WorkFlag,//工作标志
RobotProps.WeldFlag,//焊接标志
RobotProps.WeldCompleteFlag,//焊接完成标志
RobotProps.Alarm,//报警
RobotProps.Work_Time,//工作时长
RobotProps.Work_Mode,//工作模式
};
//记录设备状态
SetEquipmentRecord(context, item, propCodes);
//每小时存一条记录
var startTime = DateTime.Today.AddHours(DateTime.Now.Hour);
//浮点类型的数据
var propCodes2 = new List<RobotProps>
{
RobotProps.Weld_Speed,//米/分钟
};
foreach (var propCode in propCodes2)
{
var property = item[propCode.ToString()];
_ = float.TryParse(property.Value, out var propValue);
//结束之前的记录
context.EquipmentPropertyRecords.Where(x => x.EquipmentCode == item.Code && x.EquipmentPropertyCode == propCode.ToString() && !x.IsEnd && x.CreateTime < startTime).ExecuteUpdate(x => x.SetProperty(e => e.IsEnd, true));
if (propValue > 0)
{
var record = context.EquipmentPropertyRecords.Where(x => x.EquipmentCode == item.Code && x.EquipmentPropertyCode == propCode.ToString() && !x.IsEnd && x.CreateTime >= startTime).OrderByDescending(x => x.CreateTime).FirstOrDefault();
var val = propValue;
//送丝速度:米/分钟, 直径1.2mm,比重7.8g/cm³
if (propCode == RobotProps.Weld_Speed)
{
var weldLenght = propValue / 60 * 100;//每秒长度消耗(cm)12.5cm
var area = Math.PI * Math.Pow(1.2 / 2 / 10, 2);//(焊丝截面积cm³)0.011304
val = Convert.ToSingle(weldLenght * area * 7.85 / 1000d);
}
//存在记录则更新
if (record != null)
{
_ = float.TryParse(record.Value, out var oldValue);
context.EquipmentPropertyRecords.Where(x => x.Id == record.Id).ExecuteUpdate(x => x.SetProperty(e => e.Value, (oldValue + val).ToString()).SetProperty(x => x.UpdateTime, DateTime.Now));
}
else
{
context.EquipmentPropertyRecords.Add(new EquipmentPropertyRecord
{
EquipmentCode = item.Code,
EquipmentName = item.Name,
EquipmentPropertyCode = property.Code,
EquipmentPropertyName = property.Name,
Value = val.ToString(),
IsEnd = false,
CreateTime = DateTime.Now,
UpdateTime = DateTime.Now,
});
context.SaveChanges();
}
}
}
var currentCommunication = communications.Where(x => x.CommunicationId == item.CommunicationId).FirstOrDefault();
if (currentCommunication != null)
{
WriteWeldStatistics(context, item, currentCommunication);
}
}
}
catch (Exception ex)
{
log.LogError($"[{nameof(RobotDataAnalysis)}]数据解析出现异常:{ex.Message}");
}
}
/// <summary>
/// 记录设备状态
/// </summary>
/// <param name="context"></param>
/// <param name="equipment"></param>
/// <param name="propCodes"></param>
public void SetEquipmentRecord(DataContext context, Equipment equipment, IEnumerable<RobotProps> propCodes)
{
foreach (var propCode in propCodes)
{
//每小时存一条记录
var startTime = DateTime.Today.AddHours(DateTime.Now.Hour);
var prop = equipment[propCode.ToString()];
_ = bool.TryParse(prop.Value, out var propValue);
//结束之前的记录
context.EquipmentPropertyRecords.Where(x => x.EquipmentCode == equipment.Code && x.EquipmentPropertyCode == propCode.ToString() && !x.IsEnd && x.CreateTime < startTime).ExecuteUpdate(x => x.SetProperty(e => e.IsEnd, true));
if (propValue)
{
if (propCode == RobotProps.Alarm)
{
_ = int.TryParse(equipment[RobotProps.Work_Mode.ToString()].Value, out var mode);
//设备报警仅在自动模式下记录
if (mode < 3) continue;
}
var oldRecord = context.EquipmentPropertyRecords.Where(x => x.EquipmentCode == equipment.Code && x.EquipmentPropertyCode == propCode.ToString() && !x.IsEnd && x.CreateTime >= startTime).OrderByDescending(x => x.CreateTime).FirstOrDefault();
if (oldRecord != null)
{
context.EquipmentPropertyRecords.Where(x => x.Id == oldRecord.Id).ExecuteUpdate(x => x.SetProperty(p => p.UpdateTime, DateTime.Now));
}
else
{
context.EquipmentPropertyRecords.Add(new EquipmentPropertyRecord
{
EquipmentCode = equipment.Code,
EquipmentName = equipment.Name,
EquipmentPropertyCode = prop.Code,
EquipmentPropertyName = prop.Name,
Value = propValue.ToString(),
IsEnd = false,
CreateTime = DateTime.Now,
UpdateTime = DateTime.Now,
});
context.SaveChanges();
}
}
//结束
else
{
context.EquipmentPropertyRecords.Where(x => x.EquipmentPropertyCode == equipment.Code && x.EquipmentPropertyCode == propCode.ToString() && !x.IsEnd).ExecuteUpdate(x => x.SetProperty(p => p.IsEnd, true));
}
#region 记录加工工件
//True为开始焊接,False为焊接结束,这里用加工时长记录工件
if (prop.Code == RobotProps.Work_Time.ToString())
{
if (propValue)
{
var workRecord = context.WorkpieceProductions.Where(x => x.EquipmentCode == equipment.Code && !x.IsEnd && x.CreateTime >= DateTime.Today).OrderByDescending(x => x.CreateTime).FirstOrDefault();
if (workRecord != null)
{
if ((DateTime.Now - workRecord.UpdateTime) >= TimeSpan.FromMicroseconds(1))
{
var weld_I_Prop = equipment[RobotProps.Weld_I.ToString()];
var weld_V_Prop = equipment[RobotProps.Weld_V.ToString()];
var weldProcess = new List<WeldProcessRecord>
{
new WeldProcessRecord
{
EquipmentId = equipment.Id,
ProductionId = workRecord.Id,
Code = weld_I_Prop.Code,
Name = weld_I_Prop.Name,
Value = weld_I_Prop.Value ?? "0",
CreateTime = DateTime.Now,
},
new WeldProcessRecord
{
EquipmentId = equipment.Id,
ProductionId = workRecord.Id,
Code = weld_V_Prop.Code,
Name = weld_V_Prop.Name,
Value = weld_V_Prop.Value ?? "0",
CreateTime = DateTime.Now,
}
};
context.WeldProcessRecords.AddRange(weldProcess);
context.SaveChanges();
context.WorkpieceProductions.Where(x => x.Id == workRecord.Id).ExecuteUpdate(x => x.SetProperty(e => e.UpdateTime, DateTime.Now));
}
}
else
{
//_ = int.TryParse(equipment[RobotProps.Type.ToString()].Value, out var type);
var type = equipment[RobotProps.Type.ToString()].Value ?? string.Empty;
_ = int.TryParse(equipment[RobotProps.Program_No.ToString()].Value, out var program_No);
context.WorkpieceProductions.Add(new WorkpieceProduction
{
EquipmentCode = equipment.Code,
EquipmentName = equipment.Name,
WorkpieceCode = type,//待完善
ProgramNo = program_No,
IsEnd = false,
CreateTime = DateTime.Now,
UpdateTime = DateTime.Now,
});
context.SaveChanges();
}
}
else
{
context.WorkpieceProductions.Where(x => x.EquipmentCode == equipment.Code && !x.IsEnd).ExecuteUpdate(x => x.SetProperty(p => p.IsEnd, true));
}
}
#endregion
}
}
/// <summary>
/// 将焊丝消耗写入PLC
/// </summary>
/// <param name="context"></param>
/// <param name="plc"></param>
/// <param name="communication"></param>
private void WriteWeldStatistics(DataContext context, Equipment plc, ICommunication communication)
{
if (communication == null)
{
log.LogWarning($"{nameof(RobotDataAnalysis)}执行{nameof(WriteWeldStatistics)}方法异常,{nameof(communication)}不能为空!");
return;
}
//写入数据
var weld_Day_EquipmentProperty = plc[RobotProps.Weld_statistics_Day.ToString()];
var weld_Month_EquipmentProperty = plc[RobotProps.Weld_statistics_Month.ToString()];
var equipmentCodes = context.Equipments.Where(x => x.Area == plc.Area).Select(x => x.Code).ToList();
var monthStartTime = DateTime.Now.AddDays(1 - DateTime.Now.Day).Date;
var todayValue = context.EquipmentPropertyRecords.Where(x => equipmentCodes.Contains(x.EquipmentCode) && x.EquipmentPropertyCode == RobotProps.Weld_Speed.ToString() && x.CreateTime > DateTime.Today).Select(x => x.Value).AsEnumerable().Sum(float.Parse);
var monthValue = context.EquipmentPropertyRecords.Where(x => equipmentCodes.Contains(x.EquipmentCode) && x.EquipmentPropertyCode == RobotProps.Weld_Speed.ToString() && x.CreateTime > monthStartTime).Select(x => x.Value).AsEnumerable().Sum(float.Parse);
//待写入的数据
var properties = new List<EquipmentProperty>();
//今日焊丝消耗
if (!string.IsNullOrWhiteSpace(weld_Day_EquipmentProperty.Value) && weld_Day_EquipmentProperty.Value != todayValue.ToString())
{
properties.Add(new EquipmentProperty
{
Id = weld_Day_EquipmentProperty.Id,
Equipment = plc,
EquipmentId = plc.Id,
DataAddress = weld_Day_EquipmentProperty.DataAddress,
DataType = weld_Day_EquipmentProperty.DataType,
Value = todayValue.ToString(),
});
}
//本月焊丝消耗
if (!string.IsNullOrWhiteSpace(weld_Month_EquipmentProperty.Value) && weld_Month_EquipmentProperty.Value != monthValue.ToString())
{
properties.Add(new EquipmentProperty
{
Id = weld_Month_EquipmentProperty.Id,
Equipment = plc,
EquipmentId = plc.Id,
DataAddress = weld_Month_EquipmentProperty.DataAddress,
DataType = weld_Month_EquipmentProperty.DataType,
Value = monthValue.ToString(),
});
}
//数据已变动,则写入
if (properties.Count != 0)
{
communication.Write(properties);
}
}
}
}