1use printobserver_panels::{ViewCorrection, ViewOutcome};
16use printobserver_supervisor_api::{ImageFallback, ImageKind};
17use printobserver_types::EventPayload;
18use printobserver_types::contract::Sample;
19use printobserver_types::schemars::JsonSchema;
20use printobserver_types::serde::{Deserialize, Serialize};
21
22#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
24#[serde(crate = "printobserver_types::serde", rename_all = "snake_case")]
25#[schemars(crate = "printobserver_types::schemars")]
26pub enum AlignmentJudgedVerdict {
27 Passed,
30 Inconclusive,
32 Failed,
35}
36
37#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
39#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
40#[schemars(crate = "printobserver_types::schemars")]
41pub struct FirstLayerAlignmentJudgedPayload {
42 pub verdict: AlignmentJudgedVerdict,
44 pub offset_px: Option<f64>,
47 pub correction_px: Option<[f64; 2]>,
50 pub match_strength: Option<f64>,
53 pub reason: String,
56 pub frame_sha256: String,
58 pub view_correction: Option<ViewCorrection>,
61}
62
63impl EventPayload for FirstLayerAlignmentJudgedPayload {
64 const KIND: &'static str = "first_layer_alignment_judged";
65}
66
67#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
70#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
71#[schemars(crate = "printobserver_types::schemars")]
72pub struct CameraViewJudgedPayload {
73 pub frame_sha256: String,
75 pub outcome: ViewOutcome,
77 pub view_correction: Option<ViewCorrection>,
79 pub reason: Option<String>,
83}
84
85impl EventPayload for CameraViewJudgedPayload {
86 const KIND: &'static str = "camera_view_judged";
87}
88
89#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
93#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
94#[schemars(crate = "printobserver_types::schemars")]
95pub struct CameraViewRefitPayload {
96 pub frame_sha256: String,
98 pub view_correction: ViewCorrection,
100}
101
102impl EventPayload for CameraViewRefitPayload {
103 const KIND: &'static str = "camera_view_refit";
104}
105
106#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
110#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
111#[schemars(crate = "printobserver_types::schemars")]
112pub struct CheckImageChangedPayload {
113 pub kind: ImageKind,
115 pub fallback: Option<ImageFallback>,
117 pub frame_sha256: String,
119}
120
121impl EventPayload for CheckImageChangedPayload {
122 const KIND: &'static str = "check_image_changed";
123}
124
125const SAMPLE_SHA256: &str = "9f2c4d1e8b7a6f5e4d3c2b1a0f9e8d7c6b5a4f3e2d1c0b9a8f7e6d5c4b3a2f1e";
127
128fn sample_correction() -> ViewCorrection {
130 ViewCorrection::translation(
131 [14.0, 4.0],
132 vec![
133 printobserver_panels::ReferenceLandmark {
134 name: "frame-screws-right-front".to_owned(),
135 offset_px: [59.1, 0.7],
136 score: 0.83,
137 },
138 printobserver_panels::ReferenceLandmark {
139 name: "frame-label-right".to_owned(),
140 offset_px: [79.6, 0.2],
141 score: 0.84,
142 },
143 ],
144 )
145}
146
147impl Sample for FirstLayerAlignmentJudgedPayload {
148 fn sample_full() -> Self {
149 Self {
150 verdict: AlignmentJudgedVerdict::Passed,
151 offset_px: Some(14.6),
152 correction_px: Some([14.0, 4.0]),
153 match_strength: Some(57.6),
154 reason: "clearly_found".to_owned(),
155 frame_sha256: SAMPLE_SHA256.to_owned(),
156 view_correction: Some(sample_correction()),
157 }
158 }
159
160 fn sample_minimal() -> Self {
161 Self {
162 verdict: AlignmentJudgedVerdict::Inconclusive,
163 offset_px: None,
164 correction_px: None,
165 match_strength: None,
166 reason: "not_clearly_found".to_owned(),
167 frame_sha256: SAMPLE_SHA256.to_owned(),
168 view_correction: None,
169 }
170 }
171
172 fn sample_alternates() -> Vec<Self> {
173 vec![Self {
174 verdict: AlignmentJudgedVerdict::Failed,
175 reason: "the camera is not the stream the calibration was fitted on".to_owned(),
176 ..Self::sample_minimal()
177 }]
178 }
179}
180
181impl Sample for CameraViewJudgedPayload {
182 fn sample_full() -> Self {
183 Self {
184 frame_sha256: SAMPLE_SHA256.to_owned(),
185 outcome: ViewOutcome::ViewRefitRejected,
186 view_correction: Some(sample_correction()),
187 reason: Some(
188 "the camera has moved again since this print's first-layer correction was taken: frame-label-right is 10.2 px from where that correction's frame had it"
189 .to_owned(),
190 ),
191 }
192 }
193
194 fn sample_minimal() -> Self {
195 Self {
196 frame_sha256: SAMPLE_SHA256.to_owned(),
197 outcome: ViewOutcome::Unshifted,
198 view_correction: None,
199 reason: None,
200 }
201 }
202
203 fn sample_alternates() -> Vec<Self> {
204 [
205 ViewOutcome::Refit,
206 ViewOutcome::LandmarksNotFound,
207 ViewOutcome::FrameMismatch,
208 ]
209 .into_iter()
210 .map(|outcome| Self {
211 outcome,
212 ..Self::sample_minimal()
213 })
214 .collect()
215 }
216}
217
218impl Sample for CameraViewRefitPayload {
219 fn sample_full() -> Self {
220 Self {
221 frame_sha256: SAMPLE_SHA256.to_owned(),
222 view_correction: sample_correction(),
223 }
224 }
225}
226
227impl Sample for CheckImageChangedPayload {
228 fn sample_full() -> Self {
229 Self {
230 kind: ImageKind::WholeFrame,
231 fallback: Some(ImageFallback::sample_full()),
232 frame_sha256: SAMPLE_SHA256.to_owned(),
233 }
234 }
235
236 fn sample_minimal() -> Self {
237 Self {
238 kind: ImageKind::ThreePanel,
239 fallback: None,
240 frame_sha256: SAMPLE_SHA256.to_owned(),
241 }
242 }
243}