printobserver-server 0.18.0

The printobserver server.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
//! Each print's first-layer alignment verdict and its camera view: whether the
//! camera calibration's projection lands on that print's printed first layer,
//! and the correction every later drawing of the print goes through.
//!
//! A projection can be wrong with the camera unmoved, and a camera that has
//! shifted a little is ordinary on a real printer, so each print judges its
//! own. Once the print's first object layer is down, every scheduled check's
//! own stored frame is judged ([`Alignments::judge`]), at most once a check
//! and with no camera fetch of its own, until one passes: the layer clearly
//! found, on a frame the toolhead does not cover
//! ([`printobserver_panels::judge_first_layer`]). A covered or unclear frame
//! is inconclusive and the next check's is judged again. A check taken while
//! the printer reports the print paused is not judged: its bed is lowered off
//! the plan's height. Only a frame of another size or stream fails, and that
//! holds for the rest of the print.
//!
//! A pass puts the print's correction in force, a translation every later
//! drawing goes through, kept with the landmarks found agreeing on that frame as
//! its reference. Its translation is the pass's found shift when the frame
//! stood within one layer of the first layer
//! ([`printobserver_panels::alignment::correction_shift`]), and none from a
//! taller frame, where the search scores the part's body. Every later frame's view is judged against that
//! reference ([`printobserver_panels::judge_found`]); one showing the camera
//! moved again sets the alignment back to pending, to take a fresh correction
//! from a later check. Nothing is kept across prints: a sheet sits a few
//! millimetres apart from one print to the next.
//!
//! Every judgement is written into the print's history, naming the stored
//! frame it was made on ([`crate::judgements`]).
//!
//! The verdict is written to `<state_dir>/alignment/<print id>.json` and the
//! correction, the last inconclusive reason and the last check's image to
//! `<state_dir>/camera-view/<print id>.json`, so they stand through a restart
//! while the print is open, and are deleted once the print closes. A verdict
//! file an earlier release left failed on its offset is read as pending and
//! judged again.
//!
//! Which check draws the toolpath on its image reads the verdict through
//! [`Alignments::print_alignment`] and draws only for a print that `Passed`.
//!
//! Beside each verdict is kept the frame the print's last check was given,
//! which the next check's image shows as the previous one
//! ([`Alignments::previous_check`]): written under
//! `<state_dir>/previous-check/<print id>.json`, standing through a restart
//! while the print is open, and swept with the verdict once it closes.
//!
//! And beside that, the frames of the print's recent checks with the height
//! the plan had reached at each ([`Alignments::checked_frames`]): written
//! under `<state_dir>/checked-frames/<print id>.json`, kept and swept the same
//! way, so that a check can measure whether the parts grew since one far
//! enough below it ([`Alignments::growth_reference`]).

use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::Duration;

use printobserver_core::PlanPosition;
use printobserver_core::store::PrintStore;
use printobserver_panels::{
    AlignmentInputs, CameraCalibration, ComposeError, FirstLayerInputs, FirstLayerVerdict,
    FrameField, InconclusiveReason, ReferenceLandmark, ViewCorrection, ViewJudgement, ViewOutcome,
    judge_first_layer, judge_found, landmarks_in_frame,
};
use printobserver_supervisor_api::{ImageFallback, ImageKind};
use printobserver_types::contract::Sample;
use printobserver_types::schemars::JsonSchema;
use printobserver_types::serde::{Deserialize, Serialize};
use printobserver_types::{EventBody, EventPayload, PrintId, Timestamp};

/// Why a print's alignment failed.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(
    crate = "printobserver_types::serde",
    tag = "failure",
    rename_all = "snake_case"
)]
#[schemars(crate = "printobserver_types::schemars")]
pub enum AlignmentFailure {
    /// The printed first layer lies further from its projection than the
    /// calibration allows. No longer written: a frame that does not clearly
    /// find the layer is inconclusive, and a verdict file an earlier release
    /// left holding this is read as pending.
    Offset {
        /// How far, in calibration pixels.
        offset_px: f64,
    },
    /// The camera's frames are not the calibration's resolution.
    Resolution {
        /// The calibration's, `[width, height]`.
        expected: [u32; 2],
        /// The frame's.
        got: [u32; 2],
    },
    /// The camera configured is not the stream the calibration was fitted on.
    Stream,
}

/// One print's first-layer alignment verdict, as it is kept under the state
/// directory — which the real-print suite's record reads.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(
    crate = "printobserver_types::serde",
    tag = "verdict",
    rename_all = "snake_case"
)]
#[schemars(crate = "printobserver_types::schemars")]
pub enum PrintAlignment {
    /// Nothing can be checked: no calibration is configured, or the print's
    /// G-code lays no object.
    Unavailable,
    /// The first object layer is not down yet, or no frame of it has been
    /// judged.
    Pending,
    /// The projected first layer lands on the printed one.
    Passed {
        /// How far it lies from it, in calibration pixels.
        offset_px: f64,
        /// The first-layer correction applied to every later drawing of the
        /// print, `[dx, dy]` calibration pixels: the shift the layer was found
        /// at. `[0, 0]` in a verdict an earlier release wrote.
        #[serde(default)]
        correction_px: [f64; 2],
    },
    /// It does not, or the frames cannot be judged against the calibration.
    Failed {
        /// Why.
        reason: AlignmentFailure,
    },
}

impl Sample for PrintAlignment {
    fn sample_full() -> Self {
        Self::Passed {
            offset_px: 14.6,
            correction_px: [14.0, 4.0],
        }
    }

    fn sample_minimal() -> Self {
        Self::Pending
    }

    fn sample_alternates() -> Vec<Self> {
        vec![
            Self::Unavailable,
            Self::Failed {
                reason: AlignmentFailure::Offset { offset_px: 48.5 },
            },
            Self::Failed {
                reason: AlignmentFailure::Resolution {
                    expected: [1920, 1080],
                    got: [1280, 720],
                },
            },
            Self::Failed {
                reason: AlignmentFailure::Stream,
            },
        ]
    }
}

/// The frame one print's last check was given, and when it was captured.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(crate = "printobserver_types::serde")]
pub struct PreviousCheck {
    /// The stored frame's path.
    pub frame: PathBuf,
    /// When it was captured.
    pub captured_at: printobserver_types::Timestamp,
}

/// One check's frame and the height the plan had reached as it was captured.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(crate = "printobserver_types::serde")]
pub struct CheckedFrame {
    /// The stored frame's path.
    pub frame: PathBuf,
    /// When it was captured.
    pub captured_at: printobserver_types::Timestamp,
    /// The print's height in its plan then, in millimetres.
    pub print_z_mm: f64,
}

/// The most checked frames one print keeps; past it the oldest are dropped.
const MAX_CHECKED_FRAMES: usize = 512;

