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