libits_client/analyse/
configuration.rs

1// Software Name: its-client
2// SPDX-FileCopyrightText: Copyright (c) 2016-2022 Orange
3// SPDX-License-Identifier: MIT License
4//
5// This software is distributed under the MIT license, see LICENSE.txt file for more details.
6//
7// Author: Frédéric GARDES <frederic.gardes@orange.com> et al.
8// Software description: This Intelligent Transportation Systems (ITS) [MQTT](https://mqtt.org/) client based on the [JSon](https://www.json.org) [ETSI](https://www.etsi.org/committee/its) specification transcription provides a ready to connect project for the mobility (connected and autonomous vehicles, road side units, vulnerable road users,...).
9use std::sync::RwLock;
10
11use crate::mqtt::topic::geo_extension::GeoExtension;
12use crate::reception::information::Information;
13use std::collections::HashMap;
14
15pub struct Configuration {
16    client_id: String,
17    information: RwLock<Information>,
18    region_of_responsibility: bool,
19    // TODO add the information's of the neighbourhood
20    // TODO if you're a central node, remove from your Region Of Responsibility the Regions Of Responsibility of the neighbourhood
21    custom_settings: HashMap<String, String>,
22}
23
24impl Configuration {
25    pub fn new(
26        client_id: String,
27        region_of_responsibility: bool,
28        custom_settings: HashMap<String, String>,
29    ) -> Self {
30        Configuration {
31            client_id,
32            information: RwLock::new(Information::new()),
33            region_of_responsibility,
34            custom_settings,
35        }
36    }
37
38    pub fn gateway_component_name(&self) -> String {
39        let information_guard = self.information.read().unwrap();
40        // assumed clone :we don't maintain a link to the structure
41        information_guard.instance_id.clone()
42    }
43
44    pub fn component_name(&self, added_number: Option<u32>) -> String {
45        let information_guard = self.information.read().unwrap();
46        let number = match added_number {
47            Some(number) => information_guard.instance_id_number() + number,
48            None => information_guard.instance_id_number() + 10000,
49        };
50        format!("{}_{}", self.client_id, number)
51    }
52
53    pub fn station_id(&self, added_number: Option<u32>) -> u32 {
54        let information_guard = self.information.read().unwrap();
55        match added_number {
56            Some(number) => information_guard.instance_id_number() + number,
57            None => information_guard.instance_id_number() + 10000,
58        }
59    }
60
61    pub fn is_in_region_of_responsibility(&self, geo_extension: GeoExtension) -> bool {
62        let information_guard = self.information.read().unwrap();
63        //check broker (own) node
64        !self.region_of_responsibility
65            || information_guard.is_in_region_of_responsibility(geo_extension)
66    }
67
68    pub fn update(&self, new_information: Information) {
69        let mut information_guard = self.information.write().unwrap();
70        *information_guard = new_information;
71    }
72
73    pub fn get(&self, key: &str) -> Option<&String> {
74        self.custom_settings.get(key)
75    }
76
77    pub fn set(&mut self, key: &str, value: String) {
78        self.custom_settings.insert(key.to_string(), value);
79    }
80}