printobserver_printer_api/
snapshot.rs1use 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#[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 #[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 #[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 #[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#[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 pub connection: PrinterState,
63 pub tools: Vec<HeaterSnapshot>,
65 #[serde(default, skip_serializing_if = "Option::is_none")]
67 pub bed: Option<HeaterSnapshot>,
68 #[serde(default, skip_serializing_if = "Option::is_none")]
70 pub chamber: Option<HeaterSnapshot>,
71 #[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 #[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 #[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 pub observed_at: Timestamp,
94}
95
96#[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 #[serde(default, skip_serializing_if = "Option::is_none")]
106 pub file_name: Option<String>,
107 #[serde(default, skip_serializing_if = "Option::is_none")]
109 pub file_origin: Option<String>,
110 #[serde(default, skip_serializing_if = "Option::is_none")]
112 pub size_bytes: Option<i64>,
113 #[serde(default, skip_serializing_if = "Option::is_none")]
115 pub estimated_print_time_s: Option<i64>,
116 #[serde(
120 default,
121 skip_serializing_if = "Option::is_none",
122 deserialize_with = "de_completion"
123 )]
124 pub completion: Option<Reported<f64>>,
125 #[serde(default, skip_serializing_if = "Option::is_none")]
127 pub print_time_s: Option<i64>,
128 #[serde(default, skip_serializing_if = "Option::is_none")]
130 pub print_time_left_s: Option<i64>,
131 pub state: PrinterState,
133 #[serde(default, skip_serializing_if = "Option::is_none")]
135 pub error: Option<String>,
136}
137
138fn 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
145fn 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
152fn 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
159fn 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
166fn 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
173fn 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
180fn 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}