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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
use crate::resource::{self, Creator, Modifier, RequestMethod, Scanner};
use crate::{response::Modified, Error, Response, Result};
use serde::{de::DeserializeOwned, Deserialize};
use serde_json::Value as JsonValue;
use std::{collections::HashMap, net::IpAddr};

type ResponsesModified = Vec<Response<Modified>>;

/// Discovers bridges in the local netowork.
///
/// This sends a HTTP GET request to [https://discovery.meethue.com], to get IP addresses of bridges
/// that are in the local network.
///
/// [https://discovery.meethue.com]: https://discovery.meethue.com
///
/// # Examples
///
/// Get the IP addresses of all discovered bridges:
/// ```no_run
/// # fn main() -> Result<(), huelib::Error> {
/// let ip_addresses = huelib::bridge::discover()?;
/// # Ok(())
/// # }
/// ```
///
/// Register a user on the bridge that was first discovered:
/// ```no_run
/// use huelib::bridge;
///
/// # fn main() -> Result<(), huelib::Error> {
/// let ip = bridge::discover()?.pop().expect("found no bridges");
/// let username = bridge::register_user(ip, "example")?;
/// println!("Registered user: {}", username);
/// # Ok(())
/// # }
/// ```
pub fn discover() -> Result<Vec<IpAddr>> {
    let http_response = ureq::get("https://discovery.meethue.com").call();
    #[derive(Deserialize)]
    struct BridgeJson {
        #[serde(rename = "internalipaddress")]
        ip_address: String,
    }
    let bridges: Vec<BridgeJson> = serde_json::from_value(http_response.into_json()?)?;
    let mut ip_addresses = Vec::<IpAddr>::new();
    for b in bridges {
        ip_addresses.push(b.ip_address.parse()?);
    }
    Ok(ip_addresses)
}

/// Registers a new user on a bridge.
///
/// This function returns the new username. See the [`register_user_with_clientkey`] function if you
/// also want to generate a clientkey.
///
/// # Examples
///
/// Register a user and print the username:
/// ```no_run
/// use huelib::bridge;
/// use std::net::{IpAddr, Ipv4Addr};
///
/// # fn main() -> Result<(), huelib::Error> {
/// let bridge_ip = IpAddr::V4(Ipv4Addr::new(192, 168, 1, 2));
/// let username = bridge::register_user(bridge_ip, "example")?;
/// println!("Registered user with username `{}`", username);
/// # Ok(())
/// # }
/// ```
pub fn register_user<S>(ip_address: IpAddr, devicetype: S) -> Result<String>
where
    S: AsRef<str>,
{
    let url = format!("http://{}/api", ip_address);
    let body = format!("{{\"devicetype\":\"{}\"}}", devicetype.as_ref());
    let http_response = ureq::post(&url).send_string(&body);
    #[derive(Deserialize)]
    struct User {
        username: String,
    }
    let mut responses: Vec<Response<User>> = serde_json::from_value(http_response.into_json()?)?;
    match responses.pop() {
        Some(v) => match v.into_result() {
            Ok(user) => Ok(user.username),
            Err(e) => Err(Error::Response(e)),
        },
        None => Err(Error::GetUsername),
    }
}

/// Registers a new user on a bridge with a clientkey.
///
/// This function returns the new username and a random generated 16 byte clientkey encoded as ASCII
/// string of length 32. See the [`register_user`] function if you don't want to generate a
/// clientkey.
///
/// # Examples
///
/// Register a user and print the username and clientkey:
/// ```no_run
/// use huelib::bridge;
/// use std::net::{IpAddr, Ipv4Addr};
///
/// # fn main() -> Result<(), huelib::Error> {
/// let bridge_ip = IpAddr::V4(Ipv4Addr::new(192, 168, 1, 2));
/// let (username, clientkey) = bridge::register_user_with_clientkey(bridge_ip, "example")?;
/// println!("Registered user with username `{}` and clientkey `{}`", username, clientkey);
/// # Ok(())
/// # }
/// ```
pub fn register_user_with_clientkey<S>(
    ip_address: IpAddr,
    devicetype: S,
) -> Result<(String, String)>
where
    S: AsRef<str>,
{
    let url = format!("http://{}/api", ip_address);
    let body = format!(
        "{{\"devicetype\":\"{}\",\"generateclientkey\":true}}",
        devicetype.as_ref()
    );
    let http_response = ureq::post(&url).send_string(&body);
    #[derive(Deserialize)]
    struct User {
        username: String,
        clientkey: String,
    }
    let mut responses: Vec<Response<User>> = serde_json::from_value(http_response.into_json()?)?;
    match responses.pop() {
        Some(v) => match v.into_result() {
            Ok(user) => Ok((user.username, user.clientkey)),
            Err(e) => Err(Error::Response(e)),
        },
        None => Err(Error::GetUsername),
    }
}

