printobserver-core 0.18.0

Supervision logic for printobserver.
Documentation
//! The image a check is shown, chosen in one place.
//!
//! A frame is stored at the size the camera took it, and the operator's own
//! image read serves exactly that. What a supervision turn is shown — the
//! image attached to its prompt and the image a look answers — is whatever the
//! installed [`CheckImages`] makes of the stored frame, because every call of a
//! turn re-sends the images it holds. With none installed, a check is shown the
//! stored frame itself.
//!
//! What makes the image is told what it is for — a check, or a look — and
//! where the print stood in its own G-code when the check began, so that it can
//! draw that plan on the frame; it answers the image and one line saying what
//! that image is, which the turn's prompt carries.

use std::path::{Path, PathBuf};
use std::sync::Arc;

use printobserver_printer_api::JobSnapshot;
use printobserver_supervisor_api::{ImageFallback, ImageKind};
use printobserver_types::{EventBody, EventRecord, ImageRef, PrintId, Timestamp};

use crate::gcode_index::GcodeIndex;
use crate::records::PrintRecord;
use crate::supervisor::Supervisor;

/// What a check's image is asked for.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ImageUse {
    /// The image attached to a supervision turn's prompt: the print's next
    /// check is shown this one's frame as the previous.
    Check,
    /// The image a look answers, which moves no check along.
    Look,
}

/// Where a print stood in its own G-code: the index of its file, and the
/// job's file position in it.
#[derive(Debug, Clone)]
pub struct PlanPosition {
    /// The print's G-code index.
    pub index: Arc<GcodeIndex>,
    /// The job's file position, in bytes.
    pub file_position_bytes: u64,
}

/// One request for a check's image of a stored frame.
#[derive(Debug, Clone)]
pub struct CheckImageRequest<'a> {
    /// The print the image is of; none for a look at the printer.
    pub print_id: Option<PrintId>,
    /// Where the stored frame is.
    pub frame: &'a Path,
    /// The stored frame's SHA-256, as the image store names it: what a record
    /// of a judgement made on it names.
    pub frame_sha256: &'a str,
    /// Whether the frame is the configured camera's own rather than a
    /// detector's picture: only the camera's own frames are judged against its
    /// calibration.
    pub own_camera: bool,
    /// Whether the printer reported the print paused as the check began, its
    /// bed moved off the plan's height.
    pub paused: bool,
    /// When the frame was captured.
    pub captured_at: Timestamp,
    /// What the image is for.
    pub made_for: ImageUse,
    /// Where the print stood in its G-code, when its job is its own file and
    /// reported a position the index was read for.
    pub plan: Option<PlanPosition>,
    /// The record of the camera check the print started under, which it is
    /// drawn through from its first check; none when it started under none,
    /// or for a look at the printer.
    pub camera_check: Option<EventRecord>,
}

/// A check's image, and the line saying what it is.
#[derive(Debug, Clone, PartialEq)]
pub struct ShownImage {
    /// The image's path.
    pub path: PathBuf,
    /// What the image is, and why it is that one, in one line; none when what
    /// made it says nothing.
    pub note: Option<String>,
    /// Whether it is the three-panel image, drawn from the print's plan,
    /// rather than a frame as the camera took it or scaled whole.
    pub composite: bool,
    /// Why it is the whole frame, when it is and what made it gave a reason.
    pub fallback: Option<ImageFallback>,
    /// What was judged of the frame making the image, each to be appended to
    /// the request's print's history in this order. Each names the stored
    /// frame it was made on.
    pub records: Vec<EventBody>,
}

impl ShownImage {
    /// An image that is a frame as it is, with nothing judged of it.
    #[must_use]
    pub const fn plain(path: PathBuf, note: Option<String>) -> Self {
        Self {
            path,
            note,
            composite: false,
            fallback: None,
            records: Vec::new(),
        }
    }

    /// Which image it is.
    #[must_use]
    pub const fn kind(&self) -> ImageKind {
        if self.composite {
            ImageKind::ThreePanel
        } else {
            ImageKind::WholeFrame
        }
    }
}

/// What makes a check's image of one stored frame.
pub trait CheckImages: Send + Sync {
    /// The path of the image a check of `print_id` — or a look at the printer,
    /// when it names none — is shown of the frame stored at `frame`, or why
    /// none could be made.
    ///
    /// # Errors
    ///
    /// Answers why the image could not be made: the frame unreadable, or the
    /// image unwritable.
    fn check_image(&self, print_id: Option<PrintId>, frame: &Path) -> Result<PathBuf, String>;

    /// The image one request is shown and the line saying what it is: by
    /// default [`check_image`](Self::check_image)'s, saying nothing.
    ///
    /// # Errors
    ///
    /// As [`check_image`](Self::check_image)'s.
    fn shown_image(&self, request: &CheckImageRequest<'_>) -> Result<ShownImage, String> {
        self.check_image(request.print_id, request.frame)
            .map(|path| ShownImage::plain(path, None))
    }

    /// Whether a check of `print_id` would be shown the three-panel image of a
    /// frame from the server's own camera: by default, never.
    fn composes(&self, print_id: PrintId) -> bool {
        let _ = print_id;
        false
    }

    /// Measure `print_id`'s growth only from frames checked after now: it
    /// waited at a planned change, and a frame from before the wait would
    /// count the wait as no growth. By default there is nothing to forget.
    fn restart_growth(&self, print_id: PrintId) {
        let _ = print_id;
    }
}

