feat(native-alarms): optional MesReportable column in the native-source override CSV import (ScadaBridge#102 item 4)
CLI import no longer has to reset MES overrides to inherit; a legacy 4-column file still parses and the Central UI import keeps the stored flag for it. Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
@@ -9,8 +9,15 @@ namespace ZB.MOM.WW.ScadaBridge.Commons.Types;
|
||||
/// the 1-based source line (the header is line 1) so downstream errors can point
|
||||
/// back at the operator's file.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <see cref="HasMesColumn"/> is true when the file carried the optional fifth
|
||||
/// <c>MesReportable</c> column; only then is <see cref="MesReportable"/> authoritative
|
||||
/// (blank = inherit, <c>true</c>/<c>false</c> = explicit override). Without the column
|
||||
/// callers keep their existing behaviour for the flag.
|
||||
/// </remarks>
|
||||
public sealed record NativeAlarmSourceOverrideCsvRow(
|
||||
string SourceName, string? Connection, string? SourceReference, string? Filter, int LineNumber);
|
||||
string SourceName, string? Connection, string? SourceReference, string? Filter, int LineNumber,
|
||||
bool? MesReportable = null, bool HasMesColumn = false);
|
||||
|
||||
/// <summary>
|
||||
/// Outcome of parsing a native-alarm-source-override CSV: the successfully-parsed
|
||||
@@ -26,7 +33,8 @@ public sealed record NativeAlarmSourceOverrideCsvParseResult(
|
||||
/// Pure, dependency-free, quote-aware parser turning per-instance
|
||||
/// native-alarm-source-override CSV text into structured rows plus per-line errors.
|
||||
/// Callers supply the text (no file I/O). The header row is required and
|
||||
/// case-insensitive (<c>SourceName,Connection,SourceReference,Filter</c>). Fields
|
||||
/// case-insensitive (<c>SourceName,Connection,SourceReference,Filter</c>, optionally followed by
|
||||
/// <c>MesReportable</c> — blank/<c>true</c>/<c>false</c>, Gitea #102). Fields
|
||||
/// follow the shared <see cref="CsvLineSplitter"/> RFC-4180 quoting rules — the same
|
||||
/// splitter <see cref="OverrideCsvParser"/> uses. The parity analogue of
|
||||
/// <see cref="OverrideCsvParser"/> for native alarm source retargets.
|
||||
@@ -34,9 +42,10 @@ public sealed record NativeAlarmSourceOverrideCsvParseResult(
|
||||
public static class NativeAlarmSourceOverrideCsvParser
|
||||
{
|
||||
private const string HeaderError =
|
||||
"Missing or invalid header row. Expected 'SourceName,Connection,SourceReference,Filter'.";
|
||||
"Missing or invalid header row. Expected 'SourceName,Connection,SourceReference,Filter' "
|
||||
+ "optionally followed by ',MesReportable'.";
|
||||
|
||||
private const int ExpectedColumns = 4;
|
||||
private const int BaseColumns = 4;
|
||||
|
||||
/// <summary>
|
||||
/// Parses native-alarm-source-override CSV <paramref name="csvText"/>. Returns
|
||||
@@ -54,6 +63,7 @@ public static class NativeAlarmSourceOverrideCsvParser
|
||||
var lines = (csvText ?? string.Empty).Replace("\r\n", "\n").Replace('\r', '\n').Split('\n');
|
||||
|
||||
var headerSeen = false;
|
||||
var hasMes = false;
|
||||
|
||||
for (var i = 0; i < lines.Length; i++)
|
||||
{
|
||||
@@ -72,7 +82,7 @@ public static class NativeAlarmSourceOverrideCsvParser
|
||||
|
||||
if (!headerSeen)
|
||||
{
|
||||
if (!TryMatchHeader(fields))
|
||||
if (!TryMatchHeader(fields, out hasMes))
|
||||
{
|
||||
errors.Add(HeaderError);
|
||||
return new NativeAlarmSourceOverrideCsvParseResult(rows, errors);
|
||||
@@ -82,10 +92,11 @@ public static class NativeAlarmSourceOverrideCsvParser
|
||||
continue;
|
||||
}
|
||||
|
||||
if (fields.Count != ExpectedColumns)
|
||||
var expected = hasMes ? BaseColumns + 1 : BaseColumns;
|
||||
if (fields.Count != expected)
|
||||
{
|
||||
errors.Add(
|
||||
$"Line {lineNumber}: expected {ExpectedColumns} columns but found {fields.Count}.");
|
||||
$"Line {lineNumber}: expected {expected} columns but found {fields.Count}.");
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -96,12 +107,27 @@ public static class NativeAlarmSourceOverrideCsvParser
|
||||
continue;
|
||||
}
|
||||
|
||||
bool? mes = null;
|
||||
if (hasMes)
|
||||
{
|
||||
var raw = fields[4].Trim();
|
||||
if (raw.Length == 0) mes = null;
|
||||
else if (bool.TryParse(raw, out var b)) mes = b;
|
||||
else
|
||||
{
|
||||
errors.Add($"Line {lineNumber}: MesReportable must be blank, 'true' or 'false' but was '{raw}'.");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
rows.Add(new NativeAlarmSourceOverrideCsvRow(
|
||||
sourceName,
|
||||
NullIfEmpty(fields[1]),
|
||||
NullIfEmpty(fields[2]),
|
||||
NullIfEmpty(fields[3]),
|
||||
lineNumber));
|
||||
lineNumber,
|
||||
mes,
|
||||
hasMes));
|
||||
}
|
||||
|
||||
if (!headerSeen)
|
||||
@@ -110,13 +136,17 @@ public static class NativeAlarmSourceOverrideCsvParser
|
||||
return new NativeAlarmSourceOverrideCsvParseResult(rows, errors);
|
||||
}
|
||||
|
||||
/// <summary>Matches the required 4-column header (case-insensitive).</summary>
|
||||
private static bool TryMatchHeader(IReadOnlyList<string> fields) =>
|
||||
fields.Count == ExpectedColumns &&
|
||||
HeaderEquals(fields[0], "SourceName") &&
|
||||
HeaderEquals(fields[1], "Connection") &&
|
||||
HeaderEquals(fields[2], "SourceReference") &&
|
||||
HeaderEquals(fields[3], "Filter");
|
||||
/// <summary>Matches the required 4-column header, with an optional 5th <c>MesReportable</c> (case-insensitive).</summary>
|
||||
private static bool TryMatchHeader(IReadOnlyList<string> fields, out bool hasMes)
|
||||
{
|
||||
hasMes = fields.Count == BaseColumns + 1;
|
||||
return (fields.Count == BaseColumns || hasMes) &&
|
||||
HeaderEquals(fields[0], "SourceName") &&
|
||||
HeaderEquals(fields[1], "Connection") &&
|
||||
HeaderEquals(fields[2], "SourceReference") &&
|
||||
HeaderEquals(fields[3], "Filter") &&
|
||||
(!hasMes || HeaderEquals(fields[4], "MesReportable"));
|
||||
}
|
||||
|
||||
private static bool HeaderEquals(string field, string expected) =>
|
||||
string.Equals(field, expected, StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
Reference in New Issue
Block a user