feat(historian-gateway): AlarmHistorianEvent->HistorianEvent mapper (SendEvent properties)

Claude-Session: https://claude.ai/code/session_012SDSQ3AcaXqPcBtDESBRii
This commit is contained in:
Joseph Doherty
2026-06-26 16:32:38 -04:00
parent a54c7a9366
commit a96e85f0e4
2 changed files with 77 additions and 0 deletions
@@ -0,0 +1,42 @@
using Google.Protobuf.WellKnownTypes;
using ZB.MOM.WW.HistorianGateway.Contracts.Grpc;
using ZB.MOM.WW.OtOpcUa.Core.AlarmHistorian;
namespace ZB.MOM.WW.OtOpcUa.Driver.Historian.Gateway.Mapping;
/// <summary>
/// Maps a driver-agnostic <see cref="AlarmHistorianEvent"/> onto a gateway wire
/// <see cref="HistorianEvent"/> for the <c>SendEvent</c> write path.
/// </summary>
internal static class AlarmEventMapper
{
/// <summary>Maps an alarm historian event to a gateway event.</summary>
/// <param name="alarm">The driver-agnostic alarm event.</param>
/// <returns>The gateway wire event ready for <c>SendEvent</c>.</returns>
public static HistorianEvent ToHistorianEvent(AlarmHistorianEvent alarm)
{
// Timestamp.FromDateTime requires a Utc-kind DateTime; coerce defensively (TimestampUtc is
// already Utc by contract, but a caller could pass Unspecified).
var eventTime = Timestamp.FromDateTime(DateTime.SpecifyKind(alarm.TimestampUtc, DateTimeKind.Utc));
var historianEvent = new HistorianEvent
{
Id = string.IsNullOrWhiteSpace(alarm.AlarmId) ? Guid.NewGuid().ToString("N") : alarm.AlarmId,
SourceName = alarm.EquipmentPath,
Type = alarm.AlarmTypeName,
EventTime = eventTime,
ReceivedTime = eventTime, // the server re-stamps the received time on the SQL path
};
// Proto map<string,string> values must be non-null — only insert non-null properties.
historianEvent.Properties["AlarmName"] = alarm.AlarmName;
historianEvent.Properties["EventKind"] = alarm.EventKind;
historianEvent.Properties["Severity"] = alarm.Severity.ToString();
historianEvent.Properties["User"] = alarm.User;
historianEvent.Properties["Message"] = alarm.Message;
if (alarm.Comment is not null)
historianEvent.Properties["Comment"] = alarm.Comment;
return historianEvent;
}
}