nx_print_response.nx source
↩ module page · 112 lines · 4429 B
1// nx_print_response.nx -- map runtime-monitor issue verdicts to
2// corrective actions on the printer.
3//
4// When the runtime monitor fires an NX_RT_ISSUE_*, the operator
5// integration needs to actually DO something on the printer:
6// pause, hard-stop, notify-only. This brick:
7//
8// 1. Maps each issue kind to a recommended response class
9// (operator can override per-deployment).
10//
11// 2. Builds the HTTP wire bytes for the canonical
12// Moonraker `/printer/print/pause` request, ready for
13// nx_mr_post_ipv4 to send.
14//
15// Substrate stays pure: this function produces bytes, doesn't
16// open sockets. Operator decides when/whether to actually
17// send the wire bytes.
18//
19// license_tier: ORIGINAL
20
21import "nx_syscalls.nx"
22import "nx_moonraker_client.nx"
23import "nx_print_runtime_monitor.nx"
24
25// Sealed response-class enum.
26const NX_PRINT_RESPONSE_NONE: i64 = 0
27const NX_PRINT_RESPONSE_NOTIFY: i64 = 1 // log/page, do not pause
28const NX_PRINT_RESPONSE_PAUSE: i64 = 2 // PAUSE; operator inspects + resumes
29const NX_PRINT_RESPONSE_HARD_STOP: i64 = 3 // emergency stop (cancel; not resumable)
30const NX_PRINT_RESPONSE_N: i64 = 4
31
32// Default mapping (operator can override by wrapping this).
33// All 9 progenitor kinds currently map to PAUSE; thermal dropouts
34// could be NOTIFY-only on chambers without resume capability, but
35// pausing-and-letting-operator-decide is the conservative default.
36func nx_print_response_for_issue(kind: i64) -> i64 {
37 if kind == NX_RT_ISSUE_NONE { return NX_PRINT_RESPONSE_NONE }
38 if kind == NX_RT_ISSUE_STUCK_LAYER { return NX_PRINT_RESPONSE_PAUSE }
39 if kind == NX_RT_ISSUE_FLOW_ANOMALY { return NX_PRINT_RESPONSE_PAUSE }
40 if kind == NX_RT_ISSUE_EXTRUDER_RUNAWAY { return NX_PRINT_RESPONSE_PAUSE }
41 if kind == NX_RT_ISSUE_BED_TEMP_DROP { return NX_PRINT_RESPONSE_PAUSE }
42 if kind == NX_RT_ISSUE_HOTEND_TEMP_DROP { return NX_PRINT_RESPONSE_PAUSE }
43 if kind == NX_RT_ISSUE_CHAMBER_TEMP_DROP { return NX_PRINT_RESPONSE_NOTIFY }
44 if kind == NX_RT_ISSUE_Z_BACKWARD { return NX_PRINT_RESPONSE_HARD_STOP }
45 if kind == NX_RT_ISSUE_FILAMENT_RUNOUT { return NX_PRINT_RESPONSE_PAUSE }
46 if kind == NX_RT_ISSUE_LAYER_SHIFT { return NX_PRINT_RESPONSE_HARD_STOP }
47 return NX_PRINT_RESPONSE_NONE
48}
49
50// Build the wire bytes for POST /printer/print/pause.
51// Empty body; Moonraker's pause endpoint requires no payload.
52//
53// Returns bytes written to out_buf or -1 on overflow.
54func nx_print_build_pause_request(
55 host: *u8, host_len: i64,
56 api_key: *u8, api_key_len: i64,
57 out_buf: *u8, out_cap: i64
58) -> i64 {
59 let path: *u8 = "/printer/print/pause"
60 let path_len: i64 = 20 // strlen("/printer/print/pause")
61 let empty_body: *u8 = ""
62 return nx_mr_build_post_request(
63 host, host_len,
64 path, path_len,
65 empty_body, 0,
66 api_key, api_key_len,
67 out_buf, out_cap)
68}
69
70// Build the wire bytes for POST /printer/print/cancel.
71// Used for HARD_STOP responses (LAYER_SHIFT, Z_BACKWARD --
72// recovery from these is rarely worth attempting).
73func nx_print_build_cancel_request(
74 host: *u8, host_len: i64,
75 api_key: *u8, api_key_len: i64,
76 out_buf: *u8, out_cap: i64
77) -> i64 {
78 let path: *u8 = "/printer/print/cancel"
79 let path_len: i64 = 21 // strlen("/printer/print/cancel")
80 let empty_body: *u8 = ""
81 return nx_mr_build_post_request(
82 host, host_len,
83 path, path_len,
84 empty_body, 0,
85 api_key, api_key_len,
86 out_buf, out_cap)
87}
88
89// Dispatch: given an issue kind, build the recommended HTTP request.
90// Returns:
91// > 0 : bytes written to out_buf
92// 0 : no action (kind == NONE or NOTIFY-only)
93// -1 : overflow
94func nx_print_build_response_for_issue(
95 kind: i64,
96 host: *u8, host_len: i64,
97 api_key: *u8, api_key_len: i64,
98 out_buf: *u8, out_cap: i64
99) -> i64 {
100 let resp: i64 = nx_print_response_for_issue(kind)
101 if resp == NX_PRINT_RESPONSE_NONE { return 0 }
102 if resp == NX_PRINT_RESPONSE_NOTIFY { return 0 }
103 if resp == NX_PRINT_RESPONSE_PAUSE {
104 return nx_print_build_pause_request(
105 host, host_len, api_key, api_key_len, out_buf, out_cap)
106 }
107 if resp == NX_PRINT_RESPONSE_HARD_STOP {
108 return nx_print_build_cancel_request(
109 host, host_len, api_key, api_key_len, out_buf, out_cap)
110 }
111 return 0
112}