signalk 0.7.0

A library to parse signalk maritime data
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
use log::debug;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

use crate::communication::V1Communication;
use crate::design::V1Design;
use crate::electrical::V1Electrical;
use crate::environment::V1Environment;
use crate::full::Updatable;
use crate::helper_functions::{get_f64_value_for_path, get_path, Path};
use crate::notification::V1Notification;
use crate::performance::V1Performance;
use crate::steering::V1Steering;
use crate::{SignalKGetError, V1Navigation, V1Propulsion, V1UpdateType};

/// An object describing an individual vessel. It should be an object in vessels,
/// named using MMSI or a UUID
#[derive(Serialize, Deserialize, PartialEq, Debug, Default, Clone)]
#[serde(rename_all = "camelCase")]
pub struct V1Vessel {
    /// MMSI number of the vessel, if available.
    pub mmsi: Option<String>,

    /// URL based identity of the vessel, if available.
    pub url: Option<String>,

    /// A unique Signal K flavoured maritime resource identifier, assigned by the server.
    pub uuid: Option<String>,

    /// MMSI number of the mothership of this vessel, if available.
    pub mothership_mmsi: Option<String>,

    /// The common name of the vessel
    pub name: Option<String>,

    /// The home port of the vessel
    pub port: Option<String>,

    /// The country of ship registration, or flag state of the vessel
    pub flag: Option<String>,

    /// Navigation data including Position, Course to next WP information, etc.
    pub navigation: Option<V1Navigation>,

    // pub registrations: Option<HashMap<String, V1Registration>>,
    pub communication: Option<V1Communication>,
    /// Environmental data measured locally including Depth, Wind, Temp, etc.
    pub environment: Option<V1Environment>,

    /// Electrical data, each electrical device indentified by a unique name i.e. Battery_1
    pub electrical: Option<V1Electrical>,

    /// Notifications currently raised. Major categories have well-defined names, but the tree can be extended by any hierarchical structure
    // pub notifications: Option<V1Notification>,
    pub steering: Option<V1Steering>,
    // pub tanks: Option<V1Tanks>,
    pub design: Option<V1Design>,
    // pub sails: Option<V1Sails>,
    // pub sensors: Option<V1Sensors>,
    pub performance: Option<V1Performance>,
    /// Engine data, each engine identified by a unique name i.e. Port_Engine
    pub propulsion: Option<HashMap<String, V1Propulsion>>,
}

impl Path<f64> for V1Vessel {
    fn get_path(&self, path: &[&str]) -> Result<f64, SignalKGetError> {
        debug!("get_path({:?})", path);
        match path[0] {
            "mmsi" => Err(SignalKGetError::WrongDataType),
            "url" => Err(SignalKGetError::WrongDataType),
            "uuid" => Err(SignalKGetError::WrongDataType),
            "mothershipMmsi" => Err(SignalKGetError::WrongDataType),
            "name" => Err(SignalKGetError::WrongDataType),
            "port" => Err(SignalKGetError::WrongDataType),
            "flag" => Err(SignalKGetError::WrongDataType),
            "navigation" => get_path(path, &(self.navigation.as_ref())),
            "communication" => Err(SignalKGetError::TBD),
            "environment" => get_path(path, &(self.environment.as_ref())),
            "notifications" => Err(SignalKGetError::TBD),
            "electrical" => Err(SignalKGetError::TBD),
            "steering" => Err(SignalKGetError::TBD),
            "tanks" => Err(SignalKGetError::TBD),
            "design" => Err(SignalKGetError::TBD),
            "sails" => Err(SignalKGetError::TBD),
            "sensors" => Err(SignalKGetError::TBD),
            "performance" => Err(SignalKGetError::TBD),
            "propulsion" => Err(SignalKGetError::TBD),

            &_ => Err(SignalKGetError::NoSuchPath),
        }
    }
}

impl Updatable for V1Vessel {
    fn apply_update(&mut self, update: &V1UpdateType) {
        log::debug!("Apply update: {:?}", update);
        if let Some(ref values) = update.values {
            for value in values.iter() {
                let mut path: Vec<&str> = value.path.split('.').collect();
                self.update(&mut path, &value.value);
            }
        }
    }