fn parse_response<T>(response: JsonValue) -> crate::Result<T>
where
    T: DeserializeOwned,
{
    if let Ok(mut v) = serde_json::from_value::<Vec<Response<JsonValue>>>(response.clone()) {
        if let Some(v) = v.pop() {
            v.into_result()?;
        }
    }
    Ok(serde_json::from_value(response)?)
}

/// A bridge with IP address and username.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Bridge {
    /// Name of the user that is connected to the bridge.
    username: String,
    /// IP address of the bridge.
    ip_address: IpAddr,
    /// Url to the Philips Hue API.
    api_url: String,
}

impl Bridge {
    /// Creates a new bridge.
    ///
    /// # Examples
    ///
    /// Create a bridge with an already registered user:
    /// ```
    /// use huelib::Bridge;
    /// use std::net::{IpAddr, Ipv4Addr};
    ///
    /// let ip = IpAddr::V4(Ipv4Addr::new(192, 168, 1, 2));
    /// let bridge = Bridge::new(ip, "username");
    /// ```
    pub fn new<S>(ip_address: IpAddr, username: S) -> Self
    where
        S: Into<String>,
    {
        let username = username.into();
        Bridge {
            api_url: format!("http://{}/api/{}", ip_address, username),
            username,
            ip_address,
        }
    }

    /// Returns the name of the user that is connected to the bridge.
    pub fn username(&self) -> &str {
        &self.username
    }

    /// Returns the IP address of the bridge.
    pub fn ip_address(&self) -> &IpAddr {
        &self.ip_address
    }

    /// Sends a HTTP request to the Philips Hue API and returns the response.
    pub(crate) fn api_request<S, T>(
        &self,
        url_suffix: S,
        request_method: RequestMethod,
        body: Option<JsonValue>,
    ) -> Result<T>
    where
        S: AsRef<str>,
        T: DeserializeOwned,
    {
        let url = format!("{}/{}", self.api_url, url_suffix.as_ref());
        let mut request = match request_method {
            RequestMethod::Put => ureq::put(&url),
            RequestMethod::Post => ureq::post(&url),
            RequestMethod::Get => ureq::get(&url),
            RequestMethod::Delete => ureq::delete(&url),
        };
        let response = match body {
            Some(v) => request.send_json(v),
            None => request.call(),
        };
        Ok(serde_json::from_value(response.into_json()?)?)
    }

    /// Modifies the configuration of the bridge.
    pub fn set_config(&self, modifier: &resource::config::Modifier) -> Result<ResponsesModified> {
        modifier.execute(self, ())
    }

    /// Returns the configuration of the bridge.
    pub fn get_config(&self) -> Result<resource::Config> {
        parse_response(self.api_request("config", RequestMethod::Get, None)?)
    }

    /// Modifies attributes of a light.
    pub fn set_light_attribute<S>(
        &self,
        id: S,
        modifier: &resource::light::AttributeModifier,
    ) -> Result<ResponsesModified>
    where
        S: Into<String>,
    {
        modifier.execute(self, id.into())
    }

    /// Modifies the state of a light.
    pub fn set_light_state<S>(
        &self,
        id: S,
        modifier: &resource::light::StateModifier,
    ) -> Result<ResponsesModified>
    where
        S: Into<String>,
    {
        modifier.execute(self, id.into())
    }

    /// Returns a light.
    pub fn get_light<S>(&self, id: S) -> Result<resource::Light>
    where
        S: Into<String>,
    {
        let id = id.into();
        let light: resource::Light = parse_response(self.api_request(
            format!("lights/{}", id),
            RequestMethod::Get,
            None,
        )?)?;
        Ok(light.with_id(id))
    }

