1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use crate::core::persistence::select_last_reading;
use crate::prelude::*;
use crate::supervisor;
use crossbeam_channel::Sender;
use log::{debug, info};
use serde::Deserialize;
use std::sync::{Arc, Mutex};
#[derive(Deserialize, Debug, Clone)]
pub struct Settings {
scenarios: Vec<Scenario>,
}
pub fn spawn(service_id: &str, settings: &Settings, db: &Arc<Mutex<Connection>>, bus: &mut Bus) -> Result<()> {
let tx = bus.add_tx();
let db = db.clone();
let service_id = service_id.to_string();
let settings = settings.clone();
let rx = bus.add_rx();
supervisor::spawn(format!("my-iot::automator::{}", &service_id), tx.clone(), move || {
for message in &rx {
for scenario in settings.scenarios.iter() {
if scenario.conditions.iter().all(|c| c.is_met(&message)) {
info!(
r"{} triggered scenario: {}",
&message.sensor.sensor_id, scenario.description
);
for action in scenario.actions.iter() {
action.execute(&service_id, &db, &message, &tx).unwrap();
}
} else {
debug!("Skipped: {}", &message.sensor.sensor_id);
}
}
}
unreachable!();
})?;
Ok(())
}
#[derive(Deserialize, Debug, Clone)]
pub struct Scenario {
#[serde(default = "String::new")]
description: String,
#[serde(default = "Vec::new")]
conditions: Vec<Condition>,
#[serde(default = "Vec::new")]
actions: Vec<Action>,
}
#[derive(Deserialize, Debug, Clone)]
pub enum Condition {
Sensor(String),
SensorEndsWith(String),
SensorStartsWith(String),
SensorContains(String),
Or(Vec<Condition>),
}
impl Condition {
pub fn is_met(&self, message: &Message) -> bool {
match self {
Condition::Sensor(sensor_id) => &message.sensor.sensor_id == sensor_id,
Condition::SensorEndsWith(suffix) => message.sensor.sensor_id.ends_with(suffix),
Condition::SensorStartsWith(prefix) => message.sensor.sensor_id.starts_with(prefix),
Condition::SensorContains(infix) => message.sensor.sensor_id.contains(infix),
Condition::Or(conditions) => conditions.iter().any(|c| c.is_met(&message)),
}
}
}
#[derive(Deserialize, Debug, Clone)]
#[serde(tag = "action")]
pub enum Action {
Repeat(RepeatParameters),
ReadSensor(ReadSensorParameters),
}
#[derive(Deserialize, Debug, Clone)]
pub struct RepeatParameters {
target_type: MessageType,
target_sensor: String,
}
#[derive(Deserialize, Debug, Clone)]
pub struct ReadSensorParameters {
source_sensor: String,
target_type: MessageType,
target_sensor: String,
}
impl Action {
pub fn execute(
&self,
_service_id: &str,
db: &Arc<Mutex<Connection>>,
message: &Message,
tx: &Sender<Message>,
) -> Result<()> {
match self {
Action::Repeat(parameters) => {
tx.send(
Composer::new(¶meters.target_sensor)
.type_(parameters.target_type)
.value(message.reading.value.clone())
.into(),
)?;
Ok(())
}
Action::ReadSensor(parameters) => {
if let Some(source) = select_last_reading(&db.lock().unwrap(), ¶meters.source_sensor)? {
tx.send(
Composer::new(¶meters.target_sensor)
.type_(parameters.target_type)
.value(source.value)
.into(),
)?
}
Ok(())
}
}
}
}