Skip to content

Commit

Permalink
ArtNet config actually gets saved to project if modified
Browse files Browse the repository at this point in the history
  • Loading branch information
anselanza committed Mar 6, 2024
1 parent 8e76279 commit 7a7cf19
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 36 deletions.
9 changes: 2 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
use std::{net::SocketAddr, sync::mpsc, time::Duration};
use std::{sync::mpsc, time::Duration};

use env_logger::Env;
use log::{debug, info};

use clap::Parser;

use crate::{
artnet::{ArtNetInterface, ArtNetMode},
model::Model,
settings::Cli,
ui::SIMPLE_WIN_SIZE,
};
use crate::{model::Model, settings::Cli, ui::SIMPLE_WIN_SIZE};

mod animation;
mod artnet;
Expand Down
12 changes: 9 additions & 3 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,21 @@ impl Model {
pub fn new(cli: Cli) -> Model {
let mut current_project_path = None;

let project = match Project::load(&cli.project_path) {
let project_to_load = String::from(
&cli.project_path
.clone()
.unwrap_or("./example.project.json".into()),
);

let project = match Project::load(&project_to_load) {
Ok(p) => {
current_project_path = Some(String::from(&cli.project_path));
current_project_path = Some(project_to_load);
p
}
Err(e) => {
error!(
"Failed to load project from path \"{}\"; {:?}",
&cli.project_path, e
project_to_load, e
);
info!("Blank project will be loaded instead.");
Project::new()
Expand Down
55 changes: 39 additions & 16 deletions src/project/artnetconfig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
str::FromStr,
};

use log::{debug, warn};
use log::{debug, error, info, warn};
use serde::{Deserialize, Serialize};

use crate::{
Expand All @@ -14,7 +14,7 @@ use crate::{

use super::Project;

#[derive(Serialize, Deserialize, Clone, PartialEq)]
#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)]
pub enum ArtNetConfigMode {
Broadcast,
Unicast(String, String),
Expand All @@ -24,6 +24,7 @@ pub fn get_artnet_interface(
cli: &Cli,
project: &Project,
) -> Result<ArtNetInterface, anyhow::Error> {
debug!("get_artnet_interface");
if cli.artnet_broadcast {
warn!("CLI artnetBroadcast flag overrides any Project ArtNet settings");
ArtNetInterface::new(ArtNetMode::Broadcast, cli.artnet_update_frequency)
Expand All @@ -39,21 +40,43 @@ pub fn get_artnet_interface(
} else {
debug!("No CLI overrides, attempt to use Project ArtNet config...");
match &project.artnet_config {
Some(artnet_mode) => match artnet_mode {
ArtNetConfigMode::Broadcast => {
ArtNetInterface::new(ArtNetMode::Broadcast, cli.artnet_update_frequency)
Some(artnet_mode) => {
info!("Using project ArtNet Config {:?}", artnet_mode);
match artnet_mode {
ArtNetConfigMode::Broadcast => {
ArtNetInterface::new(ArtNetMode::Broadcast, cli.artnet_update_frequency)
}
ArtNetConfigMode::Unicast(interface_ip, destination_ip) => {
ArtNetInterface::new(
ArtNetMode::Unicast(
SocketAddr::from((Ipv4Addr::from_str(interface_ip).unwrap(), 6453)),
SocketAddr::from((
Ipv4Addr::from_str(destination_ip).unwrap(),
6454,
)),
),
cli.artnet_update_frequency,
)
}
}
ArtNetConfigMode::Unicast(interface_ip, destination_ip) => ArtNetInterface::new(
ArtNetMode::Unicast(
SocketAddr::from((Ipv4Addr::from_str(interface_ip).unwrap(), 6453)),
SocketAddr::from((Ipv4Addr::from_str(destination_ip).unwrap(), 6454)),
),
cli.artnet_update_frequency,
),
},
None => Err(anyhow!(
"No artnet settings in Project or CLI; user should connect manually"
)),
}
None => {
error!("ArtNet config could not be found or parsed from Project");
Err(anyhow!(
"No artnet settings in Project or CLI; user should connect manually"
))
}
}
}
}

impl From<&ArtNetInterface> for ArtNetConfigMode {
fn from(value: &ArtNetInterface) -> Self {
match value.mode_in_use() {
ArtNetMode::Broadcast => ArtNetConfigMode::Broadcast,
ArtNetMode::Unicast(src, dst) => {
ArtNetConfigMode::Unicast(src.ip().to_string(), dst.ip().to_string())
}
}
}
}
10 changes: 4 additions & 6 deletions src/settings.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use std::net::{IpAddr, Ipv4Addr};

use clap::Parser;

const UNICAST_SRC: std::net::IpAddr = IpAddr::V4(Ipv4Addr::new(10, 0, 0, 102));
const UNICAST_DST: std::net::IpAddr = IpAddr::V4(Ipv4Addr::new(10, 0, 0, 1));
pub const UNICAST_SRC_STRING: &str = "127.0.0.1";
pub const UNICAST_DST_STRING: &str = "127.0.0.1";

pub const DEFAULT_ARTNET_HERTZ: u64 = 44;

Expand All @@ -17,8 +15,8 @@ pub struct Cli {
#[arg(long = "headless")]
pub headless_mode: bool,

#[arg(long = "project",default_value_t=String::from("./example.project.json"))]
pub project_path: String,
#[arg(long = "project")]
pub project_path: Option<String>,

#[arg(long = "loglevel",default_value_t=String::from("info"))]
pub log_level: String,
Expand Down
9 changes: 5 additions & 4 deletions src/ui/network_controls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::{
artnet::{ArtNetInterface, ArtNetMode},
model::{attempt_connection, Model, TetherStatus},
project::artnetconfig::ArtNetConfigMode,
settings::{UNICAST_DST_STRING, UNICAST_SRC_STRING},
};
use anyhow::anyhow;

Expand Down Expand Up @@ -69,7 +70,7 @@ pub fn render_network_controls(model: &mut Model, ui: &mut Ui) {
);
ui.radio_value(
&mut model.artnet_edit_mode,
ArtNetConfigMode::Unicast(String::new(), String::new()),
ArtNetConfigMode::Unicast(UNICAST_SRC_STRING.into(), UNICAST_DST_STRING.into()),
"Unicast mode",
);
});
Expand Down Expand Up @@ -109,9 +110,9 @@ pub fn render_network_controls(model: &mut Model, ui: &mut Ui) {
}
}
};
model.artnet = match new_artnet_interface {
Ok(x) => Some(x),
Err(_) => None,
if let Ok(interface) = new_artnet_interface {
model.project.artnet_config = Some(ArtNetConfigMode::from(&interface));
model.artnet = Some(interface);
}
}
}
Expand Down

0 comments on commit 7a7cf19

Please sign in to comment.