printobserver-server 0.15.1

The printobserver server.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
//! The image every check is shown, made of the frame the camera gave.
//!
//! The one place that chooses it ([`PanelImages`]). A check of a print is shown
//! `printobserver-panels`' three-panel check image when that image can be
//! trusted: a calibration is configured, the print's G-code index names the
//! object being laid and the layer at the check's file position, the print's
//! first-layer alignment `Passed`, and the frame passes the composer's
//! resolution, stream and drift checks. Every other check, and every look at
//! the printer, is shown the whole frame scaled to [`WHOLE_FRAME_WIDTH`] by
//! [`WHOLE_FRAME_HEIGHT`] and never cropped ([`WholeFrames`]). Either way the
//! image is written under `<state_dir>/check-images/` while the stored frame
//! keeps the camera's size, and one line says which image it is and why.
//!
//! A check shown the three-panel image is also measured against the newest
//! earlier check's frame at least [`MIN_RISE_MM`] below it in the plan
//! ([`measure_growth`]). When the parts this layer lays sit where that frame
//! had them, carried down with the bed, the line says so
//! ([`not_grown_note`]): a stall shows as a few pixels between a drawn line
//! and a part's top, which the eye misreads, and the measurement does not.

use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};

use std::sync::Arc;

use printobserver_core::check_image::PlanPosition;
use printobserver_core::{CheckImageRequest, CheckImages, GcodeIndex, ImageUse, ShownImage};
use printobserver_panels::growth::MIN_RISE_MM;
use printobserver_panels::{
    CameraCalibration, CheckImage, CheckImageInputs, ComposeError, GrowthInputs,
    WHOLE_FRAME_HEIGHT, WHOLE_FRAME_WIDTH, compose_check_image, measure_growth, whole_frame,
};
use printobserver_types::{PrintId, Timestamp};

use crate::alignment::{AlignmentFailure, Alignments, CheckedFrame, PreviousCheck, PrintAlignment};

/// How many scaled frames this process has begun writing, which names each
/// write's partial file apart from any other's.
static WRITES: AtomicU64 = AtomicU64::new(0);

/// The directory under the state directory check images are written in.
pub const CHECK_IMAGES_DIRECTORY: &str = "check-images";

/// Every check shown the whole frame, scaled.
#[derive(Debug, Clone)]
pub struct WholeFrames {
    /// Where the scaled frames are written.
    directory: PathBuf,
}

impl WholeFrames {
    /// Write scaled frames under one state directory.
    #[must_use]
    pub fn under(state_dir: &Path) -> Self {
        Self {
            directory: state_dir.join(CHECK_IMAGES_DIRECTORY),
        }
    }

    /// Where the scaled copy of one stored frame is written: named for the
    /// stored frame's own directory and file, which name it uniquely.
    fn destination(&self, frame: &Path) -> PathBuf {
        let named = |part: Option<&std::ffi::OsStr>| {
            part.map_or_else(String::new, |part| part.to_string_lossy().into_owned())
        };
        let parent = named(frame.parent().and_then(Path::file_name));
        let file = named(frame.file_name());
        self.directory.join(format!(
            "{parent}-{file}-{WHOLE_FRAME_WIDTH}x{WHOLE_FRAME_HEIGHT}.jpg"
        ))
    }
}

impl CheckImages for WholeFrames {
    fn check_image(&self, _print_id: Option<PrintId>, frame: &Path) -> Result<PathBuf, String> {
        let destination = self.destination(frame);
        if destination.is_file() {
            return Ok(destination);
        }
        let bytes = std::fs::read(frame)
            .map_err(|error| format!("{} is unreadable: {error}", frame.display()))?;
        let scaled = whole_frame(&bytes).map_err(|error| error.to_string())?;
        if !scaled.scaled {
            return Ok(frame.to_path_buf());
        }
        write_image(&self.directory, &destination, &scaled.jpeg)?;
        Ok(destination)
    }
}

/// Write one check image — a scaled frame or a three-panel image — under
/// `directory` at `destination`, beside its name first
/// and moved onto it, so a reader never opens a half-written image.
fn write_image(directory: &Path, destination: &Path, bytes: &[u8]) -> Result<(), String> {
    std::fs::create_dir_all(directory)
        .map_err(|error| format!("{} cannot be made: {error}", directory.display()))?;
    let partial = destination.with_extension(format!(
        "{}-{}.partial",
        std::process::id(),
        WRITES.fetch_add(1, Ordering::Relaxed)
    ));
    let written =
        std::fs::write(&partial, bytes).and_then(|()| std::fs::rename(&partial, destination));
    written.map_err(|error| {
        // Whatever was written is no image a reader may open.
        let _ = std::fs::remove_file(&partial);
        format!("{} cannot be written: {error}", destination.display())
    })
}