/// The alignment verdicts of every print, what decides them, the frame each
/// print's last check was given, and its recent checks' frames and heights.
pub struct Alignments {
    /// Where each print's verdict is kept.
    directory: PathBuf,
    /// Where each print's last check's frame is named.
    previous_directory: PathBuf,
    /// Where each print's recent checks' frames and heights are kept.
    checked_directory: PathBuf,
    /// Where each print's correction, last inconclusive reason and last
    /// check's image are kept.
    view_directory: PathBuf,
    /// The camera's calibration, when one is configured.
    calibration: Option<Arc<CameraCalibration>>,
    /// The prints, to tell which are open.
    prints: Arc<dyn PrintStore>,
}

impl core::fmt::Debug for Alignments {
    fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        formatter
            .debug_struct("Alignments")
            .field("directory", &self.directory)
            .field("calibrated", &self.calibration.is_some())
            .finish_non_exhaustive()
    }
}

/// The directory verdicts are kept in, under a state directory.
#[must_use]
pub fn verdict_dir(state_dir: &Path) -> PathBuf {
    state_dir.join("alignment")
}

/// The directory each print's last check's frame is named in, under a state
/// directory.
#[must_use]
pub fn previous_check_dir(state_dir: &Path) -> PathBuf {
    state_dir.join("previous-check")
}

/// The directory each print's recent checks' frames and heights are kept in,
/// under a state directory.
#[must_use]
pub fn checked_frames_dir(state_dir: &Path) -> PathBuf {
    state_dir.join("checked-frames")
}

/// The directory each print's correction, last inconclusive reason and last
/// check's image are kept in, under a state directory.
#[must_use]
pub fn camera_view_dir(state_dir: &Path) -> PathBuf {
    state_dir.join("camera-view")
}

impl Alignments {
    /// The verdicts kept under one state directory.
    ///
    /// # Errors
    ///
    /// The filesystem's own error when the directory cannot be made.
    pub fn new(
        state_dir: &Path,
        calibration: Option<Arc<CameraCalibration>>,
        prints: Arc<dyn PrintStore>,
    ) -> std::io::Result<Self> {
        let directory = verdict_dir(state_dir);
        std::fs::create_dir_all(&directory)?;
        let previous_directory = previous_check_dir(state_dir);
        std::fs::create_dir_all(&previous_directory)?;
        let checked_directory = checked_frames_dir(state_dir);
        std::fs::create_dir_all(&checked_directory)?;
        let view_directory = camera_view_dir(state_dir);
        std::fs::create_dir_all(&view_directory)?;
        Ok(Self {
            directory,
            previous_directory,
            checked_directory,
            view_directory,
            calibration,
            prints,
        })
    }

    /// Where one print's verdict is kept.
    fn path_of(&self, print_id: PrintId) -> PathBuf {
        self.directory.join(format!("{print_id}.json"))
    }

    /// The camera calibration in force, when one is configured.
    #[must_use]
    pub fn calibration(&self) -> Option<&CameraCalibration> {
        self.calibration.as_deref()
    }

    /// Where one print's last check's frame is named.
    fn previous_path_of(&self, print_id: PrintId) -> PathBuf {
        self.previous_directory.join(format!("{print_id}.json"))
    }

    /// The frame one print's last check was given; nothing before its first
    /// check, or when the record cannot be read back.
    #[must_use]
    pub fn previous_check(&self, print_id: PrintId) -> Option<PreviousCheck> {
        std::fs::read(self.previous_path_of(print_id))
            .ok()
            .and_then(|bytes| printobserver_types::serde_json::from_slice(&bytes).ok())
    }

    /// Name the frame one print's check was given, which its next check shows
    /// as the previous. A record the filesystem refuses for a moment is
    /// replaced once the refusal passes ([`replace_file`]); one it goes on
    /// refusing is said on the log and leaves the record before it standing,
    /// so the next check shows an older frame as the previous rather than
    /// none. No test makes the refusal last: that needs the state directory
    /// made to refuse writes beneath a running server, which refuses the
    /// store's own writes first. What a check makes of a record it cannot use
    /// — an empty panel 3 — is proved by
    /// `a_lost_previous_frame_empties_panel_3_and_the_record_goes_with_the_print`.
    pub fn record_check(&self, print_id: PrintId, check: &PreviousCheck) {
        let Ok(bytes) = printobserver_types::serde_json::to_vec(check) else {
            return;
        };
        if let Err(error) = replace_file(&self.previous_path_of(print_id), &bytes) {
            eprintln!(
                "printobserver: print {print_id}: the last check's frame could not be recorded, so the next check shows an older one or none: {error}"
            );
        }
    }

    /// Where one print's recent checks' frames and heights are kept.
    fn checked_path_of(&self, print_id: PrintId) -> PathBuf {
        self.checked_directory.join(format!("{print_id}.json"))
    }

    /// One print's recent checks' frames and heights, oldest first; none
    /// before its first check with a height, or when the record cannot be
    /// read back.
    #[must_use]
    pub fn checked_frames(&self, print_id: PrintId) -> Vec<CheckedFrame> {
        std::fs::read(self.checked_path_of(print_id))
            .ok()
            .and_then(|bytes| printobserver_types::serde_json::from_slice(&bytes).ok())
            .unwrap_or_default()
    }

    /// Forget one print's checked frames, so its growth is measured only from
    /// checks made after now: the print waited at a planned change, and a frame
    /// from before the wait would count the wait as no growth. A record the
    /// filesystem will not remove is said on the log and left, so a later check
    /// measures from a frame before the wait, which says no more than that a
    /// check could not tell.
    pub fn restart_growth(&self, print_id: PrintId) {
        let path = self.checked_path_of(print_id);
        if let Err(error) = std::fs::remove_file(&path)
            && error.kind() != std::io::ErrorKind::NotFound
        {
            eprintln!(
                "printobserver: print {print_id}: its checked frames could not be forgotten after a planned change, so a later check may measure growth across the wait: {error}"
            );
        }
    }

    /// The frame a check at `print_z_mm` measures growth from: the newest
    /// checked frame of the print at least `rise_mm` below it in the plan.
    #[must_use]
    pub fn growth_reference(
        &self,
        print_id: PrintId,
        print_z_mm: f64,
        rise_mm: f64,
    ) -> Option<CheckedFrame> {
        reference_in(&self.checked_frames(print_id), print_z_mm, rise_mm).cloned()
    }

    /// Keep one check's frame and height among the print's, keeping only what
    /// a later check at `rise_mm` above some kept height can still measure
    /// from: the newest frame at least that far below this one and every
    /// frame after it. A height below the last kept one is a plan that went
    /// back down, as a print of one object after another does, and starts the
    /// record again. A record the filesystem refuses is said on the log and
    /// leaves the one before standing, as [`record_check`](Self::record_check)
    /// does.
    pub fn record_checked_frame(&self, print_id: PrintId, checked: CheckedFrame, rise_mm: f64) {
        let kept = kept_with(self.checked_frames(print_id), checked, rise_mm);
        let Ok(bytes) = printobserver_types::serde_json::to_vec(&kept) else {
            return;
        };
        if let Err(error) = replace_file(&self.checked_path_of(print_id), &bytes) {
            eprintln!(
                "printobserver: print {print_id}: a check's frame and height could not be recorded, so a later check measures growth from an older one or none: {error}"
            );
        }
    }