/// When an event's frame was captured: what the arrival knew, else what its
/// payload says, else when the event was received.
pub(crate) fn captured_at_of(event: &EventRecord, known: Option<Timestamp>) -> Timestamp {
    known
        .or_else(|| {
            event
                .body
                .payload
                .get("frame_captured_at")
                .cloned()
                .and_then(|value| printobserver_types::serde_json::from_value(value).ok())
        })
        .unwrap_or(event.received_at)
}

/// What re-judging a print's camera needs, read now.
#[derive(Debug, Clone)]
pub struct FreshView {
    /// A good frame from the configured camera, not yet stored, and the URL
    /// it came from; none when no camera is configured or none came.
    pub frame: Option<(String, printobserver_vision_api::FetchedImage)>,
    /// Where the print stands in its own G-code, when it is open and its job
    /// is its own file.
    pub plan: Option<PlanPosition>,
    /// Whether the printer reports the print paused.
    pub paused: bool,
    /// The record of the camera check the print started under, when it
    /// started under one.
    pub camera_check: Option<EventRecord>,
}

impl Supervisor {
    /// Read what re-judging one print's camera needs, now: a fresh frame,
    /// where the print stands in its plan, and whether it is paused. Nothing
    /// is written and nothing is asked of the printer but two reads.
    ///
    /// # Errors
    ///
    /// [`CoreError::NoSuchPrint`](crate::CoreError::NoSuchPrint) when nothing
    /// is held under that identifier, and the store's own error when the print
    /// could not be read.
    pub async fn fresh_view(&self, print_id: PrintId) -> Result<FreshView, crate::CoreError> {
        let print = self
            .stores()
            .prints
            .print(print_id)
            .await?
            .ok_or(crate::CoreError::NoSuchPrint { print_id })?;
        let job = self.read_job().await.ok();
        let paused = self
            .read_snapshot()
            .await
            .is_ok_and(|taken| taken.connection == printobserver_types::PrinterState::Paused);
        let plan = if print.ended_at.is_none() {
            self.plan_position(&print, job.as_ref()).await
        } else {
            None
        };
        let frame = self.fetch_camera_image(print_id).await;
        let camera_check = self.print_camera_check(print_id).await;
        Ok(FreshView {
            frame,
            plan,
            paused,
            camera_check,
        })
    }

    /// The record of the camera check one print started under: the one its
    /// `print_started` names, which the start held to being `aligned` on the
    /// manifest's sheet — never a manifest written afterwards, which no start
    /// checked. None when it names none, or when the log cannot be read, which
    /// leaves the print drawn as one naming none is.
    pub(crate) async fn print_camera_check(&self, print_id: PrintId) -> Option<EventRecord> {
        let named = self.print_started_of(print_id).await.ok()??.camera_check?;
        self.camera_check_record(named).await.ok().flatten()
    }

    /// Install what makes every check's image. The first installation stands;
    /// answers whether this one did.
    pub fn install_check_images(&self, images: std::sync::Arc<dyn CheckImages>) -> bool {
        self.check_images.set(images).is_ok()
    }

    /// Where a print stood in its own G-code by a job read: nothing when the
    /// job runs another file, reported no position, or the file has no index.
    pub(crate) async fn plan_position(
        &self,
        print: &PrintRecord,
        job: Option<&JobSnapshot>,
    ) -> Option<PlanPosition> {
        let job = job?;
        if job.file_name.is_none() || job.file_name != print.file_name {
            return None;
        }
        let file_position_bytes = u64::try_from(job.file_position_bytes?).ok()?;
        let index = self.print_gcode_index(print.id).await?;
        Some(PlanPosition {
            index,
            file_position_bytes,
        })
    }

    /// The image a check is shown of one stored frame, and the line saying
    /// what it is: the stored frame itself when nothing that makes one is
    /// installed, or when what is installed could not make it, which is a
    /// check shown more than it needs rather than a check shown nothing.
    #[expect(
        clippy::too_many_arguments,
        reason = "each is one fact of the request the installed images are asked"
    )]
    pub(crate) async fn shown_image_of(
        &self,
        image: &ImageRef,
        print_id: Option<PrintId>,
        captured_at: Timestamp,
        made_for: ImageUse,
        plan: Option<PlanPosition>,
        own_camera: bool,
        paused: bool,
    ) -> Option<ShownImage> {
        let stored = self.image_path(image).await?;
        let Some(images) = self.check_images.get() else {
            return Some(ShownImage::plain(stored, None));
        };
        let camera_check = match print_id {
            Some(print_id) => self.print_camera_check(print_id).await,
            None => None,
        };
        let request = CheckImageRequest {
            print_id,
            camera_check,
            frame: &stored,
            frame_sha256: &image.sha256,
            captured_at,
            made_for,
            plan,
            own_camera,
            paused,
        };
        let mut shown = images.shown_image(&request).unwrap_or_else(|error| {
            let message = format!("its check image could not be made: {error}");
            ShownImage {
                note: Some(format!(
                    "the frame as the camera took it, because {message}"
                )),
                fallback: Some(ImageFallback {
                    code: printobserver_supervisor_api::WholeFrameReasonCode::Refused,
                    message,
                }),
                ..ShownImage::plain(stored.clone(), None)
            }
        });
        // What was judged of the frame is the print's record, each naming the
        // stored frame it was made on. A record the store refuses is said on
        // the log and costs the check nothing: the image is shown either way.
        for body in std::mem::take(&mut shown.records) {
            let kind = body.kind.clone();
            if let Err(error) = self.write_body_with_frame(print_id, body, None).await {
                eprintln!("printobserver: a {kind} record could not be written: {error}");
            }
        }
        Some(shown)
    }
}