Skip to content

Commit

Permalink
feat: moonraker api url as argument
Browse files Browse the repository at this point in the history
  • Loading branch information
filipton committed Jul 3, 2023
1 parent c167604 commit efc99d7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
12 changes: 8 additions & 4 deletions serial-screen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ const RETRY_TIMEOUT: u64 = 5000;
const BOOT_TIMEOUT: u128 = 1000;
const TIMEOUT_THRESHOLD: u128 = 2000;

// TODO: make this configurable (or just hardcode it as localhost)
pub const MOONRAKER_API_URL: &str = "192.168.1.18:7125";

#[tokio::main]
async fn main() -> Result<()> {
let moonraker_api_url = std::env::args()
.nth(1)
.unwrap_or_else(|| "localhost:7125".to_string());

let screen_state = Arc::new(RwLock::new(ScreenState::new()));

let moonraker = moonraker_api::connect(MOONRAKER_API_URL).await?;
let moonraker = moonraker_api::connect(&moonraker_api_url).await?;
let moonraker_tx = Arc::new(Mutex::new(moonraker.0));
let moonraker_rx = Arc::new(Mutex::new(moonraker.1));
subscribe_websocket_events(moonraker_tx.clone()).await?;
Expand All @@ -46,6 +47,7 @@ async fn main() -> Result<()> {
screen_state.clone(),
moonraker_tx.clone(),
moonraker_rx.clone(),
moonraker_api_url.clone(),
)
.await;
if res.is_err() {
Expand All @@ -58,6 +60,7 @@ async fn connect_to_serial(
screen_state: Arc<RwLock<ScreenState>>,
moonraker_tx: MoonrakerTx,
moonraker_rx: MoonrakerRx,
moonraker_api_url: String,
) -> Result<()> {
let serial = rppal::uart::Uart::new(115200, rppal::uart::Parity::None, 8, 1);
if let Err(e) = serial {
Expand All @@ -76,6 +79,7 @@ async fn connect_to_serial(
moonraker_rx.clone(),
screen_state.clone(),
serial_tx.clone(),
moonraker_api_url,
)
.await?;

Expand Down
36 changes: 23 additions & 13 deletions serial-screen/src/moonraker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::{
serial_utils::construct_change_page,
structs::{FileMetadataRoot, PrinterObjectsRoot, PrinterStateRoot},
utils::{self, subscribe_websocket_events},
MOONRAKER_API_URL,
};
use anyhow::Result;
use moonraker_api::{MoonrakerMethod, MoonrakerMsg};
Expand Down Expand Up @@ -67,6 +66,7 @@ pub async fn recieve_moonraker_updates(
moonraker_rx: &MoonrakerRx,
serial_tx: &Arc<Mutex<UnboundedSender<Vec<u8>>>>,
client: &reqwest::Client,
moonraker_api_url: String,
) -> Result<()> {
while let Ok(msg) = moonraker_rx.lock().await.try_recv() {
if let MoonrakerMsg::MsgMethodParam {
Expand Down Expand Up @@ -117,11 +117,14 @@ pub async fn recieve_moonraker_updates(

screen_state.model_name = utils::center_pad(model_name, " ", 20);

screen_state.file_estimated_time =
get_file_estimated_time(client, filename.as_str().unwrap_or(""))
.await
.unwrap_or(Some(-1))
.unwrap_or(-1);
screen_state.file_estimated_time = get_file_estimated_time(
client,
filename.as_str().unwrap_or(""),
&moonraker_api_url,
)
.await
.unwrap_or(Some(-1))
.unwrap_or(-1);
}

if let Some(state) = print_stats.get("state") {
Expand Down Expand Up @@ -173,11 +176,14 @@ pub async fn recieve_moonraker_updates(
.unwrap_or("");
screen_state.model_name = utils::center_pad(model_name, " ", 20);

screen_state.file_estimated_time =
get_file_estimated_time(client, &result.status.print_stats.filename)
.await
.unwrap_or(Some(-1))
.unwrap_or(-1);
screen_state.file_estimated_time = get_file_estimated_time(
client,
&result.status.print_stats.filename,
&moonraker_api_url,
)
.await
.unwrap_or(Some(-1))
.unwrap_or(-1);
}
MoonrakerMethod::PrinterObjectsList => {
let result: PrinterObjectsRoot = serde_json::from_value(result)
Expand Down Expand Up @@ -220,11 +226,15 @@ pub async fn recieve_moonraker_updates(
Ok(())
}

async fn get_file_estimated_time(client: &reqwest::Client, filename: &str) -> Result<Option<i32>> {
async fn get_file_estimated_time(
client: &reqwest::Client,
filename: &str,
moonraker_api_url: &str,
) -> Result<Option<i32>> {
let file_metadata = client
.get(format!(
"http://{}/server/files/metadata?filename={}",
MOONRAKER_API_URL, filename
moonraker_api_url, filename
))
.send()
.await;
Expand Down
2 changes: 2 additions & 0 deletions serial-screen/src/screen_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ pub async fn spawn_update_task(
moonraker_rx: MoonrakerRx,
screen_state: Arc<RwLock<ScreenState>>,
serial_tx: Arc<Mutex<UnboundedSender<Vec<u8>>>>,
moonraker_api_url: String,
) -> Result<JoinHandle<()>> {
let task = tokio::spawn(async move {
let client = reqwest::Client::new();
Expand All @@ -240,6 +241,7 @@ pub async fn spawn_update_task(
&moonraker_rx,
&serial_tx,
&client,
moonraker_api_url.clone(),
)
.await;
if let Err(e) = moonraker_update_res {
Expand Down

0 comments on commit efc99d7

Please sign in to comment.