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
//! # yeelight
//!
//!  This crate provides rust bindings for [yeelight WiFi light interoperation spec][1].
//!  To communicate with the bulbs LAN mode has to be enabled through the official yeelight app.
//!
//! [1]: https://www.yeelight.com/download/Yeelight_Inter-Operation_Spec.pdf
//!

use std::collections::HashMap;
use std::error::Error;
use std::net::SocketAddr;
use std::sync::Arc;

use serde::{Deserialize, Serialize};

use tokio::net::{tcp::OwnedReadHalf, TcpListener, TcpStream};
use tokio::sync::{mpsc, Mutex};
use tokio::task::spawn;

#[cfg(feature = "from-str")]
use itertools::Itertools;

mod reader;
mod writer;

pub use reader::{BulbError, Notification, Response};

use reader::{NotifyChan, Reader};
use writer::Writer;

#[derive(Debug)]
struct Message(u64, String);

/// Bulb connection
pub struct Bulb {
    notify_chan: NotifyChan,
    writer: writer::Writer,
}

impl Bulb {
    /// Attach to existing `std::net::TcpStream`.
    ///
    /// # Example
    /// ```
    /// # async fn test() {
    /// # use yeelight::Bulb;
    /// let stream = std::net::TcpStream::connect("192.168.1.204:55443")
    ///     .expect("Connection failed");
    /// let mut bulb = Bulb::attach(stream).unwrap();
    /// bulb.toggle().await.unwrap();
    /// # }
    /// ```
    pub fn attach(stream: ::std::net::TcpStream) -> Result<Self, Box<dyn Error>> {
        let stream = TcpStream::from_std(stream)?;

        Ok(Self::attach_tokio(stream))
    }

    /// Same as `attach(stream: std::net::TcpStream)` but for `tokio::net::TcpStream`;
    pub fn attach_tokio(stream: TcpStream) -> Self {
        let (reader, writer, reader_half, notify_chan) = Self::build_rw(stream);

        spawn(reader.start(reader_half));

        Self {
            notify_chan,
            writer,
        }
    }

    /// Connect to bulb at the specified address and port.
    ///
    /// If `port` is 0, the default value (55443) is used.
    ///
    /// # Example
    /// ```
    /// # async fn test() {
    /// # use yeelight::Bulb;
    /// let my_bulb_ip = "192.168.1.204";
    /// let mut bulb = Bulb::connect(my_bulb_ip, 55443).await
    ///     .expect("Connection failed");
    /// bulb.toggle().await.unwrap();
    /// # }
    /// ```
    pub async fn connect(addr: &str, mut port: u16) -> Result<Self, Box<dyn Error>> {
        if port == 0 {
            port = 55443
        }

        let stream = TcpStream::connect(format!("{}:{}", addr, port)).await?;

        let (reader, writer, reader_half, notify_chan) = Self::build_rw(stream);

        spawn(reader.start(reader_half));

        Ok(Self {
            notify_chan,
            writer,
        })
    }

    fn build_rw(stream: TcpStream) -> (Reader, Writer, OwnedReadHalf, NotifyChan) {
        let (reader_half, writer_half) = stream.into_split();

        let resp_chan = HashMap::new();
        let resp_chan = Arc::new(Mutex::new(resp_chan));
        let notify_chan = Arc::new(Mutex::new(None));

        let reader = Reader::new(resp_chan.clone(), notify_chan.clone());
        let writer = Writer::new(writer_half, resp_chan);

        (reader, writer, reader_half, notify_chan)
    }

    /// Do not wait for response from the bulb (all commands will return None)
    ///
    /// # Example
    /// ```
    /// # async fn test() {
    /// # use yeelight::Bulb;
    /// let my_bulb_ip = "192.168.1.204";
    /// let mut bulb = Bulb::connect(my_bulb_ip, 55443).await
    ///     .expect("Connection failed").no_response();
    /// let response = bulb.toggle().await.unwrap(); // response will be `None`
    /// # }
    /// ```
    pub fn no_response(mut self) -> Self {
        self.writer.set_get_response(false);
        self
    }

    pub fn get_response(mut self) -> Self {
        self.writer.set_get_response(true);
        self
    }

    pub async fn set_notify(&mut self, chan: mpsc::Sender<Notification>) {
        self.notify_chan.lock().await.replace(chan);
    }

    pub async fn get_notify(&mut self) -> mpsc::Receiver<Notification> {
        let (sender, receiver) = mpsc::channel(10);
        self.set_notify(sender).await;
        receiver
    }

