namespace ZB.MOM.WW.OtOpcUa.Core.Abstractions;
///
/// The Galaxy alarm priority → OPC UA Part 9 severity mapping, shared by live alarms (the Galaxy driver's
/// MxAccessSeverityMapper) and AVEVA alarm history (priority, Gitea #886) so the two agree.
///
///
///
/// AVEVA System Platform alarm priority runs 1–999 and 1 is the most urgent. AVEVA groups priorities into
/// four severities with these default ranges, and OtOpcUa uses the same boundaries (lmxopcua#887):
///
///
/// AVEVA priorityAVEVA severity → bucket, OPC UA Severity
/// - 1–2501 Critical → , 1000
/// - 251–5002 High → , 750
/// - 501–7503 Medium → , 500
/// - 751–9994 Low → , 250
///
///
/// The vd03 A&E history agrees: alarms at priority 1/100 carry AVEVA severity 1, 400/500 severity 2, 600/700
/// severity 3 (docs/Historian.md, "Galaxy alarm priority → Severity").
/// A priority outside 1–999 is clamped into it first, so 0 or a negative reads as Critical and anything above 999
/// as Low. The mapping is monotonic: a lower priority number never yields a lower OPC UA Severity.
///
///
/// AVEVA lets a Galaxy reconfigure the four ranges. The live alarm feed carries only the priority, never AVEVA's
/// severity, so this table assumes the defaults on both paths; live and history therefore always agree, even on a
/// Galaxy whose ranges were changed.
///
///
/// Before lmxopcua#887 the table ran the other way (a higher number was more severe), so a priority-1 alarm read
/// 250 and a priority-999 one 1000. Change the table here only, so live and history move together.
///
///
public static class GalaxyAlarmPriority
{
/// The last priority of AVEVA's default Critical range (1–250).
public const int CriticalMaxPriority = 250;
/// The last priority of AVEVA's default High range (251–500).
public const int HighMaxPriority = 500;
/// The last priority of AVEVA's default Medium range (501–750); 751–999 is Low.
public const int MediumMaxPriority = 750;
/// OPC UA severity of the Low bucket (priority 751–999).
public const int OpcUaSeverityLow = 250;
/// OPC UA severity of the Medium bucket (priority 501–750).
public const int OpcUaSeverityMedium = 500;
/// OPC UA severity of the High bucket (priority 251–500).
public const int OpcUaSeverityHigh = 750;
/// OPC UA severity of the Critical bucket (priority 1–250).
public const int OpcUaSeverityCritical = 1000;
///
/// OPC UA severity of an AVEVA event that is not an alarm (System.Start/Stop/Deploy, User.Write, …: priority 999,
/// AVEVA severity 0). It sits below every alarm bucket. Only history has such rows; the live feed carries alarms.
///
public const int OpcUaSeverityNotAnAlarm = 1;
/// Maps a Galaxy alarm priority onto the bucket and its OPC UA severity.
/// The AVEVA priority (1 = most urgent … 999 = least; clamped into 1–999).
/// The bucket and its OPC UA Part 9 numeric severity (1000 = most severe).
public static (AlarmSeverity Bucket, int OpcUaSeverity) Map(int rawPriority) => rawPriority switch
{
// The first arm also takes 0 and negatives, the last anything above 999: the clamp into 1–999.
<= CriticalMaxPriority => (AlarmSeverity.Critical, OpcUaSeverityCritical),
<= HighMaxPriority => (AlarmSeverity.High, OpcUaSeverityHigh),
<= MediumMaxPriority => (AlarmSeverity.Medium, OpcUaSeverityMedium),
_ => (AlarmSeverity.Low, OpcUaSeverityLow),
};
}