use crate::command::Command;
use crate::types::{Scheme, WakeupDuration};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SchemeCommand {
Get,
Set(Scheme),
}
impl SchemeCommand {
#[must_use]
pub const fn set(scheme: Scheme) -> Self {
Self::Set(scheme)
}
}
impl Command for SchemeCommand {
fn name(&self) -> String {
"Scheme".to_string()
}
fn payload(&self) -> Option<String> {
match self {
Self::Get => None,
Self::Set(scheme) => Some(scheme.value().to_string()),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WakeupDurationCommand {
Get,
Set(WakeupDuration),
}
impl WakeupDurationCommand {
#[must_use]
pub const fn set(duration: WakeupDuration) -> Self {
Self::Set(duration)
}
}
impl Command for WakeupDurationCommand {
fn name(&self) -> String {
"WakeupDuration".to_string()
}
fn payload(&self) -> Option<String> {
match self {
Self::Get => None,
Self::Set(duration) => Some(duration.seconds().to_string()),
}
}
}
#[cfg(test)]
mod tests {
use std::time::Duration;
use super::*;
#[test]
fn scheme_command_get() {
let cmd = SchemeCommand::Get;
assert_eq!(cmd.name(), "Scheme");
assert_eq!(cmd.payload(), None);
}
#[test]
fn scheme_command_set() {
let cmd = SchemeCommand::Set(Scheme::WAKEUP);
assert_eq!(cmd.name(), "Scheme");
assert_eq!(cmd.payload(), Some("1".to_string()));
}
#[test]
fn scheme_command_all_values() {
assert_eq!(
SchemeCommand::Set(Scheme::SINGLE).payload(),
Some("0".to_string())
);
assert_eq!(
SchemeCommand::Set(Scheme::WAKEUP).payload(),
Some("1".to_string())
);
assert_eq!(
SchemeCommand::Set(Scheme::CYCLE_UP).payload(),
Some("2".to_string())
);
assert_eq!(
SchemeCommand::Set(Scheme::CYCLE_DOWN).payload(),
Some("3".to_string())
);
assert_eq!(
SchemeCommand::Set(Scheme::RANDOM).payload(),
Some("4".to_string())
);
}
#[test]
fn scheme_command_http() {
let cmd = SchemeCommand::Set(Scheme::RANDOM);
assert_eq!(cmd.to_http_command(), "Scheme 4");
}
#[test]
fn scheme_command_mqtt() {
let cmd = SchemeCommand::Set(Scheme::RANDOM);
assert_eq!(cmd.mqtt_topic_suffix(), "Scheme");
assert_eq!(cmd.mqtt_payload(), "4");
}
#[test]
fn wakeup_duration_command_get() {
let cmd = WakeupDurationCommand::Get;
assert_eq!(cmd.name(), "WakeupDuration");
assert_eq!(cmd.payload(), None);
}
#[test]
fn wakeup_duration_command_set() {
let duration = WakeupDuration::new(Duration::from_secs(300)).unwrap();
let cmd = WakeupDurationCommand::Set(duration);
assert_eq!(cmd.name(), "WakeupDuration");
assert_eq!(cmd.payload(), Some("300".to_string()));
}
#[test]
fn wakeup_duration_command_http() {
let duration = WakeupDuration::new(Duration::from_secs(60)).unwrap();
let cmd = WakeupDurationCommand::Set(duration);
assert_eq!(cmd.to_http_command(), "WakeupDuration 60");
}
#[test]
fn wakeup_duration_command_mqtt() {
let duration = WakeupDuration::new(Duration::from_secs(60)).unwrap();
let cmd = WakeupDurationCommand::Set(duration);
assert_eq!(cmd.mqtt_topic_suffix(), "WakeupDuration");
assert_eq!(cmd.mqtt_payload(), "60");
}
}