use printobserver_types::{
ActionKind, Actor, ActorClass, Adjustable, EffectiveBounds, PolicyDecision, PrintAction,
PrintRecord, PrinterState, RejectionReason, SafetyEnvelope, Timestamp,
};
use crate::clock::seconds_between;
#[derive(Debug, Clone, Copy)]
pub struct DecisionInput<'a> {
pub action: &'a PrintAction,
pub actor: &'a Actor,
pub requested_at: Timestamp,
pub print: Option<&'a PrintRecord>,
pub printer_state: Option<&'a PrinterState>,
pub bounds: &'a EffectiveBounds,
pub envelope: &'a SafetyEnvelope,
pub last_agent_action: Option<Timestamp>,
}
#[must_use]
pub fn adjustment(action: &PrintAction) -> Option<(Adjustable, f64)> {
match action {
PrintAction::SetFeedrateFactor { factor, .. } => Some((Adjustable::Feedrate, *factor)),
PrintAction::SetFlowrateFactor { factor, .. } => Some((Adjustable::Flowrate, *factor)),
PrintAction::SetToolTargetC { tool, target_c, .. } => {
Some((Adjustable::ToolTarget { tool: *tool }, *target_c))
}
PrintAction::SetBedTargetC { target_c, .. } => Some((Adjustable::BedTarget, *target_c)),
PrintAction::SetFanPercent { percent, .. } => Some((Adjustable::Fan, *percent)),
PrintAction::Pause { .. }
| PrintAction::Resume { .. }
| PrintAction::Cancel { .. }
| PrintAction::StartPrint { .. }
| PrintAction::AcknowledgeFailure { .. } => None,
}
}
#[must_use]
pub fn valid_from(kind: ActionKind, state: &PrinterState) -> bool {
match kind {
ActionKind::Pause => matches!(state, PrinterState::Printing),
ActionKind::Resume => matches!(state, PrinterState::Paused),
ActionKind::Cancel => matches!(state, PrinterState::Printing | PrinterState::Paused),
ActionKind::StartPrint => matches!(state, PrinterState::Operational),
ActionKind::SetFeedrateFactor
| ActionKind::SetFlowrateFactor
| ActionKind::SetToolTargetC
| ActionKind::SetBedTargetC
| ActionKind::SetFanPercent => {
matches!(state, PrinterState::Printing | PrinterState::Paused)
}
ActionKind::AcknowledgeFailure => true,
}
}
fn is_active(print: Option<&PrintRecord>) -> bool {
print.is_some_and(|record| record.ended_at.is_none())
}
#[must_use]
pub fn decide(input: &DecisionInput<'_>) -> PolicyDecision {
let kind = input.action.kind();
let actor_class = input.actor.class();
if !is_active(input.print) {
return PolicyDecision::Rejected(RejectionReason::NoActivePrint);
}
let granted = input.envelope.actions.get(&actor_class);
if !granted.is_some_and(|kinds| kinds.contains(&kind)) {
return PolicyDecision::Rejected(RejectionReason::ActorMayNotRequest {
actor_class,
action: kind,
});
}
if actor_class == ActorClass::Agent
&& let Some(last) = input.last_agent_action
{
let since_last_s = seconds_between(last, input.requested_at);
if since_last_s < input.envelope.agent_min_interval_s {
return PolicyDecision::Rejected(RejectionReason::MinIntervalNotElapsed {
interval_s: input.envelope.agent_min_interval_s,
since_last_s,
});
}
}
if let Some(state) = input.printer_state
&& !valid_from(kind, state)
{
return PolicyDecision::Rejected(RejectionReason::InvalidFromState {
state: state.clone(),
});
}
if let Some((adjustable, requested)) = adjustment(input.action) {
let Some(allowed) = input.bounds.allowed.get(&adjustable) else {
return PolicyDecision::Rejected(RejectionReason::UnsupportedAdjustable { adjustable });
};
if !allowed.contains(requested) {
return PolicyDecision::Rejected(RejectionReason::OutOfBounds {
adjustable,
requested,
allowed: *allowed,
});
}
}
PolicyDecision::Accepted
}