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 /// The print should stop. Recorded for whoever may cancel it: the
88 /// acknowledgement itself asks nothing of the machine.
89 Stop,
90}
91
92/// One action of the closed vocabulary, named without its payload.
93///
94/// This is what a safety envelope grants and what a policy rejection names; the
95/// payload lives on [`PrintAction`] itself.
96#[derive(
97 Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, JsonSchema,
98)]
99#[serde(
100 crate = "printobserver_types::serde",
101 rename_all = "snake_case",
102 deny_unknown_fields
103)]
104#[schemars(crate = "printobserver_types::schemars")]
105pub enum ActionKind {
106 /// Pause the print.
107 Pause,
108 /// Resume the print.
109 Resume,
110 /// Cancel the print.
111 Cancel,
112 /// Start a print of a named file.
113 StartPrint,
114 /// Set the feedrate factor.
115 SetFeedrateFactor,
116 /// Set the flowrate factor.
117 SetFlowrateFactor,
118 /// Set a tool's target temperature.
119 SetToolTargetC,
120 /// Set the bed's target temperature.
121 SetBedTargetC,
122 /// Set the fan percentage.
123 SetFanPercent,
124 /// Acknowledge a failure event.
125 AcknowledgeFailure,
126 /// Register a spool.
127 RegisterSpool,
128 /// Load a spool onto a tool.
129 LoadSpool,
130 /// Unload a spool from its tool.
131 UnloadSpool,
132 /// Correct a spool's remaining figure.
133 AdjustSpool,
134 /// Retire a spool.
135 RetireSpool,
136}
137
138/// The whole vocabulary an actor may ask for, and there is no other.
139#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
140#[serde(
141 crate = "printobserver_types::serde",
142 tag = "action",
143 rename_all = "snake_case",
144 deny_unknown_fields
145)]
146#[schemars(crate = "printobserver_types::schemars")]
147pub enum PrintAction {
148 /// Pause the print.
149 Pause {
150 /// Why the actor is asking for this.
151 reason: String,
152 /// Who is asking.
153 actor: Actor,
154 },
155 /// Resume the print.
156 Resume {
157 /// Why the actor is asking for this.
158 reason: String,
159 /// Who is asking.
160 actor: Actor,
161 },
162 /// Cancel the print.
163 Cancel {
164 /// Why the actor is asking for this.
165 reason: String,
166 /// Who is asking.
167 actor: Actor,
168 },
169 /// Start a print of a named file, with a manifest.
170 StartPrint {
171 /// The file to print, validated as a name a file API can be asked for.
172 file_name: FileName,
173 /// The manifest this print is bounded by.
174 manifest: JobManifest,
175 /// Start despite an insufficient or unverified spool, or a file
176 /// `OctoPrint` has not analysed; the start's own reason is the
177 /// override's reason.
178 // llmlint: ignore[invalid_states_unrepresentable] suppressions.toml has the reason.
179 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
180 override_filament_check: bool,
181 /// Why the actor is asking for this.
182 reason: String,
183 /// Who is asking.
184 actor: Actor,
185 },
186 /// Set the feedrate factor, where one means one hundred percent.
187 SetFeedrateFactor {
188 /// The multiplier asked for.
189 factor: f64,
190 /// How long the change stands for, in whole seconds.
191 #[serde(default, skip_serializing_if = "Option::is_none")]
192 duration_s: Option<i64>,
193 /// Why the actor is asking for this.
194 reason: String,
195 /// Who is asking.
196 actor: Actor,
197 },
198 /// Set the flowrate factor, where one means one hundred percent.
199 SetFlowrateFactor {
200 /// The multiplier asked for.
201 factor: f64,
202 /// How long the change stands for, in whole seconds.
203 #[serde(default, skip_serializing_if = "Option::is_none")]
204 duration_s: Option<i64>,
205 /// Why the actor is asking for this.
206 reason: String,
207 /// Who is asking.
208 actor: Actor,
209 },
210 /// Set one tool's target temperature.
211 SetToolTargetC {
212 /// The tool, in the printer's own numbering.
213 tool: i64,
214 /// The target temperature asked for, in degrees Celsius.
215 target_c: f64,
216 /// How long the change stands for, in whole seconds.
217 #[serde(default, skip_serializing_if = "Option::is_none")]
218 duration_s: Option<i64>,
219 /// Why the actor is asking for this.
220 reason: String,
221 /// Who is asking.
222 actor: Actor,
223 },
224 /// Set the bed's target temperature.
225 SetBedTargetC {
226 /// The target temperature asked for, in degrees Celsius.
227 target_c: f64,
228 /// How long the change stands for, in whole seconds.
229 #[serde(default, skip_serializing_if = "Option::is_none")]
230 duration_s: Option<i64>,
231 /// Why the actor is asking for this.
232 reason: String,
233 /// Who is asking.
234 actor: Actor,
235 },
236 /// Set the part-cooling fan percentage.
237 SetFanPercent {
238 /// The percentage asked for.
239 percent: f64,
240 /// How long the change stands for, in whole seconds.
241 #[serde(default, skip_serializing_if = "Option::is_none")]
242 duration_s: Option<i64>,
243 /// Why the actor is asking for this.
244 reason: String,
245 /// Who is asking.
246 actor: Actor,
247 },
248 /// Acknowledge a failure event, with a disposition.
249 AcknowledgeFailure {
250 /// The event being acknowledged.
251 event_id: EventId,
252 /// What to do next.
253 disposition: AcknowledgementDisposition,
254 /// Why the actor is asking for this.
255 reason: String,
256 /// Who is asking.
257 actor: Actor,
258 },
259}
260
261impl PrintAction {
262 /// Which action this is, without its payload.
263 #[must_use]
264 pub const fn kind(&self) -> ActionKind {
265 match self {
266 Self::Pause { .. } => ActionKind::Pause,
267 Self::Resume { .. } => ActionKind::Resume,
268 Self::Cancel { .. } => ActionKind::Cancel,
269 Self::StartPrint { .. } => ActionKind::StartPrint,
270 Self::SetFeedrateFactor { .. } => ActionKind::SetFeedrateFactor,
271 Self::SetFlowrateFactor { .. } => ActionKind::SetFlowrateFactor,
272 Self::SetToolTargetC { .. } => ActionKind::SetToolTargetC,
273 Self::SetBedTargetC { .. } => ActionKind::SetBedTargetC,
274 Self::SetFanPercent { .. } => ActionKind::SetFanPercent,
275 Self::AcknowledgeFailure { .. } => ActionKind::AcknowledgeFailure,
276 }
277 }
278
279 /// Why the actor asked for this.
280 #[must_use]
281 pub fn reason(&self) -> &str {
282 match self {
283 Self::Pause { reason, .. }
284 | Self::Resume { reason, .. }
285 | Self::Cancel { reason, .. }
286 | Self::StartPrint { reason, .. }
287 | Self::SetFeedrateFactor { reason, .. }
288 | Self::SetFlowrateFactor { reason, .. }
289 | Self::SetToolTargetC { reason, .. }
290 | Self::SetBedTargetC { reason, .. }
291 | Self::SetFanPercent { reason, .. }
292 | Self::AcknowledgeFailure { reason, .. } => reason,
293 }
294 }
295
296 /// Who asked for this.
297 #[must_use]
298 pub const fn actor(&self) -> &Actor {
299 match self {
300 Self::Pause { actor, .. }
301 | Self::Resume { actor, .. }
302 | Self::Cancel { actor, .. }
303 | Self::StartPrint { actor, .. }
304 | Self::SetFeedrateFactor { actor, .. }
305 | Self::SetFlowrateFactor { actor, .. }
306 | Self::SetToolTargetC { actor, .. }
307 | Self::SetBedTargetC { actor, .. }
308 | Self::SetFanPercent { actor, .. }
309 | Self::AcknowledgeFailure { actor, .. } => actor,
310 }
311 }
312}
313
314/// What happened when an accepted action reached the printer.
315#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
316#[serde(
317 crate = "printobserver_types::serde",
318 rename_all = "snake_case",
319 deny_unknown_fields
320)]
321#[schemars(crate = "printobserver_types::schemars")]
322pub enum ExecutionOutcome {
323 /// The printer took it.
324 Succeeded,
325 /// The printer did not, for this reason.
326 Failed {
327 /// Why it failed.
328 reason: String,
329 },
330}
331
332/// One request an actor made, at the instant it made it.
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 ActionRequest {
337 /// What was asked for.
338 pub action: PrintAction,
339 /// Who asked.
340 pub actor: Actor,
341 /// When they asked.
342 pub requested_at: Timestamp,
343}
344
345/// One request, the decision taken on it, and what became of it.
346///
347/// `executed_at` and `outcome` are both optional and are absent together: a
348/// record is written when the decision is taken, which is before — and, for a
349/// rejected request, instead of — anything reaching the printer.
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 ActionRecord {
354 /// This record's identifier, minted by the store.
355 pub id: ActionId,
356 /// The print this request was made against.
357 pub print_id: PrintId,
358 /// The request itself.
359 pub request: ActionRequest,
360 /// The decision policy took on it.
361 pub decision: PolicyDecision,
362 /// When it reached the printer, if it did.
363 #[serde(default, skip_serializing_if = "Option::is_none")]
364 pub executed_at: Option<Timestamp>,
365 /// What the printer made of it, if it reached the printer.
366 #[serde(default, skip_serializing_if = "Option::is_none")]
367 pub outcome: Option<ExecutionOutcome>,
368}