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        /// Why the actor is asking for this.
185        reason: String,
186        /// Who is asking.
187        actor: Actor,
188    },
189    /// Set the feedrate factor, where one means one hundred percent.
190    SetFeedrateFactor {
191        /// The multiplier asked for.
192        factor: f64,
193        /// How long the change stands for, in whole seconds.
194        #[serde(default, skip_serializing_if = "Option::is_none")]
195        duration_s: Option<i64>,
196        /// Why the actor is asking for this.
197        reason: String,
198        /// Who is asking.
199        actor: Actor,
200    },
201    /// Set the flowrate factor, where one means one hundred percent.
202    SetFlowrateFactor {
203        /// The multiplier asked for.
204        factor: f64,
205        /// How long the change stands for, in whole seconds.
206        #[serde(default, skip_serializing_if = "Option::is_none")]
207        duration_s: Option<i64>,
208        /// Why the actor is asking for this.
209        reason: String,
210        /// Who is asking.
211        actor: Actor,
212    },
213    /// Set one tool's target temperature.
214    SetToolTargetC {
215        /// The tool, in the printer's own numbering.
216        tool: i64,
217        /// The target temperature asked for, in degrees Celsius.
218        target_c: 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 the bed's target temperature.
228    SetBedTargetC {
229        /// The target temperature asked for, in degrees Celsius.
230        target_c: f64,
231        /// How long the change stands for, in whole seconds.
232        #[serde(default, skip_serializing_if = "Option::is_none")]
233        duration_s: Option<i64>,
234        /// Why the actor is asking for this.
235        reason: String,
236        /// Who is asking.
237        actor: Actor,
238    },
239    /// Set the part-cooling fan percentage.
240    SetFanPercent {
241        /// The percentage asked for.
242        percent: f64,
243        /// How long the change stands for, in whole seconds.
244        #[serde(default, skip_serializing_if = "Option::is_none")]
245        duration_s: Option<i64>,
246        /// Why the actor is asking for this.
247        reason: String,
248        /// Who is asking.
249        actor: Actor,
250    },
251    /// Acknowledge a failure event, with a disposition.
252    AcknowledgeFailure {
253        /// The event being acknowledged.
254        event_id: EventId,
255        /// What to do next.
256        disposition: AcknowledgementDisposition,
257        /// Why the actor is asking for this.
258        reason: String,
259        /// Who is asking.
260        actor: Actor,
261    },
262    /// Close a print record printobserver left open, with the outcome the
263    /// operator judges it ended with. It asks nothing of the machine and
264    /// charges no spool: the record's consumption was never seen to end.
265    ClosePrint {
266        /// How the print ended, in the operator's judgement.
267        outcome: PrintOutcome,
268        /// Why the actor is asking for this.
269        reason: String,
270        /// Who is asking.
271        actor: Actor,
272    },
273}
274
275impl PrintAction {
276    /// Which action this is, without its payload.
277    #[must_use]
278    pub const fn kind(&self) -> ActionKind {
279        match self {
280            Self::Pause { .. } => ActionKind::Pause,
281            Self::Resume { .. } => ActionKind::Resume,
282            Self::Cancel { .. } => ActionKind::Cancel,
283            Self::StartPrint { .. } => ActionKind::StartPrint,
284            Self::SetFeedrateFactor { .. } => ActionKind::SetFeedrateFactor,
285            Self::SetFlowrateFactor { .. } => ActionKind::SetFlowrateFactor,
286            Self::SetToolTargetC { .. } => ActionKind::SetToolTargetC,
287            Self::SetBedTargetC { .. } => ActionKind::SetBedTargetC,
288            Self::SetFanPercent { .. } => ActionKind::SetFanPercent,
289            Self::AcknowledgeFailure { .. } => ActionKind::AcknowledgeFailure,
290            Self::ClosePrint { .. } => ActionKind::ClosePrint,
291        }
292    }
293
294    /// Why the actor asked for this.
295    #[must_use]
296    pub fn reason(&self) -> &str {
297        match self {
298            Self::Pause { reason, .. }
299            | Self::Resume { reason, .. }
300            | Self::Cancel { reason, .. }
301            | Self::StartPrint { reason, .. }
302            | Self::SetFeedrateFactor { reason, .. }
303            | Self::SetFlowrateFactor { reason, .. }
304            | Self::SetToolTargetC { reason, .. }
305            | Self::SetBedTargetC { reason, .. }
306            | Self::SetFanPercent { reason, .. }
307            | Self::AcknowledgeFailure { reason, .. }
308            | Self::ClosePrint { reason, .. } => reason,
309        }
310    }
311
312    /// Who asked for this.
313    #[must_use]
314    pub const fn actor(&self) -> &Actor {
315        match self {
316            Self::Pause { actor, .. }
317            | Self::Resume { actor, .. }
318            | Self::Cancel { actor, .. }
319            | Self::StartPrint { actor, .. }
320            | Self::SetFeedrateFactor { actor, .. }
321            | Self::SetFlowrateFactor { actor, .. }
322            | Self::SetToolTargetC { actor, .. }
323            | Self::SetBedTargetC { actor, .. }
324            | Self::SetFanPercent { actor, .. }
325            | Self::AcknowledgeFailure { actor, .. }
326            | Self::ClosePrint { actor, .. } => actor,
327        }
328    }
329}
330
331/// What happened when an accepted action reached the printer.
332#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
333#[serde(
334    crate = "printobserver_types::serde",
335    rename_all = "snake_case",
336    deny_unknown_fields
337)]
338#[schemars(crate = "printobserver_types::schemars")]
339pub enum ExecutionOutcome {
340    /// The printer took it.
341    Succeeded,
342    /// The printer did not, for this reason.
343    Failed {
344        /// Why it failed.
345        reason: String,
346    },
347}
348
349/// One request an actor made, at the instant it made it.
350#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
351#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
352#[schemars(crate = "printobserver_types::schemars")]
353pub struct ActionRequest {
354    /// What was asked for.
355    pub action: PrintAction,
356    /// Who asked.
357    pub actor: Actor,
358    /// When they asked.
359    pub requested_at: Timestamp,
360}
361
362/// One request, the decision taken on it, and what became of it.
363///
364/// `executed_at` and `outcome` are both optional and are absent together: a
365/// record is written when the decision is taken, which is before — and, for a
366/// rejected request, instead of — anything reaching the printer.
367#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
368#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
369#[schemars(crate = "printobserver_types::schemars")]
370pub struct ActionRecord {
371    /// This record's identifier, minted by the store.
372    pub id: ActionId,
373    /// The print this request was made against.
374    pub print_id: PrintId,
375    /// The request itself.
376    pub request: ActionRequest,
377    /// The decision policy took on it.
378    pub decision: PolicyDecision,
379    /// When it reached the printer, if it did.
380    #[serde(default, skip_serializing_if = "Option::is_none")]
381    pub executed_at: Option<Timestamp>,
382    /// What the printer made of it, if it reached the printer.
383    #[serde(default, skip_serializing_if = "Option::is_none")]
384    pub outcome: Option<ExecutionOutcome>,
385}