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 => true,
92 }
93}
94
95fn is_active(print: Option<&PrintRecord>) -> bool {
97 print.is_some_and(|record| record.ended_at.is_none())
98}
99
100#[must_use]
106pub fn decide(input: &DecisionInput<'_>) -> PolicyDecision {
107 let kind = input.action.kind();
108 let actor_class = input.actor.class();
109
110 if !is_active(input.print) {
111 return PolicyDecision::Rejected(RejectionReason::NoActivePrint);
112 }
113
114 let granted = input.envelope.actions.get(&actor_class);
115 if !granted.is_some_and(|kinds| kinds.contains(&kind)) {
116 return PolicyDecision::Rejected(RejectionReason::ActorMayNotRequest {
117 actor_class,
118 action: kind,
119 });
120 }
121
122 if actor_class == ActorClass::Agent
123 && let Some(last) = input.last_agent_action
124 {
125 let since_last_s = seconds_between(last, input.requested_at);
126 if since_last_s < input.envelope.agent_min_interval_s {
127 return PolicyDecision::Rejected(RejectionReason::MinIntervalNotElapsed {
128 interval_s: input.envelope.agent_min_interval_s,
129 since_last_s,
130 });
131 }
132 }
133
134 if let Some(state) = input.printer_state
135 && !valid_from(kind, state)
136 {
137 return PolicyDecision::Rejected(RejectionReason::InvalidFromState {
138 state: state.clone(),
139 });
140 }
141
142 if let Some((adjustable, requested)) = adjustment(input.action) {
143 let Some(allowed) = input.bounds.allowed.get(&adjustable) else {
144 return PolicyDecision::Rejected(RejectionReason::UnsupportedAdjustable { adjustable });
145 };
146 if !allowed.contains(requested) {
147 return PolicyDecision::Rejected(RejectionReason::OutOfBounds {
148 adjustable,
149 requested,
150 allowed: *allowed,
151 });
152 }
153 }
154
155 PolicyDecision::Accepted
156}