open_protocol/messages/
mode.rs

1use chrono::{DateTime, Local};
2use open_protocol_codec_proc_macro::{OpenProtocolDecode, OpenProtocolEncode, OpenProtocolMessage};
3
4#[derive(Debug, Default, Eq, PartialEq, OpenProtocolEncode, OpenProtocolDecode, OpenProtocolMessage)]
5#[open_protocol_message(MID = 2600, revision = 1)]
6pub struct MID2600rev1 {
7}
8
9/// Contains the list of all modes in the controller.
10#[derive(Debug, Default, Eq, PartialEq, OpenProtocolEncode, OpenProtocolDecode, OpenProtocolMessage)]
11#[open_protocol_message(MID = 2601, revision = 1)]
12pub struct MID2601rev1 {
13    /// Number of modes in the controller
14    #[open_protocol_field(length = 3)]
15    pub number_of_modes: u16,
16
17    /// List of mode data entries
18    #[open_protocol_field(list, amount = "number_of_modes")]
19    pub mode_data: Vec<ModeData>,
20}
21
22/// Mode details within a mode list.
23#[derive(Debug, Default, Eq, PartialEq, OpenProtocolEncode, OpenProtocolDecode)]
24pub struct ModeData {
25    /// Mode ID
26    #[open_protocol_field(length = 4)]
27    pub mode_id: u32,
28
29    /// Length of the mode name
30    #[open_protocol_field(length = 2)]
31    pub mode_name_size: u16,
32
33    /// The name of the mode (variable length, depending on `mode_name_size`)
34    #[open_protocol_field(length = "mode_name_size")]
35    pub mode_name: String,
36}
37
38/// Requests detailed mode data for a specific mode ID.
39#[derive(Debug, Default, Eq, PartialEq, OpenProtocolEncode, OpenProtocolDecode, OpenProtocolMessage)]
40#[open_protocol_message(MID = 2602, revision = 1)]
41pub struct MID2602rev1 {
42    /// Mode ID of the mode to request
43    #[open_protocol_field(number = 1, length = 4)]
44    pub mode_id: u32,
45}
46
47/// Provides detailed information about a mode, including its bolts.
48#[derive(Debug, Default, Eq, PartialEq, OpenProtocolEncode, OpenProtocolDecode, OpenProtocolMessage)]
49#[open_protocol_message(MID = 2603, revision = 1)]
50pub struct MID2603rev1 {
51    /// Mode ID
52    #[open_protocol_field(length = 4)]
53    pub mode_id: u32,
54
55    /// Length of the mode name
56    #[open_protocol_field(length = 2)]
57    pub mode_name_size: u16,
58
59    /// The name of the mode
60    #[open_protocol_field(length = "mode_name_size")]
61    pub mode_name: String,
62
63    /// Number of bolts in the mode
64    #[open_protocol_field(length = 3)]
65    pub number_of_bolts: u16,
66
67    /// List of bolts assigned to the mode
68    #[open_protocol_field(list, amount = "number_of_bolts")]
69    pub bolt_data: Vec<BoltData>,
70}
71
72/// Contains details about a bolt in a mode.
73#[derive(Debug, Default, Eq, PartialEq, OpenProtocolEncode, OpenProtocolDecode)]
74pub struct BoltData {
75    /// Parameter set ID
76    #[open_protocol_field(length = 3)]
77    pub pset_id: u16,
78
79    /// Tool number
80    #[open_protocol_field(length = 3)]
81    pub tool_number: u16,
82
83    /// Bolt number
84    #[open_protocol_field(length = 4)]
85    pub bolt_number: u32,
86
87    /// Length of the bolt name
88    #[open_protocol_field(length = 2)]
89    pub bolt_name_size: u16,
90
91    /// The name of the bolt
92    #[open_protocol_field(length = "bolt_name_size")]
93    pub bolt_name: String,
94}
95
96/// Confirms mode selection and provides metadata.
97#[derive(Debug, Default, Eq, PartialEq, OpenProtocolEncode, OpenProtocolDecode, OpenProtocolMessage)]
98#[open_protocol_message(MID = 2604, revision = 1)]
99pub struct MID2604rev1 {
100    /// Mode ID of the selected mode
101    #[open_protocol_field(number = 1, length = 4)]
102    pub mode_id: u32,
103
104    /// Date of the last change in the mode setting (format YYYY-MM-DD:HH:MM:SS)
105    #[open_protocol_field(number = 2, length = 19)]
106    pub last_change_date: DateTime<Local>,
107
108    /// Number of bolts in the mode
109    #[open_protocol_field(number = 3, length = 3)]
110    pub number_of_bolts: u16,
111}
112
113/// Acknowledgment of mode selection.
114#[derive(Debug, Default, Eq, PartialEq, OpenProtocolEncode, OpenProtocolDecode, OpenProtocolMessage)]
115#[open_protocol_message(MID = 2605, revision = 1)]
116pub struct MID2605rev1 {
117}
118
119/// Requests mode selection by ID.
120#[derive(Debug, Default, Eq, PartialEq, OpenProtocolEncode, OpenProtocolDecode, OpenProtocolMessage)]
121#[open_protocol_message(MID = 2606, revision = 1)]
122pub struct MID2606rev1 {
123    /// Mode ID to be selected
124    #[open_protocol_field(number = 1, length = 4)]
125    pub mode_id: u32,
126}
127
128#[cfg(test)]
129mod tests {
130    use super::*;
131    use open_protocol_codec::{decode, encode};
132
133    #[test]
134    fn encode_2601() {
135        let mode_name_1 = "Hello";
136        let mode_name_2 = "World";
137
138        let obj = MID2601rev1 {
139            number_of_modes: 2,
140            mode_data: vec![
141                ModeData {
142                    mode_id: 1,
143                    mode_name_size: mode_name_1.len() as u16,
144                    mode_name: mode_name_1.into(),
145                },
146                ModeData {
147                    mode_id: 2,
148                    mode_name_size: mode_name_2.len() as u16,
149                    mode_name: mode_name_2.into(),
150                }
151            ]
152        };
153
154        let encoded = encode::encode(&obj);
155
156        assert_eq!(encoded, Ok("002000105Hello000205World".into()));
157    }
158
159    #[test]
160    fn decode_2601() {
161        let mode_name_1 = "Hello";
162        let mode_name_2 = "World";
163
164        let str = "002000105Hello000205World";
165
166        let decoded: decode::Result<MID2601rev1> = decode::decode(str.as_bytes());
167
168
169        let obj = MID2601rev1 {
170            number_of_modes: 2,
171            mode_data: vec![
172                ModeData {
173                    mode_id: 1,
174                    mode_name_size: mode_name_1.len() as u16,
175                    mode_name: mode_name_1.into(),
176                },
177                ModeData {
178                    mode_id: 2,
179                    mode_name_size: mode_name_2.len() as u16,
180                    mode_name: mode_name_2.into(),
181                }
182            ]
183        };
184
185        assert_eq!(decoded, Ok(obj));
186    }
187}