clasp-rules
Server-side reactive automation engine for CLASP routers.
Features
- Reactive Triggers - Fire on state change, threshold crossing, events, or intervals
- Conditional Execution - Guard rules with comparisons against live state
- Transform Pipeline - Scale, clamp, invert, or threshold values on the fly
- Loop Prevention - Automatic origin tagging prevents rule feedback loops
- Cooldowns - Per-rule minimum time between firings
- JSON Rules - Define rules in JSON for runtime loading
Installation
[]
= "3.5"
Usage
Motion Sensor to Lights (OnChange)
use ;
use Value;
use Duration;
let rule = Rule ;
let mut engine = new;
engine.add_rule?;
Threshold Alert
use ;
use ;
let rule = Rule ;
SetFromTrigger with Scale Transform
use ;
// Map a 0-1 slider to 0-255 DMX range
let rule = Rule ;
Periodic Heartbeat (OnInterval)
let rule = Rule ;
Evaluate Rules
let actions = engine.evaluate;
for action in actions
// For interval rules
let intervals = engine.interval_rules; // Vec<(rule_id, seconds)>
let actions = engine.evaluate_interval;
JSON Rule Definition
Rules are fully serializable for runtime configuration:
Configuration Reference
Rule
| Field | Type | Default | Description |
|---|---|---|---|
id |
String |
required | Unique rule identifier |
name |
String |
required | Human-readable name |
enabled |
bool |
required | Whether the rule is active |
trigger |
Trigger |
required | What triggers the rule |
conditions |
Vec<Condition> |
[] |
Additional conditions (all must be true) |
actions |
Vec<RuleAction> |
required | Actions to execute when the rule fires |
cooldown |
Option<Duration> |
None |
Minimum time between firings |
Trigger Variants
| Variant | Fields | Description |
|---|---|---|
OnChange |
pattern: String |
Fires when a param matching the pattern changes |
OnThreshold |
address: String, above: Option<f64>, below: Option<f64> |
Fires when a value crosses a threshold |
OnEvent |
pattern: String |
Fires when an event matching the pattern is published |
OnInterval |
seconds: u64 |
Fires periodically |
Condition
| Field | Type | Description |
|---|---|---|
address |
String |
CLASP address to check |
op |
CompareOp |
Eq, Ne, Gt, Gte, Lt, Lte |
value |
Value |
Value to compare against |
RuleAction Variants
| Variant | Fields | Description |
|---|---|---|
Set |
address, value |
Set a parameter to a fixed value |
Publish |
address, signal, value? |
Publish an event |
SetFromTrigger |
address, transform |
Copy trigger's value with optional transform |
Delay |
milliseconds |
Delay before the next action |
Transform Variants
| Variant | Fields | Description |
|---|---|---|
Identity |
-- | Pass through unchanged |
Scale |
scale, offset |
output = input * scale + offset |
Clamp |
min, max |
Clamp to range [min, max] |
Threshold |
value |
true if input > value, else false |
Invert |
min, max |
output = max - (input - min) |
Loop Prevention
Actions produced by rules carry an origin field ("rule:{id}" or "interval:{id}"). The engine skips evaluation when the origin starts with "rule:", preventing infinite feedback loops.
License
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Maintained by LumenCanvas