WeldRobotAnalysis.cs
4.61 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
using HHECS.BllModel;
using HHECS.DAQHandle.Common.Utils;
using HHECS.DAQHandle.Models;
using HHECS.EquipmentModel;
namespace HHECS.DAQHandle.EquipmentHandle
{
/// <summary>
/// 焊接机器人数据解析
/// </summary>
internal class WeldRobotAnalysis : BaseAnalysis
{
public WeldRobotAnalysis(EquipmentType equipmentType) : base(equipmentType) { }
public override BllResult Execute(IEnumerable<EquipmentDataRecord> records)
{
try
{
//按设备分组
foreach (var item in records.GroupBy(x => x.EquipmentCode))
{
var equipmentRecords = item.ToList();
var setStatusResult = SetEquipmentStatus(item.Key, equipmentRecords);
if (!setStatusResult.Success)
{
SystemLog.PrintError(setStatusResult.Msg);
}
var setAlarmResult = SetEquipmentAlarm(item.Key, equipmentRecords);
if (!setAlarmResult.Success)
{
SystemLog.PrintError(setStatusResult.Msg);
}
var setValueResult = SetEquipmentPropValues(item.Key, item.LastOrDefault());
if (!setValueResult.Success)
{
SystemLog.PrintError(setStatusResult.Msg);
}
}
//解析完成后清空记录
Context.EquipmentDataRecord.RemoveRange(records);
Context.SaveChanges();
return BllResultFactory.Success();
}
catch (Exception ex)
{
return BllResultFactory.Error($"[{nameof(WeldRobotAnalysis)}]解析出现异常:{ex}");
}
}
/// <summary>
/// 记录设备状态
/// </summary>
/// <param name="equipmentSN"></param>
/// <param name="equipmentDataRecords"></param>
/// <returns></returns>
protected BllResult SetEquipmentStatus(string equipmentSN, IEnumerable<EquipmentDataRecord> equipmentDataRecords)
{
try
{
//待完善
return BllResultFactory.Success();
}
catch (Exception ex)
{
return BllResultFactory.Error($"更新设备[{equipmentSN}]状态失败.{ex.Message}");
}
}
protected BllResult SetEquipmentAlarm(string equipmentSN, IEnumerable<EquipmentDataRecord> equipmentDataRecords)
{
try
{
//待完善
return BllResultFactory.Success();
}
catch (Exception ex)
{
return BllResultFactory.Error($"更新设备[{equipmentSN}]报警失败.{ex.Message}");
}
}
protected BllResult SetEquipmentPropValues(string equipmentSN, EquipmentDataRecord record)
{
try
{
var equipmentId = Context.Equipment.Where(x => x.Code == equipmentSN).First(x => x.Id);
var equipmentProps = Context.EquipmentProp.Where(x => x.EquipmentId == equipmentId).ToList();
if (equipmentProps.Count == 0)
{
return BllResultFactory.Error($"设备编号[{record.EquipmentCode}]的属性配置为空!");
}
var jsonResult = JsonConvertToTagList(record.Tags);
if (!jsonResult.Success)
{
return BllResultFactory.Error($"设备编号[{record.EquipmentCode}]的Json数据转换失败.{jsonResult.Msg}");
}
var tags = jsonResult.Data;
var temps = new List<EquipmentProp>();
foreach (var prop in equipmentProps)
{
var tag = tags.Where(x => x.Tag == prop.Address).FirstOrDefault();
if (tag == null) continue;
Context.EquipmentProp.Attach(prop);
prop.Value = tag.Value;
prop.Updated = ConvertToDateTime(record.Timestamp).Data;
temps.Add(prop);
}
if (temps.Count != 0)
{
Context.UpdateRange(temps);
Context.SaveChanges();
}
return BllResultFactory.Success();
}
catch (Exception ex)
{
return BllResultFactory.Error($"设备[{equipmentSN}]属性值更新失败.{ex.Message}");
}
}
}
}