sat-rs/satrs-core/src/power.rs

55 lines
1.6 KiB
Rust
Raw Normal View History

2023-02-05 14:15:48 +01:00
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
2023-02-05 01:18:23 +01:00
2023-02-05 14:15:48 +01:00
/// Generic trait for a device capable of switching itself on or off.
2023-02-05 01:18:23 +01:00
pub trait PowerSwitch {
2023-02-05 14:15:48 +01:00
type Error;
fn switch_on(&mut self) -> Result<(), Self::Error>;
fn switch_off(&mut self) -> Result<(), Self::Error>;
2023-02-05 01:18:23 +01:00
fn is_switch_on(&self) -> bool {
self.switch_state() == SwitchState::On
}
fn switch_state(&self) -> SwitchState;
}
2023-02-05 14:15:48 +01:00
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2023-02-05 01:18:23 +01:00
pub enum SwitchState {
Off = 0,
On = 1,
2023-02-05 14:15:48 +01:00
Unknown = 2,
Faulty = 3
2023-02-05 01:18:23 +01:00
}
2023-02-05 14:15:48 +01:00
pub type SwitchId = u16;
/// Generic trait for a device capable of turning on and off switches.
2023-02-05 01:18:23 +01:00
pub trait PowerSwitcher {
2023-02-05 14:15:48 +01:00
type Error;
2023-02-05 01:18:23 +01:00
2023-02-05 14:15:48 +01:00
fn send_switch_on_cmd(&mut self, switch_id: SwitchId) -> Result<(), Self::Error>;
fn send_switch_off_cmd(&mut self, switch_id: SwitchId) -> Result<(), Self::Error>;
2023-02-05 01:18:23 +01:00
2023-02-05 14:15:48 +01:00
fn switch_on<T: PowerSwitch>(&mut self, switch: &mut T) -> Result<(), <T as PowerSwitch>::Error> {
switch.switch_on()
2023-02-05 01:18:23 +01:00
}
2023-02-05 14:15:48 +01:00
fn switch_off<T: PowerSwitch>(&mut self, switch: &mut T) -> Result<(), <T as PowerSwitch>::Error> {
switch.switch_off()
}
/// Retrieve the switch state
fn get_switch_state(&mut self, switch_id: SwitchId) -> SwitchState;
fn get_is_switch_on(&mut self, switch_id: SwitchId) -> bool {
self.get_switch_state(switch_id) == SwitchState::On
}
/// The maximum delay it will take to change a switch.
///
/// This may take into account the time to send a command, wait for it to be executed, and
/// see the switch changed.
fn switch_delay_ms(&self) -> u32;
2023-02-05 01:18:23 +01:00
}