printobserver_core/config.rs
1//! What a running supervisor is configured with.
2
3use std::time::Duration;
4
5use crate::records::SafetyEnvelope;
6use printobserver_types::PrintId;
7use printobserver_types::serde::{Deserialize, Serialize};
8use printobserver_vision_api::WebAddress;
9
10/// The placeholder [`CoreConfig::context_command`] substitutes the print for.
11pub const PRINT_ID_PLACEHOLDER: &str = "{print_id}";
12
13/// How often the expiry driver looks for a bounded intervention that is due.
14pub const DEFAULT_EXPIRY_POLL: Duration = Duration::from_millis(250);
15
16/// How many of a print's own events a supervision turn is given.
17pub const DEFAULT_RECENT_EVENTS: u32 = 20;
18
19/// How long after the agent's last adjustment to a print the detector paused
20/// that print is resumed, when its turn has not ended first.
21///
22/// Long enough for an agent to ask for the two or three changes it decided on
23/// one after the other, so that they reach the print together as it moves
24/// again; short enough that a paused hot end is not left sitting.
25pub const DEFAULT_DETECTOR_RESUME_GRACE: Duration = Duration::from_secs(20);
26
27/// How often every active print is observed when no detector has said
28/// anything about it.
29///
30/// A frozen progress, a heater that went cold mid-print, a fan cut at a bridge
31/// and an under-extruding wall all stay below what a failure detector alerts
32/// on, so a print whose only trigger was the detector would never be looked at
33/// for any of them. Two minutes is the rate the user chose: often enough to
34/// catch a stall before it has printed far into the air, and an eight-hour
35/// print is 240 turns.
36pub const DEFAULT_OBSERVATION_INTERVAL: Duration = Duration::from_secs(120);
37
38/// The longest one look may wait for something to happen, in seconds.
39///
40/// Below the two minutes a supervising agent's own shell gives one command, so
41/// that a look is never cut off by the tool that asked for it. A longer wait is
42/// refused rather than shortened.
43pub const MAX_LOOK_WAIT_S: u32 = 90;
44
45/// What a start does when the filament check finds a spool short, unverified,
46/// or a file with no analysis.
47#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
48#[serde(crate = "printobserver_types::serde", rename_all = "snake_case")]
49pub enum PreflightMode {
50 /// Refuse the start, unless the actor overrides the check.
51 #[default]
52 Refuse,
53 /// Let the start go ahead, and record that the check warned.
54 Warn,
55 /// Make no check at all.
56 Off,
57}
58
59/// How spools are checked before a print, and the margin asked for.
60#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
61#[serde(crate = "printobserver_types::serde", default, deny_unknown_fields)]
62pub struct SpoolPolicy {
63 /// What a start does when the check finds a problem.
64 pub preflight: PreflightMode,
65 /// The margin asked for beyond a print's requirement, as a percentage of
66 /// it.
67 pub buffer_percent: f64,
68 /// The least margin asked for, in grams, however small the print.
69 pub buffer_min_g: f64,
70 /// How long a start waits for `OctoPrint`'s analysis of a file that has
71 /// none yet, in whole seconds; zero waits not at all.
72 pub analysis_wait_s: u64,
73}
74
75impl Default for SpoolPolicy {
76 fn default() -> Self {
77 Self {
78 preflight: PreflightMode::Refuse,
79 buffer_percent: 10.0,
80 buffer_min_g: 5.0,
81 analysis_wait_s: 300,
82 }
83 }
84}
85
86/// The server configuration supervision reads.
87#[derive(Debug, Clone, PartialEq)]
88pub struct CoreConfig {
89 /// The operator's safety envelope: what any actor may ask for at all.
90 pub envelope: SafetyEnvelope,
91 /// The command a supervision turn runs to read the print's context.
92 ///
93 /// [`PRINT_ID_PLACEHOLDER`] in it is replaced with the print's own
94 /// identifier, so that a turn reads the context of the print it is about.
95 pub context_command: String,
96 /// How many of the print's own events a turn is given, newest first.
97 pub recent_events: u32,
98 /// How often the expiry driver looks for an intervention that is due.
99 pub expiry_poll: Duration,
100 /// Where a fresh frame of the print is fetched from, when a camera is
101 /// configured.
102 pub camera_snapshot_url: Option<WebAddress>,
103 /// How often, and how far apart, a fetch the camera answered with a
104 /// server error is made again.
105 pub camera_retry: crate::look::CameraRetry,
106 /// How long after the agent's last adjustment a print the detector paused
107 /// is resumed.
108 pub detector_resume_grace: Duration,
109 /// How often every active print is given a periodic observation.
110 pub observation_interval: Duration,
111 /// How spools are checked before a print, and the margin asked for.
112 pub spools: SpoolPolicy,
113 /// How often the printer watch reads the machine.
114 pub printer_poll: Duration,
115 /// How long after an `interrupted` or `failed` ending the printer acting
116 /// on its own is recorded.
117 pub unattended_window: Duration,
118 /// The printer's own facts a recovery is checked against; absent, every
119 /// recovery action is refused as not configured.
120 pub recovery: Option<crate::records::RecoveryConfig>,
121 /// Who may ask for which recovery action.
122 pub recovery_grants: crate::records::RecoveryGrants,
123}
124
125impl CoreConfig {
126 /// The configuration for one envelope, with this crate's own defaults.
127 #[must_use]
128 pub fn new(envelope: SafetyEnvelope, context_command: String) -> Self {
129 Self {
130 envelope,
131 context_command,
132 recent_events: DEFAULT_RECENT_EVENTS,
133 expiry_poll: DEFAULT_EXPIRY_POLL,
134 camera_snapshot_url: None,
135 camera_retry: crate::look::CameraRetry::default(),
136 detector_resume_grace: DEFAULT_DETECTOR_RESUME_GRACE,
137 observation_interval: DEFAULT_OBSERVATION_INTERVAL,
138 spools: SpoolPolicy::default(),
139 printer_poll: crate::watch::DEFAULT_PRINTER_POLL,
140 unattended_window: crate::watch::DEFAULT_UNATTENDED_WINDOW,
141 recovery: None,
142 recovery_grants: crate::records::RecoveryGrants::default(),
143 }
144 }
145
146 /// The context command for one print.
147 #[must_use]
148 pub fn context_command_for(&self, print_id: PrintId) -> String {
149 self.context_command
150 .replace(PRINT_ID_PLACEHOLDER, &print_id.to_string())
151 }
152}