Skip to main content

printobserver_printer_api/
snapshot.rs

1//! Normalized observations of a printer and of the job it is running.
2//!
3//! These are what [`PrinterPort::snapshot`](crate::PrinterPort::snapshot) and
4//! [`PrinterPort::job`](crate::PrinterPort::job) answer, declared beside the
5//! port whose methods carry them. Every numeric field a printer can report
6//! implausibly is typed [`Reported<f64>`] and read against the plausibility
7//! range [`ranges`](crate::ranges) declares for it: a flag that disagrees with
8//! the value under that range is refused on parse, so a snapshot never carries
9//! a reading whose flag was written by something other than its range.
10
11use printobserver_types::contract::Sample;
12use printobserver_types::reported::deserialize_ranged;
13use printobserver_types::schemars::JsonSchema;
14use printobserver_types::serde::{Deserialize, Deserializer, Serialize};
15use printobserver_types::{Reported, Timestamp};
16
17use crate::file::ToolFilament;
18use crate::state::PrinterState;
19
20use crate::ranges::{
21    COMPLETION_RANGE, FAN_PERCENT_RANGE, FEEDRATE_FACTOR_RANGE, FLOWRATE_FACTOR_RANGE,
22    HEATER_ACTUAL_C_RANGE, HEATER_OFFSET_C_RANGE, HEATER_TARGET_C_RANGE,
23};
24
25/// One heater, as a source reported it.
26///
27/// Every field is optional, because a source that reports no heater at all
28/// reports none of these; each is a plausibility-ranged reported value in
29/// degrees Celsius.
30#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema, Default)]
31#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
32#[schemars(crate = "printobserver_types::schemars")]
33pub struct HeaterSnapshot {
34    /// The temperature the heater is at.
35    #[serde(
36        default,
37        skip_serializing_if = "Option::is_none",
38        deserialize_with = "de_heater_actual_c"
39    )]
40    pub actual_c: Option<Reported<f64>>,
41    /// The temperature the heater is driving towards.
42    #[serde(
43        default,
44        skip_serializing_if = "Option::is_none",
45        deserialize_with = "de_heater_target_c"
46    )]
47    pub target_c: Option<Reported<f64>>,
48    /// The offset applied to this heater's target.
49    #[serde(
50        default,
51        skip_serializing_if = "Option::is_none",
52        deserialize_with = "de_heater_offset_c"
53    )]
54    pub offset_c: Option<Reported<f64>>,
55}
56
57/// A printer, as a source reported it at one instant.
58#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
59#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
60#[schemars(crate = "printobserver_types::schemars")]
61pub struct PrinterSnapshot {
62    /// The state the printer is in.
63    pub connection: PrinterState,
64    /// The tool heaters, indexed by tool number.
65    pub tools: Vec<HeaterSnapshot>,
66    /// The bed heater, when the printer reports one.
67    #[serde(default, skip_serializing_if = "Option::is_none")]
68    pub bed: Option<HeaterSnapshot>,
69    /// The chamber heater, when the printer reports one.
70    #[serde(default, skip_serializing_if = "Option::is_none")]
71    pub chamber: Option<HeaterSnapshot>,
72    /// The feedrate multiplier, where one means one hundred percent.
73    #[serde(
74        default,
75        skip_serializing_if = "Option::is_none",
76        deserialize_with = "de_feedrate_factor"
77    )]
78    pub feedrate_factor: Option<Reported<f64>>,
79    /// The flowrate multiplier, where one means one hundred percent.
80    #[serde(
81        default,
82        skip_serializing_if = "Option::is_none",
83        deserialize_with = "de_flowrate_factor"
84    )]
85    pub flowrate_factor: Option<Reported<f64>>,
86    /// The part-cooling fan, in percent.
87    #[serde(
88        default,
89        skip_serializing_if = "Option::is_none",
90        deserialize_with = "de_fan_percent"
91    )]
92    pub fan_percent: Option<Reported<f64>>,
93    /// The instant this observation was taken.
94    pub observed_at: Timestamp,
95}
96
97/// The job a printer reports it is running.
98///
99/// Every field but `state` is optional: an absent one means the source did not
100/// report it.
101#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
102#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
103#[schemars(crate = "printobserver_types::schemars")]
104pub struct JobSnapshot {
105    /// The name of the file being printed, as the source reported it.
106    #[serde(default, skip_serializing_if = "Option::is_none")]
107    pub file_name: Option<String>,
108    /// Where the file lives, in the source's own vocabulary.
109    #[serde(default, skip_serializing_if = "Option::is_none")]
110    pub file_origin: Option<String>,
111    /// The file's size in bytes.
112    #[serde(default, skip_serializing_if = "Option::is_none")]
113    pub size_bytes: Option<i64>,
114    /// The whole print's estimated duration, in whole seconds.
115    #[serde(default, skip_serializing_if = "Option::is_none")]
116    pub estimated_print_time_s: Option<i64>,
117    /// How far through the print is, as a fraction from zero to one.
118    ///
119    /// Normalized to a fraction here regardless of how the source expresses it.
120    #[serde(
121        default,
122        skip_serializing_if = "Option::is_none",
123        deserialize_with = "de_completion"
124    )]
125    pub completion: Option<Reported<f64>>,
126    /// How long the print has been running, in whole seconds.
127    #[serde(default, skip_serializing_if = "Option::is_none")]
128    pub print_time_s: Option<i64>,
129    /// How long the print has left, in whole seconds.
130    #[serde(default, skip_serializing_if = "Option::is_none")]
131    pub print_time_left_s: Option<i64>,
132    /// The state the source reports the job to be in.
133    pub state: PrinterState,
134    /// The error the source reports, when it reports one.
135    #[serde(default, skip_serializing_if = "Option::is_none")]
136    pub error: Option<String>,
137    /// How far into the file the printer has read, in bytes, when the source
138    /// reported it.
139    #[serde(default, skip_serializing_if = "Option::is_none")]
140    pub file_position_bytes: Option<i64>,
141    /// The filament the source's analysis of the file says each tool uses;
142    /// empty when the source reported none.
143    #[serde(default, skip_serializing_if = "Vec::is_empty")]
144    pub filament: Vec<ToolFilament>,
145}
146
147/// Read `PrinterSnapshot::feedrate_factor` against its declared range.
148fn de_feedrate_factor<'de, D: Deserializer<'de>>(
149    deserializer: D,
150) -> Result<Option<Reported<f64>>, D::Error> {
151    deserialize_ranged(deserializer, "feedrate_factor", FEEDRATE_FACTOR_RANGE)
152}
153
154/// Read `PrinterSnapshot::flowrate_factor` against its declared range.
155fn de_flowrate_factor<'de, D: Deserializer<'de>>(
156    deserializer: D,
157) -> Result<Option<Reported<f64>>, D::Error> {
158    deserialize_ranged(deserializer, "flowrate_factor", FLOWRATE_FACTOR_RANGE)
159}
160
161/// Read `PrinterSnapshot::fan_percent` against its declared range.
162fn de_fan_percent<'de, D: Deserializer<'de>>(
163    deserializer: D,
164) -> Result<Option<Reported<f64>>, D::Error> {
165    deserialize_ranged(deserializer, "fan_percent", FAN_PERCENT_RANGE)
166}
167
168/// Read `JobSnapshot::completion` against its declared range.
169fn de_completion<'de, D: Deserializer<'de>>(
170    deserializer: D,
171) -> Result<Option<Reported<f64>>, D::Error> {
172    deserialize_ranged(deserializer, "completion", COMPLETION_RANGE)
173}
174
175/// Read `HeaterSnapshot::actual_c` against its declared range.
176fn de_heater_actual_c<'de, D: Deserializer<'de>>(
177    deserializer: D,
178) -> Result<Option<Reported<f64>>, D::Error> {
179    deserialize_ranged(deserializer, "actual_c", HEATER_ACTUAL_C_RANGE)
180}
181
182/// Read `HeaterSnapshot::target_c` against its declared range.
183fn de_heater_target_c<'de, D: Deserializer<'de>>(
184    deserializer: D,
185) -> Result<Option<Reported<f64>>, D::Error> {
186    deserialize_ranged(deserializer, "target_c", HEATER_TARGET_C_RANGE)
187}
188
189/// Read `HeaterSnapshot::offset_c` against its declared range.
190fn de_heater_offset_c<'de, D: Deserializer<'de>>(
191    deserializer: D,
192) -> Result<Option<Reported<f64>>, D::Error> {
193    deserialize_ranged(deserializer, "offset_c", HEATER_OFFSET_C_RANGE)
194}
195
196impl Sample for HeaterSnapshot {
197    fn sample_full() -> Self {
198        Self {
199            actual_c: Some(Reported::new(214.5, HEATER_ACTUAL_C_RANGE)),
200            target_c: Some(Reported::new(215.0, HEATER_TARGET_C_RANGE)),
201            offset_c: Some(Reported::new(0.0, HEATER_OFFSET_C_RANGE)),
202        }
203    }
204
205    fn sample_minimal() -> Self {
206        Self {
207            actual_c: None,
208            target_c: None,
209            offset_c: None,
210        }
211    }
212}
213
214impl Sample for PrinterSnapshot {
215    fn sample_full() -> Self {
216        Self {
217            connection: PrinterState::Printing,
218            tools: vec![HeaterSnapshot::sample_full()],
219            bed: Some(HeaterSnapshot::sample_full()),
220            chamber: Some(HeaterSnapshot::sample_full()),
221            feedrate_factor: Some(Reported::new(1.0, FEEDRATE_FACTOR_RANGE)),
222            flowrate_factor: Some(Reported::new(1.0, FLOWRATE_FACTOR_RANGE)),
223            fan_percent: Some(Reported::new(100.0, FAN_PERCENT_RANGE)),
224            observed_at: Timestamp::sample_full(),
225        }
226    }
227
228    fn sample_minimal() -> Self {
229        Self {
230            connection: PrinterState::Printing,
231            tools: vec![],
232            bed: None,
233            chamber: None,
234            feedrate_factor: None,
235            flowrate_factor: None,
236            fan_percent: None,
237            observed_at: Timestamp::sample_full(),
238        }
239    }
240}
241
242impl Sample for JobSnapshot {
243    fn sample_full() -> Self {
244        Self {
245            file_name: Some("benchy.gcode".to_owned()),
246            file_origin: Some("local".to_owned()),
247            size_bytes: Some(4_194_304),
248            estimated_print_time_s: Some(7200),
249            completion: Some(Reported::new(0.42, COMPLETION_RANGE)),
250            print_time_s: Some(3024),
251            print_time_left_s: Some(4176),
252            state: PrinterState::Printing,
253            error: Some("none".to_owned()),
254            file_position_bytes: Some(1_761_607),
255            filament: vec![ToolFilament::sample_full()],
256        }
257    }
258
259    fn sample_minimal() -> Self {
260        Self {
261            file_name: None,
262            file_origin: None,
263            size_bytes: None,
264            estimated_print_time_s: None,
265            completion: None,
266            print_time_s: None,
267            print_time_left_s: None,
268            state: PrinterState::Printing,
269            error: None,
270            file_position_bytes: None,
271            filament: Vec::new(),
272        }
273    }
274}