    fn id(&self) -> String {
        if let Some(ref id) = self.mmsi {
            return id.clone();
        }
        if let Some(ref id) = self.uuid {
            return id.clone();
        }
        "".into()
    }

    fn type_name(&self) -> String {
        "V1Vessel".to_string()
    }
}

impl V1Vessel {
    pub fn builder() -> V1VesselBuilder {
        V1VesselBuilder::default()
    }

    pub fn new_with_id(id: &str) -> Self {
        let id_parts: Vec<&str> = id.split(':').collect();
        if id_parts.len() != 5 && id_parts[0] != "urn" && id_parts[1] != "mrn" {
            Self::default()
        } else if id_parts[2] == "signalk" {
            Self::builder().uuid(id_parts[4].to_string()).build()
        } else if id_parts[2] == "imo" {
            Self::builder().mmsi(id_parts[4].to_string()).build()
        } else {
            Self::default()
        }
    }
    pub fn update(&mut self, path: &mut Vec<&str>, value: &serde_json::value::Value) {
        if path.is_empty() {
            return;
        }
        match path[0] {
            "mmsi" => {
                if let serde_json::Value::String(ref string) = value {
                    self.mmsi = Some(string.to_string());
                }
            }
            "url" => {
                if let serde_json::Value::String(ref string) = value {
                    self.url = Some(string.to_string());
                }
            }
            "uuid" => {
                if let serde_json::Value::String(ref string) = value {
                    self.uuid = Some(string.to_string());
                }
            }
            "mothership_mmsi" => {
                if let serde_json::Value::String(ref string) = value {
                    self.mothership_mmsi = Some(string.to_string());
                }
            }
            "name" => {
                if let serde_json::Value::String(ref string) = value {
                    self.name = Some(string.to_string());
                }
            }
            "port" => {
                if let serde_json::Value::String(ref string) = value {
                    self.port = Some(string.to_string());
                }
            }
            "flag" => {
                if let serde_json::Value::String(ref string) = value {
                    self.flag = Some(string.to_string());
                }
            }
            "navigation" => {
                if self.navigation.is_none() {
                    self.navigation = Some(V1Navigation::default());
                }
                if let Some(ref mut navigation) = self.navigation {
                    path.remove(0);
                    navigation.update(path, value);
                }
            }
            "environment" => {
                if self.environment.is_none() {
                    self.environment = Some(V1Environment::default());
                }
                if let Some(ref mut environment) = self.environment {
                    path.remove(0);
                    environment.update(path, value);
                }
            }
            "electrical" => {
                if self.electrical.is_none() {
                    self.electrical = Some(V1Electrical::default());
                }
                if let Some(ref mut electrical) = self.electrical {
                    path.remove(0);
                    electrical.update(path, value);
                }
            }
            "communication" => {
                if self.communication.is_none() {
                    self.communication = Some(V1Communication::default());
                }
                if let Some(ref mut communication) = self.communication {
                    path.remove(0);
                    communication.update(path, value);
                }
            }
            "design" => {
                if self.design.is_none() {
                    self.design = Some(V1Design::default());
                }
                if let Some(ref mut design) = self.design {
                    path.remove(0);
                    design.update(path, value);
                }
            }
            "performance" => {
                if self.performance.is_none() {
                    self.performance = Some(V1Performance::default());
                }
                if let Some(ref mut performance) = self.performance {
                    path.remove(0);
                    performance.update(path, value);
                }
            }
            "" => {
                log::debug!("root path to vessel: {:?}::{:?}", path, value);
                if let serde_json::Value::Object(ref map) = value {
                    for (k, v) in map.iter() {
                        log::debug!(" key: {:?} value: {:?}", k, v);
                        let mut path = vec![k.as_str()];
                        self.update(&mut path, v);
                    }
                }
            }
            &_ => {
                log::warn!("V1Vessel: Unknown update pattern: {:?}::{:?}", path, value);
            }
        }
    }

