fix conditions logic
This commit is contained in:
@@ -77,24 +77,46 @@ public class TelemetryController : ControllerBase
|
||||
|
||||
/// <summary>
|
||||
/// Encode relay conditions into a compact string format for STM32 parsing.
|
||||
/// Format: E{enabled}T{timeType}R{relay}S{sensor}O{op}V{value1}[,{value2}]
|
||||
/// Multiple conditions separated by |, multiple rules within a condition separated by &
|
||||
/// Format (relays separated by |):
|
||||
/// E{enabled}R{relayIndex}{groups}
|
||||
/// Group (OR between groups):
|
||||
/// G{action} where action = 0 (turn on) | 1 (turn off)
|
||||
/// followed by its conditions joined by & (AND inside a group):
|
||||
/// Sensor condition: S{sensor}O{op}V{value1}[,{value2}]
|
||||
/// Time condition: T{start}-{end} (minutes from midnight)
|
||||
/// Time ranges with start > end cross midnight (e.g. T1200-360 = 20:00..06:00);
|
||||
/// the device must treat that as (now >= start || now < end).
|
||||
/// </summary>
|
||||
private static string EncodeRelayConditions(IReadOnlyList<RelayConditionDto> conditions)
|
||||
{
|
||||
return string.Join("|", conditions.Select(c =>
|
||||
{
|
||||
var rules = string.Join("&", c.Rules.Select(r =>
|
||||
{
|
||||
var rule = $"S{r.Sensor}O{r.Op}V{r.V1}";
|
||||
if (r.V2.HasValue)
|
||||
rule += $",{r.V2.Value}";
|
||||
return rule;
|
||||
}));
|
||||
return $"E{c.Enabled}T{c.TimeType}R{c.Relay}{rules}";
|
||||
var groups = new List<string>();
|
||||
groups.AddRange(c.On.Select(g => EncodeGroup(g, 0)));
|
||||
groups.AddRange(c.Off.Select(g => EncodeGroup(g, 1)));
|
||||
return $"E{c.Enabled}R{c.RelayIndex}{string.Concat(groups)}";
|
||||
}));
|
||||
}
|
||||
|
||||
private static string EncodeGroup(RelayGroupDto group, int action)
|
||||
{
|
||||
return $"G{action}{string.Join("&", group.Conditions.Select(EncodeCondition))}";
|
||||
}
|
||||
|
||||
private static string EncodeCondition(RelayRuleDto condition)
|
||||
{
|
||||
if (condition.Kind == 1)
|
||||
{
|
||||
return $"T{condition.Start ?? 0}-{condition.End ?? 0}";
|
||||
}
|
||||
|
||||
var encoded = $"S{condition.Sensor}O{condition.Op}V{condition.V1}";
|
||||
if (condition.V2.HasValue)
|
||||
encoded += $",{condition.V2.Value}";
|
||||
|
||||
return encoded;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Collect relay states from query params r1, r2, ... (missing relay = off/0).
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user