feat(triggers): add Expression to the script & alarm trigger codecs

This commit is contained in:
Joseph Doherty
2026-05-16 05:27:33 -04:00
parent 8050a1996f
commit 199cdbe798
3 changed files with 38 additions and 5 deletions
@@ -93,6 +93,10 @@ internal static class AlarmTriggerConfigCodec
model.HiMessage = TryReadString(root, "hiMessage");
model.HiHiMessage = TryReadString(root, "hiHiMessage");
break;
case AlarmTriggerType.Expression:
model.Expression = TryReadString(root, "expression");
break;
}
}
catch (JsonException)
@@ -105,8 +109,10 @@ internal static class AlarmTriggerConfigCodec
/// <summary>
/// Serializes the model to the JSON shape AlarmActor.ParseEvalConfig
/// expects. Always writes <c>attributeName</c> (canonical key) and only
/// the keys relevant to the current trigger type.
/// expects. Writes <c>attributeName</c> (canonical key) for the
/// attribute-bound trigger types and only the keys relevant to the
/// current trigger type. <c>Expression</c> is not bound to a single
/// attribute, so <c>attributeName</c> is omitted for it.
/// </summary>
internal static string Serialize(AlarmTriggerModel model, AlarmTriggerType type)
{
@@ -114,7 +120,8 @@ internal static class AlarmTriggerConfigCodec
using (var w = new Utf8JsonWriter(stream))
{
w.WriteStartObject();
w.WriteString("attributeName", model.AttributeName ?? "");
if (type != AlarmTriggerType.Expression)
w.WriteString("attributeName", model.AttributeName ?? "");
switch (type)
{
@@ -155,6 +162,10 @@ internal static class AlarmTriggerConfigCodec
if (!string.IsNullOrEmpty(model.HiMessage)) w.WriteString("hiMessage", model.HiMessage);
if (!string.IsNullOrEmpty(model.HiHiMessage)) w.WriteString("hiHiMessage", model.HiHiMessage);
break;
case AlarmTriggerType.Expression:
w.WriteString("expression", model.Expression ?? "");
break;
}
w.WriteEndObject();
@@ -241,4 +252,7 @@ internal sealed class AlarmTriggerModel
public string? LoMessage { get; set; }
public string? HiMessage { get; set; }
public string? HiHiMessage { get; set; }
// Expression — boolean C# expression evaluated on attribute updates.
public string? Expression { get; set; }
}