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
//! Modifiers which can be injected by the application logic to change the
//! state dynamically per request.

use crate::api;

/// `StatusModifier`s are used to modify the status
pub trait StatusModifier: Send + Sync {
    /// Called after all registered sensors are read
    fn modify(&self, status: &mut api::Status);
}

/// This modifier updates the opening state based on the
/// first people now present sensor (if present).
pub struct StateFromPeopleNowPresent;

impl StatusModifier for StateFromPeopleNowPresent {
    fn modify(&self, status: &mut api::Status) {
        // Update state depending on number of people present
        let people_now_present: Option<u64> = status
            .sensors
            .as_ref()
            .and_then(|sensors: &api::Sensors| sensors.people_now_present.first())
            .map(|sensor: &api::PeopleNowPresentSensor| sensor.value);
        if let Some(count) = people_now_present {
            let mut state = status.state.clone().unwrap_or_default();
            state.open = Some(count > 0);
            // comparison chain is actually cleaner here IMO
            #[allow(clippy::comparison_chain)]
            if count == 1 {
                state.message = Some(format!("{} person here right now", count));
            } else if count > 1 {
                state.message = Some(format!("{} people here right now", count));
            }
            status.state = Some(state);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    mod state_from_people_now_present {
        use super::*;

        #[test]
        fn no_sensors() {
            let mut status = api::Status {
                sensors: None,
                ..api::Status::default()
            };
            assert_eq!(status.state, None);
            StateFromPeopleNowPresent.modify(&mut status);
            assert_eq!(status.sensors, None);
            assert_eq!(status.state, None);
        }

        #[test]
        fn no_people_present_sensor() {
            let mut status = api::Status {
                sensors: Some(api::Sensors {
                    people_now_present: vec![],
                    temperature: vec![],
                }),
                ..api::Status::default()
            };
            assert_eq!(status.state, None);
            StateFromPeopleNowPresent.modify(&mut status);
            assert_eq!(status.state, None);
        }

        fn make_pnp_sensor(value: u64) -> api::PeopleNowPresentSensor {
            api::PeopleNowPresentSensor {
                location: None,
                name: None,
                names: None,
                description: None,
                value,
            }
        }

        #[test]
        fn zero_people_present() {
            let mut status = api::Status {
                sensors: Some(api::Sensors {
                    people_now_present: vec![make_pnp_sensor(0)],
                    temperature: vec![],
                }),
                state: Some(api::State::default()),
                ..api::Status::default()
            };
            status.state.as_mut().unwrap().message = Some("This will remain unchanged.".to_string());
            assert_eq!(
                status.state.as_ref().unwrap().message,
                Some("This will remain unchanged.".to_string())
            );
            StateFromPeopleNowPresent.modify(&mut status);
            assert_eq!(
                status.state.unwrap().message,
                Some("This will remain unchanged.".to_string())
            );
        }

        #[test]
        fn one_person_present() {
            let mut status = api::Status {
                sensors: Some(api::Sensors {
                    people_now_present: vec![make_pnp_sensor(1)],
                    temperature: vec![],
                }),
                ..api::Status::default()
            };
            assert_eq!(status.state, None);
            StateFromPeopleNowPresent.modify(&mut status);
            assert_eq!(
                status.state.unwrap().message,
                Some("1 person here right now".to_string())
            );
        }

        #[test]
        fn two_people_present() {
            let mut status = api::Status {
                sensors: Some(api::Sensors {
                    people_now_present: vec![make_pnp_sensor(2)],
                    temperature: vec![],
                }),
                ..api::Status::default()
            };
            assert_eq!(status.state, None);
            StateFromPeopleNowPresent.modify(&mut status);
            assert_eq!(
                status.state.as_ref().unwrap().message,
                Some("2 people here right now".to_string())
            );
        }
    }
}