    /// Establishes a Music mode connection with bulb.
    ///
    /// This method returns another `Bulb` object to send commands to the bulb in music mode. Note
    /// that all commands send to the bulb get no response and produce no notification message, so
    /// there is no way to know if the command was executed successfully by the bulb.
    pub async fn start_music(&mut self, host: &str, port: u16) -> Result<Self, Box<dyn Error>> {
        let addr = format!("127.0.0.1:{}", port).parse::<SocketAddr>()?;
        let mut listener = TcpListener::bind(&addr).await?;

        self.set_music(MusicAction::On, host, port).await?;

        let (socket, _) = listener.accept().await?;
        Ok(Self::attach_tokio(socket).no_response())
    }
}

/// Error generated when parsing value from string.
#[cfg(feature = "from-str")]
#[derive(Debug)]
pub struct ParseError(String);

#[cfg(feature = "from-str")]
impl ToString for ParseError {
    fn to_string(&self) -> String {
        self.0.to_string()
    }
}

#[cfg(feature = "from-str")]
impl From<::std::num::ParseIntError> for ParseError {
    fn from(e: ::std::num::ParseIntError) -> Self {
        ParseError(e.to_string())
    }
}

trait Stringify {
    fn stringify(&self) -> String;
}

impl Stringify for str {
    fn stringify(&self) -> String {
        format!("\"{}\"", self)
    }
}

macro_rules! stringify_nums {
    ($($type:ty),*) => {
        $(
        impl Stringify for $type {
            fn stringify(&self) -> String {
                self.to_string()
            }
        }
        )*
    };
}

stringify_nums!(u8, u16, u32, u64, i8);

// Create enum and its ToString implementation using stringify (quoted strings)
macro_rules! enum_str {
    ($name:ident: $($variant:ident -> $val:literal),* $(,)?) => {

        #[derive(Debug, Clone, Copy, Serialize, Deserialize)]
        pub enum $name {
            $($variant),*
        }

        impl ::std::fmt::Display for $name {
            fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
                match *self {
                    $($name::$variant => write!(f, stringify!($val)),)+
                }
            }
        }

        impl Stringify for $name {
            fn stringify(&self) -> String {
                self.to_string()
            }
        }

        #[cfg(feature="from-str")]
        impl ::std::str::FromStr for $name {
            type Err = ParseError;

            fn from_str(s: &str) -> Result<Self,Self::Err> {
                match s {
                    $(stringify!($variant) |
                    _ if s.eq_ignore_ascii_case(stringify!($variant)) => Ok($name::$variant),)+
                    _                => Err(ParseError({
                                            let v = vec![
                                                $(stringify!($variant),)+
                                            ];
                                            format!("Could not parse {} \n Valid values:{}", s,
                                                v.iter().fold(String::new(), |a, i| {
                                                    a + &format!(" {}", i)[..]
                                                }))
                                        }))
                }
            }
        }

        #[cfg(feature="from-str")]
        impl $name {
            #[allow(dead_code)]
            pub fn variants() -> Vec<&'static str> {
                vec![
                    $(stringify!($variant),)+
                ]
            }
        }

    };
}

enum_str!(Property:
    Power -> "power",
    Bright -> "bright",
    CT -> "ct",
    RGB -> "rgb",
    Hue -> "hue",
    Sat -> "sat",
    ColorMode -> "color_mode",
    Flowing -> "flowing",
    DelayOff -> "delayoff",
    FlowParams -> "flow_params",
    MusicOn -> "music_on",
    Name -> "name",
    BgPower -> "bg_power",
    BgFlowing -> "bg_flowing",
    BgFlowParams -> "bg_flow_params",
    BgCT -> "bg_ct",
    BgColorMode -> "bg_lmode",
    BgBright -> "bg_bright",
    BgRGB -> "bg_rgb",
    BgHue -> "bg_hue",
    BgSat -> "bg_sat",
    NightLightBright -> "nl_br",
    ActiveMode -> "active_mode",
);

enum_str!(Power:
    On -> "on",
    Off -> "off",
);
enum_str!(Effect:
    Sudden -> "sudden",
    Smooth -> "smooth",
);
enum_str!(Prop:
    Bright -> "bright",
    CT -> "ct",
    Color -> "color",
);
enum_str!(Class:
    Color -> "color",
    HSV -> "hsv",
    CT -> "ct",
    CF -> "cf",
    AutoDelayOff -> "auto_delay_off",
);
enum_str!(Mode:
    Normal -> 0,
    CT -> 1,
    RGB -> 2,
    HSV -> 3,
    CF -> 4,
    NightLight -> 5,
);
enum_str!(CronType:
    Off -> 0,
);
enum_str!(CfAction:
    Recover -> 0,
    Stay -> 1,
    Off -> 2,
);
enum_str!(AdjustAction:
    Increase -> "increase",
    Decrease -> "decrease",
    Circle -> "circle",
);
enum_str!(MusicAction:
    Off -> 0,
    On -> 1,
);
enum_str!(FlowMode:
    Color -> 1,
    CT -> 2,
    Sleep -> 7,
);

