Skip to main content

printobserver_core/records/
action.rs

1//! The closed vocabulary an actor may ask for, and the record of one request.
2//!
3//! [`PrintAction`] has no arbitrary-command variant, no G-code field and no
4//! escape hatch, which is the whole point of the vocabulary being closed: it is
5//! the reason an agent is allowed near a machine that can destroy itself. The
6//! only free-form text any variant carries is the `reason` every action
7//! carries, and the only caller-supplied text that reaches the printer's own
8//! file API is a [`FileName`](printobserver_types::FileName), which validates itself.
9//!
10//! Filtration is deliberately not a variant: nobody has yet confirmed that the
11//! target printer exposes filtration through `OctoPrint` at all, and an action
12//! the machine cannot perform is one an agent can call that silently does
13//! nothing.
14
15use printobserver_types::schemars::JsonSchema;
16use printobserver_types::serde::{Deserialize, Serialize};
17use printobserver_types::{EventId, FileName, PrintId, Timestamp};
18
19use super::charge::PrintOutcome;
20use super::ids::ActionId;
21use super::manifest::JobManifest;
22use super::policy::PolicyDecision;
23
24/// Who asked for something.
25#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
26#[serde(
27    crate = "printobserver_types::serde",
28    rename_all = "snake_case",
29    deny_unknown_fields
30)]
31#[schemars(crate = "printobserver_types::schemars")]
32pub enum Actor {
33    /// The supervising agent, naming its session.
34    Agent {
35        /// The supervision session the agent is acting in.
36        session_name: String,
37    },
38    /// A person.
39    Operator,
40    /// The supervisor itself.
41    System,
42}
43
44impl Actor {
45    /// The class this actor belongs to.
46    #[must_use]
47    pub const fn class(&self) -> ActorClass {
48        match self {
49            Self::Agent { .. } => ActorClass::Agent,
50            Self::Operator => ActorClass::Operator,
51            Self::System => ActorClass::System,
52        }
53    }
54}
55
56/// An actor class, which is what a safety envelope grants actions to.
57#[derive(
58    Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, JsonSchema,
59)]
60#[serde(
61    crate = "printobserver_types::serde",
62    rename_all = "snake_case",
63    deny_unknown_fields
64)]
65#[schemars(crate = "printobserver_types::schemars")]
66pub enum ActorClass {
67    /// The supervising agent.
68    Agent,
69    /// A person.
70    Operator,
71    /// The supervisor itself.
72    System,
73}
74
75/// What an operator's acknowledgement of a failure event asks for next.
76#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
77#[serde(
78    crate = "printobserver_types::serde",
79    rename_all = "snake_case",
80    deny_unknown_fields
81)]
82#[schemars(crate = "printobserver_types::schemars")]
83pub enum AcknowledgementDisposition {
84    /// Carry on printing.
85    Continue,
86    /// Carry on printing, watched more closely.
87    Watch,
88    /// The print should stop. Recorded for whoever may cancel it: the
89    /// acknowledgement itself asks nothing of the machine.
90    Stop,
91}
92
93/// One action of the closed vocabulary, named without its payload.
94///
95/// This is what a safety envelope grants and what a policy rejection names; the
96/// payload lives on [`PrintAction`] itself.
97#[derive(
98    Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, JsonSchema,
99)]
100#[serde(
101    crate = "printobserver_types::serde",
102    rename_all = "snake_case",
103    deny_unknown_fields
104)]
105#[schemars(crate = "printobserver_types::schemars")]
106pub enum ActionKind {
107    /// Pause the print.
108    Pause,
109    /// Resume the print.
110    Resume,
111    /// Cancel the print.
112    Cancel,
113    /// Start a print of a named file.
114    StartPrint,
115    /// Set the feedrate factor.
116    SetFeedrateFactor,
117    /// Set the flowrate factor.
118    SetFlowrateFactor,
119    /// Set a tool's target temperature.
120    SetToolTargetC,
121    /// Set the bed's target temperature.
122    SetBedTargetC,
123    /// Set the fan percentage.
124    SetFanPercent,
125    /// Acknowledge a failure event.
126    AcknowledgeFailure,
127    /// Close a print record printobserver left open.
128    ClosePrint,
129    /// Register a spool.
130    RegisterSpool,
131    /// Load a spool onto a tool.
132    LoadSpool,
133    /// Unload a spool from its tool.
134    UnloadSpool,
135    /// Correct a spool's remaining figure.
136    AdjustSpool,
137    /// Retire a spool.
138    RetireSpool,
139}
140
141/// The whole vocabulary an actor may ask for, and there is no other.
142#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
143#[serde(
144    crate = "printobserver_types::serde",
145    tag = "action",
146    rename_all = "snake_case",
147    deny_unknown_fields
148)]
149#[schemars(crate = "printobserver_types::schemars")]
150pub enum PrintAction {
151    /// Pause the print.
152    Pause {
153        /// Why the actor is asking for this.
154        reason: String,
155        /// Who is asking.
156        actor: Actor,
157    },
158    /// Resume the print.
159    Resume {
160        /// Why the actor is asking for this.
161        reason: String,
162        /// Who is asking.
163        actor: Actor,
164    },
165    /// Cancel the print.
166    Cancel {
167        /// Why the actor is asking for this.
168        reason: String,
169        /// Who is asking.
170        actor: Actor,
171    },
172    /// Start a print of a named file, with a manifest.
173    StartPrint {
174        /// The file to print, validated as a name a file API can be asked for.
175        file_name: FileName,
176        /// The manifest this print is bounded by.
177        manifest: JobManifest,
178        /// Start despite an insufficient or unverified spool, or a file
179        /// `OctoPrint` has not analysed; the start's own reason is the
180        /// override's reason.
181        // llmlint: ignore[invalid_states_unrepresentable] suppressions.toml has the reason.
182        #[serde(default, skip_serializing_if = "std::ops::Not::not")]
183        override_filament_check: bool,
184        /// Start despite a file sliced for another material than the spool
185        /// loaded on tool 0, or naming none; only an operator's asking counts,
186        /// and the start's own reason is the override's reason.
187        // llmlint: ignore[invalid_states_unrepresentable] suppressions.toml has the reason.
188        #[serde(default, skip_serializing_if = "std::ops::Not::not")]
189        #[schemars(extend("x-two-valued-fact" = true))]
190        override_material_check: bool,
191        /// Start despite a declared build sheet the sheet table does not allow
192        /// the loaded spool's material on; only an operator's asking counts,
193        /// and it never lets through a manifest that declares no sheet.
194        // llmlint: ignore[invalid_states_unrepresentable] suppressions.toml has the reason.
195        #[serde(default, skip_serializing_if = "std::ops::Not::not")]
196        #[schemars(extend("x-two-valued-fact" = true))]
197        override_sheet_check: bool,
198        /// Why the actor is asking for this.
199        reason: String,
200        /// Who is asking.
201        actor: Actor,
202    },
203    /// Set the feedrate factor, where one means one hundred percent.
204    SetFeedrateFactor {
205        /// The multiplier asked for.
206        factor: f64,
207        /// How long the change stands for, in whole seconds.
208        #[serde(default, skip_serializing_if = "Option::is_none")]
209        duration_s: Option<i64>,
210        /// Why the actor is asking for this.
211        reason: String,
212        /// Who is asking.
213        actor: Actor,
214    },
215    /// Set the flowrate factor, where one means one hundred percent.
216    SetFlowrateFactor {
217        /// The multiplier asked for.
218        factor: f64,
219        /// How long the change stands for, in whole seconds.
220        #[serde(default, skip_serializing_if = "Option::is_none")]
221        duration_s: Option<i64>,
222        /// Why the actor is asking for this.
223        reason: String,
224        /// Who is asking.
225        actor: Actor,
226    },
227    /// Set one tool's target temperature.
228    SetToolTargetC {
229        /// The tool, in the printer's own numbering.
230        tool: i64,
231        /// The target temperature asked for, in degrees Celsius.
232        target_c: f64,
233        /// How long the change stands for, in whole seconds.
234        #[serde(default, skip_serializing_if = "Option::is_none")]
235        duration_s: Option<i64>,
236        /// Why the actor is asking for this.
237        reason: String,
238        /// Who is asking.
239        actor: Actor,
240    },
241    /// Set the bed's target temperature.
242    SetBedTargetC {
243        /// The target temperature asked for, in degrees Celsius.
244        target_c: f64,
245        /// How long the change stands for, in whole seconds.
246        #[serde(default, skip_serializing_if = "Option::is_none")]
247        duration_s: Option<i64>,
248        /// Why the actor is asking for this.
249        reason: String,
250        /// Who is asking.
251        actor: Actor,
252    },
253    /// Set the part-cooling fan percentage.
254    SetFanPercent {
255        /// The percentage asked for.
256        percent: f64,
257        /// How long the change stands for, in whole seconds.
258        #[serde(default, skip_serializing_if = "Option::is_none")]
259        duration_s: Option<i64>,
260        /// Why the actor is asking for this.
261        reason: String,
262        /// Who is asking.
263        actor: Actor,
264    },
265    /// Acknowledge a failure event, with a disposition.
266    AcknowledgeFailure {
267        /// The event being acknowledged.
268        event_id: EventId,
269        /// What to do next.
270        disposition: AcknowledgementDisposition,
271        /// Why the actor is asking for this.
272        reason: String,
273        /// Who is asking.
274        actor: Actor,
275    },
276    /// Close a print record printobserver left open, with the outcome the
277    /// operator judges it ended with. It asks nothing of the machine and
278    /// charges no spool: the record's consumption was never seen to end.
279    ClosePrint {
280        /// How the print ended, in the operator's judgement.
281        outcome: PrintOutcome,
282        /// Why the actor is asking for this.
283        reason: String,
284        /// Who is asking.
285        actor: Actor,
286    },
287}
288
289impl PrintAction {
290    /// Which action this is, without its payload.
291    #[must_use]
292    pub const fn kind(&self) -> ActionKind {
293        match self {
294            Self::Pause { .. } => ActionKind::Pause,
295            Self::Resume { .. } => ActionKind::Resume,
296            Self::Cancel { .. } => ActionKind::Cancel,
297            Self::StartPrint { .. } => ActionKind::StartPrint,
298            Self::SetFeedrateFactor { .. } => ActionKind::SetFeedrateFactor,
299            Self::SetFlowrateFactor { .. } => ActionKind::SetFlowrateFactor,
300            Self::SetToolTargetC { .. } => ActionKind::SetToolTargetC,
301            Self::SetBedTargetC { .. } => ActionKind::SetBedTargetC,
302            Self::SetFanPercent { .. } => ActionKind::SetFanPercent,
303            Self::AcknowledgeFailure { .. } => ActionKind::AcknowledgeFailure,
304            Self::ClosePrint { .. } => ActionKind::ClosePrint,
305        }
306    }
307
308    /// Why the actor asked for this.
309    #[must_use]
310    pub fn reason(&self) -> &str {
311        match self {
312            Self::Pause { reason, .. }
313            | Self::Resume { reason, .. }
314            | Self::Cancel { reason, .. }
315            | Self::StartPrint { reason, .. }
316            | Self::SetFeedrateFactor { reason, .. }
317            | Self::SetFlowrateFactor { reason, .. }
318            | Self::SetToolTargetC { reason, .. }
319            | Self::SetBedTargetC { reason, .. }
320            | Self::SetFanPercent { reason, .. }
321            | Self::AcknowledgeFailure { reason, .. }
322            | Self::ClosePrint { reason, .. } => reason,
323        }
324    }
325
326    /// Who asked for this.
327    #[must_use]
328    pub const fn actor(&self) -> &Actor {
329        match self {
330            Self::Pause { actor, .. }
331            | Self::Resume { actor, .. }
332            | Self::Cancel { actor, .. }
333            | Self::StartPrint { actor, .. }
334            | Self::SetFeedrateFactor { actor, .. }
335            | Self::SetFlowrateFactor { actor, .. }
336            | Self::SetToolTargetC { actor, .. }
337            | Self::SetBedTargetC { actor, .. }
338            | Self::SetFanPercent { actor, .. }
339            | Self::AcknowledgeFailure { actor, .. }
340            | Self::ClosePrint { actor, .. } => actor,
341        }
342    }
343}
344
345/// What happened when an accepted action reached the printer.
346#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
347#[serde(
348    crate = "printobserver_types::serde",
349    rename_all = "snake_case",
350    deny_unknown_fields
351)]
352#[schemars(crate = "printobserver_types::schemars")]
353pub enum ExecutionOutcome {
354    /// The printer took it.
355    Succeeded,
356    /// The printer did not, for this reason.
357    Failed {
358        /// Why it failed.
359        reason: String,
360    },
361}
362
363/// One request an actor made, at the instant it made it.
364#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
365#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
366#[schemars(crate = "printobserver_types::schemars")]
367pub struct ActionRequest {
368    /// What was asked for.
369    pub action: PrintAction,
370    /// Who asked.
371    pub actor: Actor,
372    /// When they asked.
373    pub requested_at: Timestamp,
374}
375
376/// One request, the decision taken on it, and what became of it.
377///
378/// `executed_at` and `outcome` are both optional and are absent together: a
379/// record is written when the decision is taken, which is before — and, for a
380/// rejected request, instead of — anything reaching the printer.
381#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
382#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
383#[schemars(crate = "printobserver_types::schemars")]
384pub struct ActionRecord {
385    /// This record's identifier, minted by the store.
386    pub id: ActionId,
387    /// The print this request was made against.
388    pub print_id: PrintId,
389    /// The request itself.
390    pub request: ActionRequest,
391    /// The decision policy took on it.
392    pub decision: PolicyDecision,
393    /// When it reached the printer, if it did.
394    #[serde(default, skip_serializing_if = "Option::is_none")]
395    pub executed_at: Option<Timestamp>,
396    /// What the printer made of it, if it reached the printer.
397    #[serde(default, skip_serializing_if = "Option::is_none")]
398    pub outcome: Option<ExecutionOutcome>,
399}