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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
use super::*;
use std::time::Duration;

/// I/O (sensor or actuator)
///
/// # Example
/// ```rust,no_run
/// use msr::*;
///
/// let tcr001 = IoGate::new("tcr001".into());
///
/// // or create it from a str
/// let tcr002 : IoGate = "tcr002".into();
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct IoGate {
    /// The unique ID of the gate
    pub id: String,
    /// Value mapping
    pub mapping: Option<ValueMapping>,
    /// Value cropping
    pub cropping: Option<Cropping>,
    /// Value calibration
    pub calib: Option<Calibration>,
}

/// Map a number **from** one range **to** another.
///
/// That is, a value of `from.low` would get mapped to `to.low`,
/// a value of `from.high` to `to.high`,
/// values in-between to values in-between, etc.
// TODO: Make this more gerneric
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct ValueMapping {
    /// The bounds of the value’s current range
    pub from: ValueBounds,
    /// The bounds of the value’s target range
    pub to: ValueBounds,
}

impl ValueMapping {
    pub fn map(&self, x: f64) -> f64 {
        util::map_value(x, self.from.low, self.from.high, self.to.low, self.to.high)
    }
}

/// Bounds of a value’s range.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct ValueBounds {
    /// The lower bound of the value’s range
    pub low: f64,
    /// The upper bound of the value’s range
    pub high: f64,
}

/// Cropping value
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Cropping {
    /// The lower threshold
    pub low: Option<f64>,
    /// The upper threshold
    pub high: Option<f64>,
}

impl Cropping {
    /// Crop a value
    ///
    /// e.g. with `low = 2.0`
    /// - `1.9` -> `2.0`
    /// - `2.0` -> `2.0`
    ///
    /// with `high = 2.0`
    /// - `2.1` -> `2.0`
    /// - `1.9` -> `1.9`
    pub fn crop(&self, x: f64) -> f64 {
        util::limit(self.low, self.high, x)
    }
}

/// Calibration coefficients
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Calibration {
    pub a: Option<f64>,
    pub b: Option<f64>,
    pub c: Option<f64>,
}

impl IoGate {
    pub fn new(id: String) -> Self {
        IoGate {
            id,
            mapping: None,
            cropping: None,
            calib: None,
        }
    }
}

impl<'a> From<&'a str> for IoGate {
    fn from(id: &'a str) -> Self {
        IoGate::new(id.into())
    }
}

/// A loop continuously triggers a controller again and again.
#[derive(Debug, Clone)]
pub struct Loop {
    /// The unique ID of the rule
    pub id: String,
    /// Used inputs
    pub inputs: Vec<String>,
    /// Used outputs
    pub outputs: Vec<String>,
    /// The controller configuration
    pub controller: ControllerConfig,
}

/// A periodic interval with a fixed duration
#[derive(Debug, Clone)]
pub struct Interval {
    /// The unique ID of the interval
    pub id: String,
    /// The duration between two events
    pub duration: Duration,
}

/// A Rule connects a condition with a list of actions.
#[derive(Debug, Clone, PartialEq)]
pub struct Rule {
    /// The unique ID of the rule
    pub id: String,
    /// The condition
    pub condition: BoolExpr<Comparison>,
    /// Actions that should be triggerd
    pub actions: Vec<String>,
}

/// An action can modify outputs and setpoints.
#[derive(Debug, Clone, PartialEq)]
pub struct Action {
    /// The unique ID of the action
    pub id: String,
    /// Define output values
    pub outputs: HashMap<String, Source>,
    /// Define memory values
    pub memory: HashMap<String, Source>,
    /// Define setpoint values
    pub setpoints: HashMap<String, Source>,
    /// Define controller states
    pub controllers: HashMap<String, ControllerAction>,
    /// Define timeouts
    pub timeouts: HashMap<String, Option<Duration>>,
}

/// An action to modify the state or behaviour of a controller.
#[derive(Debug, Clone, PartialEq)]
pub struct ControllerAction {
    /// Reset controlle state
    pub reset: bool,
    /// Start/Stop a controller
    pub active: Option<bool>,
}

#[cfg(test)]
mod tests {

    use super::*;

    #[test]
    fn map_input() {
        let map = ValueMapping {
            from: ValueBounds {
                low: 4.0,
                high: 20.0,
            },
            to: ValueBounds {
                low: 0.0,
                high: 100.0,
            },
        };
        assert_eq!(map.map(4.0), 0.0);
        assert_eq!(map.map(12.0), 50.0);
        assert_eq!(map.map(20.0), 100.0);
    }

    #[test]
    fn crop_input() {
        let cropping = Cropping {
            low: Some(2.0),
            high: Some(3.0),
        };
        assert_eq!(cropping.crop(1.9), 2.0);
        assert_eq!(cropping.crop(-1.9), 2.0);
        assert_eq!(cropping.crop(2.0), 2.0);
        assert_eq!(cropping.crop(2.1), 2.1);
        assert_eq!(cropping.crop(3.0), 3.0);
        assert_eq!(cropping.crop(3.1), 3.0);
    }
}