    /// Returns all lights that are connected to the bridge.
    pub fn get_all_lights(&self) -> Result<Vec<resource::Light>> {
        let map: HashMap<String, resource::Light> =
            parse_response(self.api_request("lights", RequestMethod::Get, None)?)?;
        Ok(map
            .into_iter()
            .map(|(id, light)| light.with_id(id))
            .collect())
    }

    /// Starts searching for new lights.
    ///
    /// The bridge will open the network for 40 seconds. The overall search might take longer since
    /// the configuration of new devices can take longer. If many devices are found the command
    /// will have to be issued a second time after discovery time has elapsed. If the command is
    /// received again during search the search will continue for at least an additional 40
    /// seconds.
    ///
    /// When the search has finished, new lights will be available using the [`get_new_lights`]
    /// function.
    ///
    /// [`get_new_lights`]: #method.get_new_lights
    pub fn search_new_lights(&self, scanner: &resource::light::Scanner) -> Result<()> {
        scanner.execute(self)
    }

    /// Returns discovered lights.
    pub fn get_new_lights(&self) -> Result<resource::Scan> {
        parse_response(self.api_request("lights/new", RequestMethod::Get, None)?)
    }

    /// Deletes a light from the bridge.
    pub fn delete_light<S>(&self, id: S) -> Result<()>
    where
        S: Into<String>,
    {
        let response: Vec<Response<JsonValue>> = self.api_request(
            &format!("lights/{}", id.into()),
            RequestMethod::Delete,
            None,
        )?;
        for i in response {
            i.into_result()?;
        }
        Ok(())
    }

    /// Creates a new group.
    pub fn create_group(&self, creator: &resource::group::Creator) -> Result<String> {
        creator.execute(self)
    }

    /// Modifies attributes of a group.
    pub fn set_group_attribute<S>(
        &self,
        id: S,
        modifier: &resource::group::AttributeModifier,
    ) -> Result<ResponsesModified>
    where
        S: Into<String>,
    {
        modifier.execute(self, id.into())
    }

    /// Modifies the state of a group.
    pub fn set_group_state<S>(
        &self,
        id: S,
        modifier: &resource::group::StateModifier,
    ) -> Result<ResponsesModified>
    where
        S: Into<String>,
    {
        modifier.execute(self, id.into())
    }

    /// Returns a group.
    pub fn get_group<S>(&self, id: S) -> Result<resource::Group>
    where
        S: Into<String>,
    {
        let id = id.into();
        let group: resource::Group = parse_response(self.api_request(
            format!("groups/{}", id),
            RequestMethod::Get,
            None,
        )?)?;
        Ok(group.with_id(id))
    }

    /// Returns all groups.
    pub fn get_all_groups(&self) -> Result<Vec<resource::Group>> {
        let map: HashMap<String, resource::Group> =
            parse_response(self.api_request("groups", RequestMethod::Get, None)?)?;
        Ok(map
            .into_iter()
            .map(|(id, group)| group.with_id(id))
            .collect())
    }

    /// Deletes a group from the bridge.
    pub fn delete_group<S>(&self, id: S) -> Result<()>
    where
        S: Into<String>,
    {
        let response: Vec<Response<JsonValue>> = self.api_request(
            &format!("groups/{}", id.into()),
            RequestMethod::Delete,
            None,
        )?;
        for i in response {
            i.into_result()?;
        }
        Ok(())
    }

    /// Creates a new scene.
    pub fn create_scene(&self, creator: &resource::scene::Creator) -> Result<String> {
        creator.execute(self)
    }

    /// Modifies the state and attributes of a scene.
    pub fn set_scene<S>(
        &self,
        id: S,
        modifier: &resource::scene::Modifier,
    ) -> Result<ResponsesModified>
    where
        S: Into<String>,
    {
        modifier.execute(self, id.into())
    }

    /// Returns a scene.
    pub fn get_scene<S>(&self, id: S) -> Result<resource::Scene>
    where
        S: Into<String>,
    {
        let id = id.into();
        let scene: resource::Scene = parse_response(self.api_request(
            format!("scenes/{}", id),
            RequestMethod::Get,
            None,
        )?)?;
        Ok(scene.with_id(id))
    }