/// The first words of every line saying a check was shown the three-panel
/// image, which the real-print suite's record reads the image's kind from.
pub const COMPOSITE_NOTE: &str = "the three-panel check image";

/// The first words of every line saying a check was shown the whole frame.
pub const WHOLE_FRAME_NOTE: &str = "the whole camera frame";

/// Why a check is shown the whole frame rather than the three-panel image.
#[derive(Debug, Clone, PartialEq)]
pub enum WholeFrameReason {
    /// No print is named: a look at the printer, before, between or after
    /// prints, a start's bed check among them.
    NoPrint,
    /// No camera calibration is configured.
    NoCalibration,
    /// No camera snapshot address is configured, so the frame's stream is
    /// unknown.
    NoStream,
    /// The job is not the print's file, reported no position, or its file has
    /// no index.
    NoPosition,
    /// The print's G-code names no extruded object, or none laid yet, at the
    /// check's file position.
    NoObject,
    /// It names no layer there.
    NoLayer,
    /// The layer's planned toolpath could not be read from the print's spool.
    ToolpathUnread(String),
    /// The print's first-layer alignment is not decided yet.
    AlignmentPending,
    /// The print's G-code lays no object to align.
    AlignmentUnavailable,
    /// The print's first-layer alignment failed.
    AlignmentFailed(AlignmentFailure),
    /// The composer refused this frame.
    Refused(ComposeError),
}

impl core::fmt::Display for WholeFrameReason {
    fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::NoPrint => formatter
                .write_str("no print is named, so there is no planned toolpath to draw on it"),
            Self::NoCalibration => formatter.write_str("no camera calibration is configured"),
            Self::NoStream => formatter.write_str(
                "no camera snapshot address is configured, so the frame's stream is unknown",
            ),
            Self::NoPosition => formatter.write_str(
                "the print's G-code index gives no file position for this check",
            ),
            Self::NoObject => formatter.write_str(
                "the print's G-code names no extruded object laid at this point",
            ),
            Self::NoLayer => formatter.write_str("the print's G-code names no layer at this point"),
            Self::ToolpathUnread(detail) => write!(
                formatter,
                "the layer's planned toolpath could not be read from the print's spool: {detail}"
            ),
            Self::AlignmentPending => formatter.write_str(
                "the print's first-layer alignment is pending: its first object layer is not down, or no frame of it has been judged yet",
            ),
            Self::AlignmentUnavailable => {
                formatter.write_str("the print's G-code lays no object to align")
            }
            Self::AlignmentFailed(AlignmentFailure::Offset { offset_px }) => write!(
                formatter,
                "the print's first-layer alignment failed: the printed first layer lies {offset_px:.1} px from where the calibration projects it"
            ),
            Self::AlignmentFailed(AlignmentFailure::Resolution { expected, got }) => write!(
                formatter,
                "the print's first-layer alignment failed: the camera's frames are {}x{} and the calibration's {}x{}",
                got[0], got[1], expected[0], expected[1]
            ),
            Self::AlignmentFailed(AlignmentFailure::Stream) => formatter.write_str(
                "the print's first-layer alignment failed: the camera is not the stream the calibration was fitted on",
            ),
            Self::Refused(error) => error.fmt(formatter),
        }
    }
}

/// The line saying a check was shown the whole frame, and why.
#[must_use]
pub fn whole_frame_note(reason: &WholeFrameReason) -> String {
    format!(
        "{WHOLE_FRAME_NOTE}, scaled to fit {WHOLE_FRAME_WIDTH}x{WHOLE_FRAME_HEIGHT} and uncropped, because {reason}"
    )
}

/// What a check's note says of the drawing before it says what a part off it
/// means: an agent shown a part off its walls otherwise explains the
/// difference as the camera or the bed having shifted in view.
const CAMERA_HOLDS: &str = "the camera does not shift in regular operation and the drawing already accounts for the bed's travel, so ";

