refactor: rename ScadaLink → ZB.MOM.WW.ScadaBridge (code + projects + namespaces)
Solution + 23 src projects + 26 test projects renamed; folders, csproj, namespaces, and ScadaLinkDbContext/ScadaBridgeDbContext class updated. ActorSystem "scadalink" → "scadabridge", Akka seed-node URLs migrated. SQL roles/logins, LDAP domains, CLI command name, and CLI config dir (~/.scadalink → ~/.scadabridge) also renamed. Build green; 5 Host.Tests fail awaiting SQL login rename in next commit. Pre-existing StaleTagMonitor timing flakes unchanged. Rename script committed at tools/rename-to-scadabridge.sh.
This commit is contained in:
@@ -0,0 +1,252 @@
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace ZB.MOM.WW.ScadaBridge.CentralUI.Components.Shared;
|
||||
|
||||
/// <summary>
|
||||
/// Which kind of trigger a template script has. <see cref="None"/> is no
|
||||
/// trigger; <see cref="Unknown"/> is a stored trigger-type string the runtime
|
||||
/// does not recognize (preserved as-is by the editor).
|
||||
/// </summary>
|
||||
internal enum ScriptTriggerKind { None, Interval, ValueChange, Conditional, Call, Expression, Unknown }
|
||||
|
||||
/// <summary>
|
||||
/// When a Conditional/Expression trigger fires. <see cref="OnTrue"/> fires once
|
||||
/// as the condition becomes true; <see cref="WhileTrue"/> re-fires on a timer
|
||||
/// (cadence = the script's MinTimeBetweenRuns) while the condition stays true.
|
||||
/// </summary>
|
||||
internal enum ScriptTriggerMode { OnTrue, WhileTrue }
|
||||
|
||||
/// <summary>A script's trigger as the editor emits it: a type string + config JSON.</summary>
|
||||
public sealed record ScriptTriggerValue(string? TriggerType, string? Config);
|
||||
|
||||
/// <summary>Parsed/editable view of a script trigger's configuration.</summary>
|
||||
internal sealed class ScriptTriggerModel
|
||||
{
|
||||
/// <summary>Interval period in milliseconds.</summary>
|
||||
public long? IntervalMs { get; set; }
|
||||
|
||||
/// <summary>Monitored attribute (ValueChange + Conditional).</summary>
|
||||
public string? AttributeName { get; set; }
|
||||
|
||||
/// <summary>Comparison operator (Conditional) — one of <see cref="ScriptTriggerConfigCodec.Operators"/>.</summary>
|
||||
public string Operator { get; set; } = ">";
|
||||
|
||||
/// <summary>Comparison threshold (Conditional).</summary>
|
||||
public double? Threshold { get; set; }
|
||||
|
||||
/// <summary>Boolean C# expression (Expression).</summary>
|
||||
public string? Expression { get; set; }
|
||||
|
||||
/// <summary>Fire mode (Conditional + Expression). Defaults to <see cref="ScriptTriggerMode.OnTrue"/>.</summary>
|
||||
public ScriptTriggerMode Mode { get; set; } = ScriptTriggerMode.OnTrue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Round-trip codec for a template script's <c>TriggerType</c> +
|
||||
/// <c>TriggerConfiguration</c>, shared by <see cref="ScriptTriggerEditor"/> (UI
|
||||
/// editing) and consumed at the site by <c>ScriptActor.ParseTriggerConfig</c>.
|
||||
/// Serialized config shapes:
|
||||
/// Interval { intervalMs }
|
||||
/// ValueChange { attributeName }
|
||||
/// Conditional { attributeName, operator, threshold, mode }
|
||||
/// Call { }
|
||||
/// Expression { expression, mode }
|
||||
///
|
||||
/// <c>mode</c> (Conditional + Expression) is <c>OnTrue</c> or <c>WhileTrue</c>;
|
||||
/// an absent or unrecognized value parses as <c>OnTrue</c>.
|
||||
///
|
||||
/// Parsing also accepts the legacy aliases <c>attribute</c> and <c>value</c> so
|
||||
/// older configs survive a round-trip through the editor.
|
||||
/// </summary>
|
||||
internal static class ScriptTriggerConfigCodec
|
||||
{
|
||||
/// <summary>The six comparison operators <c>ScriptActor.EvaluateCondition</c> accepts.</summary>
|
||||
internal static readonly string[] Operators = { ">", ">=", "<", "<=", "==", "!=" };
|
||||
|
||||
/// <summary>Classifies a raw <c>TriggerType</c> string (case-insensitive).</summary>
|
||||
/// <param name="triggerType">The raw trigger type string from the template script entity.</param>
|
||||
internal static ScriptTriggerKind ParseKind(string? triggerType)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(triggerType)) return ScriptTriggerKind.None;
|
||||
return triggerType.Trim().ToLowerInvariant() switch
|
||||
{
|
||||
"interval" => ScriptTriggerKind.Interval,
|
||||
"valuechange" => ScriptTriggerKind.ValueChange,
|
||||
"conditional" => ScriptTriggerKind.Conditional,
|
||||
"call" => ScriptTriggerKind.Call,
|
||||
"expression" => ScriptTriggerKind.Expression,
|
||||
_ => ScriptTriggerKind.Unknown
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether a trigger type honours the script's <c>MinTimeBetweenRuns</c>.
|
||||
/// True for the auto-firing triggers it throttles — ValueChange, Conditional,
|
||||
/// Expression. False for Interval (its own period is the cadence), Call
|
||||
/// (invoked explicitly, never throttled), and None/Unknown.
|
||||
/// </summary>
|
||||
/// <param name="triggerType">The raw trigger type string to classify.</param>
|
||||
internal static bool SupportsMinTimeBetweenRuns(string? triggerType) =>
|
||||
ParseKind(triggerType) is ScriptTriggerKind.ValueChange
|
||||
or ScriptTriggerKind.Conditional
|
||||
or ScriptTriggerKind.Expression;
|
||||
|
||||
/// <summary>Canonical <c>TriggerType</c> string for a kind; null for None/Unknown.</summary>
|
||||
/// <param name="kind">The trigger kind to convert.</param>
|
||||
internal static string? KindToString(ScriptTriggerKind kind) => kind switch
|
||||
{
|
||||
ScriptTriggerKind.Interval => "Interval",
|
||||
ScriptTriggerKind.ValueChange => "ValueChange",
|
||||
ScriptTriggerKind.Conditional => "Conditional",
|
||||
ScriptTriggerKind.Call => "Call",
|
||||
ScriptTriggerKind.Expression => "Expression",
|
||||
_ => null
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Parses a trigger configuration JSON in the context of the given kind.
|
||||
/// Returns a model with default values on null/empty/malformed input or for
|
||||
/// missing keys — never throws.
|
||||
/// </summary>
|
||||
/// <param name="json">The raw JSON trigger configuration string.</param>
|
||||
/// <param name="kind">The trigger kind, used to determine which fields to parse.</param>
|
||||
internal static ScriptTriggerModel Parse(string? json, ScriptTriggerKind kind)
|
||||
{
|
||||
var model = new ScriptTriggerModel();
|
||||
if (string.IsNullOrWhiteSpace(json)) return model;
|
||||
|
||||
try
|
||||
{
|
||||
using var doc = JsonDocument.Parse(json);
|
||||
var root = doc.RootElement;
|
||||
|
||||
switch (kind)
|
||||
{
|
||||
case ScriptTriggerKind.Interval:
|
||||
model.IntervalMs = TryReadLong(root, "intervalMs");
|
||||
break;
|
||||
|
||||
case ScriptTriggerKind.ValueChange:
|
||||
model.AttributeName = TryReadAttributeName(root);
|
||||
break;
|
||||
|
||||
case ScriptTriggerKind.Conditional:
|
||||
model.AttributeName = TryReadAttributeName(root);
|
||||
var op = root.TryGetProperty("operator", out var o) ? o.GetString() : null;
|
||||
model.Operator = NormalizeOperator(op);
|
||||
model.Threshold = TryReadDouble(root, "threshold") ?? TryReadDouble(root, "value");
|
||||
model.Mode = ReadMode(root);
|
||||
break;
|
||||
|
||||
case ScriptTriggerKind.Expression:
|
||||
model.Expression = root.TryGetProperty("expression", out var e) ? e.GetString() : null;
|
||||
model.Mode = ReadMode(root);
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (JsonException)
|
||||
{
|
||||
// Malformed JSON — fall through with default model.
|
||||
}
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Serializes the model to the JSON shape <c>ScriptActor.ParseTriggerConfig</c>
|
||||
/// expects. Returns null for None/Unknown (no structured config to emit).
|
||||
/// </summary>
|
||||
/// <param name="model">The trigger model to serialize.</param>
|
||||
/// <param name="kind">The trigger kind, used to determine which fields to emit.</param>
|
||||
internal static string? Serialize(ScriptTriggerModel model, ScriptTriggerKind kind)
|
||||
{
|
||||
if (kind is ScriptTriggerKind.None or ScriptTriggerKind.Unknown) return null;
|
||||
|
||||
using var stream = new MemoryStream();
|
||||
using (var w = new Utf8JsonWriter(stream))
|
||||
{
|
||||
w.WriteStartObject();
|
||||
switch (kind)
|
||||
{
|
||||
case ScriptTriggerKind.Interval:
|
||||
if (model.IntervalMs.HasValue)
|
||||
w.WriteNumber("intervalMs", model.IntervalMs.Value);
|
||||
break;
|
||||
|
||||
case ScriptTriggerKind.ValueChange:
|
||||
w.WriteString("attributeName", model.AttributeName ?? "");
|
||||
break;
|
||||
|
||||
case ScriptTriggerKind.Conditional:
|
||||
w.WriteString("attributeName", model.AttributeName ?? "");
|
||||
w.WriteString("operator", model.Operator);
|
||||
if (model.Threshold.HasValue)
|
||||
w.WriteNumber("threshold", model.Threshold.Value);
|
||||
w.WriteString("mode", model.Mode.ToString());
|
||||
break;
|
||||
|
||||
case ScriptTriggerKind.Expression:
|
||||
w.WriteString("expression", model.Expression ?? "");
|
||||
w.WriteString("mode", model.Mode.ToString());
|
||||
break;
|
||||
|
||||
// Call → empty object.
|
||||
}
|
||||
w.WriteEndObject();
|
||||
}
|
||||
return Encoding.UTF8.GetString(stream.ToArray());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads the optional <c>mode</c> property; an absent or unrecognized value
|
||||
/// (case-insensitive) yields <see cref="ScriptTriggerMode.OnTrue"/>.
|
||||
/// </summary>
|
||||
private static ScriptTriggerMode ReadMode(JsonElement root)
|
||||
{
|
||||
var raw = root.TryGetProperty("mode", out var m) ? m.GetString() : null;
|
||||
return string.Equals(raw?.Trim(), "WhileTrue", StringComparison.OrdinalIgnoreCase)
|
||||
? ScriptTriggerMode.WhileTrue
|
||||
: ScriptTriggerMode.OnTrue;
|
||||
}
|
||||
|
||||
/// <summary>Returns <paramref name="raw"/> if it is a recognized operator, else ">".</summary>
|
||||
/// <param name="raw">The raw operator string to normalize.</param>
|
||||
internal static string NormalizeOperator(string? raw)
|
||||
{
|
||||
var op = raw?.Trim();
|
||||
return op != null && Array.IndexOf(Operators, op) >= 0 ? op : ">";
|
||||
}
|
||||
|
||||
private static string? TryReadAttributeName(JsonElement root) =>
|
||||
root.TryGetProperty("attributeName", out var a) ? a.GetString()
|
||||
: root.TryGetProperty("attribute", out var a2) ? a2.GetString()
|
||||
: null;
|
||||
|
||||
private static long? TryReadLong(JsonElement el, string name)
|
||||
{
|
||||
if (!el.TryGetProperty(name, out var p)) return null;
|
||||
return p.ValueKind switch
|
||||
{
|
||||
JsonValueKind.Number when p.TryGetInt64(out var i) => i,
|
||||
JsonValueKind.Number => (long)p.GetDouble(),
|
||||
JsonValueKind.String when long.TryParse(
|
||||
p.GetString(), NumberStyles.Integer, CultureInfo.InvariantCulture, out var v) => v,
|
||||
_ => null
|
||||
};
|
||||
}
|
||||
|
||||
private static double? TryReadDouble(JsonElement el, string name)
|
||||
{
|
||||
if (!el.TryGetProperty(name, out var p)) return null;
|
||||
return p.ValueKind switch
|
||||
{
|
||||
JsonValueKind.Number => p.GetDouble(),
|
||||
JsonValueKind.String when double.TryParse(
|
||||
p.GetString(), NumberStyles.Float, CultureInfo.InvariantCulture, out var v) => v,
|
||||
_ => null
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user