    pub fn get_f64_for_path(&self, path: &mut Vec<&str>) -> Result<f64, SignalKGetError> {
        debug!("get_f64_for_path({:?})", path);
        match path[0] {
            "mmsi" => Err(SignalKGetError::WrongDataType),
            "url" => Err(SignalKGetError::WrongDataType),
            "uuid" => Err(SignalKGetError::WrongDataType),
            "mothership_mmsi" => Err(SignalKGetError::WrongDataType),
            "name" => Err(SignalKGetError::WrongDataType),
            "port" => Err(SignalKGetError::WrongDataType),
            "flag" => Err(SignalKGetError::WrongDataType),
            "navigation" => get_path(path, &self.navigation.as_ref()),
            "environment" => {
                if let Some(ref environment) = self.environment {
                    path.remove(0);
                    environment.get_f64_for_path(path)
                } else {
                    Err(SignalKGetError::NoSuchPath)
                }
            }
            "electrical" => {
                log::info!("get: path {:?}", path);
                if let Some(ref electrical) = self.electrical {
                    path.remove(0);
                    electrical.get_f64_for_path(path)
                } else {
                    Err(SignalKGetError::NoSuchPath)
                }
            }
            "notifications" => Err(SignalKGetError::TBD),
            "steering" => get_f64_value_for_path(path, &self.steering),
            "tanks" => Err(SignalKGetError::TBD),
            "design" => Err(SignalKGetError::TBD),
            "sails" => Err(SignalKGetError::TBD),
            "sensors" => Err(SignalKGetError::TBD),
            "performance" => get_f64_value_for_path(path, &self.performance),
            "propulsion" => Err(SignalKGetError::TBD),
            &_ => Err(SignalKGetError::NoSuchPath),
        }
        // Ok(5.1)
    }
}

#[derive(Default)]
pub struct V1VesselBuilder {
    mmsi: Option<String>,
    url: Option<String>,
    uuid: Option<String>,
    mothership_mmsi: Option<String>,
    name: Option<String>,
    flag: Option<String>,
    port: Option<String>,
    navigation: Option<V1Navigation>,
    communication: Option<V1Communication>,
    environment: Option<V1Environment>,
    electrical: Option<V1Electrical>,
    notifications: Option<V1Notification>,
    propulsion: Option<HashMap<String, V1Propulsion>>,
    steering: Option<V1Steering>,
    design: Option<V1Design>,
    performance: Option<V1Performance>,
}

impl V1VesselBuilder {
    pub fn uuid(mut self, value: String) -> V1VesselBuilder {
        self.uuid = Some(value);
        self
    }
    pub fn url(mut self, value: String) -> V1VesselBuilder {
        self.url = Some(value);
        self
    }
    pub fn mmsi(mut self, value: String) -> V1VesselBuilder {
        self.mmsi = Some(value);
        self
    }
    pub fn mothership_mmsi(mut self, value: String) -> V1VesselBuilder {
        self.mothership_mmsi = Some(value);
        self
    }
    pub fn name(mut self, value: String) -> V1VesselBuilder {
        self.name = Some(value);
        self
    }
    pub fn port(mut self, value: String) -> V1VesselBuilder {
        self.port = Some(value);
        self
    }
    pub fn flag(mut self, value: String) -> V1VesselBuilder {
        self.flag = Some(value);
        self
    }
    pub fn navigation(mut self, value: V1Navigation) -> V1VesselBuilder {
        self.navigation = Some(value);
        self
    }
    pub fn communication(mut self, value: V1Communication) -> V1VesselBuilder {
        self.communication = Some(value);
        self
    }
    pub fn electrical(mut self, value: V1Electrical) -> V1VesselBuilder {
        self.electrical = Some(value);
        self
    }
    pub fn environment(mut self, value: V1Environment) -> V1VesselBuilder {
        self.environment = Some(value);
        self
    }
    pub fn notifications(mut self, value: V1Notification) -> V1VesselBuilder {
        self.notifications = Some(value);
        self
    }
    pub fn design(mut self, value: V1Design) -> V1VesselBuilder {
        self.design = Some(value);
        self
    }
    pub fn add_propulsion(mut self, key: String, value: V1Propulsion) -> V1VesselBuilder {
        if self.propulsion.is_none() {
            self.propulsion = Some(HashMap::new());
        }
        if let Some(ref mut x) = self.propulsion {
            x.insert(key, value);
        }
        self
    }
    pub fn build(self) -> V1Vessel {
        V1Vessel {
            uuid: self.uuid,
            mmsi: self.mmsi,
            name: self.name,
            port: self.port,
            flag: self.flag,
            navigation: self.navigation,
            communication: self.communication,
            environment: self.environment,
            electrical: self.electrical,
            // notifications: self.notifications,
            steering: self.steering,
            design: self.design,
            propulsion: self.propulsion,
            url: self.url,
            mothership_mmsi: self.mothership_mmsi,
            performance: self.performance,
        }
    }
}

