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::ids::ActionId;
20use super::manifest::JobManifest;
21use super::policy::PolicyDecision;
22
23/// Who asked for something.
24#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
25#[serde(
26    crate = "printobserver_types::serde",
27    rename_all = "snake_case",
28    deny_unknown_fields
29)]
30#[schemars(crate = "printobserver_types::schemars")]
31pub enum Actor {
32    /// The supervising agent, naming its session.
33    Agent {
34        /// The supervision session the agent is acting in.
35        session_name: String,
36    },
37    /// A person.
38    Operator,
39    /// The supervisor itself.
40    System,
41}
42
43impl Actor {
44    /// The class this actor belongs to.
45    #[must_use]
46    pub const fn class(&self) -> ActorClass {
47        match self {
48            Self::Agent { .. } => ActorClass::Agent,
49            Self::Operator => ActorClass::Operator,
50            Self::System => ActorClass::System,
51        }
52    }
53}
54
55/// An actor class, which is what a safety envelope grants actions to.
56#[derive(
57    Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, JsonSchema,
58)]
59#[serde(
60    crate = "printobserver_types::serde",
61    rename_all = "snake_case",
62    deny_unknown_fields
63)]
64#[schemars(crate = "printobserver_types::schemars")]
65pub enum ActorClass {
66    /// The supervising agent.
67    Agent,
68    /// A person.
69    Operator,
70    /// The supervisor itself.
71    System,
72}
73
74/// What an operator's acknowledgement of a failure event asks for next.
75#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
76#[serde(
77    crate = "printobserver_types::serde",
78    rename_all = "snake_case",
79    deny_unknown_fields
80)]
81#[schemars(crate = "printobserver_types::schemars")]
82pub enum AcknowledgementDisposition {
83    /// Carry on printing.
84    Continue,
85    /// Carry on printing, watched more closely.
86    Watch,
87    /// Stop the print.
88    Stop,
89}
90
91/// One action of the closed vocabulary, named without its payload.
92///
93/// This is what a safety envelope grants and what a policy rejection names; the
94/// payload lives on [`PrintAction`] itself.
95#[derive(
96    Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, JsonSchema,
97)]
98#[serde(
99    crate = "printobserver_types::serde",
100    rename_all = "snake_case",
101    deny_unknown_fields
102)]
103#[schemars(crate = "printobserver_types::schemars")]
104pub enum ActionKind {
105    /// Pause the print.
106    Pause,
107    /// Resume the print.
108    Resume,
109    /// Cancel the print.
110    Cancel,
111    /// Start a print of a named file.
112    StartPrint,
113    /// Set the feedrate factor.
114    SetFeedrateFactor,
115    /// Set the flowrate factor.
116    SetFlowrateFactor,
117    /// Set a tool's target temperature.
118    SetToolTargetC,
119    /// Set the bed's target temperature.
120    SetBedTargetC,
121    /// Set the fan percentage.
122    SetFanPercent,
123    /// Acknowledge a failure event.
124    AcknowledgeFailure,
125}
126
127/// The whole vocabulary an actor may ask for, and there is no other.
128#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
129#[serde(
130    crate = "printobserver_types::serde",
131    tag = "action",
132    rename_all = "snake_case",
133    deny_unknown_fields
134)]
135#[schemars(crate = "printobserver_types::schemars")]
136pub enum PrintAction {
137    /// Pause the print.
138    Pause {
139        /// Why the actor is asking for this.
140        reason: String,
141        /// Who is asking.
142        actor: Actor,
143    },
144    /// Resume the print.
145    Resume {
146        /// Why the actor is asking for this.
147        reason: String,
148        /// Who is asking.
149        actor: Actor,
150    },
151    /// Cancel the print.
152    Cancel {
153        /// Why the actor is asking for this.
154        reason: String,
155        /// Who is asking.
156        actor: Actor,
157    },
158    /// Start a print of a named file, with a manifest.
159    StartPrint {
160        /// The file to print, validated as a name a file API can be asked for.
161        file_name: FileName,
162        /// The manifest this print is bounded by.
163        manifest: JobManifest,
164        /// Why the actor is asking for this.
165        reason: String,
166        /// Who is asking.
167        actor: Actor,
168    },
169    /// Set the feedrate factor, where one means one hundred percent.
170    SetFeedrateFactor {
171        /// The multiplier asked for.
172        factor: f64,
173        /// How long the change stands for, in whole seconds.
174        #[serde(default, skip_serializing_if = "Option::is_none")]
175        duration_s: Option<i64>,
176        /// Why the actor is asking for this.
177        reason: String,
178        /// Who is asking.
179        actor: Actor,
180    },
181    /// Set the flowrate factor, where one means one hundred percent.
182    SetFlowrateFactor {
183        /// The multiplier asked for.
184        factor: f64,
185        /// How long the change stands for, in whole seconds.
186        #[serde(default, skip_serializing_if = "Option::is_none")]
187        duration_s: Option<i64>,
188        /// Why the actor is asking for this.
189        reason: String,
190        /// Who is asking.
191        actor: Actor,
192    },
193    /// Set one tool's target temperature.
194    SetToolTargetC {
195        /// The tool, in the printer's own numbering.
196        tool: i64,
197        /// The target temperature asked for, in degrees Celsius.
198        target_c: f64,
199        /// How long the change stands for, in whole seconds.
200        #[serde(default, skip_serializing_if = "Option::is_none")]
201        duration_s: Option<i64>,
202        /// Why the actor is asking for this.
203        reason: String,
204        /// Who is asking.
205        actor: Actor,
206    },
207    /// Set the bed's target temperature.
208    SetBedTargetC {
209        /// The target temperature asked for, in degrees Celsius.
210        target_c: f64,
211        /// How long the change stands for, in whole seconds.
212        #[serde(default, skip_serializing_if = "Option::is_none")]
213        duration_s: Option<i64>,
214        /// Why the actor is asking for this.
215        reason: String,
216        /// Who is asking.
217        actor: Actor,
218    },
219    /// Set the part-cooling fan percentage.
220    SetFanPercent {
221        /// The percentage asked for.
222        percent: f64,
223        /// How long the change stands for, in whole seconds.
224        #[serde(default, skip_serializing_if = "Option::is_none")]
225        duration_s: Option<i64>,
226        /// Why the actor is asking for this.
227        reason: String,
228        /// Who is asking.
229        actor: Actor,
230    },
231    /// Acknowledge a failure event, with a disposition.
232    AcknowledgeFailure {
233        /// The event being acknowledged.
234        event_id: EventId,
235        /// What to do next.
236        disposition: AcknowledgementDisposition,
237        /// Why the actor is asking for this.
238        reason: String,
239        /// Who is asking.
240        actor: Actor,
241    },
242}
243
244impl PrintAction {
245    /// Which action this is, without its payload.
246    #[must_use]
247    pub const fn kind(&self) -> ActionKind {
248        match self {
249            Self::Pause { .. } => ActionKind::Pause,
250            Self::Resume { .. } => ActionKind::Resume,
251            Self::Cancel { .. } => ActionKind::Cancel,
252            Self::StartPrint { .. } => ActionKind::StartPrint,
253            Self::SetFeedrateFactor { .. } => ActionKind::SetFeedrateFactor,
254            Self::SetFlowrateFactor { .. } => ActionKind::SetFlowrateFactor,
255            Self::SetToolTargetC { .. } => ActionKind::SetToolTargetC,
256            Self::SetBedTargetC { .. } => ActionKind::SetBedTargetC,
257            Self::SetFanPercent { .. } => ActionKind::SetFanPercent,
258            Self::AcknowledgeFailure { .. } => ActionKind::AcknowledgeFailure,
259        }
260    }
261
262    /// Why the actor asked for this.
263    #[must_use]
264    pub fn reason(&self) -> &str {
265        match self {
266            Self::Pause { reason, .. }
267            | Self::Resume { reason, .. }
268            | Self::Cancel { reason, .. }
269            | Self::StartPrint { reason, .. }
270            | Self::SetFeedrateFactor { reason, .. }
271            | Self::SetFlowrateFactor { reason, .. }
272            | Self::SetToolTargetC { reason, .. }
273            | Self::SetBedTargetC { reason, .. }
274            | Self::SetFanPercent { reason, .. }
275            | Self::AcknowledgeFailure { reason, .. } => reason,
276        }
277    }
278
279    /// Who asked for this.
280    #[must_use]
281    pub const fn actor(&self) -> &Actor {
282        match self {
283            Self::Pause { actor, .. }
284            | Self::Resume { actor, .. }
285            | Self::Cancel { actor, .. }
286            | Self::StartPrint { actor, .. }
287            | Self::SetFeedrateFactor { actor, .. }
288            | Self::SetFlowrateFactor { actor, .. }
289            | Self::SetToolTargetC { actor, .. }
290            | Self::SetBedTargetC { actor, .. }
291            | Self::SetFanPercent { actor, .. }
292            | Self::AcknowledgeFailure { actor, .. } => actor,
293        }
294    }
295}
296
297/// What happened when an accepted action reached the printer.
298#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
299#[serde(
300    crate = "printobserver_types::serde",
301    rename_all = "snake_case",
302    deny_unknown_fields
303)]
304#[schemars(crate = "printobserver_types::schemars")]
305pub enum ExecutionOutcome {
306    /// The printer took it.
307    Succeeded,
308    /// The printer did not, for this reason.
309    Failed {
310        /// Why it failed.
311        reason: String,
312    },
313}
314
315/// One request an actor made, at the instant it made it.
316#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
317#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
318#[schemars(crate = "printobserver_types::schemars")]
319pub struct ActionRequest {
320    /// What was asked for.
321    pub action: PrintAction,
322    /// Who asked.
323    pub actor: Actor,
324    /// When they asked.
325    pub requested_at: Timestamp,
326}
327
328/// One request, the decision taken on it, and what became of it.
329///
330/// `executed_at` and `outcome` are both optional and are absent together: a
331/// record is written when the decision is taken, which is before — and, for a
332/// rejected request, instead of — anything reaching the printer.
333#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
334#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
335#[schemars(crate = "printobserver_types::schemars")]
336pub struct ActionRecord {
337    /// This record's identifier, minted by the store.
338    pub id: ActionId,
339    /// The print this request was made against.
340    pub print_id: PrintId,
341    /// The request itself.
342    pub request: ActionRequest,
343    /// The decision policy took on it.
344    pub decision: PolicyDecision,
345    /// When it reached the printer, if it did.
346    #[serde(default, skip_serializing_if = "Option::is_none")]
347    pub executed_at: Option<Timestamp>,
348    /// What the printer made of it, if it reached the printer.
349    #[serde(default, skip_serializing_if = "Option::is_none")]
350    pub outcome: Option<ExecutionOutcome>,
351}