1use crate::records::{
15 AcknowledgementDisposition, ActionId, Actor, InterventionId, InterventionOutcome,
16 PolicyDecision, PrintAction,
17};
18use printobserver_printer_api::Adjustable;
19use printobserver_supervisor_api::AgentAssessment;
20use printobserver_types::contract::Sample;
21use printobserver_types::schemars::JsonSchema;
22use printobserver_types::serde::{Deserialize, Serialize};
23use printobserver_types::{EventId, EventPayload, EventSource};
24
25pub const SYSTEM_SOURCE: &str = "system";
27
28pub const OPERATOR_SOURCE: &str = "operator";
30
31pub const AGENT_SOURCE: &str = "agent";
33
34#[must_use]
36pub fn system_source() -> EventSource {
37 EventSource::new(SYSTEM_SOURCE)
38}
39
40#[must_use]
42pub fn operator_source() -> EventSource {
43 EventSource::new(OPERATOR_SOURCE)
44}
45
46#[must_use]
48pub fn agent_source() -> EventSource {
49 EventSource::new(AGENT_SOURCE)
50}
51
52#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
54#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
55#[schemars(crate = "printobserver_types::schemars")]
56pub struct ActionRequestedPayload {
57 pub action_id: ActionId,
59 pub action: PrintAction,
61 pub actor: Actor,
63}
64
65impl EventPayload for ActionRequestedPayload {
66 const KIND: &'static str = "action_requested";
67}
68
69impl Sample for ActionRequestedPayload {
70 fn sample_full() -> Self {
71 Self {
72 action_id: ActionId::sample_full(),
73 action: PrintAction::sample_full(),
74 actor: Actor::sample_full(),
75 }
76 }
77
78 fn sample_minimal() -> Self {
79 Self {
80 action: PrintAction::sample_minimal(),
81 ..Self::sample_full()
82 }
83 }
84}
85
86#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
88#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
89#[schemars(crate = "printobserver_types::schemars")]
90pub struct ActionExecutedPayload {
91 pub action_id: ActionId,
93 #[serde(default, skip_serializing_if = "Option::is_none")]
95 pub intervention_id: Option<InterventionId>,
96}
97
98impl EventPayload for ActionExecutedPayload {
99 const KIND: &'static str = "action_executed";
100}
101
102impl Sample for ActionExecutedPayload {
103 fn sample_full() -> Self {
104 Self {
105 action_id: ActionId::sample_full(),
106 intervention_id: Some(InterventionId::sample_full()),
107 }
108 }
109
110 fn sample_minimal() -> Self {
111 Self {
112 action_id: ActionId::sample_full(),
113 intervention_id: None,
114 }
115 }
116}
117
118#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
120#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
121#[schemars(crate = "printobserver_types::schemars")]
122pub struct ActionRejectedPayload {
123 pub action_id: ActionId,
125 pub decision: PolicyDecision,
127}
128
129impl EventPayload for ActionRejectedPayload {
130 const KIND: &'static str = "action_rejected";
131}
132
133impl Sample for ActionRejectedPayload {
134 fn sample_full() -> Self {
135 Self {
136 action_id: ActionId::sample_full(),
137 decision: PolicyDecision::sample_full(),
138 }
139 }
140}
141
142#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
144#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
145#[schemars(crate = "printobserver_types::schemars")]
146pub struct InterventionExpiredPayload {
147 pub intervention_id: InterventionId,
149 pub adjustable: Adjustable,
151 pub outcome: InterventionOutcome,
153}
154
155impl EventPayload for InterventionExpiredPayload {
156 const KIND: &'static str = "intervention_expired";
157}
158
159impl Sample for InterventionExpiredPayload {
160 fn sample_full() -> Self {
161 Self {
162 intervention_id: InterventionId::sample_full(),
163 adjustable: Adjustable::Feedrate,
164 outcome: InterventionOutcome::Restored,
165 }
166 }
167}
168
169#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
171#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
172#[schemars(crate = "printobserver_types::schemars")]
173pub struct AgentAssessmentPayload {
174 pub session_name: String,
176 pub assessment: AgentAssessment,
178}
179
180impl EventPayload for AgentAssessmentPayload {
181 const KIND: &'static str = "agent_assessment";
182}
183
184impl Sample for AgentAssessmentPayload {
185 fn sample_full() -> Self {
186 Self {
187 session_name: "print-0191f0a0".to_owned(),
188 assessment: AgentAssessment::sample_full(),
189 }
190 }
191}
192
193#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
195#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
196#[schemars(crate = "printobserver_types::schemars")]
197pub struct OperatorAcknowledgementPayload {
198 pub acknowledged_event_id: EventId,
200 pub disposition: AcknowledgementDisposition,
202}
203
204impl EventPayload for OperatorAcknowledgementPayload {
205 const KIND: &'static str = "operator_acknowledgement";
206}
207
208impl Sample for OperatorAcknowledgementPayload {
209 fn sample_full() -> Self {
210 Self {
211 acknowledged_event_id: EventId::sample_full(),
212 disposition: AcknowledgementDisposition::Watch,
213 }
214 }
215}
216
217#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
226#[serde(
227 crate = "printobserver_types::serde",
228 rename_all = "snake_case",
229 deny_unknown_fields
230)]
231#[schemars(crate = "printobserver_types::schemars")]
232pub enum PortFailureSite {
233 PrinterSnapshot,
235 PrinterJob,
237 ImageWrite,
239 SupervisionTurn,
241}
242
243impl Sample for PortFailureSite {
244 fn sample_full() -> Self {
245 Self::PrinterSnapshot
246 }
247}
248
249#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
257#[serde(crate = "printobserver_types::serde", deny_unknown_fields)]
258#[schemars(crate = "printobserver_types::schemars")]
259pub struct PortFailurePayload {
260 pub event_id: EventId,
262 pub site: PortFailureSite,
264 pub detail: String,
266}
267
268impl EventPayload for PortFailurePayload {
269 const KIND: &'static str = "port_failure";
270}
271
272impl Sample for PortFailurePayload {
273 fn sample_full() -> Self {
274 Self {
275 event_id: EventId::sample_full(),
276 site: PortFailureSite::sample_full(),
277 detail: "the printer is unreachable: connection refused".to_owned(),
278 }
279 }
280}