1
0
Fork 0
forked from sigil-03/power

code cleanup + add CLI bin

This commit is contained in:
sigil-03 2025-04-06 15:35:10 -06:00
parent c5866eeccc
commit 230034c7f0
5 changed files with 64 additions and 7 deletions

53
src/bin/cli.rs Normal file
View file

@ -0,0 +1,53 @@
use clap::{Parser, Subcommand};
#[derive(Parser)]
pub struct PowerCommand {
index: usize,
#[command(subcommand)]
state: power::types::PowerState,
}
#[derive(Subcommand)]
pub enum Commands {
Monitor,
Set(PowerCommand),
}
impl Commands {
pub async fn execute(self, config_file: &str) {
let s = power::system::System::new_from_file(config_file).unwrap();
let handle = match self {
Self::Monitor => tokio::spawn(async move {
s.try_get_power().await.unwrap();
}),
Self::Set(command) => {
// let c = Controller::new_from_file(config_file).unwrap();
tokio::spawn(async move {
s.try_set_power(command.index, command.state).await.unwrap();
})
}
};
handle.await.unwrap();
}
}
#[derive(Parser)]
pub struct Cli {
#[arg(short, long)]
config_file: String,
#[command(subcommand)]
command: Commands,
}
impl Cli {
pub async fn execute(self) {
self.command.execute(&self.config_file).await;
}
}
#[tokio::main]
async fn main() {
let cli = Cli::parse();
cli.execute().await;
}

View file

@ -1,5 +1,8 @@
use crate::types::{Error, PowerState};
pub trait Control {
async fn set_power(&self, state: PowerState) -> Result<(), Error>;
fn set_power(
&self,
state: PowerState,
) -> impl std::future::Future<Output = Result<(), Error>> + Send;
}

5
src/lib.rs Normal file
View file

@ -0,0 +1,5 @@
pub mod control;
pub mod monitor;
pub mod system;
pub mod tasmota;
pub mod types;

View file

@ -1,9 +1,5 @@
use crate::tasmota::{PowerStatusData, StatusResponse, TasmotaInterface, TasmotaInterfaceConfig};
use crate::types::Error;
use reqwest::Client;
use serde::Deserialize;
use std::fs;
pub trait Monitoring {
async fn get_power(&self) -> Result<isize, Error>;
fn get_power(&self) -> impl std::future::Future<Output = Result<isize, Error>> + Send;
}

View file

@ -2,7 +2,6 @@ use crate::control::Control;
use crate::monitor::Monitoring;
use crate::tasmota::{TasmotaInterface, TasmotaInterfaceConfig};
use crate::types::{self, Error};
use reqwest::Client;
use serde::Deserialize;
use std::fs;
@ -11,6 +10,7 @@ pub struct SystemConfig {
components: Vec<TasmotaInterfaceConfig>,
}
impl SystemConfig {
#[allow(unused)]
fn print(&self) {
for t in &self.components {
t.print();