printobserver_core/
decision.rs1use crate::records::{
14 ActionKind, Actor, ActorClass, EffectiveBounds, PolicyDecision, PrintAction, PrintRecord,
15 RejectionReason, SafetyEnvelope,
16};
17use printobserver_printer_api::{Adjustable, PrinterState};
18use printobserver_types::Timestamp;
19
20use crate::clock::seconds_between;
21
22#[derive(Debug, Clone, Copy)]
24pub struct DecisionInput<'a> {
25 pub action: &'a PrintAction,
27 pub actor: &'a Actor,
29 pub requested_at: Timestamp,
31 pub print: Option<&'a PrintRecord>,
33 pub printer_state: Option<&'a PrinterState>,
42 pub bounds: &'a EffectiveBounds,
44 pub envelope: &'a SafetyEnvelope,
46 pub last_agent_action: Option<Timestamp>,
48}
49
50#[must_use]
55pub fn adjustment(action: &PrintAction) -> Option<(Adjustable, f64)> {
56 match action {
57 PrintAction::SetFeedrateFactor { factor, .. } => Some((Adjustable::Feedrate, *factor)),
58 PrintAction::SetFlowrateFactor { factor, .. } => Some((Adjustable::Flowrate, *factor)),
59 PrintAction::SetToolTargetC { tool, target_c, .. } => {
60 Some((Adjustable::ToolTarget { tool: *tool }, *target_c))
61 }
62 PrintAction::SetBedTargetC { target_c, .. } => Some((Adjustable::BedTarget, *target_c)),
63 PrintAction::SetFanPercent { percent, .. } => Some((Adjustable::Fan, *percent)),
64 PrintAction::Pause { .. }
65 | PrintAction::Resume { .. }
66 | PrintAction::Cancel { .. }
67 | PrintAction::StartPrint { .. }
68 | PrintAction::AcknowledgeFailure { .. } => None,
69 }
70}
71
72#[must_use]
78pub fn valid_from(kind: ActionKind, state: &PrinterState) -> bool {
79 match kind {
80 ActionKind::Pause => matches!(state, PrinterState::Printing),
81 ActionKind::Resume => matches!(state, PrinterState::Paused),
82 ActionKind::Cancel => matches!(state, PrinterState::Printing | PrinterState::Paused),
83 ActionKind::StartPrint => matches!(state, PrinterState::Operational),
84 ActionKind::SetFeedrateFactor
85 | ActionKind::SetFlowrateFactor
86 | ActionKind::SetToolTargetC
87 | ActionKind::SetBedTargetC
88 | ActionKind::SetFanPercent => {
89 matches!(state, PrinterState::Printing | PrinterState::Paused)
90 }
91 ActionKind::AcknowledgeFailure
92 | ActionKind::RegisterSpool
93 | ActionKind::LoadSpool
94 | ActionKind::UnloadSpool
95 | ActionKind::AdjustSpool
96 | ActionKind::RetireSpool => true,
97 }
98}
99
100#[must_use]
110pub const fn changes_the_machine(kind: ActionKind) -> bool {
111 !matches!(
112 kind,
113 ActionKind::AcknowledgeFailure
114 | ActionKind::RegisterSpool
115 | ActionKind::LoadSpool
116 | ActionKind::UnloadSpool
117 | ActionKind::AdjustSpool
118 | ActionKind::RetireSpool
119 )
120}
121
122fn is_active(print: Option<&PrintRecord>) -> bool {
124 print.is_some_and(|record| record.ended_at.is_none())
125}
126
127#[must_use]
133pub fn decide(input: &DecisionInput<'_>) -> PolicyDecision {
134 let kind = input.action.kind();
135 let actor_class = input.actor.class();
136
137 if !is_active(input.print) {
138 return PolicyDecision::Rejected(RejectionReason::NoActivePrint);
139 }
140
141 let granted = input.envelope.actions.get(&actor_class);
142 if !granted.is_some_and(|kinds| kinds.contains(&kind)) {
143 return PolicyDecision::Rejected(RejectionReason::ActorMayNotRequest {
144 actor_class,
145 action: kind,
146 });
147 }
148
149 let adjusting_a_paused_print = adjustment(input.action).is_some()
154 && matches!(input.printer_state, Some(PrinterState::Paused));
155 if actor_class == ActorClass::Agent
156 && changes_the_machine(kind)
157 && !adjusting_a_paused_print
158 && let Some(last) = input.last_agent_action
159 {
160 let since_last_s = seconds_between(last, input.requested_at);
161 if since_last_s < input.envelope.agent_min_interval_s {
162 return PolicyDecision::Rejected(RejectionReason::MinIntervalNotElapsed {
163 interval_s: input.envelope.agent_min_interval_s,
164 since_last_s,
165 });
166 }
167 }
168
169 if let Some(state) = input.printer_state
170 && !valid_from(kind, state)
171 {
172 return PolicyDecision::Rejected(RejectionReason::InvalidFromState {
173 state: state.clone(),
174 });
175 }
176
177 if let Some((adjustable, requested)) = adjustment(input.action) {
178 let Some(allowed) = input.bounds.allowed.get(&adjustable) else {
179 return PolicyDecision::Rejected(RejectionReason::UnsupportedAdjustable { adjustable });
180 };
181 if !allowed.contains(requested) {
182 return PolicyDecision::Rejected(RejectionReason::OutOfBounds {
183 adjustable,
184 requested,
185 allowed: *allowed,
186 });
187 }
188 }
189
190 PolicyDecision::Accepted
191}