forked from sigil-03/power
code cleanup + add CLI bin
This commit is contained in:
parent
c5866eeccc
commit
230034c7f0
5 changed files with 64 additions and 7 deletions
53
src/bin/cli.rs
Normal file
53
src/bin/cli.rs
Normal 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;
|
||||||
|
}
|
||||||
|
|
@ -1,5 +1,8 @@
|
||||||
use crate::types::{Error, PowerState};
|
use crate::types::{Error, PowerState};
|
||||||
|
|
||||||
pub trait Control {
|
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
5
src/lib.rs
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
pub mod control;
|
||||||
|
pub mod monitor;
|
||||||
|
pub mod system;
|
||||||
|
pub mod tasmota;
|
||||||
|
pub mod types;
|
||||||
|
|
@ -1,9 +1,5 @@
|
||||||
use crate::tasmota::{PowerStatusData, StatusResponse, TasmotaInterface, TasmotaInterfaceConfig};
|
|
||||||
use crate::types::Error;
|
use crate::types::Error;
|
||||||
use reqwest::Client;
|
|
||||||
use serde::Deserialize;
|
|
||||||
use std::fs;
|
|
||||||
|
|
||||||
pub trait Monitoring {
|
pub trait Monitoring {
|
||||||
async fn get_power(&self) -> Result<isize, Error>;
|
fn get_power(&self) -> impl std::future::Future<Output = Result<isize, Error>> + Send;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@ use crate::control::Control;
|
||||||
use crate::monitor::Monitoring;
|
use crate::monitor::Monitoring;
|
||||||
use crate::tasmota::{TasmotaInterface, TasmotaInterfaceConfig};
|
use crate::tasmota::{TasmotaInterface, TasmotaInterfaceConfig};
|
||||||
use crate::types::{self, Error};
|
use crate::types::{self, Error};
|
||||||
use reqwest::Client;
|
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
|
|
@ -11,6 +10,7 @@ pub struct SystemConfig {
|
||||||
components: Vec<TasmotaInterfaceConfig>,
|
components: Vec<TasmotaInterfaceConfig>,
|
||||||
}
|
}
|
||||||
impl SystemConfig {
|
impl SystemConfig {
|
||||||
|
#[allow(unused)]
|
||||||
fn print(&self) {
|
fn print(&self) {
|
||||||
for t in &self.components {
|
for t in &self.components {
|
||||||
t.print();
|
t.print();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue