basic command structure, implement reset
Some checks failed
Rust / build (push) Failing after 49s

This commit is contained in:
Vinzenz Schroeter 2025-02-08 13:21:34 +01:00
parent a5ad583f2d
commit 8390b739da
6 changed files with 790 additions and 2 deletions

58
src/cli.rs Normal file
View file

@ -0,0 +1,58 @@
#[derive(clap::Parser, std::fmt::Debug)]
#[clap(version)]
pub struct Cli {
#[arg(
short,
long,
help = "ip:port of the servicepoint display",
default_value = "127.0.0.1:2342"
)]
pub destination: String,
#[arg(
short,
long,
help = "protocol to use for communication with display",
value_enum,
default_value = "udp"
)]
pub transport: Protocol,
#[clap(subcommand)]
pub command: Mode,
#[clap(short, long, help = "verbose logging")]
pub verbose: bool
}
#[derive(clap::Parser, std::fmt::Debug)]
pub enum Mode {
#[command(visible_alias = "r")]
ResetEverything,
#[command(visible_alias = "p")]
Pixels {
#[clap(subcommand)]
pixel_command: PixelCommand,
},
#[command(visible_alias = "b")]
Brightness {
#[clap(subcommand)]
brightness_command: BrightnessCommand,
},
}
#[derive(clap::Parser, std::fmt::Debug)]
pub enum PixelCommand {
#[command(visible_alias = "r")]
Reset,
}
#[derive(clap::Parser, std::fmt::Debug)]
pub enum BrightnessCommand {
#[command(visible_alias = "r")]
Reset,
}
#[derive(clap::ValueEnum, Clone, Debug)]
pub enum Protocol {
Udp,
WebSocket,
Fake,
}

40
src/execute.rs Normal file
View file

@ -0,0 +1,40 @@
use log::info;
use servicepoint::{Brightness, Command, Connection};
use crate::cli::{BrightnessCommand, Mode, PixelCommand};
pub fn execute_mode(mode: Mode, connection: Connection) {
match mode {
Mode::ResetEverything => {
brightness_reset(&connection);
pixels_reset(&connection);
}
Mode::Pixels { pixel_command } => pixels(&connection, pixel_command),
Mode::Brightness { brightness_command } => brightness(&connection, brightness_command),
}
}
fn pixels(connection: &Connection, pixel_command: PixelCommand) {
match pixel_command {
PixelCommand::Reset => pixels_reset(&connection),
}
}
fn brightness(connection: &Connection, brightness_command: BrightnessCommand) {
match brightness_command {
BrightnessCommand::Reset => brightness_reset(&connection),
}
}
fn pixels_reset(connection: &Connection) {
connection
.send(Command::Clear)
.expect("failed to clear pixels");
info!("Reset pixels");
}
fn brightness_reset(connection: &Connection) {
connection
.send(Command::Brightness(Brightness::MAX))
.expect("Failed to reset brightness to maximum");
info!("Reset brightness");
}

View file

@ -1,3 +1,44 @@
use crate::cli::{Cli, Protocol};
use clap::Parser;
use log::debug;
use servicepoint::Connection;
mod cli;
mod execute;
fn main() {
println!("Hello, world!");
let cli = Cli::parse();
init_logging(cli.verbose);
debug!("running with arguments: {:?}", cli);
let connection = make_connection(cli.destination, cli.transport);
debug!("connection established: {:#?}", connection);
execute::execute_mode(cli.command, connection);
}
fn make_connection(destination: String, transport: Protocol) -> Connection {
let connection = match transport {
Protocol::Udp => Connection::open(destination).expect("Failed to open UDP connection"),
Protocol::WebSocket => {
let url = destination.parse().expect(
"provided destination is not a valid url - make sure it starts with 'ws://'",
);
Connection::open_websocket(url).expect("Failed to open WebSocket connection")
}
Protocol::Fake => Connection::Fake,
};
connection
}
fn init_logging(debug: bool) {
let filter = if debug {
log::LevelFilter::Debug
} else {
log::LevelFilter::Info
};
env_logger::builder()
.filter_level(filter)
.parse_default_env()
.init();
}