/// The line saying a check or a look was shown the three-panel image of the
/// frame captured at `current_at`, beside the frame `previous_at` names: panel
/// 3 empty when no earlier check's frame is at hand — the print's first check,
/// or one whose earlier frame is gone from the store.
///
/// A check's names which frame each panel is and when each was captured, and
/// says what the skill says: the camera does not shift and the drawing follows
/// the bed, so a part off it has moved unless there is strong evidence
/// otherwise, and no later frame undoes it. A turn handed a check's image decides on it, with no look to
/// stall on, so this is what it decides from. A look's says only what each
/// panel is.
#[must_use]
pub fn composite_note(
    made_for: ImageUse,
    current_at: Timestamp,
    previous_at: Option<Timestamp>,
) -> String {
    let (current, previous, moved) = match (made_for, previous_at) {
        (ImageUse::Look, at) => (
            "this layer's planned toolpath drawn on the frame".to_owned(),
            if at.is_none() {
                "panel 3 empty, no earlier check's frame being at hand".to_owned()
            } else {
                "panel 3 the previous check's frame".to_owned()
            },
            String::new(),
        ),
        (ImageUse::Check, None) => (
            format!(
                "this check's own frame, captured {current_at}, with this layer's planned toolpath drawn on it"
            ),
            "panel 3 empty, no earlier check's frame being at hand".to_owned(),
            format!(
                "; {CAMERA_HOLDS}a part off its drawn walls in panel 1 has moved unless there is strong evidence otherwise, and no later frame undoes that"
            ),
        ),
        (ImageUse::Check, Some(at)) => (
            format!(
                "this check's own frame, captured {current_at}, with this layer's planned toolpath drawn on it"
            ),
            format!("panel 3 the previous check's frame, captured {at}"),
            format!(
                "; {CAMERA_HOLDS}a part off its drawn walls in panel 1, or moved since panel 3, has moved unless there is strong evidence otherwise, and no later frame undoes that"
            ),
        ),
    };
    format!(
        "{COMPOSITE_NOTE}, {}x{}: panel 1 {current}, panel 2 a close-up of the part being printed, {previous}{moved}",
        printobserver_panels::compose::PANEL_WIDTH,
        printobserver_panels::compose::IMAGE_HEIGHT
    )
}

/// What a check's line adds when the measurement finds the parts this layer
/// lays have not grown since the frame captured at `since`, `rise_mm` lower in
/// the plan: the skill's own words for it, `not risen as planned`.
#[must_use]
pub fn not_grown_note(since: Timestamp, rise_mm: f64) -> String {
    format!(
        "; measured against the check frame captured {since}, {rise_mm:.1} mm lower in the plan: the parts this layer lays have not risen as planned, sitting where that frame had them, carried down with the bed, so nothing has been laid on them since"
    )
}

/// The print's height in its plan at a check's position.
fn plan_z(plan: &PlanPosition) -> Option<f64> {
    plan.index
        .layer_at(plan.file_position_bytes)
        .and_then(|layer| plan.index.layer_z_mm(layer))
}

/// What a check image is composed from, beyond the frames: the print's plan at
/// one file position.
#[derive(Debug, Clone, Copy)]
pub struct PlanAt<'a> {
    /// The print's G-code index, its toolpaths read from its spool.
    pub index: &'a GcodeIndex,
    /// The check's file position.
    pub file_position_bytes: u64,
}

/// Compose the three-panel check image of one frame at one file position, as
/// every check of a print whose alignment passed is shown it: the active
/// object at that position, the layer there — and, when that layer lays
/// supports alone, the nearest object layer below it — over the frame, with
/// `previous` as panel 3.
///
/// # Errors
///
/// [`WholeFrameReason`] naming what the index lacks at that position, or the
/// composer's own refusal.
pub fn compose_at(
    plan: PlanAt<'_>,
    current: (&[u8], Timestamp),
    previous: Option<(&[u8], Timestamp)>,
    camera_snapshot_url: &str,
    calibration: &CameraCalibration,
) -> Result<CheckImage, WholeFrameReason> {
    let index = plan.index;
    let position = plan.file_position_bytes;
    let active_object = index
        .object_at(position)
        .filter(|object| *object < index.object_outlines().len())
        .ok_or(WholeFrameReason::NoObject)?;
    let layer = index.layer_at(position).ok_or(WholeFrameReason::NoLayer)?;
    let print_z_mm = index.layer_z_mm(layer).ok_or(WholeFrameReason::NoLayer)?;
    let unread =
        |error: printobserver_core::SpoolError| WholeFrameReason::ToolpathUnread(error.to_string());
    let toolpath = index.layer_toolpath(layer).map_err(unread)?;
    let below = if toolpath.carries_object_extrusion() {
        None
    } else {
        index
            .object_layer_at_or_below(layer)
            .map(|below| index.layer_toolpath(below))
            .transpose()
            .map_err(unread)?
    };
    compose_check_image(&CheckImageInputs {
        current_jpeg: current.0,
        current_at: current.1,
        camera_snapshot_url,
        previous,
        outlines: index.object_outlines(),
        active_object,
        toolpath: &toolpath,
        object_layer_below: below.as_ref(),
        print_z_mm,
        calibration,
    })
    .map_err(WholeFrameReason::Refused)
}

/// Every check's image, the three-panel one where it can be trusted and the
/// whole frame otherwise, each with the line saying which and why.
pub struct PanelImages {
    /// The whole frames, and where every image is written.
    whole: WholeFrames,
    /// Each print's alignment verdict and last check, and the calibration.
    alignments: Arc<Alignments>,
    /// The configured camera's snapshot address: the stream every frame of a
    /// print is of, an alert's included, which the camera's own feed relays.
    camera_snapshot_url: Option<String>,
}

impl core::fmt::Debug for PanelImages {
    fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        formatter
            .debug_struct("PanelImages")
            .field("whole", &self.whole)
            .field("camera_snapshot_url", &self.camera_snapshot_url)
            .finish_non_exhaustive()
    }
}

impl PanelImages {
    /// The images of one state directory's frames.
    #[must_use]
    pub fn new(
        state_dir: &Path,
        alignments: Arc<Alignments>,
        camera_snapshot_url: Option<String>,
    ) -> Self {
        Self {
            whole: WholeFrames::under(state_dir),
            alignments,
            camera_snapshot_url,
        }
    }

    /// The whole frame, with the line saying why.
    fn whole_frame(&self, frame: &Path, reason: &WholeFrameReason) -> Result<ShownImage, String> {
        Ok(ShownImage {
            path: self.whole.check_image(None, frame)?,
            note: Some(whole_frame_note(reason)),
            composite: false,
        })
    }

    /// The three-panel image of one request of a print, or why it is shown
    /// the whole frame instead.
    fn composite(
        &self,
        print_id: PrintId,
        request: &CheckImageRequest<'_>,
        previous: Option<&PreviousCheck>,
    ) -> Result<Result<ShownImage, WholeFrameReason>, String> {
        let Some(calibration) = self.alignments.calibration() else {
            return Ok(Err(WholeFrameReason::NoCalibration));
        };
        let Some(url) = self.camera_snapshot_url.as_deref() else {
            return Ok(Err(WholeFrameReason::NoStream));
        };
        let Some(plan) = &request.plan else {
            return Ok(Err(WholeFrameReason::NoPosition));
        };
        let at = PlanAt {
            index: &plan.index,
            file_position_bytes: plan.file_position_bytes,
        };
        if at.index.object_at(at.file_position_bytes).is_none()
            || at.index.object_outlines().is_empty()
        {
            return Ok(Err(WholeFrameReason::NoObject));
        }
        if at.index.layer_at(at.file_position_bytes).is_none() {
            return Ok(Err(WholeFrameReason::NoLayer));
        }
        match self.alignments.print_alignment(print_id) {
            PrintAlignment::Passed { .. } => {}
            PrintAlignment::Pending => return Ok(Err(WholeFrameReason::AlignmentPending)),
            PrintAlignment::Unavailable => {
                return Ok(Err(WholeFrameReason::AlignmentUnavailable));
            }
            PrintAlignment::Failed { reason } => {
                return Ok(Err(WholeFrameReason::AlignmentFailed(reason)));
            }
        }
        let current = std::fs::read(request.frame)
            .map_err(|error| format!("{} is unreadable: {error}", request.frame.display()))?;
        // A previous frame gone from the store, or one that no longer decodes,
        // is one the panel cannot show, which leaves panel 3 empty rather than
        // the check without an image.
        let earlier = previous.and_then(|check| {
            std::fs::read(&check.frame)
                .ok()
                .filter(|bytes| whole_frame(bytes).is_ok())
                .map(|bytes| (bytes, check.captured_at))
        });
        let composed = match compose_at(
            at,
            (&current, request.captured_at),
            earlier.as_ref().map(|(bytes, at)| (bytes.as_slice(), *at)),
            url,
            calibration,
        ) {
            Ok(composed) => composed,
            // The current frame was judged whole by the vision port before it
            // was stored, and the previous one was decoded above, so neither
            // refusal is reached by a frame the camera gave; one that is is a
            // check shown the stored frame, as any check image that cannot be
            // made is, which `a check image that cannot be written` proves.
            Err(WholeFrameReason::Refused(
                ComposeError::Undecodable { detail } | ComposeError::Encode { detail },
            )) => return Err(detail),
            Err(reason) => return Ok(Err(reason)),
        };
        let destination = self.composite_destination(request.frame, previous);
        write_image(&self.whole.directory, &destination, &composed.jpeg)?;
        let mut note = composite_note(
            request.made_for,
            request.captured_at,
            earlier.as_ref().map(|(_, at)| *at),
        );
        if request.made_for == ImageUse::Check
            && let Some(clause) = self.growth_clause(print_id, plan, &current, url, calibration)
        {
            note.push_str(&clause);
        }
        Ok(Ok(ShownImage {
            path: destination,
            note: Some(note),
            composite: true,
        }))
    }