#[cfg(test)]
mod context_tests {
    use serde_json::{Number, Value};

    use crate::full::Updatable;
    use crate::{V1Navigation, V1NumberValue, V1UpdateType, V1UpdateValue, V1Vessel};

    #[test]
    fn update_navigation_sog_12_6_in_existing_tree() {
        let mut vessel = V1Vessel::builder()
            .navigation(
                V1Navigation::builder()
                    .speed_over_ground(V1NumberValue::builder().value(10.0).build())
                    .build(),
            )
            .build();
        let update = V1UpdateType::builder()
            .add_update(V1UpdateValue::new(
                "navigation.speedOverGround".into(),
                Value::Number(Number::from_f64(12.6).unwrap()),
            ))
            .build();

        vessel.apply_update(&update);

        assert_eq!(
            vessel
                .navigation
                .unwrap()
                .speed_over_ground
                .unwrap()
                .value
                .unwrap(),
            12.6
        );
    }

    #[test]
    fn update_navigation_sog_5_1_in_existing_tree() {
        let mut vessel = V1Vessel::builder()
            .navigation(
                V1Navigation::builder()
                    .speed_over_ground(V1NumberValue::builder().value(10.0).build())
                    .build(),
            )
            .build();
        let update = V1UpdateType::builder()
            .add_update(V1UpdateValue::new(
                "navigation.speedOverGround".into(),
                Value::Number(Number::from_f64(5.1).unwrap()),
            ))
            .build();

        vessel.apply_update(&update);

        assert_eq!(
            vessel
                .navigation
                .unwrap()
                .speed_over_ground
                .unwrap()
                .value
                .unwrap(),
            5.1
        );
    }

    #[test]
    fn update_navigation_sog_and_cog_true() {
        let mut vessel = V1Vessel::builder()
            .navigation(
                V1Navigation::builder()
                    .speed_over_ground(V1NumberValue::builder().value(10.0).build())
                    .course_over_ground_true(V1NumberValue::builder().value(0.0).build())
                    .build(),
            )
            .build();
        let update = V1UpdateType::builder()
            .add_update(V1UpdateValue::new(
                "navigation.speedOverGround".into(),
                Value::Number(Number::from_f64(7.2).unwrap()),
            ))
            .add_update(V1UpdateValue::new(
                "navigation.courseOverGroundTrue".into(),
                Value::Number(Number::from_f64(4.71238898).unwrap()),
            ))
            .build();

        vessel.apply_update(&update);

        assert_eq!(
            vessel
                .navigation
                .as_ref()
                .unwrap()
                .speed_over_ground
                .as_ref()
                .unwrap()
                .value
                .unwrap(),
            7.2
        );
        assert_eq!(
            vessel
                .navigation
                .as_ref()
                .unwrap()
                .course_over_ground_true
                .as_ref()
                .unwrap()
                .value
                .unwrap(),
            4.71238898
        );
    }

    #[test]
    fn new_from_id() {
        let vessel = V1Vessel::new_with_id("urn:mrn:imo:mmsi:366982330");
        assert_eq!(vessel.mmsi, Some("366982330".to_string()));
    }
}