/// State Change used to build [`FlowExpresion`](struct.FlowExpresion.html)s
///
/// The state change can be either: color (rgb), color temperature (ct) or sleep.
///
#[derive(Debug, Serialize, Deserialize)]
pub struct FlowTuple {
    pub duration: u64,
    pub mode: FlowMode,
    pub value: u32,
    pub brightness: i8,
}

impl FlowTuple {
    /// Create FlowTuple specifying the mode as a parameter
    /// # Arguments
    ///
    /// * `duration`: duration of change in milliseconds.
    /// * `mode`: [`FlowMode`](enum.FlowMode.html) Color / CT / Sleep.
    /// * `value`: RGB color for color mode, CT for ct mode (ignored by sleep)
    /// * `brightness`: percentage (`1` to `100`) `-1` to keep previous value (ignored by sleep)
    ///
    pub fn new(duration: u64, mode: FlowMode, value: u32, brightness: i8) -> Self {
        Self {
            duration,
            mode,
            value,
            brightness,
        }
    }

    /// Create RGB FlowTuple
    ///
    /// # Arguments
    ///
    /// * `duration`: duration of change in milliseconds.
    /// * `rgb`: color in RGB format (`0x00_00_00` to `0xff_ff_ff`)
    /// * `brightness`: percentage (`1` to `100`) `-1` to keep previous value.
    ///
    pub fn rgb(duration: u64, rgb: u32, brightness: i8) -> Self {
        Self {
            duration,
            mode: FlowMode::Color,
            value: rgb,
            brightness,
        }
    }

    /// Create Color Temperature FlowTuple
    ///
    /// # Arguments
    ///
    /// * `duration`: duration of change in milliseconds.
    /// * `ct`: color temperature (`1700` to `6500`) K (may vary between models).
    /// * `brightness`: percentage (`1` to `100`) or `-1` to keep previous value.
    ///
    pub fn ct(duration: u64, ct: u32, brightness: i8) -> Self {
        Self {
            duration,
            mode: FlowMode::CT,
            value: ct,
            brightness,
        }
    }

    /// Create Sleep FlowTuple
    ///
    /// # Arguments
    ///
    /// * `duration`: time to sleep in milliseconds
    ///
    pub fn sleep(duration: u64) -> Self {
        Self {
            duration,
            mode: FlowMode::Sleep,
            value: 0,
            brightness: -1,
        }
    }
}

impl ToString for FlowTuple {
    fn to_string(&self) -> String {
        format!(
            "{},{},{},{}",
            self.duration,
            self.mode.to_string(),
            self.value,
            self.brightness
        )
    }
}

/// FlowExpresion consisting of a series of `FlowTuple`s
///
/// # Example
///```
///# use yeelight::{FlowTuple, FlowExpresion};
/// let duration = 1000; // miliseconds
/// let brightness = 100; // percentage 1..100 (-1 to keep previous)
///
/// let police = FlowExpresion(vec![
///     FlowTuple::rgb(duration, 0xff_00_00, brightness),
///     FlowTuple::rgb(duration, 0x00_00_ff, brightness),
/// ]);
///
/// let police2 = FlowExpresion(vec![
///     FlowTuple::rgb(duration, 0xff_00_00, brightness),
///     FlowTuple::rgb(duration, 0xff_00_00, 1),
///     FlowTuple::rgb(duration, 0xff_00_00, brightness),
///     FlowTuple::sleep(duration),
///     FlowTuple::rgb(duration, 0x00_00_ff, brightness),
///     FlowTuple::rgb(duration, 0x00_00_ff, 1),
///     FlowTuple::rgb(duration, 0x00_00_ff, brightness),
///     FlowTuple::sleep(duration),
/// ]);
///```
#[derive(Debug, Serialize, Deserialize)]
pub struct FlowExpresion(pub Vec<FlowTuple>);

impl Stringify for FlowExpresion {
    fn stringify(&self) -> String {
        let mut s = '"'.to_string();
        for tuple in self.0.iter() {
            s.push_str(&tuple.to_string());
            s.push(',');
        }
        s.pop();
        s.push('"');
        s
    }
}

#[cfg(feature = "from-str")]
impl ::std::str::FromStr for FlowExpresion {
    type Err = ParseError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut v = Vec::new();
        for (duration, mode, value, brightness) in s.split(',').tuples() {
            let duration = duration.parse::<u64>()?;
            let value = value.parse::<u32>()?;
            let mode = match FlowMode::from_str(mode) {
                Ok(m) => Ok(m),
                Err(_) => match mode {
                    "1" => Ok(FlowMode::Color),
                    "2" => Ok(FlowMode::CT),
                    "7" => Ok(FlowMode::Sleep),
                    _ => Err(ParseError(format!(
                        "Could not parse FlowMode: {}\nvalid values: 1 (Color), 2(CT), 7(Sleep)",
                        mode.to_string()
                    ))),
                },
            }?;
            let brightness = brightness.parse::<i8>()?;
            v.push(FlowTuple {
                duration,
                mode,
                value,
                brightness,
            });
        }
        Ok(FlowExpresion(v))
    }
}

