SingleForkSRMAnalysis.cs
14.9 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
using FreeSql;
using HHECS.BllModel;
using HHECS.DAQHandle.Common.Enums;
using HHECS.DAQHandle.Common.Utils;
using HHECS.DAQHandle.Models;
using HHECS.EquipmentModel;
using System.Configuration;
using static FreeSql.Internal.GlobalFilter;
namespace HHECS.DAQHandle.EquipmentHandle
{
/// <summary>
/// 单叉堆垛机数据解析
/// </summary>
internal class SingleForkSRMAnalysis : BaseAnalysis
{
public SingleForkSRMAnalysis(EquipmentType equipmentType) : base(equipmentType) { }
//public override BllResult Execute(IEnumerable<EquipmentDataRecord> records)
//{
// try
// {
// //if (!records.Any()) return;
// var group = records.GroupBy(x => x.EquipmentCode);
// using var enumerator = group.GetEnumerator();
// enumerator.MoveNext();
// do
// {
// var item = enumerator.Current;
// var equipmentSN = item?.Key;
// if (item != null)
// {
// Context.AttachRange(item);
// var isSetValueSuccess = UpdateTagValue(item.OrderByDescending(x => x.Timestamp).FirstOrDefault());
// foreach (var item1 in item)
// {
// //更新数据状态为设置数值报错
// if (!isSetValueSuccess)
// {
// item1.HandleStage = (byte)(item1.HandleStage ^ EquipmentDataRecord.SET_VALUE_ERROR);
// }
// else
// {
// item1.HandleStage = (byte)(item1.HandleStage ^ EquipmentDataRecord.SET_VAULE_HANDLE);
// }
// }
// ////性能太慢
// ////更新状态到数据库
// //if (item != null)
// {
// Context.UpdateRange(item);
// Context.SaveChanges();
// }
// }
// var orderItem = item?.OrderBy(x => x.Timestamp);
// UpdateEquipmentAlarm(equipmentSN, orderItem);
// UpdateEquipmentStatus(equipmentSN, orderItem);
// //更新状态到数据库
// if (item != null)
// {
// Context.UpdateRange(item);
// Context.SaveChanges();
// }
// } while (enumerator.MoveNext());
// return BllResultFactory.Success();
// }
// catch (Exception ex)
// {
// return BllResultFactory.Error($"[{nameof(SingleForkSRMAnalysis)}]解析出现异常:{ex}");
// }
//}
//protected override void UpdateEquipmentAlarm(string equipmentSN, IEnumerable<EquipmentDataRecord> records)
//{
// base.UpdateEquipmentAlarm(equipmentSN, records);
// if (records == null || !records.Any()) return;
// records = records.Where(x => (x.HandleStage & EquipmentDataRecord.SET_ERROR_HANDLE) != EquipmentDataRecord.SET_ERROR_HANDLE);
// try
// {
// var lastAlarm = Context.EquipmentAlarmRecords.Where(x => x.EquipmentCode == equipmentSN)
// .OrderByDescending(x => x.UpdateTime)
// .Take(1).First();
// foreach (var equipmentInfo in records)
// {
// try
// {
// Context.UnitOfWork.GetOrBeginTransaction();
// DateTime acceptTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Local).AddSeconds(equipmentInfo.Timestamp);
// if (lastAlarm != null)
// {
// //记录过期处理
// if (acceptTime < lastAlarm.UpdateTime)
// {
// SystemLog.PrintWarn($"错误统计:记录{equipmentInfo.Id}已过期");
// continue;
// }
// }
// var alarmRecords = Context.EquipmentAlarmRecords.Where(x => x.EquipmentCode == equipmentInfo.EquipmentCode && x.IsEnd == false).ToList();
// var alarms = EquipmentTypePropTemps.FindAll(x =>
// {
// return
// x.PropType == EquipmentPropType.PLCMonitorAddress
// && x.MonitorCompareValue != null
// && equipmentInfo.Props[x.Address] != x.MonitorCompareValue;
// });
// //如果alarms考虑直接结束报警,目前是隔五秒刷新
// foreach (var alarm in alarms)
// {
// //匹配数据库中已有记录,如果当前的时间戳小于已记录的异常,不做处理
// var record = alarmRecords.Find(t => t.PropCode == alarm.Address);
// if (record == null)
// {
// //说明记录中没有,或者超过5秒,则直接新增这个报警;
// EquipmentAlarmRecord equipmentAlarm = new EquipmentAlarmRecord
// {
// EquipmentCode = equipmentInfo.EquipmentCode,
// EquipmentName = equipmentInfo.EquipmentName,
// PropCode = alarm.Address,
// AlarmMessage = string.IsNullOrWhiteSpace(equipmentInfo.Props[alarm.Address]) ? "值为空" : alarm.MonitorFailure,
// WarehouseCode = ConfigurationManager.AppSettings["WarehouseCode"],
// CreateTime = acceptTime,
// UpdateTime = acceptTime,//确保不为null
// HandleTime = DateTime.Now,
// };
// Context.EquipmentAlarmRecords.Add(equipmentAlarm);
// }
// else
// {
// //更新这个报警
// record.UpdateTime = acceptTime;
// record.ErrorDuration = (acceptTime - (DateTime)record.CreateTime).TotalSeconds;
// Context.EquipmentAlarmRecords.Update(record);
// }
// }
// //设置状态为已经处理
// equipmentInfo.HandleStage = (byte)(equipmentInfo.HandleStage ^ EquipmentDataRecord.SET_ERROR_HANDLE);
// Context.Update(equipmentInfo);
// Context.SaveChanges();
// Context.UnitOfWork.Commit();
// }
// catch (Exception ex)
// {
// Context.UnitOfWork.Rollback();
// equipmentInfo.HandleStage = (byte)(equipmentInfo.HandleStage ^ EquipmentDataRecord.SET_ALARM_ERROR);
// Context.Update(equipmentInfo);
// Context.SaveChanges();
// SystemLog.PrintError($"记录{equipmentInfo.Id}设置报警信息异常:{ex}");
// }
// }
// }
// catch (Exception ex)
// {
// SystemLog.PrintError($"{ex}");
// }
//}
//protected override void UpdateEquipmentStatus(string equipmentSN, IEnumerable<EquipmentDataRecord> records)
//{
// base.UpdateEquipmentStatus(equipmentSN, records);
// if (records == null || !records.Any()) return;
// records = records.Where(x => (x.HandleStage & EquipmentDataRecord.SET_STATUS_HANDLE) != EquipmentDataRecord.SET_STATUS_HANDLE);
// foreach (var srm in records)
// {
// try
// {
// //与此设备相关的所有状态记录
// var statusRecords = Context.EquipmentStatusRecordHistorys.Where(t => t.EquipmentCode == srm.EquipmentCode && t.IsEnd == false).OrderBy(x => x.UpdateTime).ToList();
// if (statusRecords.Count > 1)
// {
// SystemLog.PrintError($"设备{srm.EquipmentCode}同时存在多条未结束的状态;{string.Join(",", statusRecords.Select(x => x.Status))}");
// var statusRecord = statusRecords.First();
// statusRecord.IsEnd = true;
// statusRecord.Remark = $"同时存在多条未结束的状态,结束较早的数据{DateTime.Now}";
// statusRecords.Remove(statusRecord);
// Context.Update(statusRecord);
// Context.SaveChanges();
// }
// DateTime acceptTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Local).AddSeconds(srm.Timestamp);
// //记录过期处理
// if (statusRecords.Count != 0 && acceptTime < statusRecords.First().UpdateTime)
// {
// SystemLog.PrintWarn($"状态记录:记录{srm.Id}已过期");
// continue;
// }
// EquipmentStatusRecordStatus? equipmentStatus = null;
// var totalError = EquipmentTypePropTemps.Where(x => x.Code == SRMProps.TotalError.ToString()).First().Address;
// //故障
// if (string.IsNullOrWhiteSpace(srm.Props[totalError]))
// {
// SystemLog.PrintError($"记录{srm.Id}的totalError属性无有效数据");
// }
// else if (srm.Props[totalError] == "True")
// {
// equipmentStatus = EquipmentStatusRecordStatus.Error;
// // RecordEquipmentStatus(srm, statusRecords, EquipmentStatusRecordStatus.Error);
// }
// else
// {
// var operationModel = EquipmentTypePropTemps.Where(x => x.Code == SRMProps.OperationModel.ToString()).First().Address;
// var fork1TaskExcuteStatus = EquipmentTypePropTemps.Where(x => x.Code == SRMProps.Fork1TaskExcuteStatus.ToString()).First().Address;
// if (string.IsNullOrWhiteSpace(srm.Props[operationModel]) || string.IsNullOrWhiteSpace(fork1TaskExcuteStatus))
// {
// SystemLog.PrintError($"记录{srm.Id}的operationModel属性无有效数据{srm.Props[operationModel]}或fork1TaskExcuteStatus 无有效数据{srm.Props[fork1TaskExcuteStatus]}");
// }
// //空闲等
// if (srm.Props[operationModel] == SRMOperationModel.Maintain.GetIndexString())
// {
// equipmentStatus = EquipmentStatusRecordStatus.Maintain;
// //维修
// //RecordEquipmentStatus(srm, statusRecords, EquipmentStatusRecordStatus.Maintain);
// }
// else if (srm.Props[operationModel] == SRMOperationModel.Manual.GetIndexString()
// || srm.Props[operationModel] == SRMOperationModel.StandAlone.GetIndexString()
// || srm.Props[operationModel] == SRMOperationModel.Airborne.GetIndexString())
// {
// equipmentStatus = EquipmentStatusRecordStatus.Manual;
// //手动
// //RecordEquipmentStatus(srm, statusRecords, EquipmentStatusRecordStatus.Manual);
// }
// else if (srm.Props[operationModel] == SRMOperationModel.Online.GetIndexString()
// && srm.Props[fork1TaskExcuteStatus] == SRMTaskExcuteStatus.TaskExecuting.GetIndexString())
// {
// equipmentStatus = EquipmentStatusRecordStatus.Running;
// //运行中
// //RecordEquipmentStatus(srm, statusRecords, EquipmentStatusRecordStatus.Running);
// }
// else if (srm.Props[operationModel] == SRMOperationModel.Online.GetIndexString()
// && srm.Props[fork1TaskExcuteStatus] == SRMTaskExcuteStatus.Standby.GetIndexString())
// {
// equipmentStatus = EquipmentStatusRecordStatus.Free;
// // RecordEquipmentStatus(srm, statusRecords, EquipmentStatusRecordStatus.Free);
// }
// else if (srm.Props[operationModel] == SRMOperationModel.Online.GetIndexString()
// && (srm.Props[fork1TaskExcuteStatus] == SRMTaskExcuteStatus.TaskInterruptError.GetIndexString()
// || srm.Props[fork1TaskExcuteStatus] == SRMTaskExcuteStatus.TaskSendError.GetIndexString()))
// {
// equipmentStatus = EquipmentStatusRecordStatus.TaskExcuteError;
// // RecordEquipmentStatus(srm, statusRecords, EquipmentStatusRecordStatus.TaskExcuteError);
// }
// }
// if (equipmentStatus != null)
// {
// RecordEquipmentStatus(srm, statusRecords, (EquipmentStatusRecordStatus)equipmentStatus);
// }
// srm.HandleStage = (byte)(srm.HandleStage ^ EquipmentDataRecord.SET_STATUS_HANDLE);
// }
// catch (Exception ex)
// {
// SystemLog.PrintError($"记录{srm.Id}状态记录处理失败:{ex}");
// srm.HandleStage = (byte)(srm.HandleStage ^ EquipmentDataRecord.SET_STATUS_ERROR);
// }
// finally
// {
// //设置状态为已经处理
// srm.IsHandle = true;
// Context.Update(srm);
// Context.SaveChanges();
// }
// }
// //性能太慢
// ////更新状态到数据库
// //if (records != null)
// //{
// // Context.UpdateRange(records);
// // Context.SaveChanges();
// //}
//}
}
}