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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/// Bindings to the [Capabilities API].
///
/// [Capabilities API]: https://developers.meethue.com/develop/hue-api/10-capabilities-api/
pub mod capabilities;
/// Bindings to the [Configuration API].
///
/// [Configuration API]: https://developers.meethue.com/develop/hue-api/7-configuration-api
pub mod config;
/// Bindings to the [Groups API].
///
/// [Groups API]: https://developers.meethue.com/develop/hue-api/groupds-api
pub mod group;
/// Bindings to the [Lights API].
///
/// [Lights API]: https://developers.meethue.com/develop/hue-api/lights-api
pub mod light;
/// Bindings to the [Resourcelinks API].
///
/// [Resourcelinks API]: https://developers.meethue.com/develop/hue-api/9-resourcelinks-api
pub mod resourcelink;
/// Bindings to the [Rules API].
///
/// [Rules API]: https://developers.meethue.com/develop/hue-api/6-rules-api
pub mod rule;
/// Bindings to the [Scenes API].
///
/// [Scenes API]: https://developers.meethue.com/develop/hue-api/4-scenes
pub mod scene;
/// Bindings to the [Schedules API].
///
/// [Schedules API]: https://developers.meethue.com/develop/hue-api/3-schedules-api
pub mod schedule;
/// Bindings to the [Sensors API].
///
/// [Sensors API]: https://developers.meethue.com/develop/hue-api/5-sensors-api
pub mod sensor;

pub use capabilities::Capabilities;
pub use config::Config;
pub use group::Group;
pub use light::Light;
pub use resourcelink::Resourcelink;
pub use rule::Rule;
pub use scene::Scene;
pub use schedule::Schedule;
pub use sensor::Sensor;

use crate::{response::Modified, Bridge, Error, Response};
use chrono::NaiveDateTime;
use serde::{de, de::Error as _, Deserialize, Serialize};
use serde_json::Value as JsonValue;
use std::fmt;

/// Alert effect of a light.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Alert {
    /// Performs one breathe cycle.
    Select,
    /// Performs breathe cycles for 15 seconds or until the alert attribute is set to `None`.
    LSelect,
    /// Disables any alert.
    None,
}

/// Dynamic effect of a light.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Deserialize, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Effect {
    /// Cycles through all hues with the current brightness and saturation.
    Colorloop,
    /// Disables any effect.
    None,
}

/// Color mode of a light.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Deserialize)]
pub enum ColorMode {
    /// Uses a color temperatue to set the color of a light.
    #[serde(rename = "ct")]
    ColorTemperature,
    /// Uses hue and saturation to set the color of a light.
    #[serde(rename = "hs")]
    HueAndSaturation,
    /// Uses x and y coordinates in the color space to set the color of a light.
    #[serde(rename = "xy")]
    ColorSpaceCoordinates,
}

/// Struct for new resources that were scanned by the bridge.
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct Scan {
    /// When the bridge last scanned for new resources.
    pub last_scan: LastScan,
    /// New resources that were discovered.
    pub resources: Vec<ScanResource>,
}

impl<'de> Deserialize<'de> for Scan {
    fn deserialize<D: de::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        enum Field {
            LastScan,
            ResourceId(String),
        }

        impl<'de> Deserialize<'de> for Field {
            fn deserialize<D: de::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
                let value: String = Deserialize::deserialize(deserializer)?;
                Ok(match value.as_ref() {
                    "lastscan" => Field::LastScan,
                    v => Field::ResourceId(v.to_owned()),
                })
            }
        }

        struct ScanVisitor;

        impl<'de> de::Visitor<'de> for ScanVisitor {
            type Value = Scan;

            fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                f.write_str("struct Scan")
            }

            fn visit_map<V: de::MapAccess<'de>>(self, mut map: V) -> Result<Scan, V::Error> {
                #[derive(Deserialize)]
                struct ResourceInfo {
                    name: String,
                }
                let mut resources = Vec::new();
                let mut last_scan = None;
                while let Some(key) = map.next_key()? {
                    match key {
                        Field::LastScan => {
                            last_scan = serde_json::from_value(map.next_value()?)
                                .map_err(V::Error::custom)?
                        }
                        Field::ResourceId(v) => {
                            let info: ResourceInfo = map.next_value()?;
                            let resource = ScanResource {
                                id: v,
                                name: info.name,
                            };
                            resources.push(resource);
                        }
                    }
                }
                let last_scan = last_scan.ok_or_else(|| de::Error::missing_field("lastscan"))?;
                Ok(Scan {
                    last_scan,
                    resources,
                })
            }
        }

        const FIELDS: &[&str] = &["lastscan", "resources"];
        deserializer.deserialize_struct("Scan", FIELDS, ScanVisitor)
    }
}

