use serde::{Deserialize, Serialize};
use schemars::JsonSchema;
use crate::file_name::FileName;
use crate::ids::{ActionId, EventId, PrintId};
use crate::manifest::JobManifest;
use crate::policy::PolicyDecision;
use crate::timestamp::Timestamp;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub enum Actor {
Agent {
session_name: String,
},
Operator,
System,
}
impl Actor {
#[must_use]
pub const fn class(&self) -> ActorClass {
match self {
Self::Agent { .. } => ActorClass::Agent,
Self::Operator => ActorClass::Operator,
Self::System => ActorClass::System,
}
}
}
#[derive(
Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, JsonSchema,
)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub enum ActorClass {
Agent,
Operator,
System,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub enum AcknowledgementDisposition {
Continue,
Watch,
Stop,
}
#[derive(
Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, JsonSchema,
)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub enum ActionKind {
Pause,
Resume,
Cancel,
StartPrint,
SetFeedrateFactor,
SetFlowrateFactor,
SetToolTargetC,
SetBedTargetC,
SetFanPercent,
AcknowledgeFailure,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "action", rename_all = "snake_case", deny_unknown_fields)]
pub enum PrintAction {
Pause {
reason: String,
actor: Actor,
},
Resume {
reason: String,
actor: Actor,
},
Cancel {
reason: String,
actor: Actor,
},
StartPrint {
file_name: FileName,
manifest: JobManifest,
reason: String,
actor: Actor,
},
SetFeedrateFactor {
factor: f64,
#[serde(default, skip_serializing_if = "Option::is_none")]
duration_s: Option<i64>,
reason: String,
actor: Actor,
},
SetFlowrateFactor {
factor: f64,
#[serde(default, skip_serializing_if = "Option::is_none")]
duration_s: Option<i64>,
reason: String,
actor: Actor,
},
SetToolTargetC {
tool: i64,
target_c: f64,
#[serde(default, skip_serializing_if = "Option::is_none")]
duration_s: Option<i64>,
reason: String,
actor: Actor,
},
SetBedTargetC {
target_c: f64,
#[serde(default, skip_serializing_if = "Option::is_none")]
duration_s: Option<i64>,
reason: String,
actor: Actor,
},
SetFanPercent {
percent: f64,
#[serde(default, skip_serializing_if = "Option::is_none")]
duration_s: Option<i64>,
reason: String,
actor: Actor,
},
AcknowledgeFailure {
event_id: EventId,
disposition: AcknowledgementDisposition,
reason: String,
actor: Actor,
},
}
impl PrintAction {
#[must_use]
pub const fn kind(&self) -> ActionKind {
match self {
Self::Pause { .. } => ActionKind::Pause,
Self::Resume { .. } => ActionKind::Resume,
Self::Cancel { .. } => ActionKind::Cancel,
Self::StartPrint { .. } => ActionKind::StartPrint,
Self::SetFeedrateFactor { .. } => ActionKind::SetFeedrateFactor,
Self::SetFlowrateFactor { .. } => ActionKind::SetFlowrateFactor,
Self::SetToolTargetC { .. } => ActionKind::SetToolTargetC,
Self::SetBedTargetC { .. } => ActionKind::SetBedTargetC,
Self::SetFanPercent { .. } => ActionKind::SetFanPercent,
Self::AcknowledgeFailure { .. } => ActionKind::AcknowledgeFailure,
}
}
#[must_use]
pub fn reason(&self) -> &str {
match self {
Self::Pause { reason, .. }
| Self::Resume { reason, .. }
| Self::Cancel { reason, .. }
| Self::StartPrint { reason, .. }
| Self::SetFeedrateFactor { reason, .. }
| Self::SetFlowrateFactor { reason, .. }
| Self::SetToolTargetC { reason, .. }
| Self::SetBedTargetC { reason, .. }
| Self::SetFanPercent { reason, .. }
| Self::AcknowledgeFailure { reason, .. } => reason,
}
}
#[must_use]
pub const fn actor(&self) -> &Actor {
match self {
Self::Pause { actor, .. }
| Self::Resume { actor, .. }
| Self::Cancel { actor, .. }
| Self::StartPrint { actor, .. }
| Self::SetFeedrateFactor { actor, .. }
| Self::SetFlowrateFactor { actor, .. }
| Self::SetToolTargetC { actor, .. }
| Self::SetBedTargetC { actor, .. }
| Self::SetFanPercent { actor, .. }
| Self::AcknowledgeFailure { actor, .. } => actor,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub enum ExecutionOutcome {
Succeeded,
Failed {
reason: String,
},
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct ActionRequest {
pub action: PrintAction,
pub actor: Actor,
pub requested_at: Timestamp,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct ActionRecord {
pub id: ActionId,
pub print_id: PrintId,
pub request: ActionRequest,
pub decision: PolicyDecision,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub executed_at: Option<Timestamp>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub outcome: Option<ExecutionOutcome>,
}