use crate::command::Command;
use crate::types::{ColorTemperature, Dimmer, FadeDuration, HsbColor};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DimmerCommand {
Get,
Set(Dimmer),
Increase,
Decrease,
Minimum,
Maximum,
Stop,
}
impl DimmerCommand {
#[must_use]
pub const fn set(value: Dimmer) -> Self {
Self::Set(value)
}
}
impl Command for DimmerCommand {
fn name(&self) -> String {
"Dimmer".to_string()
}
fn payload(&self) -> Option<String> {
match self {
Self::Get => None,
Self::Set(dim) => Some(dim.value().to_string()),
Self::Increase => Some("+".to_string()),
Self::Decrease => Some("-".to_string()),
Self::Minimum => Some("<".to_string()),
Self::Maximum => Some(">".to_string()),
Self::Stop => Some("!".to_string()),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ColorTemperatureCommand {
Get,
Set(ColorTemperature),
Increase,
Decrease,
}
impl ColorTemperatureCommand {
#[must_use]
pub const fn set(value: ColorTemperature) -> Self {
Self::Set(value)
}
}
impl Command for ColorTemperatureCommand {
fn name(&self) -> String {
"CT".to_string()
}
fn payload(&self) -> Option<String> {
match self {
Self::Get => None,
Self::Set(ct) => Some(ct.value().to_string()),
Self::Increase => Some("+".to_string()),
Self::Decrease => Some("-".to_string()),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HsbColorCommand {
Get,
Set(HsbColor),
SetHue(u16),
SetSaturation(u8),
SetBrightness(u8),
}
impl HsbColorCommand {
#[must_use]
pub const fn set(color: HsbColor) -> Self {
Self::Set(color)
}
#[must_use]
pub const fn hue(value: u16) -> Self {
Self::SetHue(value)
}
#[must_use]
pub const fn saturation(value: u8) -> Self {
Self::SetSaturation(value)
}
#[must_use]
pub const fn brightness(value: u8) -> Self {
Self::SetBrightness(value)
}
}
impl Command for HsbColorCommand {
fn name(&self) -> String {
match self {
Self::Get | Self::Set(_) => "HSBColor".to_string(),
Self::SetHue(_) => "HSBColor1".to_string(),
Self::SetSaturation(_) => "HSBColor2".to_string(),
Self::SetBrightness(_) => "HSBColor3".to_string(),
}
}
fn payload(&self) -> Option<String> {
match self {
Self::Get => None,
Self::Set(color) => Some(color.to_command_string()),
Self::SetHue(h) => Some(h.to_string()),
Self::SetSaturation(s) => Some(s.to_string()),
Self::SetBrightness(b) => Some(b.to_string()),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FadeDurationCommand {
Get,
Set(FadeDuration),
Increase,
Decrease,
}
impl FadeDurationCommand {
#[must_use]
pub const fn set(value: FadeDuration) -> Self {
Self::Set(value)
}
}
impl Command for FadeDurationCommand {
fn name(&self) -> String {
"Speed".to_string()
}
fn payload(&self) -> Option<String> {
match self {
Self::Get => None,
Self::Set(duration) => Some(duration.value().to_string()),
Self::Increase => Some("+".to_string()),
Self::Decrease => Some("-".to_string()),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct StateCommand;
impl Command for StateCommand {
fn name(&self) -> String {
"State".to_string()
}
fn payload(&self) -> Option<String> {
None
}
}
#[cfg(test)]
mod tests {
use std::time::Duration;
use super::*;
#[test]
fn dimmer_command_set() {
let cmd = DimmerCommand::Set(Dimmer::new(50).unwrap());
assert_eq!(cmd.name(), "Dimmer");
assert_eq!(cmd.payload(), Some("50".to_string()));
}
#[test]
fn dimmer_command_adjustments() {
assert_eq!(DimmerCommand::Increase.payload(), Some("+".to_string()));
assert_eq!(DimmerCommand::Decrease.payload(), Some("-".to_string()));
assert_eq!(DimmerCommand::Minimum.payload(), Some("<".to_string()));
assert_eq!(DimmerCommand::Maximum.payload(), Some(">".to_string()));
assert_eq!(DimmerCommand::Stop.payload(), Some("!".to_string()));
}
#[test]
fn color_temp_command_set() {
let cmd = ColorTemperatureCommand::Set(ColorTemperature::COOL);
assert_eq!(cmd.name(), "CT");
assert_eq!(cmd.payload(), Some("153".to_string()));
}
#[test]
fn color_temp_command_adjustments() {
assert_eq!(
ColorTemperatureCommand::Increase.payload(),
Some("+".to_string())
);
assert_eq!(
ColorTemperatureCommand::Decrease.payload(),
Some("-".to_string())
);
}
#[test]
fn hsb_color_command_set() {
let cmd = HsbColorCommand::Set(HsbColor::red());
assert_eq!(cmd.name(), "HSBColor");
assert_eq!(cmd.payload(), Some("0,100,100".to_string()));
}
#[test]
fn hsb_color_command_individual() {
assert_eq!(HsbColorCommand::SetHue(120).name(), "HSBColor1");
assert_eq!(
HsbColorCommand::SetHue(120).payload(),
Some("120".to_string())
);
assert_eq!(HsbColorCommand::SetSaturation(50).name(), "HSBColor2");
assert_eq!(
HsbColorCommand::SetSaturation(50).payload(),
Some("50".to_string())
);
assert_eq!(HsbColorCommand::SetBrightness(75).name(), "HSBColor3");
assert_eq!(
HsbColorCommand::SetBrightness(75).payload(),
Some("75".to_string())
);
}
#[test]
fn fade_duration_command_set() {
let cmd = FadeDurationCommand::Set(FadeDuration::new(Duration::from_secs(20)).unwrap());
assert_eq!(cmd.name(), "Speed");
assert_eq!(cmd.payload(), Some("40".to_string()));
}
#[test]
fn fade_duration_command_adjustments() {
assert_eq!(
FadeDurationCommand::Increase.payload(),
Some("+".to_string())
);
assert_eq!(
FadeDurationCommand::Decrease.payload(),
Some("-".to_string())
);
}
#[test]
fn state_command() {
let cmd = StateCommand;
assert_eq!(cmd.name(), "State");
assert_eq!(cmd.payload(), None);
assert_eq!(cmd.to_http_command(), "State");
assert_eq!(cmd.mqtt_topic_suffix(), "State");
assert_eq!(cmd.mqtt_payload(), "");
}
}