Skip to content

Commit 4a0a352

Browse files
Make connections more reliable
1 parent c3270f6 commit 4a0a352

5 files changed

Lines changed: 21 additions & 11 deletions

File tree

moonraker-rs/src/connector/read_deserialize.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::printer_objects::*;
55
#[derive(Debug, Deserialize, Clone)]
66
pub struct MoonrakerErrorReplyRaw
77
{
8-
pub code: u32,
8+
pub code: i32,
99
pub message: String
1010
}
1111

moonraker-rs/src/error.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ pub enum Error {
88
#[error("Failed to write message to websocket")]
99
WebsocketWriteError(#[from] WebSocketError),
1010
#[error("Moonraker returned an error reply")]
11-
MoonrakerErrorReply(u32, String),
11+
MoonrakerErrorReply(i32, String),
1212
#[error("Unknown error")]
1313
Unknown(String),
1414
#[error("Internally used")]
1515
BreakError,
16+
#[error("Timeout")]
17+
Timeout,
1618
}

moonraker-rs/src/moonraker_connection.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use serde_json::Value;
1414
use std::error::Error;
1515
use std::fmt::Debug;
1616
use std::sync::Arc;
17-
use std::time::Duration;
17+
use std::time::{Duration, Instant};
1818
use tokio::io::{ReadHalf, WriteHalf};
1919
use tokio::net::TcpStream;
2020
use tokio::sync::broadcast::{Receiver, Sender};
@@ -44,7 +44,7 @@ pub struct MoonrakerReply {
4444
#[derive(Debug, Clone)]
4545
pub struct MoonrakerErrorReply
4646
{
47-
pub code: u32,
47+
pub code: i32,
4848
pub message: String,
4949
pub id: u32,
5050
}
@@ -234,7 +234,8 @@ impl MoonrakerConnection {
234234
.recv()
235235
.await
236236
.expect("Failed to retrieve internal event");
237-
237+
238+
let now = Instant::now();
238239
match &*event {
239240
WebsocketEvent::MoonrakerReply(reply) if reply.id == id => {
240241
let parsed_result: Result<T, serde_json::Error> =
@@ -247,10 +248,17 @@ impl MoonrakerConnection {
247248
}
248249

249250
WebsocketEvent::MoonrakerErrorReply(error) if error.id == id => {
250-
println!("AAAAAAAAAAAAAAAAAAAAAA");
251251
return Err(crate::error::Error::MoonrakerErrorReply(error.code, error.message.clone()));
252252
}
253-
_ => continue, // TODO: This should eventually end
253+
_ => {
254+
if now.elapsed().as_secs() > 20
255+
{
256+
return Err(crate::error::Error::Timeout);
257+
}
258+
else {
259+
continue;
260+
}
261+
}, // TODO: This should eventually end
254262
}
255263
}
256264
}

src/event_loop/event_loop.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ impl EventLoop
6060
self.ui_weak
6161
.upgrade_in_event_loop(move |ui| {
6262
ui.global::<AppState>().set_moonraker_connected(false);
63-
ui.global::<AppState>().set_klipper_state("Disconnected".into());
64-
ui.global::<AppState>().set_klipper_state_message("Disconnected".into());
63+
ui.global::<AppState>().set_klipper_state("".into());
64+
ui.global::<AppState>().set_klipper_state_message("".into());
6565
})?;
6666

6767
Ok(())

ui/main.slint

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ export component AppWindow inherits Window {
555555
height: 100%;
556556
}
557557

558-
if AppState.moonraker_connected && AppState.klipper_state != "Ready" : VerticalLayout {
558+
if AppState.moonraker_connected && AppState.klipper_state != "Ready" && AppState.klipper_state != "" : VerticalLayout {
559559
width: 100%;
560560
spacing: 20px;
561561
Rectangle {}
@@ -589,7 +589,7 @@ export component AppWindow inherits Window {
589589
Rectangle {}
590590
}
591591

592-
if !AppState.moonraker_connected: VerticalLayout
592+
if !AppState.moonraker_connected || AppState.klipper_state == "": VerticalLayout
593593
{
594594
VerticalLayout {
595595
width: 100%;

0 commit comments

Comments
 (0)