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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//! Automation service.
//!
//! Automation _is not_ a special core functionality. Instead, it's implemented as a service,
//! that listens to other services readings and reacts by emitting its own readings.
//!
//! The latter ones, automator-generated readings, are treated in the same way, allowing those to be
//! displayed on the dashboard or caught by other services.
//!
//! Basically, this is a case of "multi-producer multi-consumer" pattern.

use crate::db::Db;
use crate::reading::{Message, Reading, Type};
use crate::services::Service;
use crate::{threading, Result};
use bus::Bus;
use chrono::Local;
use crossbeam_channel::Sender;
use log::{debug, info};
use serde::Deserialize;
use std::sync::{Arc, Mutex};

/// Automator settings.
///
/// # Example
///
/// ```yaml
/// services:
///   heartbeat:
///     Clock:
///       interval_ms: 2000
///   automator:
///     Automator:
///       scenarios:
///         - description: re-emit heartbeat readings
///           conditions:
///             - Sensor: heartbeat
///           actions:
///             - Message: OneOff
/// ```
#[derive(Deserialize, Debug, Clone)]
pub struct Settings {
    scenarios: Vec<Scenario>,
}

/// Single automation scenario.
///
/// # Example
///
/// ```yaml
/// scenarios:
///   - description: Re-emit heartbeat readings
///     conditions:
///       - Sensor: heartbeat
///     actions:
///       - Message: OneOff
/// ```
#[derive(Deserialize, Debug, Clone)]
pub struct Scenario {
    /// User-defined description. Brings no functional effect, but helps to debug scenarios.
    #[serde(default = "String::new")]
    description: String,

    /// Conditions which trigger a scenario to run. All of them must be met in order to trigger
    /// the scenario.
    conditions: Vec<Condition>,

    /// Actions executed when scenario is run.
    actions: Vec<Action>,
}

#[derive(Deserialize, Debug, Clone)]
pub enum Condition {
    /// Sensor matches a specified string.
    ///
    /// # Example
    ///
    /// ```yaml
    /// conditions:
    //    - Sensor: heartbeat
    /// ```
    Sensor(String),

    /// Sensor ends with a specified string.
    SensorEndsWith(String),

    /// Sensor starts with a specified string.
    SensorStartsWith(String),

    /// Sensor contains a specified string.
    SensorContains(String),

    /// At least one of conditions is met.
    Or(Box<Condition>, Box<Condition>),
}

#[derive(Deserialize, Debug, Clone)]
pub enum Action {
    /// Emit a simple reading with original reading value and sensor concatenated from the automator
    /// service ID and original sensor.
    Message(Type),
}

/// Automation service.
pub struct Automator {
    service_id: String,
    settings: Settings,
}

impl Automator {
    pub fn new(service_id: &str, settings: &Settings) -> Automator {
        Automator {
            service_id: service_id.into(),
            settings: settings.clone(),
        }
    }
}

impl Service for Automator {
    fn spawn(self: Box<Self>, _db: Arc<Mutex<Db>>, tx: &Sender<Message>, bus: &mut Bus<Message>) -> Result<()> {
        let tx = tx.clone();
        let rx = bus.add_rx();

        threading::spawn(format!("my-iot::automator:{}", &self.service_id), move || {
            for message in rx {
                for scenario in self.settings.scenarios.iter() {
                    if scenario.conditions.iter().all(|c| c.is_met(&message.reading)) {
                        info!(
                            r#"{} triggered scenario: "{}"."#,
                            &message.reading.sensor, scenario.description
                        );
                        for action in scenario.actions.iter() {
                            action.execute(&self.service_id, &message.reading, &tx).unwrap();
                        }
                    } else {
                        debug!("Skipped: {}.", &message.reading.sensor);
                    }
                }
            }
            unreachable!();
        })?;

        Ok(())
    }
}

impl Condition {
    pub fn is_met(&self, reading: &Reading) -> bool {
        match self {
            Condition::Sensor(sensor) => &reading.sensor == sensor,
            Condition::SensorEndsWith(suffix) => reading.sensor.ends_with(suffix),
            Condition::SensorStartsWith(prefix) => reading.sensor.starts_with(prefix),
            Condition::SensorContains(infix) => reading.sensor.contains(infix),
            Condition::Or(condition_1, condition_2) => condition_1.is_met(&reading) || condition_2.is_met(&reading),
        }
    }
}

impl Action {
    pub fn execute(&self, service_id: &str, reading: &Reading, tx: &Sender<Message>) -> Result<()> {
        match self {
            Action::Message(type_) => tx
                .try_send(Message {
                    type_: *type_,
                    reading: Reading {
                        sensor: format!("{}::{}", &service_id, &reading.sensor),
                        timestamp: Local::now(),
                        value: reading.value.clone(),
                    },
                })
                .map_err(|e| e.into()),
        }
    }
}