libits-client 3.1.0

library to connect on an ITS MQTT server
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/*
 * Software Name : libits-client
 * SPDX-FileCopyrightText: Copyright (c) Orange SA
 * SPDX-License-Identifier: MIT
 *
 * This software is distributed under the MIT license,
 * see the "LICENSE.txt" file for more details or https://opensource.org/license/MIT/
 *
 * Authors: see CONTRIBUTORS.md
 */
use serde::{Deserialize, Serialize};

/// Represents a Shape according to an ETSI standard.
///
/// This message is used to describe a shape.
/// It implements the schema defined in the [CPM version 2.1.0][1].
///
/// [1]: https://github.com/Orange-OpenSource/its-client/blob/master/schema/cpm/cpm_schema_2-1-0.json#L1104
#[serde_with::skip_serializing_none]
#[derive(Default, Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct Shape {
    // optional fields
    /// Rectangular shape.
    pub rectangular: Option<Rectangular>,
    /// Circular shape.
    pub circular: Option<Circular>,
    /// Polygonal shape.
    pub polygonal: Option<Polygonal>,
    /// Elliptical shape.
    pub elliptical: Option<Elliptical>,
    /// Radial shape.
    pub radial: Option<Radial>,
    /// Radial shapes.
    pub radial_shapes: Option<RadialShapes>,
}

#[serde_with::skip_serializing_none]
#[derive(Default, Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct Rectangular {
    /// Semi-length and semi-breadth of the rectangular shape.
    pub semi_length: u8,
    /// Semi-breadth of the rectangular shape.
    pub semi_breadth: u8,

    // optional fields
    /// Center point of the rectangular shape.
    pub center_point: Option<CartesianPosition3D>,
    /// Orientation of the rectangular shape in centidegrees.
    pub orientation: Option<u16>,
    /// Height of the rectangular shape.
    pub height: Option<u16>,
}

#[serde_with::skip_serializing_none]
#[derive(Default, Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct Circular {
    /// Radius of the circular shape.
    pub radius: u16,

    // optional fields
    /// Reference point of the circular shape.
    pub shape_reference_point: Option<CartesianPosition3D>,
    /// Height of the circular shape.
    pub height: Option<u16>,
}

#[serde_with::skip_serializing_none]
#[derive(Default, Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct Polygonal {
    /// Polygon represented as a list of Cartesian positions.
    pub polygon: Vec<CartesianPosition3D>,

    // optional fields
    /// Reference point of the polygonal shape.
    pub shape_reference_point: Option<CartesianPosition3D>,
    /// Height of the polygonal shape.
    pub height: Option<u16>,
}

#[serde_with::skip_serializing_none]
#[derive(Default, Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct Elliptical {
    /// Semi-major axis length of the elliptical shape.
    pub semi_major_axis_length: u16,
    /// Semi-minor axis length of the elliptical shape.
    pub semi_minor_axis_length: u16,

    // optional fields
    /// Reference point of the elliptical shape.
    pub shape_reference_point: Option<CartesianPosition3D>,
    /// Orientation of the elliptical shape in centidegrees.
    pub orientation: Option<u16>,
    /// Height of the elliptical shape.
    pub height: Option<u16>,
}

#[serde_with::skip_serializing_none]
#[derive(Default, Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct RadialShapes {
    /// Reference point ID for the radial shapes.
    pub ref_point_id: u8,
    /// X coordinate of the reference point.
    pub x_coordinate: i16,
    /// Y coordinate of the reference point.
    pub y_coordinate: i16,
    /// Z coordinate of the reference point, if available.
    pub radial_shapes_list: Vec<Radial>,

    // optional fields
    /// Z coordinate of the reference point.
    pub z_coordinate: Option<i16>,
}

#[serde_with::skip_serializing_none]
#[derive(Default, Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct Radial {
    /// Range of the radial shape.
    pub range: u16,
    /// Start angle of the stationary horizontal opening in centidegrees.
    pub stationary_horizontal_opening_angle_start: u16,
    /// End angle of the stationary horizontal opening in centidegrees.
    pub stationary_horizontal_opening_angle_end: u16,

    // optional fields
    /// Reference point of the radial shape.
    pub shape_reference_point: Option<CartesianPosition3D>,
    /// Start angle of the vertical opening in centidegrees.
    pub vertical_opening_angle_start: Option<u16>,
    /// End angle of the vertical opening in centidegrees.
    pub vertical_opening_angle_end: Option<u16>,
}

#[serde_with::skip_serializing_none]
#[derive(Default, Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct CartesianPosition3D {
    /// X coordinate in the Cartesian position.
    pub x_coordinate: i16,
    /// Y coordinate in the Cartesian position.
    pub y_coordinate: i16,

    // optional field
    /// Z coordinate in the Cartesian position, if available.
    pub z_coordinate: Option<i16>,
}