    /// One print's verdict as it stands.
    #[must_use]
    pub fn print_alignment(&self, print_id: PrintId) -> PrintAlignment {
        if self.calibration.is_none() {
            return PrintAlignment::Unavailable;
        }
        match std::fs::read(self.path_of(print_id))
            .ok()
            .and_then(|bytes| printobserver_types::serde_json::from_slice(&bytes).ok())
        {
            // A failure on the offset is what an earlier release wrote for a
            // frame that did not find the layer, which is now inconclusive.
            Some(PrintAlignment::Failed {
                reason: AlignmentFailure::Offset { .. },
            })
            | None => PrintAlignment::Pending,
            Some(verdict) => verdict,
        }
    }

    /// Where one print's camera view is kept.
    fn view_path_of(&self, print_id: PrintId) -> PathBuf {
        self.view_directory.join(format!("{print_id}.json"))
    }

    /// One print's camera view as it stands: nothing kept before its first
    /// judgement, or when the record cannot be read back.
    fn view_state(&self, print_id: PrintId) -> CameraViewState {
        std::fs::read(self.view_path_of(print_id))
            .ok()
            .and_then(|bytes| printobserver_types::serde_json::from_slice(&bytes).ok())
            .unwrap_or_default()
    }

    /// Keep one print's camera view. A record the filesystem refuses is said
    /// on the log and leaves the one before standing, as
    /// [`record_check`](Self::record_check) does.
    fn keep_view_state(&self, print_id: PrintId, state: &CameraViewState) {
        let Ok(bytes) = printobserver_types::serde_json::to_vec(state) else {
            return;
        };
        if let Err(error) = replace_file(&self.view_path_of(print_id), &bytes) {
            eprintln!(
                "printobserver: print {print_id}: its camera view could not be recorded, so a later check reads the one before: {error}"
            );
        }
    }

    /// Keep a verdict, and say it. A verdict the filesystem goes on refusing
    /// ([`replace_file`]) is said on the log and leaves the one before
    /// standing, so the next check judges again from it.
    fn decide(&self, print_id: PrintId, verdict: &PrintAlignment) {
        let Ok(bytes) = printobserver_types::serde_json::to_vec(verdict) else {
            return;
        };
        match replace_file(&self.path_of(print_id), &bytes) {
            Ok(()) => eprintln!(
                "printobserver: print {print_id}: first-layer alignment {}",
                said(verdict)
            ),
            Err(error) => eprintln!(
                "printobserver: print {print_id}: first-layer alignment {} could not be recorded: {error}",
                said(verdict)
            ),
        }
    }

    /// The correction in force for one print: the camera check's it started
    /// under, when it started under one; else the one its last pass took, or,
    /// for a pass an earlier release recorded with no reference, its
    /// correction held against the calibration's own landmarks where it
    /// placed them. Nothing unless the print's alignment passed.
    #[must_use]
    pub fn correction_in_force(&self, print_id: PrintId) -> Option<ViewCorrection> {
        let calibration = self.calibration.as_deref()?;
        if let Some(checked) = self.view_state(print_id).camera_check {
            return Some(checked.correction);
        }
        let PrintAlignment::Passed { correction_px, .. } = self.print_alignment(print_id) else {
            return None;
        };
        Some(self.view_state(print_id).correction.unwrap_or_else(|| {
            ViewCorrection::translation(
                correction_px,
                calibration
                    .landmarks
                    .iter()
                    .map(|landmark| ReferenceLandmark {
                        name: landmark.name.clone(),
                        offset_px: [0.0, 0.0],
                        score: 1.0,
                    })
                    .collect(),
            )
        }))
    }

    /// Keep the camera check a print started under, from its record, so every
    /// check of the print is drawn through its correction from now on: once,
    /// on the first check handed it. A record that is no `aligned` check
    /// carrying a correction — which a start refuses to name — puts nothing in
    /// force.
    pub fn adopt_camera_check(&self, print_id: PrintId, record: &printobserver_types::EventRecord) {
        let mut state = self.view_state(print_id);
        let Some(Ok(payload)) = record.payload_as::<crate::camera_check::CameraCheckedPayload>()
        else {
            return;
        };
        let check = payload.camera_check;
        if state
            .camera_check
            .as_ref()
            .is_some_and(|kept| kept.id == check.id)
        {
            return;
        }
        let (printobserver_core::CameraCheckVerdict::Aligned, Some(correction)) =
            (check.verdict, check.correction)
        else {
            return;
        };
        eprintln!(
            "printobserver: print {print_id}: drawn through camera check {} from its first check, corrected by [{:.1}, {:.1}] px",
            check.id,
            correction.shift_px()[0],
            correction.shift_px()[1]
        );
        state.camera_check = Some(CheckedCamera {
            id: check.id,
            correction,
        });
        self.keep_view_state(print_id, &state);
    }

    /// Whether a print is drawn through the camera check it started under.
    #[must_use]
    pub fn started_under_a_camera_check(&self, print_id: PrintId) -> bool {
        self.view_state(print_id).camera_check.is_some()
    }

    /// Why the print's alignment was last left pending, when the frame judged
    /// last was inconclusive.
    #[must_use]
    pub fn inconclusive(&self, print_id: PrintId) -> Option<InconclusiveReason> {
        self.view_state(print_id).inconclusive
    }

    /// The image the print's last check was shown, and when its frame was
    /// captured; nothing before its first check.
    #[must_use]
    pub fn last_check_image(&self, print_id: PrintId) -> Option<LastCheckImage> {
        self.view_state(print_id).last_check_image
    }

    /// Keep the image one check of a print was shown, and answer the change
    /// notice owed for it: on the print's first check image, and whenever its
    /// kind or its fallback's code differs from the last one kept.
    #[must_use]
    pub fn note_check_image(
        &self,
        print_id: PrintId,
        shown: LastCheckImage,
        frame_sha256: &str,
    ) -> Option<EventBody> {
        let mut state = self.view_state(print_id);
        let code = |image: &LastCheckImage| image.fallback.as_ref().map(|fallback| fallback.code);
        let changed = state
            .last_check_image
            .as_ref()
            .is_none_or(|last| last.kind != shown.kind || code(last) != code(&shown));
        let notice = changed.then(|| {
            body_of(&crate::judgements::CheckImageChangedPayload {
                kind: shown.kind,
                fallback: shown.fallback.clone(),
                frame_sha256: frame_sha256.to_owned(),
            })
        });
        state.last_check_image = Some(shown);
        self.keep_view_state(print_id, &state);
        notice.flatten()
    }

    /// Judge one frame of a print — its view always, and its first-layer
    /// alignment when `judging` asks and the alignment is due — keeping what
    /// the judgement decided and answering it with the records to append.
    ///
    /// The alignment is due on a frame of the configured camera once the
    /// plan has passed the print's first object layer: while it is pending,
    /// for [`AlignmentJudging::WhilePending`], and whatever it stands at for
    /// [`AlignmentJudging::Afresh`], a latched failure included. A frame whose
    /// view shows the camera moved again since the correction in force was
    /// taken sets the alignment back to pending, and is not judged for it: a
    /// later check's frame takes the fresh correction.
    ///
    /// Nothing is judged without a calibration, and a frame that is no JPEG is
    /// answered as such, with nothing recorded.
    #[must_use]
    pub fn judge(&self, print_id: PrintId, judging: &Judging<'_>) -> Judged {
        let Some(calibration) = self.calibration.as_deref() else {
            return Judged::unjudged(PrintAlignment::Unavailable);
        };
        let mut judged = Judged::unjudged(self.print_alignment(print_id));
        let mut state = self.view_state(print_id);
        let in_force = self.correction_in_force(print_id);
        let sha = judging.frame_sha256;
        let landmarks = match landmarks_in_frame(
            calibration,
            judging.frame_jpeg,
            judging.camera_snapshot_url,
        ) {
            Ok(landmarks) => landmarks,
            Err(error @ ComposeError::FrameMismatch { .. }) => {
                return self.mismatched(print_id, calibration, &error, judging, in_force, judged);
            }
            Err(error) => {
                judged.unreadable = Some(error.to_string());
                return judged;
            }
        };
        // A plan laying no object leaves nothing a frame could be judged
        // against: the verdict file says so, and records nothing.
        if judging.alignment != AlignmentJudging::Never
            && judged.alignment == PrintAlignment::Pending
            && judging
                .plan
                .is_some_and(|plan| plan.index.first_object_layer().is_none())
        {
            judged.alignment = PrintAlignment::Unavailable;
            self.decide(print_id, &judged.alignment);
        }
        if let Some(checked) = state.camera_check.clone() {
            return self.judge_under_check(
                print_id,
                calibration,
                landmarks,
                judging,
                &checked,
                &mut state,
                judged,
            );
        }
        let due = Self::alignment_due(&judged.alignment, judging);
        let view = match (due, &judged.alignment) {
            (None, PrintAlignment::Passed { .. }) => {
                let view = judge_found(calibration, landmarks, in_force.as_ref());
                if view.outcome == ViewOutcome::ViewRefitRejected {
                    judged.alignment = PrintAlignment::Pending;
                    self.decide(print_id, &judged.alignment);
                    state.correction = None;
                    state.inconclusive = None;
                    self.keep_view_state(print_id, &state);
                    judged.rejected.clone_from(&view.reason);
                } else {
                    judged.correction.clone_from(&in_force);
                }
                judged
                    .records
                    .extend(view_record(sha, &view, in_force.clone()));
                view
            }
            (Some(first), _) => self.judge_alignment(
                print_id,
                calibration,
                landmarks,
                &first,
                judging,
                in_force,
                &mut state,
                &mut judged,
            ),
            (None, _) => {
                let view = judge_found(calibration, landmarks, None);
                judged.records.extend(view_record(sha, &view, None));
                view
            }
        };
        judged.inconclusive = state.inconclusive;
        judged.view = Some((view.outcome, view.reason));
        judged
    }

    /// What a frame of another size or stream than the calibration's
    /// decides: its view refused, and — when the alignment is due on it — a
    /// failure the rest of the print holds to.
    fn mismatched(
        &self,
        print_id: PrintId,
        calibration: &CameraCalibration,
        error: &ComposeError,
        judging: &Judging<'_>,
        in_force: Option<ViewCorrection>,
        mut judged: Judged,
    ) -> Judged {
        let sha = judging.frame_sha256;
        let reason = error.to_string();
        judged
            .records
            .extend(body_of(&crate::judgements::CameraViewJudgedPayload {
                frame_sha256: sha.to_owned(),
                outcome: ViewOutcome::FrameMismatch,
                view_correction: in_force.clone(),
                reason: Some(reason.clone()),
            }));
        if Self::alignment_due(&judged.alignment, judging).is_some() {
            let failure = failure_of(error, calibration);
            judged.records.extend(body_of(
                &crate::judgements::FirstLayerAlignmentJudgedPayload {
                    verdict: crate::judgements::AlignmentJudgedVerdict::Failed,
                    offset_px: None,
                    correction_px: None,
                    match_strength: None,
                    reason: failure_sentence(&failure),
                    frame_sha256: sha.to_owned(),
                    view_correction: in_force.clone(),
                },
            ));
            judged.alignment = PrintAlignment::Failed { reason: failure };
            self.decide(print_id, &judged.alignment);
            let mut state = self.view_state(print_id);
            state.correction = None;
            state.inconclusive = None;
            self.keep_view_state(print_id, &state);
        }
        judged.correction = in_force.filter(|_| judged.alignment.passed());
        judged.mismatch = Some(reason);
        judged
    }