    /// What a check's line adds of whether the parts grew: the
    /// [`not_grown_note`] when the measurement against the newest earlier
    /// check's frame at least [`MIN_RISE_MM`] below finds they did not, and
    /// nothing otherwise — no such frame, one gone from the store, a layer
    /// laying supports alone, or a measurement that found growth or could not
    /// tell. Each measurement made is said on the log.
    fn growth_clause(
        &self,
        print_id: PrintId,
        plan: &PlanPosition,
        current: &[u8],
        url: &str,
        calibration: &CameraCalibration,
    ) -> Option<String> {
        let layer = plan.index.layer_at(plan.file_position_bytes)?;
        let print_z_mm = plan.index.layer_z_mm(layer)?;
        let reference = self
            .alignments
            .growth_reference(print_id, print_z_mm, MIN_RISE_MM)?;
        let earlier = std::fs::read(&reference.frame).ok()?;
        let toolpath = plan.index.layer_toolpath(layer).ok()?;
        let measured = measure_growth(&GrowthInputs {
            earlier_jpeg: &earlier,
            earlier_print_z_mm: reference.print_z_mm,
            current_jpeg: current,
            current_print_z_mm: print_z_mm,
            camera_snapshot_url: url,
            toolpath: &toolpath,
            calibration,
        })
        .ok()??;
        let not_grown = measured.not_grown();
        eprintln!(
            "printobserver: print {print_id}: growth from Z {:.1} ({}) to Z {print_z_mm:.1}: carried {:.2}, held {:.2}, {:.0}% carried over {} samples {:.1} px apart: {}",
            reference.print_z_mm,
            reference.captured_at,
            measured.carried,
            measured.held,
            measured.carried_share * 100.0,
            measured.samples,
            measured.separation_px,
            if not_grown { "not grown" } else { "no verdict" }
        );
        not_grown.then(|| not_grown_note(reference.captured_at, print_z_mm - reference.print_z_mm))
    }

    /// Where the three-panel image of one stored frame, after one previous
    /// check, is written: named for both, which name it uniquely.
    fn composite_destination(&self, frame: &Path, previous: Option<&PreviousCheck>) -> PathBuf {
        let named = |frame: &Path| {
            let part = |part: Option<&std::ffi::OsStr>| {
                part.map_or_else(String::new, |part| part.to_string_lossy().into_owned())
            };
            format!(
                "{}-{}",
                part(frame.parent().and_then(Path::file_name)),
                part(frame.file_name())
            )
        };
        let after = previous.map_or_else(|| "first".to_owned(), |check| named(&check.frame));
        self.whole
            .directory
            .join(format!("{}-composite-after-{after}.jpg", named(frame)))
    }
}

impl CheckImages for PanelImages {
    fn check_image(&self, print_id: Option<PrintId>, frame: &Path) -> Result<PathBuf, String> {
        self.whole.check_image(print_id, frame)
    }

    fn composes(&self, print_id: PrintId) -> bool {
        self.alignments.calibration().is_some()
            && self.camera_snapshot_url.is_some()
            && matches!(
                self.alignments.print_alignment(print_id),
                PrintAlignment::Passed { .. }
            )
    }

    fn shown_image(&self, request: &CheckImageRequest<'_>) -> Result<ShownImage, String> {
        let Some(print_id) = request.print_id else {
            return self.whole_frame(request.frame, &WholeFrameReason::NoPrint);
        };
        let previous = self.alignments.previous_check(print_id);
        let shown = match self.composite(print_id, request, previous.as_ref())? {
            Ok(composite) => composite,
            Err(reason) => self.whole_frame(request.frame, &reason)?,
        };
        if request.made_for == ImageUse::Check {
            self.alignments.record_check(
                print_id,
                &PreviousCheck {
                    frame: request.frame.to_path_buf(),
                    captured_at: request.captured_at,
                },
            );
            if let Some(print_z_mm) = request.plan.as_ref().and_then(plan_z) {
                self.alignments.record_checked_frame(
                    print_id,
                    CheckedFrame {
                        frame: request.frame.to_path_buf(),
                        captured_at: request.captured_at,
                        print_z_mm,
                    },
                    MIN_RISE_MM,
                );
            }
        }
        Ok(shown)
    }
}