use printobserver_types::{
ActionExecutedPayload, ActionRecord, ActionRejectedPayload, ActionRequest,
ActionRequestedPayload, Actor, ActorClass, Adjustable, EventPayload, EventSource, Intervention,
InterventionOutcome, PolicyDecision, PrintAction, PrintId, PrintRecord, PrinterSnapshot,
Timestamp,
};
use crate::bounds::{Bounds, effective_bounds};
use crate::clock::plus_seconds;
use crate::decision::{DecisionInput, adjustment, decide};
use crate::error::CoreError;
use crate::supervisor::{Issued, Supervisor};
#[derive(Debug, Clone, PartialEq)]
pub struct ActionOutcome {
pub record: ActionRecord,
pub executed: Option<Result<(), printobserver_printer_api::PrinterError>>,
pub intervention: Option<Intervention>,
}
impl ActionOutcome {
#[must_use]
pub const fn decision(&self) -> &PolicyDecision {
&self.record.decision
}
}
const fn source_of(actor: &Actor) -> EventSource {
match actor.class() {
ActorClass::Agent => EventSource::Agent,
ActorClass::Operator => EventSource::Operator,
ActorClass::System => EventSource::System,
}
}
fn duration_of(action: &PrintAction) -> Option<i64> {
match action {
PrintAction::SetFeedrateFactor { duration_s, .. }
| PrintAction::SetFlowrateFactor { duration_s, .. }
| PrintAction::SetToolTargetC { duration_s, .. }
| PrintAction::SetBedTargetC { duration_s, .. }
| PrintAction::SetFanPercent { duration_s, .. } => *duration_s,
PrintAction::Pause { .. }
| PrintAction::Resume { .. }
| PrintAction::Cancel { .. }
| PrintAction::StartPrint { .. }
| PrintAction::AcknowledgeFailure { .. } => None,
}
}
#[must_use]
pub fn reported_value(snapshot: &PrinterSnapshot, adjustable: Adjustable) -> Option<f64> {
let reported = match adjustable {
Adjustable::Feedrate => snapshot.feedrate_factor,
Adjustable::Flowrate => snapshot.flowrate_factor,
Adjustable::Fan => snapshot.fan_percent,
Adjustable::BedTarget => snapshot.bed.as_ref().and_then(|heater| heater.target_c),
Adjustable::ToolTarget { tool } => usize::try_from(tool)
.ok()
.and_then(|index| snapshot.tools.get(index))
.and_then(|heater| heater.target_c),
};
reported.map(|value| value.value())
}
impl Supervisor {
pub async fn request_action(
&self,
print_id: PrintId,
action: PrintAction,
) -> Result<ActionOutcome, CoreError> {
let requested_at = self.clock().now();
let actor = action.actor().clone();
let print = self.store().print(print_id).await?;
let bounds = self.bounds_for(print.as_ref(), print_id).await?;
let snapshot = self.read_snapshot().await.ok();
let decision = decide(&DecisionInput {
action: &action,
actor: &actor,
requested_at,
print: print.as_ref(),
printer_state: snapshot.as_ref().map(|taken| &taken.connection),
bounds: &bounds.effective,
envelope: &self.config().envelope,
last_agent_action: self.last_agent_action(print_id),
});
let request = ActionRequest {
action: action.clone(),
actor: actor.clone(),
requested_at,
};
let issued = self.issue_decided_action(request, decision).await?;
let source = source_of(&actor);
self.append_action_event(
print_id,
source,
EventPayload::ActionRequested(ActionRequestedPayload {
action_id: issued.record.id,
action: action.clone(),
actor: actor.clone(),
}),
)
.await?;
if let PolicyDecision::Rejected(_) = &issued.record.decision {
self.append_action_event(
print_id,
source,
EventPayload::ActionRejected(ActionRejectedPayload {
action_id: issued.record.id,
decision: issued.record.decision.clone(),
}),
)
.await?;
}
if !issued.succeeded() {
return Ok(ActionOutcome {
record: issued.record,
executed: issued.executed,
intervention: None,
});
}
if actor.class() == ActorClass::Agent {
self.note_agent_action(print_id, requested_at);
}
if let PrintAction::StartPrint { manifest, .. } = &action {
self.attach_manifest(print_id, manifest.clone()).await?;
}
let intervention = self
.open_bounded_intervention(print_id, &action, &issued, snapshot.as_ref(), requested_at)
.await?;
self.append_action_event(
print_id,
source,
EventPayload::ActionExecuted(ActionExecutedPayload {
action_id: issued.record.id,
intervention_id: intervention.as_ref().map(|opened| opened.id),
}),
)
.await?;
Ok(ActionOutcome {
record: issued.record,
executed: issued.executed,
intervention,
})
}
async fn append_action_event(
&self,
print_id: PrintId,
source: EventSource,
payload: EventPayload,
) -> Result<(), CoreError> {
self.store()
.append_event(printobserver_store_api::EventDraft {
print_id: Some(print_id),
source,
received_at: self.clock().now(),
payload,
raw: None,
})
.await?;
Ok(())
}
pub(crate) async fn bounds_for(
&self,
print: Option<&PrintRecord>,
print_id: PrintId,
) -> Result<Bounds, CoreError> {
let manifest = if print.is_some() {
self.store().manifest(print_id).await?
} else {
None
};
Ok(effective_bounds(&self.config().envelope, manifest.as_ref()))
}
async fn attach_manifest(
&self,
print_id: PrintId,
manifest: printobserver_types::JobManifest,
) -> Result<(), CoreError> {
let narrowed = effective_bounds(&self.config().envelope, Some(&manifest));
self.store().put_manifest(print_id, manifest).await?;
for narrowing in narrowed.narrowings {
self.store().record_narrowing(print_id, narrowing).await?;
}
Ok(())
}
async fn open_bounded_intervention(
&self,
print_id: PrintId,
action: &PrintAction,
issued: &Issued,
snapshot: Option<&PrinterSnapshot>,
applied_at: Timestamp,
) -> Result<Option<Intervention>, CoreError> {
let (Some((adjustable, applied_value)), Some(duration_s)) =
(adjustment(action), duration_of(action))
else {
return Ok(None);
};
let mut superseded = self.store().active_interventions(print_id).await?;
superseded.retain(|held| held.adjustable == adjustable);
superseded.sort_by_key(|held| held.applied_at);
let prior_value = superseded.first().map_or_else(
|| snapshot.and_then(|taken| reported_value(taken, adjustable)),
|earliest| earliest.prior_value,
);
let expires_at =
plus_seconds(applied_at, duration_s).map_err(|error| CoreError::Unrepresentable {
detail: error.to_string(),
})?;
let opened = self
.store()
.open_intervention(
issued.record.id,
adjustable,
prior_value,
applied_value,
applied_at,
expires_at,
)
.await?;
for earlier in superseded {
self.store()
.settle_intervention(
earlier.id,
InterventionOutcome::Superseded { by: opened.id },
)
.await?;
}
Ok(Some(opened))
}
}