/// Status of the last scan for a new resource type.
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub enum LastScan {
    /// Date and time of the last scan.
    DateTime(NaiveDateTime),
    /// The bridge is currently scanning.
    Active,
    /// The bridge did not scan since it was powered on.
    None,
}

impl<'de> Deserialize<'de> for LastScan {
    fn deserialize<D: de::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let value: String = Deserialize::deserialize(deserializer)?;
        Ok(match value.as_ref() {
            "active" => LastScan::Active,
            "none" => LastScan::None,
            v => LastScan::DateTime(
                NaiveDateTime::parse_from_str(v, "%Y-%m-%dT%H:%M:%S").map_err(D::Error::custom)?,
            ),
        })
    }
}

/// Information about a resource that is returned from a scan.
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct ScanResource {
    /// Identifier of the resource.
    pub id: String,
    /// Name of the resource.
    pub name: String,
}

/// Enum for adjusting an attribute of a modifier or creator.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub enum Adjust<T> {
    /// Overrides the current value.
    Override(T),
    /// Adds the value to the current value.
    Increment(T),
    /// Subtracts the value to the current value.
    Decrement(T),
}

/// Represents a HTTP method.
#[allow(missing_docs)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub enum RequestMethod {
    Put,
    Post,
    Get,
    Delete,
}

/// Marker trait for resources.
pub trait Resource {}

/// Trait for creating a resource.
pub trait Creator: Serialize {
    /// Returns the suffix of the API URL.
    fn url_suffix() -> String;

    /// Sends the request to create the resource.
    fn execute(&self, bridge: &Bridge) -> crate::Result<String> {
        #[derive(Deserialize)]
        struct CreationInfo {
            id: String,
        }
        let mut response: Vec<Response<CreationInfo>> = bridge.api_request(
            Self::url_suffix(),
            RequestMethod::Post,
            Some(serde_json::to_value(self)?),
        )?;
        match response.pop() {
            Some(v) => Ok(v.into_result()?.id),
            None => Err(Error::GetCreatedId),
        }
    }
}

/// Trait for modifying a resource.
pub trait Modifier: Serialize {
    /// The type of the identifier.
    ///
    /// Set to `()` if only one resource of the same type exists.
    type Id;

    /// Returns the suffix of the API URL.
    fn url_suffix(id: Self::Id) -> String;

    /// Sends the request to modify the resource.
    fn execute(&self, bridge: &Bridge, id: Self::Id) -> crate::Result<Vec<Response<Modified>>> {
        bridge.api_request(
            Self::url_suffix(id),
            RequestMethod::Put,
            Some(serde_json::to_value(self)?),
        )
    }
}

/// Trait for scanning new resources.
pub trait Scanner: Serialize {
    /// Returns the suffix of the API URL.
    fn url_suffix() -> String;

    /// Sends the request to scan for new resources.
    fn execute(&self, bridge: &Bridge) -> crate::Result<()> {
        let responses: Vec<Response<JsonValue>> = bridge.api_request(
            Self::url_suffix(),
            RequestMethod::Post,
            Some(serde_json::to_value(self)?),
        )?;
        for response in responses {
            response.into_result()?;
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::{NaiveDate, NaiveTime};
    use serde_json::json;

    #[test]
    fn deserialize_last_scan() {
        let json = json!("none");
        let value: LastScan = serde_json::from_value(json).unwrap();
        assert_eq!(value, LastScan::None);

        let json = json!("active");
        let value: LastScan = serde_json::from_value(json).unwrap();
        assert_eq!(value, LastScan::Active);

        let json = json!("2020-01-01T00:10:00");
        let value: LastScan = serde_json::from_value(json).unwrap();
        let date = NaiveDate::from_ymd(2020, 1, 1);
        let time = NaiveTime::from_hms(0, 10, 0);
        assert_eq!(value, LastScan::DateTime(NaiveDateTime::new(date, time)))
    }

    #[test]
    fn deserialize_scan() {
        let json = json!({
            "lastscan": "active",
            "1": {"name": "light one"},
            "2": {"name": "light two"}
        });
        let value: Scan = serde_json::from_value(json).unwrap();
        let scan = Scan {
            last_scan: LastScan::Active,
            resources: vec![
                ScanResource {
                    id: "1".to_owned(),
                    name: "light one".to_owned(),
                },
                ScanResource {
                    id: "2".to_owned(),
                    name: "light two".to_owned(),
                },
            ],
        };
        assert_eq!(value, scan);
    }
}