Skip to main content

printobserver_core/
camera_check.rs

1//! The camera checks a start may name, read back from the printer timeline.
2//!
3//! A check is the server's record, written as a `camera_checked` event that
4//! belongs to no print ([`CAMERA_CHECKED_KIND`]); it is read from the log
5//! rather than held, so a server started again finds every check the one
6//! before recorded. A start naming one is held to it here, and every check of
7//! the print it starts is handed it ([`crate::CheckImageRequest`]).
8
9use printobserver_types::{EventKind, EventRecord};
10
11use crate::error::CoreError;
12use crate::records::{
13    CAMERA_CHECKED_KIND, CameraCheckId, CameraCheckObjection, CameraCheckStanding,
14    CameraCheckVerdict, RejectionReason, SheetId,
15};
16use crate::store::{MAX_HISTORY_LIMIT, TimelineQuery};
17use crate::supervisor::Supervisor;
18
19impl Supervisor {
20    /// The record of one camera check, when one is recorded: its event,
21    /// newest first among the checks, whose record carries that identifier.
22    ///
23    /// # Errors
24    ///
25    /// Returns the store's own error when the log could not be read.
26    pub async fn camera_check_record(
27        &self,
28        camera_check: CameraCheckId,
29    ) -> Result<Option<EventRecord>, CoreError> {
30        let kind =
31            EventKind::new(CAMERA_CHECKED_KIND).map_err(|error| CoreError::Unrepresentable {
32                detail: error.to_string(),
33            })?;
34        let mut before = None;
35        loop {
36            let page = self
37                .stores()
38                .timeline
39                .timeline(TimelineQuery {
40                    kinds: vec![kind.clone()],
41                    since: None,
42                    until: None,
43                    spool_id: None,
44                    before,
45                    limit: Some(MAX_HISTORY_LIMIT),
46                })
47                .await?;
48            let Some(last) = page.last() else {
49                return Ok(None);
50            };
51            before = Some(last.id);
52            if let Some(found) = page
53                .into_iter()
54                .find(|event| standing_of(event).is_some_and(|read| read.id == camera_check))
55            {
56                return Ok(Some(found));
57            }
58        }
59    }
60
61    /// The refusal a start naming `camera_check` on `declared` is held to:
62    /// none for a check recorded `aligned` on that sheet.
63    ///
64    /// # Errors
65    ///
66    /// Returns the store's own error when the log could not be read.
67    pub(crate) async fn camera_check_refusal(
68        &self,
69        camera_check: CameraCheckId,
70        declared: &SheetId,
71    ) -> Result<Option<RejectionReason>, CoreError> {
72        let standing = self
73            .camera_check_record(camera_check)
74            .await?
75            .as_ref()
76            .and_then(standing_of);
77        let objection = match standing {
78            None => Some(CameraCheckObjection::Unknown),
79            Some(read) if read.verdict != CameraCheckVerdict::Aligned => {
80                Some(CameraCheckObjection::NotAligned {
81                    verdict: read.verdict,
82                })
83            }
84            Some(read) if read.sheet != *declared => Some(CameraCheckObjection::OtherSheet {
85                checked_on: read.sheet,
86                declared: declared.clone(),
87            }),
88            Some(_) => None,
89        };
90        Ok(
91            objection.map(|objection| RejectionReason::CameraCheckRefused {
92                camera_check,
93                objection,
94            }),
95        )
96    }
97}
98
99/// The fields of a camera check a start is held to, from its event; nothing
100/// for an event of another kind, or a record that does not carry them.
101#[must_use]
102pub fn standing_of(event: &EventRecord) -> Option<CameraCheckStanding> {
103    if event.kind().as_str() != CAMERA_CHECKED_KIND {
104        return None;
105    }
106    printobserver_types::serde_json::from_value(event.body.payload.get("camera_check")?.clone())
107        .ok()
108}