    /// Returns all scenes.
    pub fn get_all_scenes(&self) -> Result<Vec<resource::Scene>> {
        let map: HashMap<String, resource::Scene> =
            parse_response(self.api_request("scenes", RequestMethod::Get, None)?)?;
        Ok(map
            .into_iter()
            .map(|(id, scene)| scene.with_id(id))
            .collect())
    }

    /// Deletes a scene.
    pub fn delete_scene<S>(&self, id: S) -> Result<()>
    where
        S: Into<String>,
    {
        let response: Vec<Response<JsonValue>> = self.api_request(
            &format!("scenes/{}", id.into()),
            RequestMethod::Delete,
            None,
        )?;
        for i in response {
            i.into_result()?;
        }
        Ok(())
    }

    /// Returns the capabilities of resources.
    pub fn get_capabilities(&self) -> Result<resource::Capabilities> {
        parse_response(self.api_request("capabilities", RequestMethod::Get, None)?)
    }

    /// Creates a new schedule and returns the identifier.
    pub fn create_schedule(&self, creator: &resource::schedule::Creator) -> Result<String> {
        creator.execute(&self)
    }

    /// Modifies attributes of a schedule.
    pub fn set_schedule<S>(
        &self,
        id: S,
        modifier: &resource::schedule::Modifier,
    ) -> Result<ResponsesModified>
    where
        S: Into<String>,
    {
        modifier.execute(self, id.into())
    }

    /// Returns a schedule.
    pub fn get_schedule<S>(&self, id: S) -> Result<resource::Schedule>
    where
        S: Into<String>,
    {
        let id = id.into();
        let schedule: resource::Schedule = parse_response(self.api_request(
            format!("schedules/{}", id),
            RequestMethod::Get,
            None,
        )?)?;
        Ok(schedule.with_id(id))
    }

    /// Returns all schedules.
    pub fn get_all_schedules(&self) -> Result<Vec<resource::Schedule>> {
        let map: HashMap<String, resource::Schedule> =
            parse_response(self.api_request("schedules", RequestMethod::Get, None)?)?;
        Ok(map
            .into_iter()
            .map(|(id, schedule)| schedule.with_id(id))
            .collect())
    }

    /// Deletes a schedule.
    pub fn delete_schedule<S>(&self, id: S) -> Result<()>
    where
        S: Into<String>,
    {
        let response: Vec<Response<JsonValue>> = self.api_request(
            &format!("schedules/{}", id.into()),
            RequestMethod::Delete,
            None,
        )?;
        for i in response {
            i.into_result()?;
        }
        Ok(())
    }

    /// Creates a new resourcelink and returns the identifier.
    pub fn create_resourcelink(&self, creator: &resource::resourcelink::Creator) -> Result<String> {
        creator.execute(self)
    }

    /// Modifies attributes of a resourcelink.
    pub fn set_resourcelink<S>(
        &self,
        id: S,
        modifier: &resource::resourcelink::Modifier,
    ) -> Result<ResponsesModified>
    where
        S: Into<String>,
    {
        modifier.execute(self, id.into())
    }

    /// Returns a resourcelink.
    pub fn get_resourcelink<S>(&self, id: S) -> Result<resource::Resourcelink>
    where
        S: Into<String>,
    {
        let id = id.into();
        let resourcelink: resource::Resourcelink = parse_response(self.api_request(
            format!("resourcelinks/{}", id),
            RequestMethod::Get,
            None,
        )?)?;
        Ok(resourcelink.with_id(id))
    }

    /// Returns all resourcelinks.
    pub fn get_all_resourcelinks(&self) -> Result<Vec<resource::Resourcelink>> {
        let map: HashMap<String, resource::Resourcelink> =
            parse_response(self.api_request("resourcelinks", RequestMethod::Get, None)?)?;
        Ok(map
            .into_iter()
            .map(|(id, resourcelink)| resourcelink.with_id(id))
            .collect())
    }

    /// Deletes a resourcelink.
    pub fn delete_resourcelink<S>(&self, id: S) -> Result<()>
    where
        S: Into<String>,
    {
        let response: Vec<Response<JsonValue>> = self.api_request(
            &format!("resourcelinks/{}", id.into()),
            RequestMethod::Delete,
            None,
        )?;
        for i in response {
            i.into_result()?;
        }
        Ok(())
    }

    /// Modifies attributes of a sensor.
    pub fn set_sensor_attribute<S>(
        &self,
        id: S,
        modifier: &resource::sensor::AttributeModifier,
    ) -> Result<ResponsesModified>
    where
        S: Into<String>,
    {
        modifier.execute(self, id.into())
    }

    /// Modifies the state of a sensor.
    pub fn set_sensor_state<S>(
        &self,
        id: S,
        modifier: &resource::sensor::StateModifier,
    ) -> Result<ResponsesModified>
    where
        S: Into<String>,
    {
        modifier.execute(self, id.into())
    }

    /// Modifies the configuration of a sensor.
    pub fn set_sensor_config<S>(
        &self,
        id: S,
        modifier: &resource::sensor::ConfigModifier,
    ) -> Result<ResponsesModified>
    where
        S: Into<String>,
    {
        modifier.execute(self, id.into())
    }

    /// Returns a sensor.
    pub fn get_sensor<S>(&self, id: S) -> Result<resource::Sensor>
    where
        S: Into<String>,
    {
        let id = id.into();
        let sensor: resource::Sensor = parse_response(self.api_request(
            format!("sensors/{}", id),
            RequestMethod::Get,
            None,
        )?)?;
        Ok(sensor.with_id(id))
    }

    /// Returns all sensors that are connected to the bridge.
    pub fn get_all_sensors(&self) -> Result<Vec<resource::Sensor>> {
        let map: HashMap<String, resource::Sensor> =
            parse_response(self.api_request("sensors", RequestMethod::Get, None)?)?;
        Ok(map
            .into_iter()
            .map(|(id, sensor)| sensor.with_id(id))
            .collect())
    }

    /// Starts searching for new sensors.
    ///
    /// The bridge will open the network for 40 seconds. The overall search might take longer since
    /// the configuration of new devices can take longer. If many devices are found the command
    /// will have to be issued a second time after discovery time has elapsed. If the command is
    /// received again during search the search will continue for at least an additional 40
    /// seconds.
    ///
    /// When the search has finished, new sensors will be available using the [`get_new_sensors`]
    /// function.
    ///
    /// [`get_new_sensors`]: #method.get_new_sensors
    pub fn search_new_sensors(&self, scanner: &resource::sensor::Scanner) -> Result<()> {
        scanner.execute(self)
    }

    /// Returns discovered sensors.
    pub fn get_new_sensors(&self) -> Result<resource::Scan> {
        parse_response(self.api_request("senors/new", RequestMethod::Get, None)?)
    }

    /// Deletes a sensor from the bridge.
    pub fn delete_sensor<S>(&self, id: S) -> Result<()>
    where
        S: Into<String>,
    {
        let response: Vec<Response<JsonValue>> = self.api_request(
            &format!("sensors/{}", id.into()),
            RequestMethod::Delete,
            None,
        )?;
        for i in response {
            i.into_result()?;
        }
        Ok(())
    }

    /// Creates a new rule.
    pub fn create_rule(&self, creator: &resource::rule::Creator) -> Result<String> {
        creator.execute(self)
    }

    /// Modifies attributes of a rule.
    pub fn set_rule<S>(
        &self,
        id: S,
        modifier: &resource::rule::Modifier,
    ) -> Result<ResponsesModified>
    where
        S: Into<String>,
    {
        modifier.execute(self, id.into())
    }

    /// Returns a rule.
    pub fn get_rule<S>(&self, id: S) -> Result<resource::Rule>
    where
        S: Into<String>,
    {
        let id = id.into();
        let rule: resource::Rule =
            parse_response(self.api_request(format!("rules/{}", id), RequestMethod::Get, None)?)?;
        Ok(rule.with_id(id))
    }

    /// Returns all rules.
    pub fn get_all_rules(&self) -> Result<Vec<resource::Rule>> {
        let map: HashMap<String, resource::Rule> =
            parse_response(self.api_request("rules", RequestMethod::Get, None)?)?;
        Ok(map.into_iter().map(|(id, rule)| rule.with_id(id)).collect())
    }

    /// Deletes a rule.
    pub fn delete_rule<S>(&self, id: S) -> Result<()>
    where
        S: Into<String>,
    {
        let response: Vec<Response<JsonValue>> =
            self.api_request(&format!("rules/{}", id.into()), RequestMethod::Delete, None)?;
        for i in response {
            i.into_result()?;
        }
        Ok(())
    }
}