    /// Judge one frame of a print started under a camera check: its view
    /// against the check's reference, and, when it is due, its first layer —
    /// recorded, and kept as the print's verdict, but neither gating the
    /// drawing nor taking it away. On the 2026-11-28 print the first-layer
    /// search scored the toolhead at 46.04 and 42.38 px from a box the
    /// calibration drew within 2 px, so its offset is no evidence the camera
    /// moved. Only landmarks moved again beyond the check's reference refuse
    /// the drawing.
    #[expect(
        clippy::too_many_arguments,
        reason = "the one judgement's inputs and the two records it updates"
    )]
    fn judge_under_check(
        &self,
        print_id: PrintId,
        calibration: &CameraCalibration,
        landmarks: Vec<printobserver_panels::FoundLandmark>,
        judging: &Judging<'_>,
        checked: &CheckedCamera,
        state: &mut CameraViewState,
        mut judged: Judged,
    ) -> Judged {
        let sha = judging.frame_sha256;
        if let Some(first) = Self::alignment_due(&judged.alignment, judging) {
            self.record_first_layer(
                print_id,
                calibration,
                &first,
                judging,
                checked,
                state,
                &mut judged,
            );
        }
        let view = judge_found(calibration, landmarks, Some(&checked.correction));
        if view.outcome == ViewOutcome::ViewRefitRejected {
            judged.rejected.clone_from(&view.reason);
        } else if view.outcome.drawable() {
            judged.correction = Some(checked.correction.clone());
        }
        judged
            .records
            .extend(body_of(&crate::judgements::CameraViewJudgedPayload {
                frame_sha256: sha.to_owned(),
                outcome: view.outcome,
                view_correction: Some(checked.correction.clone()),
                reason: view.reason.clone(),
            }));
        judged.camera_check = Some(checked.id);
        judged.inconclusive = state.inconclusive;
        judged.view = Some((view.outcome, view.reason));
        judged
    }

    /// Judge the first layer of a print started under a camera check, and keep
    /// what it found as the print's verdict, leaving the check's correction in
    /// force whatever it found.
    #[expect(
        clippy::too_many_arguments,
        reason = "the one judgement's inputs and the two records it updates"
    )]
    fn record_first_layer(
        &self,
        print_id: PrintId,
        calibration: &CameraCalibration,
        first: &DueAlignment,
        judging: &Judging<'_>,
        checked: &CheckedCamera,
        state: &mut CameraViewState,
        judged: &mut Judged,
    ) {
        let Ok(judgement) = judge_first_layer(&FirstLayerInputs {
            alignment: AlignmentInputs {
                frame_jpeg: judging.frame_jpeg,
                camera_snapshot_url: judging.camera_snapshot_url,
                first_layer: &first.first_layer,
                print_z_mm: first.print_z_mm,
                calibration,
            },
            nozzle_xy_mm: first.nozzle_xy_mm,
        }) else {
            // The frame was decoded and held to the calibration already, so no
            // refusal is reached here; one that is is judged again next check.
            return;
        };
        let found = judgement.found;
        let shift = |found: &printobserver_panels::AlignmentVerdict| {
            printobserver_panels::alignment::correction_shift(
                found,
                &first.first_layer,
                first.print_z_mm,
            )
        };
        let (verdict, reason) = match judgement.verdict {
            FirstLayerVerdict::Passed => (
                crate::judgements::AlignmentJudgedVerdict::Passed,
                "clearly_found".to_owned(),
            ),
            FirstLayerVerdict::Inconclusive(why) => (
                crate::judgements::AlignmentJudgedVerdict::Inconclusive,
                why.as_str().to_owned(),
            ),
        };
        judged.records.extend(body_of(
            &crate::judgements::FirstLayerAlignmentJudgedPayload {
                verdict,
                offset_px: found.map(|found| found.offset_px),
                correction_px: (judgement.verdict == FirstLayerVerdict::Passed)
                    .then(|| found.as_ref().map(shift))
                    .flatten(),
                match_strength: found.map(|found| found.match_strength),
                reason,
                frame_sha256: judging.frame_sha256.to_owned(),
                view_correction: Some(checked.correction.clone()),
            },
        ));
        match (judgement.verdict, found) {
            (FirstLayerVerdict::Passed, Some(found)) => {
                judged.alignment = PrintAlignment::Passed {
                    offset_px: found.offset_px,
                    correction_px: shift(&found),
                };
                state.inconclusive = None;
            }
            (FirstLayerVerdict::Inconclusive(why), _) => {
                judged.alignment = PrintAlignment::Pending;
                state.inconclusive = Some(why);
            }
            (FirstLayerVerdict::Passed, None) => return,
        }
        self.decide(print_id, &judged.alignment);
        self.keep_view_state(print_id, state);
    }

    /// Judge the first layer on a frame it is due on, and the frame's view
    /// against what that leaves in force.
    #[expect(
        clippy::too_many_arguments,
        reason = "the one judgement's inputs and the two records it updates"
    )]
    fn judge_alignment(
        &self,
        print_id: PrintId,
        calibration: &CameraCalibration,
        landmarks: Vec<printobserver_panels::FoundLandmark>,
        first: &DueAlignment,
        judging: &Judging<'_>,
        in_force: Option<ViewCorrection>,
        state: &mut CameraViewState,
        judged: &mut Judged,
    ) -> ViewJudgement {
        let sha = judging.frame_sha256;
        let uncorrected = judge_found(calibration, landmarks.clone(), None);
        let judgement = judge_first_layer(&FirstLayerInputs {
            alignment: AlignmentInputs {
                frame_jpeg: judging.frame_jpeg,
                camera_snapshot_url: judging.camera_snapshot_url,
                first_layer: &first.first_layer,
                print_z_mm: first.print_z_mm,
                calibration,
            },
            nozzle_xy_mm: first.nozzle_xy_mm,
        });
        let Ok(judgement) = judgement else {
            // The frame was decoded and held to the calibration above, so no
            // refusal is reached here; one that is leaves the alignment as it
            // stood, judged again on the next check's frame.
            judged.records.extend(view_record(sha, &uncorrected, None));
            return uncorrected;
        };
        let found = judgement.found;
        let (verdict, correction_px, reason) = match judgement.verdict {
            FirstLayerVerdict::Passed => (
                crate::judgements::AlignmentJudgedVerdict::Passed,
                found.map(|found| {
                    printobserver_panels::alignment::correction_shift(
                        &found,
                        &first.first_layer,
                        first.print_z_mm,
                    )
                }),
                "clearly_found".to_owned(),
            ),
            FirstLayerVerdict::Inconclusive(why) => (
                crate::judgements::AlignmentJudgedVerdict::Inconclusive,
                None,
                why.as_str().to_owned(),
            ),
        };
        judged.records.extend(body_of(
            &crate::judgements::FirstLayerAlignmentJudgedPayload {
                verdict,
                offset_px: found.map(|found| found.offset_px),
                correction_px,
                match_strength: found.map(|found| found.match_strength),
                reason,
                frame_sha256: sha.to_owned(),
                view_correction: in_force,
            },
        ));
        match (judgement.verdict, found) {
            (FirstLayerVerdict::Passed, Some(found)) => {
                let shift = printobserver_panels::alignment::correction_shift(
                    &found,
                    &first.first_layer,
                    first.print_z_mm,
                );
                eprintln!(
                    "printobserver: print {print_id}: the first layer was found shifted [{:.0}, {:.0}] px at Z {:.2}, at a contrast of {:.1}; corrected by [{:.0}, {:.0}] px",
                    found.shift_px[0],
                    found.shift_px[1],
                    first.print_z_mm,
                    found.match_strength,
                    shift[0],
                    shift[1]
                );
                let correction = ViewCorrection::translation(shift, uncorrected.agreeing());
                judged.alignment = PrintAlignment::Passed {
                    offset_px: found.offset_px,
                    correction_px: shift,
                };
                self.decide(print_id, &judged.alignment);
                state.correction = Some(correction.clone());
                state.inconclusive = None;
                self.keep_view_state(print_id, state);
                if uncorrected.moved(calibration) {
                    judged
                        .records
                        .extend(body_of(&crate::judgements::CameraViewRefitPayload {
                            frame_sha256: sha.to_owned(),
                            view_correction: correction.clone(),
                        }));
                }
                let view = judge_found(calibration, landmarks, Some(&correction));
                judged
                    .records
                    .extend(view_record(sha, &view, Some(correction.clone())));
                judged.correction = Some(correction);
                view
            }
            (FirstLayerVerdict::Inconclusive(why), _) => {
                // Written even when it was pending already: a reader of the
                // verdict file learns a frame was judged.
                judged.alignment = PrintAlignment::Pending;
                self.decide(print_id, &judged.alignment);
                state.correction = None;
                state.inconclusive = Some(why);
                self.keep_view_state(print_id, state);
                judged.records.extend(view_record(sha, &uncorrected, None));
                uncorrected
            }
            (FirstLayerVerdict::Passed, None) => {
                judged.records.extend(view_record(sha, &uncorrected, None));
                uncorrected
            }
        }
    }

    /// The first layer a frame's judgement scores, when the alignment is due
    /// on it; nothing otherwise.
    fn alignment_due(alignment: &PrintAlignment, judging: &Judging<'_>) -> Option<DueAlignment> {
        let asked = match judging.alignment {
            AlignmentJudging::Never => false,
            AlignmentJudging::WhilePending => *alignment == PrintAlignment::Pending,
            AlignmentJudging::Afresh => true,
        };
        if !asked {
            return None;
        }
        let plan = judging.plan?;
        let index = &plan.index;
        let position = plan.file_position_bytes;
        let (first, ends) = index.first_object_layer()?;
        if position <= ends {
            return None;
        }
        Some(DueAlignment {
            first_layer: index.layer_toolpath(first).ok()?,
            print_z_mm: index
                .layer_at(position)
                .and_then(|layer| index.layer_z_mm(layer))?,
            nozzle_xy_mm: index.planned_head_at(position).ok().flatten(),
        })
    }

    /// Delete the verdict, the last check's record, the checked frames and the
    /// camera view of every print that is not open.
    ///
    /// An open print's record still being written — the `.json.part` beside it
    /// that [`replace_file`] renames over it — is its own, and kept: the watch
    /// sweeps after every read, so a sweep removing it between the write and the
    /// rename lost that record, and the print's next check showed no previous
    /// frame.
    ///
    /// A store that cannot list the open prints, a directory that cannot be
    /// listed and a file that cannot be removed each leave a verdict behind,
    /// which the next read's sweep tries again. No test makes either refuse,
    /// since that needs a store or filesystem made to fail beneath a running
    /// server; the `aligning` journeys prove the sweep removes a closed
    /// print's verdict and keeps an open one's, after a close and at start.
    pub async fn sweep(&self) {
        let Ok(open) = self.prints.open_prints().await else {
            return;
        };
        let open: Vec<String> = open
            .iter()
            .flat_map(|print| {
                [
                    format!("{}.json", print.id),
                    format!("{}.json.part", print.id),
                ]
            })
            .collect();
        for directory in [
            &self.directory,
            &self.previous_directory,
            &self.checked_directory,
            &self.view_directory,
        ] {
            let Ok(entries) = std::fs::read_dir(directory) else {
                continue;
            };
            for entry in entries.flatten() {
                let name = entry.file_name();
                if !open.iter().any(|kept| name.to_str() == Some(kept.as_str())) {
                    let _ = std::fs::remove_file(entry.path());
                }
            }
        }
    }
}