/// List of `Property` (used by `get_prop`)
///
/// # Example
///```
///# use yeelight::{Properties, Property};
/// let props = Properties(vec![
///     Property::Name,
///     Property::Power,
///     Property::Bright,
///     Property::CT,
///     Property::RGB,
///     Property::ColorMode,
///     Property::Flowing,
/// ]);
///```
#[derive(Debug, Serialize, Deserialize)]
pub struct Properties(pub Vec<Property>);

impl Stringify for Properties {
    fn stringify(&self) -> String {
        self.0
            .iter()
            .map(|x| x.to_string())
            .collect::<Vec<String>>()
            .join(",")
    }
}

// Convert function parameters into comma separated string
macro_rules! params {
    ($($v:tt),+) => {
        vec!( $( $v.stringify() ),+ ).join(", ")
    };
    () => {""};
}

// Generate function
macro_rules! gen_func {
    ($name:ident - $( $p:ident : $t:ty ),* ) => {

            pub async fn $name(&mut self, $($p : $t),*) -> Result<Option<Response>, BulbError> {
                self.writer.send(
                    &stringify!($name), &params!($($p),*)
                ).await
            }

    };
    ($fn_default:ident / $fn_bg:ident - $( $p:ident : $t:ty ),* ) => {

        gen_func!($fn_default - $($p : $t),*);
        gen_func!($fn_bg - $($p : $t),*);

    };
    ($name:ident) => { gen_func!($name - ); };
    ($fn_default:ident / $fn_bg:ident) => { gen_func!($fn_default / $fn_bg - ); };
}

/// # Messages
///
/// This are all the methods as by the yeelight API spec.
/// They all take the parameters specified by the spec, build a message, send
/// it to the bulb and wait for the response which is parsed into a
/// [`Response`].
///
/// ## Example
///
/// ```
/// # async fn test() {
/// # use yeelight::*;
/// let mut bulb = Bulb::connect("192.168.1.204", 0).await.expect("Connection failed");
/// let response = bulb.set_power(Power::On, Effect::Smooth, 1000, Mode::Normal).await.unwrap();
///
/// match response {
///     Some(vec) => {
///         // In this case, the response should be ["ok"].
///         for v in vec.iter() {
///             println!("{}", v);
///         }
///     },
///     None => eprintln!("This should not happen"),
/// }
/// # }
/// ```
///
/// [`Response`]: enum.Response.html
#[rustfmt::skip]
impl Bulb {
    gen_func!(get_prop                          - properties: &Properties);

    gen_func!(set_power     / bg_set_power      - power: Power,         effect: Effect, duration: u64, mode: Mode);
    gen_func!(toggle        / bg_toggle);
    gen_func!(dev_toggle);

    gen_func!(set_ct_abx    / bg_set_ct_abx     - ct_value: u16,        effect: Effect, duration: u64);
    gen_func!(set_rgb       / bg_set_rgb        - rgb_value: u32,       effect: Effect, duration: u64);
    gen_func!(set_hsv       / bg_set_hsv        - hue: u16, sat: u8,    effect: Effect, duration: u64);
    gen_func!(set_bright    / bg_set_bright     - brightness: u8,       effect: Effect, duration: u64);
    gen_func!(set_scene     / bg_set_scene      - class: Class,         val1: u64, val2: u64, val3: u64);

    gen_func!(start_cf      / bg_start_cf       - count: u8, action: CfAction, flow_expression: FlowExpresion);
    gen_func!(stop_cf       / bg_stop_cf);

    gen_func!(set_adjust    / bg_set_adjust     - action: AdjustAction, prop: Prop);
    gen_func!(adjust_bright / bg_adjust_bright  - percentage: i8, duration: u64);
    gen_func!(adjust_ct     / bg_adjust_ct      - percentage: i8, duration: u64);
    gen_func!(adjust_color  / bg_adjust_color   - percentage: i8, duration: u64);

    gen_func!(set_default   / bg_set_default);

    gen_func!(set_name                          - name: &str);
    gen_func!(set_music                         - action: MusicAction, host: &str, port: u16);

    gen_func!(cron_add                          - cron_type: CronType, value: u64);
    gen_func!(cron_del                          - cron_type: CronType);
    // gen_func!(cron_get                          - cron_type: CronType);
    // cron_get response is a dictionary which is difficult to parse,
    // instead use delayoff property which should give the same values.
    pub async fn cron_get(&mut self, _cron_type: CronType) -> Result<Option<Response>, BulbError> {
        self.get_prop(&Properties(vec![Property::DelayOff])).await
    }
}