lotus_script/message/types.rs
1use serde::{Deserialize, Serialize};
2
3use super::MessageType;
4
5/// Represents an event triggered by a sensor in the system.
6///
7/// A trigger event occurs when an object enters or leaves a sensor's detection area.
8/// Each sensor has a unique index and can detect both entry and exit events.
9#[derive(Debug, Serialize, Deserialize)]
10pub struct TriggerEvent {
11 /// Unique identifier for the trigger event
12 pub id: String,
13 /// Index of the sensor that generated this event
14 pub sensor_index: i32,
15 /// The type of trigger (Enter or Leave)
16 pub kind: TriggerKind,
17}
18
19impl TriggerEvent {
20 /// Returns true if this is an entry event
21 pub fn is_enter(&self) -> bool {
22 self.kind.is_enter()
23 }
24
25 /// Returns true if this is a leave event
26 pub fn is_leave(&self) -> bool {
27 self.kind.is_leave()
28 }
29}
30
31impl MessageType for TriggerEvent {
32 fn id() -> &'static str {
33 "builtin:trigger_event"
34 }
35}
36
37/// Represents the type of trigger event that occurred.
38#[derive(Debug, Serialize, Deserialize)]
39pub enum TriggerKind {
40 /// Indicates an object entered the sensor's detection area
41 Enter,
42 /// Indicates an object left the sensor's detection area
43 Leave,
44}
45
46impl TriggerKind {
47 /// Returns true if this trigger represents an entry event
48 pub fn is_enter(&self) -> bool {
49 matches!(self, Self::Enter)
50 }
51
52 /// Returns true if this trigger represents a leave event
53 pub fn is_leave(&self) -> bool {
54 matches!(self, Self::Leave)
55 }
56}
57
58/// Represents a button press or release event in the cockpit.
59///
60/// Button events capture the state changes of buttons in different
61/// cockpit positions, tracking whether they are pressed or released.
62#[derive(Debug, Serialize, Deserialize)]
63pub struct ButtonEvent {
64 /// Unique identifier for the button event
65 pub id: String,
66 /// Current state of the button (true = pressed, false = released)
67 pub value: bool,
68 /// Index identifying the cockpit position of this button
69 pub cockpit_index: u8,
70}
71
72impl MessageType for ButtonEvent {
73 fn id() -> &'static str {
74 "builtin:button_event"
75 }
76}
77
78/// Represents the state of the battery switch.
79///
80/// A simple wrapper around a boolean value indicating whether
81/// the battery is switched on (true) or off (false).
82#[derive(Debug, Serialize, Deserialize)]
83pub struct BatterySwitch(pub bool);
84
85impl MessageType for BatterySwitch {
86 fn id() -> &'static str {
87 "builtin:battery_switch"
88 }
89}