/// The newest of a print's checked frames, oldest first, at least `rise_mm`
/// below `print_z_mm` in the plan.
fn reference_in(kept: &[CheckedFrame], print_z_mm: f64, rise_mm: f64) -> Option<&CheckedFrame> {
    kept.iter()
        .rev()
        .find(|checked| checked.print_z_mm <= print_z_mm - rise_mm)
}

/// A print's checked frames once `checked` is kept among them: the newest
/// earlier frame at least `rise_mm` below it and every frame after that, then
/// it, at most [`MAX_CHECKED_FRAMES`] of them, the oldest dropped first; or it
/// alone when it is below the last kept, a plan gone back down.
fn kept_with(
    mut kept: Vec<CheckedFrame>,
    checked: CheckedFrame,
    rise_mm: f64,
) -> Vec<CheckedFrame> {
    if kept
        .last()
        .is_some_and(|last| checked.print_z_mm < last.print_z_mm)
    {
        kept.clear();
    }
    if let Some(reference) = kept
        .iter()
        .rposition(|earlier| earlier.print_z_mm <= checked.print_z_mm - rise_mm)
    {
        kept.drain(..reference);
    }
    kept.push(checked);
    if kept.len() > MAX_CHECKED_FRAMES {
        kept.drain(..kept.len() - MAX_CHECKED_FRAMES);
    }
    kept
}

/// How many times one file's replacement is attempted while the filesystem
/// refuses it for a moment, and the pause between two attempts: half a second
/// in all, on a check that is otherwise waiting on no one.
const REPLACE_ATTEMPTS: u32 = 20;
const REPLACE_PAUSE: Duration = Duration::from_millis(25);

/// Put `bytes` at `path` whole: written beside it, then renamed over it, so a
/// reader finds the old file or the new one and never part of either.
///
/// Windows refuses to replace a file another process holds open without
/// letting it be deleted — a scanner reading what was just written does — and
/// that refusal ends when the other process closes it. So a refusal of that
/// kind is attempted again, up to [`REPLACE_ATTEMPTS`] times; any other, or
/// one that outlasts them, is answered, with the file beside it removed.
///
/// # Errors
///
/// The filesystem's own error for the last attempt.
fn replace_file(path: &Path, bytes: &[u8]) -> std::io::Result<()> {
    let held = path.with_extension("json.part");
    attempt_while_refused(
        || {
            std::fs::write(&held, bytes)?;
            std::fs::rename(&held, path)
        },
        refused_for_a_moment,
        REPLACE_ATTEMPTS,
        REPLACE_PAUSE,
    )
    .inspect_err(|_| {
        let _ = std::fs::remove_file(&held);
    })
}

/// Run `attempt` until it succeeds, fails with an error `refused` does not
/// call momentary, or has been run `attempts` times, pausing between two.
fn attempt_while_refused(
    mut attempt: impl FnMut() -> std::io::Result<()>,
    refused: impl Fn(&std::io::Error) -> bool,
    attempts: u32,
    pause: Duration,
) -> std::io::Result<()> {
    let mut made = 1;
    loop {
        match attempt() {
            Err(error) if made < attempts && refused(&error) => {
                std::thread::sleep(pause);
                made += 1;
            }
            answered => return answered,
        }
    }
}

/// Whether Windows refused a replacement only because another handle has the
/// file open: `ERROR_ACCESS_DENIED` (5) or `ERROR_SHARING_VIOLATION` (32).
#[cfg(windows)]
fn refused_for_a_moment(error: &std::io::Error) -> bool {
    matches!(error.raw_os_error(), Some(5 | 32))
}

/// Elsewhere a rename replaces a file whoever has it open, so no refusal is
/// momentary.
#[cfg(not(windows))]
fn refused_for_a_moment(_error: &std::io::Error) -> bool {
    false
}

/// A frame size as the refusal spells it, `<w>x<h>`.
fn size_of(text: &str) -> [u32; 2] {
    let mut parts = text.split('x').map(|part| part.parse().unwrap_or(0));
    [parts.next().unwrap_or(0), parts.next().unwrap_or(0)]
}

/// A verdict as the log says it.
fn said(verdict: &PrintAlignment) -> String {
    match verdict {
        PrintAlignment::Unavailable => "unavailable: the G-code lays no object".to_owned(),
        PrintAlignment::Pending => "pending".to_owned(),
        PrintAlignment::Passed {
            offset_px,
            correction_px,
        } => format!(
            "passed, offset {offset_px:.1} px, corrected by [{:.0}, {:.0}] px",
            correction_px[0], correction_px[1]
        ),
        PrintAlignment::Failed {
            reason: AlignmentFailure::Offset { offset_px },
        } => format!("failed, offset {offset_px:.1} px"),
        PrintAlignment::Failed {
            reason: AlignmentFailure::Resolution { expected, got },
        } => format!(
            "failed: the camera's frames are {}x{}, the calibration's {}x{}",
            got[0], got[1], expected[0], expected[1]
        ),
        PrintAlignment::Failed {
            reason: AlignmentFailure::Stream,
        } => "failed: the camera is not the stream the calibration was fitted on".to_owned(),
    }
}

