From 52f39d58cbebfb22cee9dc55dc892740b5047b0f Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Wed, 18 Nov 2026 15:18:53 +0100 Subject: [PATCH] Refresh device state after rejected sends without disconnecting --- Cargo.lock | 2 +- Cargo.toml | 2 +- crates/canvas-pilot/src/device_ui.rs | 110 ++++++++++++++++++++++++++- docs/canvas-pilot/changelog.md | 7 ++ docs/canvas-pilot/guide/device.md | 8 +- docs/interfaces/midra-device-send.md | 9 +++ 6 files changed, 130 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f70b444..2d751e3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -602,7 +602,7 @@ dependencies = [ [[package]] name = "canvas-pilot" -version = "0.9.61" +version = "0.9.62" dependencies = [ "base64 0.22.1", "craft-dwg", diff --git a/Cargo.toml b/Cargo.toml index 13318c8..f6e8fb5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ default-members = ["crates/canvas-pilot"] resolver = "3" [workspace.package] -version = "0.9.61" +version = "0.9.62" edition = "2024" license = "MIT" repository = "https://proxy.dandaedre.com/gitea/idle-org/canvas-pilot" diff --git a/crates/canvas-pilot/src/device_ui.rs b/crates/canvas-pilot/src/device_ui.rs index 56de17d..83e070d 100644 --- a/crates/canvas-pilot/src/device_ui.rs +++ b/crates/canvas-pilot/src/device_ui.rs @@ -5,6 +5,39 @@ use canvas_pilot::{ }; use std::sync::mpsc::{self, Receiver, TryRecvError}; +#[derive(Debug)] +struct DeviceFailure { + message: String, + refreshed: Option>, +} +impl From for DeviceFailure { + fn from(message: String) -> Self { + Self { + message, + refreshed: None, + } + } +} +impl From<&str> for DeviceFailure { + fn from(message: &str) -> Self { + message.to_owned().into() + } +} +impl DeviceFailure { + fn after_send(message: String, refresh: Result) -> Self { + match refresh { + Ok(snapshot) => Self { + message, + refreshed: Some(Box::new(snapshot)), + }, + Err(error) => Self { + message: format!("{message}\nCould not refresh device state: {error}"), + refreshed: None, + }, + } + } +} + fn linked_layer_label(layout: &canvas_pilot::Layout, id: usize) -> String { (0..layout.layers().len()) .find(|&i| layout.layer_info(i).id == id) @@ -116,7 +149,7 @@ pub(super) struct DeviceUi { message: String, pub(super) host: String, pub(super) recent_hosts: Vec, - pending: Option>>, + pending: Option>>, cancel_read: Option>, pub(super) snapshot: Option, loading: Option>, @@ -298,7 +331,7 @@ impl DeviceUi { let host = self.host.clone(); let ctx = ctx.clone(); std::thread::spawn(move || { - let result = (|| { + let result: Result = (|| { let mut last = None; for (i, plan) in plans.iter().enumerate() { last = Some(device_send::send(&host, plan).map_err(|e| { @@ -310,6 +343,10 @@ impl DeviceUi { } last.ok_or_else(|| "No linked layers to send.".into()) })(); + // A refused/failed write is not evidence that the connection was lost. + // Refresh read-only, never retry any screen send. + let result = + result.map_err(|error| DeviceFailure::after_send(error, device::fetch(&host))); let _ = tx.send(result); ctx.request_repaint(); }); @@ -338,7 +375,7 @@ impl DeviceUi { let host = self.host.clone(); let ctx = ctx.clone(); std::thread::spawn(move || { - let result = device::fetch_cancellable(&host, &cancel); + let result = device::fetch_cancellable(&host, &cancel).map_err(DeviceFailure::from); let _ = tx.send(result); ctx.request_repaint(); }); @@ -355,6 +392,22 @@ impl DeviceUi { self.pending = None; self.cancel_read = None; let was_sending = std::mem::take(&mut self.sending); + let send_failed = was_sending && result.is_err(); + let result = match result { + Err(failure) if was_sending => { + self.prepare_send = true; + self.open = true; + self.error = failure.message.clone(); + match failure.refreshed { + Some(snapshot) => { + self.message = "Device state refreshed. Check the updated layers before sending again; the send was not retried.".into(); + Ok(*snapshot) + } + None => Err(failure.message), + } + } + other => other.map_err(|failure| failure.message), + }; match result { Ok(snapshot) => { if let Ok(address) = device::address(&self.host) { @@ -363,7 +416,7 @@ impl DeviceUi { self.recent_hosts.insert(0, host); self.recent_hosts.truncate(4); } - if was_sending { + if was_sending && !send_failed { self.message = "Send complete. Preset update sent; geometry verified by device readback." .into(); @@ -2103,6 +2156,55 @@ mod tests { assert!(targets.is_empty()); } #[test] + fn rejected_send_keeps_connection_with_fresh_state_without_retry() { + let mut state = DeviceUi { + snapshot: Some(linked_test_snapshot()), + sending: true, + ..Default::default() + }; + let mut fresh = linked_test_snapshot(); + fresh.identity = "Refreshed device".into(); + let (tx, rx) = mpsc::channel(); + state.pending = Some(rx); + tx.send(Err(DeviceFailure::after_send( + "Source changed; no writes performed".into(), + Ok(fresh), + ))) + .unwrap(); + state.poll(); + assert!(state.online()); + assert_eq!( + state.snapshot.as_ref().unwrap().identity, + "Refreshed device" + ); + assert!(state.error.contains("Source changed")); + assert!(state.message.contains("not retried")); + assert!(!state.message.contains("Send complete")); + assert!(state.open && state.prepare_send); + assert!(state.pending.is_none()); + assert!(!state.sending); + } + #[test] + fn failed_send_and_failed_refresh_mark_connection_unavailable() { + let mut state = DeviceUi { + snapshot: Some(linked_test_snapshot()), + sending: true, + ..Default::default() + }; + let (tx, rx) = mpsc::channel(); + state.pending = Some(rx); + tx.send(Err(DeviceFailure::after_send( + "Send failed after one screen".into(), + Err("Connection refused".into()), + ))) + .unwrap(); + state.poll(); + assert!(!state.online()); + assert!(state.error.contains("after one screen")); + assert!(state.error.contains("Connection refused")); + assert!(state.pending.is_none()); + } + #[test] fn successful_read_sets_online_and_failure_clears_it() { let mut state = DeviceUi::default(); assert!(!state.online()); diff --git a/docs/canvas-pilot/changelog.md b/docs/canvas-pilot/changelog.md index 428c65f..993f34e 100644 --- a/docs/canvas-pilot/changelog.md +++ b/docs/canvas-pilot/changelog.md @@ -4,6 +4,13 @@ ## Unreleased +### v0.9.62 + +- A rejected or failed layer send now refreshes device state read-only and keeps + the connection available when that refresh succeeds. The send error remains + visible and sending is never retried automatically. A failed refresh still + marks the device unavailable. + ### v0.9.61 - Removed Crop Finder from shared input settings and replaced the review step with diff --git a/docs/canvas-pilot/guide/device.md b/docs/canvas-pilot/guide/device.md index b8aee05..01f7288 100644 --- a/docs/canvas-pilot/guide/device.md +++ b/docs/canvas-pilot/guide/device.md @@ -82,8 +82,12 @@ device); the Load label follows the selected transfer scope. The button is disabled while a send is in progress. Matching stored values can still be sent to apply staged geometry to hardware. The source's native dimensions must match the destination's current content. -The target bank is fixed when configured. Sending starts immediately without a stale-state check. Writes are sequential; -partial failures require refreshing, not automatic retries or rollback. Success +The target bank is fixed when configured. Sending checks for changed source, +framing and destination state before writing. Writes are sequential. A rejected +or failed send refreshes the device read-only: if it responds, the connection +stays available with updated state and the send error remains visible. Check the +updated layers before sending again. Failed refreshes mark the device unavailable; +there is no automatic send retry or rollback after partial failures. Success means all changed fields matched device readback. There are no automatic sends from edits, imports or undo. Closing the connection window does not cancel a send already started; wait for its result before loading another project. diff --git a/docs/interfaces/midra-device-send.md b/docs/interfaces/midra-device-send.md index 969ef46..954f36b 100644 --- a/docs/interfaces/midra-device-send.md +++ b/docs/interfaces/midra-device-send.md @@ -344,3 +344,12 @@ sends remain separate and explicit. The local source-model permission is now an independent editor-only checkbox. Existing serialized `input_proposals` are retained and filtered by the same layer/link identity rules, but cannot be activated by the removed UI. No project, recovery or exchange schema/version change is required. + +### Send failure and connection state + +A failed layer-send job performs one read-only device refresh. A successful refresh +replaces the stale snapshot and keeps the connection available, while preserving +the send error and any completed-screen count. No send is retried automatically; +check the refreshed state before explicitly sending again. If refreshing fails, +the snapshot is cleared and both errors are reported. This also applies to partial +send failures: refreshing does not imply rollback or successful completion.