#[cfg(test)]
mod tests {
    use crate::exchange::etsi::shape::{CartesianPosition3D, Radial, Shape};

    #[test]
    fn test_deserialize_cartesian_position_3d() {
        let data = r#"{
            "x_coordinate": 32767,
            "y_coordinate": -32768,
            "z_coordinate": 0
        }"#;

        match serde_json::from_str::<CartesianPosition3D>(data) {
            Ok(object) => {
                assert_eq!(object.x_coordinate, 32767);
                assert_eq!(object.y_coordinate, -32768);
                assert_eq!(object.z_coordinate, Some(0));
            }
            Err(e) => panic!("Failed to deserialize a CartesianPosition3D: '{e}'"),
        }
    }

    #[test]
    fn test_deserialize_radial() {
        let data = r#"{
            "range": 32767,
            "stationary_horizontal_opening_angle_start": 3601,
            "stationary_horizontal_opening_angle_end": 0,
            "shape_reference_point": {
                "x_coordinate": 32767,
                "y_coordinate": -32768,
                "z_coordinate": 0
            },
            "vertical_opening_angle_start": 1300,
            "vertical_opening_angle_end": 650
        }"#;

        match serde_json::from_str::<Radial>(data) {
            Ok(object) => {
                assert_eq!(object.range, 32767);
                assert_eq!(object.stationary_horizontal_opening_angle_start, 3601);
                assert_eq!(object.stationary_horizontal_opening_angle_end, 0);
                assert_eq!(
                    object.shape_reference_point,
                    Some(CartesianPosition3D {
                        x_coordinate: 32767,
                        y_coordinate: -32768,
                        z_coordinate: Some(0),
                    })
                );
                assert_eq!(object.vertical_opening_angle_start, Some(1300));
                assert_eq!(object.vertical_opening_angle_end, Some(650));
            }
            Err(e) => panic!("Failed to deserialize a Radial: '{e}'"),
        }
    }

    #[test]
    fn test_deserialize_rectangular_shape() {
        let data = r#"{
        "rectangular": {
            "center_point": {
                "x_coordinate": 32767,
                "y_coordinate": -32768,
                "z_coordinate": 0
            },
            "semi_length": 102,
            "semi_breadth": 0,
            "orientation": 3601,
            "height": 4095
        }
    }"#;

        match serde_json::from_str::<Shape>(data) {
            Ok(object) => {
                let rectangular = object.rectangular.unwrap();
                assert_eq!(rectangular.semi_length, 102);
                assert_eq!(rectangular.semi_breadth, 0);
                assert_eq!(
                    rectangular.center_point,
                    Some(CartesianPosition3D {
                        x_coordinate: 32767,
                        y_coordinate: -32768,
                        z_coordinate: Some(0),
                    })
                );
                assert_eq!(rectangular.orientation, Some(3601));
                assert_eq!(rectangular.height, Some(4095));
            }
            Err(e) => panic!("Failed to deserialize a rectangular Shape: '{e}'"),
        }
    }

    #[test]
    fn test_deserialize_circular_shape() {
        let data = r#"{
        "circular": {
            "radius": 4095,
            "shape_reference_point": {
                "x_coordinate": 32767,
                "y_coordinate": -32768,
                "z_coordinate": 0
            },
            "height": 2048
        }
    }"#;

        match serde_json::from_str::<Shape>(data) {
            Ok(object) => {
                let circular = object.circular.unwrap();
                assert_eq!(circular.radius, 4095);
                assert_eq!(
                    circular.shape_reference_point,
                    Some(CartesianPosition3D {
                        x_coordinate: 32767,
                        y_coordinate: -32768,
                        z_coordinate: Some(0),
                    })
                );
                assert_eq!(circular.height, Some(2048));
            }
            Err(e) => panic!("Failed to deserialize a circular Shape: '{e}'"),
        }
    }

    #[test]
    fn test_deserialize_polygonal_shape() {
        let data = r#"{
        "polygonal": {
            "polygon": [
                {
                    "x_coordinate": 32767,
                    "y_coordinate": -32768,
                    "z_coordinate": 0
                },
                {
                    "x_coordinate": 16384,
                    "y_coordinate": -16384,
                    "z_coordinate": 0
                }
            ],
            "shape_reference_point": {
                "x_coordinate": 32767,
                "y_coordinate": -32768,
                "z_coordinate": 0
            },
            "height": 2048
        }
    }"#;

        match serde_json::from_str::<Shape>(data) {
            Ok(object) => {
                let polygonal = object.polygonal.unwrap();
                assert_eq!(polygonal.polygon.len(), 2);
                assert_eq!(
                    polygonal.polygon[0],
                    CartesianPosition3D {
                        x_coordinate: 32767,
                        y_coordinate: -32768,
                        z_coordinate: Some(0),
                    }
                );
                assert_eq!(
                    polygonal.polygon[1],
                    CartesianPosition3D {
                        x_coordinate: 16384,
                        y_coordinate: -16384,
                        z_coordinate: Some(0),
                    }
                );
                assert_eq!(
                    polygonal.shape_reference_point,
                    Some(CartesianPosition3D {
                        x_coordinate: 32767,
                        y_coordinate: -32768,
                        z_coordinate: Some(0),
                    })
                );
                assert_eq!(polygonal.height, Some(2048));
            }
            Err(e) => panic!("Failed to deserialize a polygonal Shape: '{e}'"),
        }
    }

    #[test]
    fn test_deserialize_elliptical_shape() {
        let data = r#"{
        "elliptical": {
            "semi_major_axis_length": 4095,
            "semi_minor_axis_length": 2048,
            "shape_reference_point": {
                "x_coordinate": 32767,
                "y_coordinate": -32768,
                "z_coordinate": 0
            },
            "orientation": 3601,
            "height": 1024
        }
    }"#;

        match serde_json::from_str::<Shape>(data) {
            Ok(object) => {
                let elliptical = object.elliptical.unwrap();
                assert_eq!(elliptical.semi_major_axis_length, 4095);
                assert_eq!(elliptical.semi_minor_axis_length, 2048);
                assert_eq!(
                    elliptical.shape_reference_point,
                    Some(CartesianPosition3D {
                        x_coordinate: 32767,
                        y_coordinate: -32768,
                        z_coordinate: Some(0),
                    })
                );
                assert_eq!(elliptical.orientation, Some(3601));
                assert_eq!(elliptical.height, Some(1024));
            }
            Err(e) => panic!("Failed to deserialize an elliptical Shape: '{e}'"),
        }
    }

    #[test]
    fn test_deserialize_radial_shape() {
        let data = r#"{
        "radial": {
            "range": 4095,
            "stationary_horizontal_opening_angle_start": 900,
            "stationary_horizontal_opening_angle_end": 2700,
            "shape_reference_point": {
                "x_coordinate": 32767,
                "y_coordinate": -32768,
                "z_coordinate": 0
            },
            "vertical_opening_angle_start": 1300,
            "vertical_opening_angle_end": 650
        }
    }"#;

        match serde_json::from_str::<Shape>(data) {
            Ok(object) => {
                let radial = object.radial.unwrap();
                assert_eq!(radial.range, 4095);
                assert_eq!(radial.stationary_horizontal_opening_angle_start, 900);
                assert_eq!(radial.stationary_horizontal_opening_angle_end, 2700);
                assert_eq!(
                    radial.shape_reference_point,
                    Some(CartesianPosition3D {
                        x_coordinate: 32767,
                        y_coordinate: -32768,
                        z_coordinate: Some(0),
                    })
                );
                assert_eq!(radial.vertical_opening_angle_start, Some(1300));
                assert_eq!(radial.vertical_opening_angle_end, Some(650));
            }
            Err(e) => panic!("Failed to deserialize a radial Shape: '{e}'"),
        }
    }

    #[test]
    fn test_deserialize_radial_shapes_shape() {
        let data = r#"{
        "radial_shapes": {
            "ref_point_id": 255,
            "x_coordinate": 1001,
            "y_coordinate": -3094,
            "z_coordinate": 1047,
            "radial_shapes_list": [
                {
                    "range": 4095,
                    "stationary_horizontal_opening_angle_start": 900,
                    "stationary_horizontal_opening_angle_end": 2700
                },
                {
                    "range": 2048,
                    "stationary_horizontal_opening_angle_start": 0,
                    "stationary_horizontal_opening_angle_end": 1800
                }
            ]
        }
    }"#;

        match serde_json::from_str::<Shape>(data) {
            Ok(object) => {
                let radial_shapes = object.radial_shapes.unwrap();
                assert_eq!(radial_shapes.ref_point_id, 255);
                assert_eq!(radial_shapes.x_coordinate, 1001);
                assert_eq!(radial_shapes.y_coordinate, -3094);
                assert_eq!(radial_shapes.z_coordinate, Some(1047));
                assert_eq!(radial_shapes.radial_shapes_list.len(), 2);
                assert_eq!(radial_shapes.radial_shapes_list[0].range, 4095);
                assert_eq!(
                    radial_shapes.radial_shapes_list[0].stationary_horizontal_opening_angle_start,
                    900
                );
                assert_eq!(
                    radial_shapes.radial_shapes_list[0].stationary_horizontal_opening_angle_end,
                    2700
                );
                assert_eq!(radial_shapes.radial_shapes_list[1].range, 2048);
                assert_eq!(
                    radial_shapes.radial_shapes_list[1].stationary_horizontal_opening_angle_start,
                    0
                );
                assert_eq!(
                    radial_shapes.radial_shapes_list[1].stationary_horizontal_opening_angle_end,
                    1800
                );
            }
            Err(e) => panic!("Failed to deserialize a radial shapes Shape: '{e}'"),
        }
    }
}