/// One print's camera view as it is kept.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(crate = "printobserver_types::serde")]
struct CameraViewState {
    /// The correction the print's last pass took, with its reference.
    #[serde(default)]
    correction: Option<ViewCorrection>,
    /// Why the frame judged last was inconclusive, while the alignment is
    /// pending.
    #[serde(default)]
    inconclusive: Option<InconclusiveReason>,
    /// The image the print's last check was shown.
    #[serde(default)]
    last_check_image: Option<LastCheckImage>,
    /// The camera check the print started under, whose correction every
    /// check of it is drawn through from its first.
    #[serde(default)]
    camera_check: Option<CheckedCamera>,
}

/// The camera check a print started under, as its view keeps it.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(crate = "printobserver_types::serde")]
struct CheckedCamera {
    /// The check.
    id: printobserver_core::CameraCheckId,
    /// The correction it put in force.
    correction: ViewCorrection,
}

/// The image a print's last check was shown, and when its frame was captured.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
#[schemars(crate = "printobserver_types::schemars")]
pub struct LastCheckImage {
    /// When the check's frame was captured.
    pub at: Timestamp,
    /// Which image the check was shown.
    pub kind: ImageKind,
    /// Why it was the whole frame; null for the three-panel image.
    pub fallback: Option<ImageFallback>,
}

impl Sample for LastCheckImage {
    fn sample_full() -> Self {
        Self {
            at: Timestamp::sample_full(),
            kind: ImageKind::WholeFrame,
            fallback: Some(ImageFallback::sample_full()),
        }
    }

    fn sample_minimal() -> Self {
        Self {
            at: Timestamp::sample_full(),
            kind: ImageKind::ThreePanel,
            fallback: None,
        }
    }
}

/// Whether a judgement of a frame judges the print's first-layer alignment.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AlignmentJudging {
    /// Never: a look, or a frame not the configured camera's own.
    Never,
    /// While the alignment is pending: every scheduled check's frame.
    WhilePending,
    /// Whatever it stands at, a latched failure included: the operator's
    /// recheck of the camera.
    Afresh,
}

impl AlignmentJudging {
    /// How the operator's recheck of the camera judges the first layer: never
    /// while the printer reports the print paused, whose bed stands off the
    /// plan's height, and afresh otherwise.
    #[must_use]
    pub const fn of_recheck(paused: bool) -> Self {
        if paused { Self::Never } else { Self::Afresh }
    }
}

/// One frame to judge.
#[derive(Debug, Clone, Copy)]
pub struct Judging<'a> {
    /// The frame's bytes.
    pub frame_jpeg: &'a [u8],
    /// The stored frame's SHA-256, which every record names.
    pub frame_sha256: &'a str,
    /// The stream it is of.
    pub camera_snapshot_url: &'a str,
    /// Where the print stood in its plan as it was taken.
    pub plan: Option<&'a PlanPosition>,
    /// Whether the first-layer alignment is judged.
    pub alignment: AlignmentJudging,
}

/// What one judgement of a frame decided, and the records it owes.
#[derive(Debug, Clone, PartialEq)]
pub struct Judged {
    /// The records to append, in order, each naming the frame.
    pub records: Vec<EventBody>,
    /// The print's alignment once judged.
    pub alignment: PrintAlignment,
    /// The correction the frame may be drawn through, when the alignment
    /// passed and the view allows it.
    pub correction: Option<ViewCorrection>,
    /// The view's outcome and its refusal's reason; nothing when the frame was
    /// not judged for its view.
    pub view: Option<(ViewOutcome, Option<String>)>,
    /// Why the correction in force was given up on this frame: the camera
    /// moved again.
    pub rejected: Option<String>,
    /// Why the frame is not the calibration's size or stream.
    pub mismatch: Option<String>,
    /// Why the frame could not be read at all.
    pub unreadable: Option<String>,
    /// Why the alignment was left pending, when the frame judged last was
    /// inconclusive.
    pub inconclusive: Option<InconclusiveReason>,
    /// The camera check the print started under, when the frame was judged
    /// against its reference: the drawing then rests on the check alone.
    pub camera_check: Option<printobserver_core::CameraCheckId>,
}

impl Judged {
    /// Nothing that could be judged of a frame — no calibration, or no stream
    /// it was fitted on — the alignment standing as it is, and the one record
    /// the frame is kept with.
    #[must_use]
    pub fn nothing_to_judge(alignment: PrintAlignment, kept_with: Option<EventBody>) -> Self {
        Self {
            records: kept_with.into_iter().collect(),
            ..Self::unjudged(alignment)
        }
    }

    /// Nothing judged, the alignment standing as it is.
    const fn unjudged(alignment: PrintAlignment) -> Self {
        Self {
            records: Vec::new(),
            alignment,
            correction: None,
            view: None,
            rejected: None,
            mismatch: None,
            unreadable: None,
            inconclusive: None,
            camera_check: None,
        }
    }
}

impl PrintAlignment {
    /// Whether it passed.
    #[must_use]
    pub const fn passed(&self) -> bool {
        matches!(self, Self::Passed { .. })
    }
}

/// What a first-layer judgement due on a frame scores.
struct DueAlignment {
    /// The first object layer's toolpath.
    first_layer: printobserver_core::LayerToolpath,
    /// The print's height in its plan as the frame was taken.
    print_z_mm: f64,
    /// Where the plan has the nozzle then.
    nozzle_xy_mm: Option<[f64; 2]>,
}

/// One payload as the body appended for it; nothing for one that will not
/// render, which no declared payload is.
fn body_of<P: EventPayload>(payload: &P) -> Option<EventBody> {
    EventBody::of(payload).ok()
}

/// The record of one view judgement, its reason the fallback it leads to: a
/// refit view, its landmarks moved from the calibration, is drawn on no more
/// than a refused one.
fn view_record(
    frame_sha256: &str,
    view: &ViewJudgement,
    view_correction: Option<ViewCorrection>,
) -> Option<EventBody> {
    let reason = if view.outcome == ViewOutcome::Refit {
        Some(crate::check_images::CALIBRATION_STALE.to_owned())
    } else {
        view.reason.clone()
    };
    body_of(&crate::judgements::CameraViewJudgedPayload {
        frame_sha256: frame_sha256.to_owned(),
        outcome: view.outcome,
        view_correction,
        reason,
    })
}

/// The failure a frame refused for its size or stream latches.
fn failure_of(error: &ComposeError, calibration: &CameraCalibration) -> AlignmentFailure {
    match error {
        ComposeError::FrameMismatch {
            field: FrameField::Resolution,
            got,
            ..
        } => AlignmentFailure::Resolution {
            expected: calibration.image_size_px,
            got: size_of(got),
        },
        _ => AlignmentFailure::Stream,
    }
}

