feat: Simple client/server implementation to control lights

This commit is contained in:
Arne Dußin 2024-08-22 22:01:48 +02:00
parent 6202f86265
commit 797ab60b8b
8 changed files with 620 additions and 0 deletions

65
src/client.rs Normal file
View file

@ -0,0 +1,65 @@
use std::net::{SocketAddr, TcpStream};
use clap::Parser;
use clap_derive::Subcommand;
use packet::Packet;
mod packet;
#[derive(Parser, Debug)]
#[command(version, about)]
struct Args
{
#[arg(short, long)]
connect: SocketAddr,
#[command(subcommand)]
command: Command,
}
#[derive(Subcommand, Debug)]
enum Command
{
/// Set a constant brightness in Range 0.0..=1.0
Constant
{
brightness: f64
},
/// Sinusoidal curve with a period of 0.0.. seconds
Sine
{
period: f64
},
/// Triangle curve with a period of 0.0.. seconds
Triangle
{
period: f64
},
/// Sawtooth curve with a period of 0.0.. seconds
Sawtooth
{
period: f64
},
/// Square curve witha period of 0.0.. seconds and a duty cycle of 0.0..1.0
Square
{
period: f64, duty: f64
},
}
fn main()
{
let args = Args::parse();
let mut stream = TcpStream::connect(args.connect).expect("Unable to connect to server");
let packet = match args.command {
Command::Constant { brightness } => Packet::Constant(brightness),
Command::Sine { period } => Packet::Sine(period),
Command::Triangle { period } => Packet::Triangle(period),
Command::Sawtooth { period } => Packet::Sawtooth(period),
Command::Square { period, duty } => Packet::Square { period, duty },
};
packet
.write_to_stream(&mut stream)
.expect("Unable to send packet");
}