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
use ambassador::{delegatable_trait, Delegate};
use derive_more::From;
use serde_derive::{Deserialize, Serialize};
use strum_macros::IntoStaticStr;
use validator::Validate;

use super::{default_false, ColorOrder};

#[delegatable_trait]
pub trait DeviceConfig: Sync + Send {
    fn hardware_led_count(&self) -> usize;

    fn rewrite_time(&self) -> Option<std::time::Duration> {
        None
    }

    fn latch_time(&self) -> std::time::Duration {
        Default::default()
    }
}

macro_rules! impl_device_config {
    ($t:ty) => {
        impl DeviceConfig for $t {
            fn hardware_led_count(&self) -> usize {
                self.hardware_led_count as _
            }

            fn rewrite_time(&self) -> Option<std::time::Duration> {
                if self.rewrite_time == 0 {
                    None
                } else {
                    Some(std::time::Duration::from_millis(self.rewrite_time as _))
                }
            }

            fn latch_time(&self) -> std::time::Duration {
                std::time::Duration::from_millis(self.latch_time as _)
            }
        }
    };
}

#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum DummyDeviceMode {
    Text,
    Ansi,
}

impl Default for DummyDeviceMode {
    fn default() -> Self {
        Self::Text
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Validate)]
#[serde(default, rename_all = "camelCase", deny_unknown_fields)]
pub struct Dummy {
    #[validate(range(min = 1))]
    pub hardware_led_count: u32,
    pub rewrite_time: u32,
    pub latch_time: u32,
    pub mode: DummyDeviceMode,
}

impl_device_config!(Dummy);

impl Default for Dummy {
    fn default() -> Self {
        Self {
            hardware_led_count: 1,
            rewrite_time: 0,
            latch_time: 0,
            mode: Default::default(),
        }
    }
}

fn default_ws_spi_rate() -> i32 {
    3000000
}

fn default_ws_spi_rewrite_time() -> u32 {
    1000
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Validate)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct Ws2812Spi {
    #[serde(default = "Default::default")]
    pub color_order: ColorOrder,
    #[validate(range(min = 1))]
    pub hardware_led_count: u32,
    #[serde(default = "default_false")]
    pub invert: bool,
    #[serde(default = "Default::default")]
    pub latch_time: u32,
    pub output: String,
    #[serde(default = "default_ws_spi_rate")]
    pub rate: i32,
    #[serde(default = "default_ws_spi_rewrite_time")]
    pub rewrite_time: u32,
}

impl_device_config!(Ws2812Spi);

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Validate)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct PhilipsHue {
    pub black_lights_timeout: i32,
    pub brightness_factor: f32,
    pub brightness_max: f32,
    pub brightness_min: f32,
    pub brightness_threshold: f32,
    #[serde(rename = "clientkey")]
    pub client_key: String,
    pub color_order: ColorOrder,
    pub debug_level: String,
    pub debug_streamer: bool,
    pub group_id: i32,
    #[validate(range(min = 1))]
    pub hardware_led_count: u32,
    pub light_ids: Vec<String>,
    pub output: String,
    pub restore_original_state: bool,
    #[serde(rename = "sslHSTimeoutMax")]
    pub ssl_hs_timeout_max: i32,
    #[serde(rename = "sslHSTimeoutMin")]
    pub ssl_hs_timeout_min: i32,
    pub ssl_read_timeout: i32,
    pub switch_off_on_black: bool,
    #[serde(rename = "transitiontime")]
    pub transition_time: f32,
    #[serde(rename = "useEntertainmentAPI")]
    pub use_entertainment_api: bool,
    pub username: String,
    pub verbose: bool,
}

impl DeviceConfig for PhilipsHue {
    fn hardware_led_count(&self) -> usize {
        self.hardware_led_count as _
    }
}

fn default_file_rewrite_time() -> u32 {
    1000
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Validate)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct File {
    #[serde(default = "Default::default")]
    pub color_order: ColorOrder,
    #[validate(range(min = 1))]
    pub hardware_led_count: u32,
    #[serde(default = "Default::default")]
    pub latch_time: u32,
    pub output: String,
    #[serde(default = "default_file_rewrite_time")]
    pub rewrite_time: u32,
    #[serde(default = "Default::default")]
    pub print_time_stamp: bool,
}

impl DeviceConfig for File {
    fn hardware_led_count(&self) -> usize {
        self.hardware_led_count as _
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, IntoStaticStr, Delegate, From)]
#[serde(rename_all = "lowercase", tag = "type", deny_unknown_fields)]
#[delegate(DeviceConfig)]
pub enum Device {
    Dummy(Dummy),
    Ws2812Spi(Ws2812Spi),
    PhilipsHue(PhilipsHue),
    File(File),
}

impl Default for Device {
    fn default() -> Self {
        Self::Dummy(Dummy::default())
    }
}

impl Validate for Device {
    fn validate(&self) -> Result<(), validator::ValidationErrors> {
        match self {
            Device::Dummy(device) => device.validate(),
            Device::Ws2812Spi(device) => device.validate(),
            Device::PhilipsHue(device) => device.validate(),
            Device::File(device) => device.validate(),
        }
    }
}