60 lines
1.2 KiB
Rust
60 lines
1.2 KiB
Rust
|
use anyhow::Result;
|
||
|
|
||
|
pub struct Tasmota {
|
||
|
device: String,
|
||
|
}
|
||
|
|
||
|
impl Tasmota {
|
||
|
pub fn new(device: impl ToString) -> Self {
|
||
|
Self {
|
||
|
device: device.to_string(),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub fn name(&self) -> &str {
|
||
|
&self.device
|
||
|
}
|
||
|
|
||
|
fn command(&self, command: &str) -> String {
|
||
|
format!("http://{}/cm?cmnd={}", self.device, command)
|
||
|
}
|
||
|
|
||
|
async fn post(&self, command: &str) -> Result<String> {
|
||
|
Ok(reqwest::Client::new()
|
||
|
.post(&self.command(command))
|
||
|
.send()
|
||
|
.await?
|
||
|
.text()
|
||
|
.await?)
|
||
|
}
|
||
|
|
||
|
async fn get(&self, command: &str) -> Result<String> {
|
||
|
Ok(reqwest::Client::new()
|
||
|
.get(&self.command(command))
|
||
|
.send()
|
||
|
.await?
|
||
|
.text()
|
||
|
.await?)
|
||
|
}
|
||
|
|
||
|
pub async fn turn_on_led(&self) -> Result<()> {
|
||
|
todo!("LedPower=1")
|
||
|
}
|
||
|
|
||
|
pub async fn turn_off_led(&self) -> Result<()> {
|
||
|
todo!("LedPower=2")
|
||
|
}
|
||
|
|
||
|
pub async fn switch_on(&self) -> Result<()> {
|
||
|
todo!("Power0=1")
|
||
|
}
|
||
|
|
||
|
pub async fn switch_off(&self) -> Result<()> {
|
||
|
todo!("Power0=0")
|
||
|
}
|
||
|
|
||
|
pub async fn read_power_usage(&self) -> Result<f32> {
|
||
|
todo!("Status=8")
|
||
|
}
|
||
|
}
|