AlarmLogMappings.cs
3.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
using Rcs.Application.DTOs;
using Rcs.Domain.Entities;
namespace Rcs.Infrastructure.MessageBus.Handlers.Commands;
internal static class AlarmLogMappings
{
public static AlarmLogListItemDto ToListItemDto(this AlarmLog entity)
{
return new AlarmLogListItemDto
{
Id = entity.AlarmLogId.ToString(),
AlarmCode = entity.AlarmCode,
Title = entity.Title,
Content = entity.Message,
Severity = MapSeverity(entity.Level),
Status = MapStatus(entity),
Source = MapSource(entity.SourceType),
TargetCode = entity.SourceCode ?? string.Empty,
TargetName = entity.SourceName ?? string.Empty,
OccurredAt = ToIsoOffsetString(entity.OccurredAt),
Acknowledged = entity.IsAcknowledged,
AcknowledgedBy = entity.AcknowledgedBy,
AcknowledgedAt = ToIsoOffsetString(entity.AcknowledgedAt),
RecoveredAt = ToIsoOffsetString(entity.ResolvedAt),
RecoveryNote = null
};
}
public static AlarmLogDto ToDetailDto(this AlarmLog entity)
{
return new AlarmLogDto
{
Id = entity.AlarmLogId.ToString(),
AlarmCode = entity.AlarmCode,
Title = entity.Title,
Content = entity.Message,
Severity = MapSeverity(entity.Level),
Status = MapStatus(entity),
Source = MapSource(entity.SourceType),
TargetCode = entity.SourceCode ?? string.Empty,
TargetName = entity.SourceName ?? string.Empty,
OccurredAt = ToIsoOffsetString(entity.OccurredAt),
Acknowledged = entity.IsAcknowledged,
AcknowledgedBy = entity.AcknowledgedBy,
AcknowledgedAt = ToIsoOffsetString(entity.AcknowledgedAt),
RecoveredAt = ToIsoOffsetString(entity.ResolvedAt),
RecoveryNote = null,
Details = entity.Details,
ExtraData = entity.ExtraData,
RawMessage = entity.Message,
AlarmType = entity.AlarmType,
SourceType = entity.SourceType,
SourceCode = entity.SourceCode,
SourceName = entity.SourceName,
AcknowledgeRemark = entity.AcknowledgeRemark,
CreatedAt = ToIsoOffsetString(entity.CreatedAt),
UpdatedAt = ToIsoOffsetString(entity.UpdatedAt)
};
}
public static string MapSeverity(string? level)
{
return level?.Trim().ToLowerInvariant() switch
{
"critical" => "critical",
"error" => "critical",
"warning" => "warning",
"warn" => "warning",
"info" => "info",
_ => "warning"
};
}
public static string MapStatus(AlarmLog entity)
{
if (entity.ResolvedAt.HasValue)
{
return "resolved";
}
return entity.IsAcknowledged ? "acknowledged" : "active";
}
public static string MapSource(string? sourceType)
{
return sourceType?.Trim().ToLowerInvariant() switch
{
"robot" => "robot",
"charging" => "charging",
"storage" => "storage",
"protocol" => "system",
"system" => "system",
_ => "system"
};
}
public static string? ToIsoOffsetString(DateTime? value)
{
if (!value.HasValue)
{
return null;
}
var unspecified = DateTime.SpecifyKind(value.Value, DateTimeKind.Unspecified);
return new DateTimeOffset(unspecified, TimeSpan.FromHours(8))
.ToString("yyyy-MM-ddTHH:mm:sszzz");
}
}