/// A failure in the sentence its record and its fallback give it in.
#[must_use]
pub fn failure_sentence(failure: &AlignmentFailure) -> String {
    match failure {
        AlignmentFailure::Offset { offset_px } => format!(
            "the printed first layer lies {offset_px:.1} px from where the calibration projects it"
        ),
        AlignmentFailure::Resolution { expected, got } => format!(
            "the camera's frames are {}x{} and the calibration's {}x{}",
            got[0], got[1], expected[0], expected[1]
        ),
        AlignmentFailure::Stream => {
            "the camera is not the stream the calibration was fitted on".to_owned()
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{CheckedFrame, MAX_CHECKED_FRAMES, attempt_while_refused, kept_with, reference_in};
    use std::cell::Cell;
    use std::io::{Error, ErrorKind};
    use std::time::Duration;

    /// An attempt that is refused `refusals` times and then succeeds, counting
    /// how often it was made.
    fn refused_then_done(refusals: u32, made: &Cell<u32>) -> impl FnMut() -> std::io::Result<()> {
        move || {
            made.set(made.get() + 1);
            if made.get() <= refusals {
                Err(Error::from(ErrorKind::PermissionDenied))
            } else {
                Ok(())
            }
        }
    }

    #[test]
    fn a_momentary_refusal_is_attempted_again_until_it_passes() {
        let made = Cell::new(0);
        let answered =
            attempt_while_refused(refused_then_done(2, &made), |_| true, 5, Duration::ZERO);
        assert!(answered.is_ok(), "{answered:?}");
        assert_eq!(made.get(), 3);
    }

    #[test]
    fn a_refusal_that_outlasts_every_attempt_is_answered() {
        let made = Cell::new(0);
        let answered =
            attempt_while_refused(refused_then_done(9, &made), |_| true, 4, Duration::ZERO);
        assert_eq!(
            answered.map_err(|error| error.kind()),
            Err(ErrorKind::PermissionDenied)
        );
        assert_eq!(made.get(), 4);
    }

    #[test]
    fn a_refusal_that_is_not_momentary_is_answered_at_once() {
        let made = Cell::new(0);
        let answered =
            attempt_while_refused(refused_then_done(9, &made), |_| false, 4, Duration::ZERO);
        assert!(answered.is_err());
        assert_eq!(made.get(), 1);
    }

    /// The refusal the retry answers, made the way Windows makes it: the
    /// record held open by another handle that does not allow deleting it,
    /// and let go a moment later.
    #[cfg(windows)]
    #[test]
    fn a_record_held_open_by_another_handle_is_replaced_once_it_is_let_go() {
        use std::os::windows::fs::OpenOptionsExt;
        let directory = tempfile::TempDir::new().expect("a directory");
        let path = directory.path().join("record.json");
        std::fs::write(&path, b"before").expect("a record");
        let held = std::fs::OpenOptions::new()
            .read(true)
            .share_mode(0)
            .open(&path)
            .expect("the record held");
        let release = std::thread::spawn(move || {
            std::thread::sleep(Duration::from_millis(150));
            drop(held);
        });
        super::replace_file(&path, b"after").expect("the record replaced");
        release.join().expect("the handle let go");
        assert_eq!(std::fs::read(&path).expect("the record"), b"after");
        assert!(!path.with_extension("json.part").exists());
    }

    #[cfg(windows)]
    #[test]
    fn only_a_held_file_is_a_momentary_refusal_on_windows() {
        assert!(super::refused_for_a_moment(&Error::from_raw_os_error(5)));
        assert!(super::refused_for_a_moment(&Error::from_raw_os_error(32)));
        assert!(!super::refused_for_a_moment(&Error::from_raw_os_error(2)));
    }

    /// A checked frame at `z` mm, captured `second` seconds into a print.
    fn at(second: i64, z: f64) -> CheckedFrame {
        CheckedFrame {
            frame: format!("frame-{second}.jpg").into(),
            captured_at: printobserver_types::Timestamp::from_unix_seconds(1_791_489_600 + second)
                .expect("a time"),
            print_z_mm: z,
        }
    }

    /// The heights of checked frames, in their order.
    fn heights(kept: &[CheckedFrame]) -> Vec<f64> {
        kept.iter().map(|checked| checked.print_z_mm).collect()
    }

    /// Once a print carries on from a planned change, its growth is measured
    /// only from frames checked after: one from before the wait, however far
    /// below, is no longer a reference, and the first checked after it is.
    #[test]
    fn after_a_planned_change_growth_is_measured_from_frames_after_it() {
        let state = tempfile::TempDir::new().expect("a state directory");
        let store =
            printobserver_store_sqlite::MemoryStore::new(state.path()).expect("an in-memory store");
        let alignments = super::Alignments::new(state.path(), None, std::sync::Arc::new(store))
            .expect("the verdicts' directories");
        let print_id = printobserver_types::PrintId::new();
        for (second, z) in [(0, 0.2), (60, 0.6)] {
            alignments.record_checked_frame(print_id, at(second, z), 0.4);
        }
        let before = alignments.growth_reference(print_id, 1.0, 0.4);
        assert_eq!(before.map(|checked| checked.print_z_mm), Some(0.6));

        alignments.restart_growth(print_id);
        assert_eq!(alignments.growth_reference(print_id, 1.0, 0.4), None);
        alignments.record_checked_frame(print_id, at(3_000, 0.8), 0.4);
        alignments.record_checked_frame(print_id, at(3_120, 1.4), 0.4);
        let after = alignments.growth_reference(print_id, 1.4, 0.4);
        assert_eq!(
            after.map(|checked| checked.captured_at),
            Some(at(3_000, 0.8).captured_at)
        );
        // A second change with nothing checked since forgets nothing more.
        alignments.restart_growth(print_id);
        alignments.restart_growth(print_id);
        assert!(alignments.checked_frames(print_id).is_empty());
    }

    #[test]
    fn a_check_measures_from_the_newest_frame_far_enough_below_it() {
        let kept = [at(0, 0.2), at(60, 0.8), at(120, 2.0), at(180, 2.6)];
        assert_eq!(
            reference_in(&kept, 5.8, 3.0).map(|checked| checked.print_z_mm),
            Some(2.6)
        );
        assert_eq!(
            reference_in(&kept, 3.5, 3.0).map(|checked| checked.print_z_mm),
            Some(0.2)
        );
        assert_eq!(reference_in(&kept, 3.1, 3.0), None, "none is 3 mm below");
    }

    #[test]
    fn only_what_a_later_check_can_measure_from_is_kept() {
        let mut kept = Vec::new();
        for (second, z) in [(0, 0.2), (60, 0.8), (120, 2.0), (180, 3.4), (240, 4.0)] {
            kept = kept_with(kept, at(second, z), 3.0);
        }
        assert_eq!(
            heights(&kept),
            [0.8, 2.0, 3.4, 4.0],
            "0.2 is below 0.8, which is itself 3 mm below 4.0, so no later check needs it"
        );
        assert_eq!(
            heights(&kept_with(kept, at(300, 1.0), 3.0)),
            [1.0],
            "a plan gone back down starts the record again"
        );
    }

    #[test]
    fn the_oldest_checked_frames_go_past_the_most_kept() {
        let mut kept = Vec::new();
        for second in 0..=i64::try_from(MAX_CHECKED_FRAMES).expect("a count") {
            kept = kept_with(kept, at(second, 5.0), 3.0);
        }
        assert_eq!(kept.len(), MAX_CHECKED_FRAMES);
        assert_eq!(kept[0].captured_at, at(1, 5.0).captured_at);
    }
}