fix(triggers): bound expression evaluation, align AlarmActor error handling, dedupe config parsing

This commit is contained in:
Joseph Doherty
2026-05-16 05:43:18 -04:00
parent 41c3fa3d84
commit 78b10d00d8
4 changed files with 80 additions and 45 deletions

View File

@@ -1,4 +1,5 @@
using Akka.Actor;
using Microsoft.CodeAnalysis.Scripting;
using Microsoft.Extensions.Logging;
using ScadaLink.Commons.Messages.DataConnection;
using ScadaLink.Commons.Messages.DebugView;
@@ -540,7 +541,7 @@ public class InstanceActor : ReceiveActor
// Create Alarm Actors
foreach (var alarm in _configuration.Alarms)
{
Microsoft.CodeAnalysis.Scripting.Script<object?>? onTriggerScript = null;
Script<object?>? onTriggerScript = null;
// Compile on-trigger script if defined
if (!string.IsNullOrEmpty(alarm.OnTriggerScriptCanonicalName))
@@ -599,29 +600,14 @@ public class InstanceActor : ReceiveActor
/// expression, or a compilation failure (logged) — in which case the
/// trigger is inert and the actor still starts.
/// </summary>
private Microsoft.CodeAnalysis.Scripting.Script<object?>? CompileTriggerExpression(
private Script<object?>? CompileTriggerExpression(
string? triggerType, string? triggerConfigJson, string compileName)
{
if (!string.Equals(triggerType, "Expression", StringComparison.OrdinalIgnoreCase))
return null;
if (string.IsNullOrEmpty(triggerConfigJson))
return null;
string? expression;
try
{
var doc = JsonSerializer.Deserialize<JsonElement>(triggerConfigJson);
expression = doc.TryGetProperty("expression", out var e) ? e.GetString() : null;
}
catch (Exception ex)
{
_logger.LogWarning(ex,
"Failed to read trigger expression config for {Name} on {Instance}",
compileName, _instanceUniqueName);
return null;
}
if (string.IsNullOrWhiteSpace(expression))
var expression = TriggerExpressionGlobals.ExtractExpression(triggerConfigJson);
if (expression == null)
return null;
var result = _compilationService.CompileTriggerExpression(compileName, expression);