Some refactor and some error handling
This commit is contained in:
parent
a2bc751c9a
commit
e8114810c3
12
Cargo.toml
12
Cargo.toml
|
|
@ -12,11 +12,21 @@ actix-web = "4.3.1"
|
|||
reqwest = { version = "0.11.16", features = ["blocking"] }
|
||||
base64 = "0.21.0"
|
||||
lazy_static = "1.4.0"
|
||||
ratatui = "0.29.0"
|
||||
serde = "1.0.215"
|
||||
thiserror = "2.0.4"
|
||||
tracing = "0.1.41"
|
||||
tokio = "1.42.0"
|
||||
|
||||
[[bin]]
|
||||
name = "rusty_midicommander"
|
||||
path = "src/main.rs"
|
||||
|
||||
[lib]
|
||||
name = "rmc_lib"
|
||||
path = "src/lib.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "webtest"
|
||||
path = "src/webtest.rs"
|
||||
path = "src/webtest.rs"
|
||||
|
||||
|
|
|
|||
|
|
@ -1,32 +0,0 @@
|
|||
// The commands module should define the commands that the program can send.
|
||||
// This module should provide functions to send each command asynchronously.
|
||||
// The functions should return immediately, without waiting for the command to complete.
|
||||
|
||||
struct MidiData {
|
||||
// Define your MIDI data struct here
|
||||
}
|
||||
|
||||
trait Command {
|
||||
fn execute(&self, midi_data: &MidiData);
|
||||
}
|
||||
|
||||
struct CommandA;
|
||||
|
||||
impl Command for CommandA {
|
||||
fn execute(&self, midi_data: &MidiData) {
|
||||
println!("Executing CommandA with MIDI data {:?}", midi_data);
|
||||
}
|
||||
}
|
||||
|
||||
struct CommandB {
|
||||
message: String,
|
||||
}
|
||||
|
||||
impl Command for CommandB {
|
||||
fn execute(&self, midi_data: &MidiData) {
|
||||
println!(
|
||||
"Executing CommandB with message '{}' and MIDI data {:?}",
|
||||
self.message, midi_data
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
//! Generic rusty midi commander error
|
||||
//!
|
||||
|
||||
use thiserror::Error;
|
||||
|
||||
/// A generic error
|
||||
///
|
||||
#[derive(Error, Debug)]
|
||||
pub enum RustyMidiError {
|
||||
#[error("network request failed {0}")]
|
||||
NetworkRequest(#[from] reqwest::Error),
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
//! The Rusty Midi Commander lib holds the the following parts of the project
|
||||
//! - The Async Web Requesting: Connect and Disconnect of VP and send requests
|
||||
//! - The TUI (optionnal): To have a "nice" review of what is happening
|
||||
//! - The Dispatch: In the middle to handle all orders and communications
|
||||
//! - The CLI parser: Handle both cli and config
|
||||
//!
|
||||
|
||||
#![warn(clippy::missing_docs_in_private_items)]
|
||||
// use midir::MidiOutput;
|
||||
pub mod error;
|
||||
pub mod midi;
|
||||
pub mod projector;
|
||||
|
||||
pub use error::RustyMidiError;
|
||||
23
src/main.rs
23
src/main.rs
|
|
@ -6,24 +6,17 @@
|
|||
|
||||
#![warn(clippy::missing_docs_in_private_items)]
|
||||
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use clap::Parser;
|
||||
use midi::cleanup;
|
||||
use midi::connect_loop_with_callback;
|
||||
use midi::create_output_interface;
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
|
||||
use midir::MidiInput;
|
||||
|
||||
use midir::MidiOutput;
|
||||
use midir::MidiOutputConnection;
|
||||
|
||||
// use midir::MidiOutput;
|
||||
pub mod midi;
|
||||
mod projector;
|
||||
use rmc_lib::{
|
||||
midi::{self, cleanup, connect_loop_with_callback, create_output_interface},
|
||||
projector,
|
||||
};
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(author, version, about, long_about = None)]
|
||||
/// Description of the cli
|
||||
struct Args {
|
||||
/// The midi interface to select
|
||||
midi_interface: Option<usize>,
|
||||
|
|
@ -40,7 +33,7 @@ fn main() {
|
|||
return;
|
||||
}
|
||||
println!("{:?}", args);
|
||||
let proj = Arc::new(Mutex::new(projector::Projector::new(
|
||||
let proj = Arc::new(Mutex::new(projector::panasonic::Projector::new(
|
||||
"127.0.0.1:8080",
|
||||
"admin1",
|
||||
"panasonic",
|
||||
|
|
|
|||
31
src/midi.rs
31
src/midi.rs
|
|
@ -1,19 +1,24 @@
|
|||
// The midi module should provide a high-level interface for receiving and sending MIDI messages.
|
||||
// It should handle the low-level details of communicating with the MIDI devices.
|
||||
|
||||
use crate::MidiInput;
|
||||
use crate::MidiOutput;
|
||||
use crate::MidiOutputConnection;
|
||||
use lazy_static::lazy_static;
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
use std::thread::sleep;
|
||||
use std::time::Duration;
|
||||
use std::{
|
||||
sync::{Arc, Mutex},
|
||||
thread::sleep,
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
use lazy_static::lazy_static;
|
||||
use midir::{MidiInput, MidiOutput, MidiOutputConnection};
|
||||
|
||||
// TODO: All this should be generic with an impl for every midi pad, possibly from a config file
|
||||
// As: GenericFromFile, GenericAkai, etc...
|
||||
|
||||
// TODO: NO
|
||||
lazy_static! {
|
||||
static ref INIT_NOTES: Vec<(u8, u8)> = vec![(3, 1), (4, 3)];
|
||||
}
|
||||
|
||||
// TODO: Meh ?
|
||||
pub fn print_midi_interfaces() {
|
||||
// Create MIDI input and output instances
|
||||
let midi_in = MidiInput::new("Rusty MIDI Commander Input").unwrap();
|
||||
|
|
@ -29,6 +34,7 @@ pub fn print_midi_interfaces() {
|
|||
}
|
||||
}
|
||||
|
||||
// Acceptable, should be more generic maybe ?
|
||||
pub fn connect_loop_with_callback(
|
||||
port: usize,
|
||||
name: &str,
|
||||
|
|
@ -44,7 +50,8 @@ pub fn connect_loop_with_callback(
|
|||
// Connect to port
|
||||
let input_port = &input_ports[port];
|
||||
let conn_clone = Arc::clone(out_port);
|
||||
// let fn_clone = Arc::clone(&calls);
|
||||
|
||||
// TODO: Unacceptable
|
||||
let _connection = input_interface
|
||||
.connect(
|
||||
input_port,
|
||||
|
|
@ -61,6 +68,7 @@ pub fn connect_loop_with_callback(
|
|||
}
|
||||
let mut list = vec![];
|
||||
if note == 3 {
|
||||
// Meh
|
||||
list.push([command, 3, 2]);
|
||||
list.push([command, 4, 3]);
|
||||
} else {
|
||||
|
|
@ -68,6 +76,7 @@ pub fn connect_loop_with_callback(
|
|||
list.push([command, 4, 4]);
|
||||
}
|
||||
for n in list.iter() {
|
||||
// Meh
|
||||
conn_clone.lock().unwrap().send(n).unwrap();
|
||||
conn_clone.lock().unwrap().send(n).unwrap();
|
||||
}
|
||||
|
|
@ -82,6 +91,7 @@ pub fn connect_loop_with_callback(
|
|||
}
|
||||
}
|
||||
|
||||
// TODO: Doc
|
||||
pub fn create_output_interface(port: usize, name: &str) -> Arc<Mutex<MidiOutputConnection>> {
|
||||
let output_interface = MidiOutput::new(name).unwrap();
|
||||
let output_port = &output_interface.ports()[port];
|
||||
|
|
@ -92,6 +102,7 @@ pub fn create_output_interface(port: usize, name: &str) -> Arc<Mutex<MidiOutputC
|
|||
))
|
||||
}
|
||||
|
||||
// TODO: A more generic cleanup
|
||||
pub fn cleanup(
|
||||
out_port: &mut MidiOutputConnection, /*, range: &dyn std::iter::Iterator<Item = usize>*/
|
||||
) {
|
||||
|
|
@ -109,11 +120,13 @@ pub fn cleanup(
|
|||
prepare(out_port, INIT_NOTES.to_vec())
|
||||
}
|
||||
|
||||
// TODO: more generic interface
|
||||
pub fn send_note_on(out_port: &mut MidiOutputConnection, pitch: u8, velocity: u8) {
|
||||
let msg = &[0x90, pitch, velocity];
|
||||
out_port.send(msg).unwrap();
|
||||
}
|
||||
|
||||
// TODO: doc
|
||||
pub fn prepare(out_port: &mut MidiOutputConnection, notes_values: Vec<(u8, u8)>) {
|
||||
for (note, val) in notes_values.iter() {
|
||||
send_note_on(out_port, *note, *val);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,8 @@
|
|||
//! Contains all projector related commands
|
||||
|
||||
pub mod panasonic;
|
||||
pub mod proj_traits;
|
||||
|
||||
/// The standard wait time
|
||||
// TODO: NO
|
||||
static WAIT_TIME: f64 = 0.7;
|
||||
|
|
@ -1,11 +1,14 @@
|
|||
//! Contains all projector related commands
|
||||
//! A panasonic webpage interface implementation
|
||||
//!
|
||||
|
||||
use base64::{engine::general_purpose, Engine as _};
|
||||
use std::time::Duration;
|
||||
|
||||
use base64::{engine::general_purpose, Engine};
|
||||
use reqwest::blocking::{Client, RequestBuilder};
|
||||
use std::{error::Error, time::Duration};
|
||||
|
||||
/// The standard wait time
|
||||
static WAIT_TIME: f64 = 0.7;
|
||||
use crate::error::RustyMidiError;
|
||||
|
||||
use super::WAIT_TIME;
|
||||
|
||||
/// A projector and all info to connect to it
|
||||
pub struct Projector {
|
||||
|
|
@ -15,8 +18,11 @@ pub struct Projector {
|
|||
username: String,
|
||||
/// The password for the connection
|
||||
password: String,
|
||||
/// An optionnal port for connection
|
||||
port: Option<usize>,
|
||||
}
|
||||
|
||||
// TODO: Trait
|
||||
impl Projector {
|
||||
/// Create a new projector with the basic information
|
||||
pub fn new(ip: &str, username: &str, password: &str) -> Projector {
|
||||
|
|
@ -24,6 +30,7 @@ impl Projector {
|
|||
ip: ip.to_string(),
|
||||
username: username.to_string(),
|
||||
password: password.to_string(),
|
||||
port: None,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -43,7 +50,7 @@ impl Projector {
|
|||
}
|
||||
|
||||
/// Make a request from a url
|
||||
fn make_request(&self, url: &str, headers: &str) -> Result<RequestBuilder, Box<dyn Error>> {
|
||||
fn make_request(&self, url: &str, headers: &str) -> Result<RequestBuilder, RustyMidiError> {
|
||||
let client = Client::new();
|
||||
let builder = client
|
||||
.get(url)
|
||||
|
|
@ -53,7 +60,7 @@ impl Projector {
|
|||
}
|
||||
|
||||
/// Make the actual request
|
||||
pub fn open_url(&self, cmd: &str) -> Result<String, Box<dyn Error>> {
|
||||
pub fn open_url(&self, cmd: &str) -> Result<String, RustyMidiError> {
|
||||
let headers = self.make_auth_header();
|
||||
let url = self.make_url(cmd);
|
||||
let builder = self.make_request(&url, &headers)?;
|
||||
|
|
@ -63,17 +70,17 @@ impl Projector {
|
|||
}
|
||||
|
||||
/// A request to turn the projector on
|
||||
pub fn shutter_on(&self) -> Result<String, Box<dyn Error>> {
|
||||
pub fn shutter_on(&self) -> Result<String, RustyMidiError> {
|
||||
self.open_url("shutter_on")
|
||||
}
|
||||
|
||||
/// A request to turn the projector off
|
||||
pub fn shutter_off(&self) -> Result<String, Box<dyn Error>> {
|
||||
pub fn shutter_off(&self) -> Result<String, RustyMidiError> {
|
||||
self.open_url("shutter_off")
|
||||
}
|
||||
|
||||
/// The request to the the osd
|
||||
pub fn get_osd(&self) -> Result<String, Box<dyn Error>> {
|
||||
pub fn get_osd(&self) -> Result<String, RustyMidiError> {
|
||||
let headers = self.make_auth_header();
|
||||
let url = format!("http://{}/cgi-bin/get_osd.cgi?lang=e", self.ip);
|
||||
let builder = self.make_request(&url, &headers)?;
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
//! Generic trait for a projector
|
||||
//!
|
||||
//!
|
||||
|
||||
/// A generic Projector, contains all methods for a controllable vp
|
||||
trait GenericProjector {}
|
||||
|
||||
/// A generic Projector OSD, to retreive information from the vp
|
||||
trait GenericProjectorOSD {}
|
||||
|
|
@ -1,2 +0,0 @@
|
|||
// The utils module should provide utility functions that are used throughout the program.
|
||||
// These functions could include functions to convert MIDI messages to and from human-readable formats, or to handle errors.
|
||||
Reference in New Issue