use crate::{BehaviorNode, Context, NodeStatus};
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Debug)]
pub struct SensorCheckNode {
#[allow(dead_code)] topic: String,
field: String,
operator: ComparisonOperator,
threshold: f64,
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
enum ComparisonOperator {
LessThan,
GreaterThan,
Equal,
}
#[derive(Debug, Serialize, Deserialize)]
struct SensorCheckConfig {
topic: String,
field: String,
operator: ComparisonOperator,
threshold: f64,
}
impl SensorCheckNode {
pub fn from_json(config: Value) -> anyhow::Result<Self> {
let cfg: SensorCheckConfig = serde_json::from_value(config)?;
Ok(Self {
topic: cfg.topic,
field: cfg.field,
operator: cfg.operator,
threshold: cfg.threshold,
})
}
}
#[async_trait]
impl BehaviorNode for SensorCheckNode {
async fn tick(&mut self, _ctx: &Context) -> anyhow::Result<NodeStatus> {
tracing::debug!(
"SensorCheckNode: checking {} {} {}",
self.field,
match self.operator {
ComparisonOperator::LessThan => "<",
ComparisonOperator::GreaterThan => ">",
ComparisonOperator::Equal => "==",
},
self.threshold
);
Ok(NodeStatus::Success)
}
fn name(&self) -> &str {
"SensorCheckNode"
}
}