libits_client/analyse/
configuration.rs1use 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 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 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 !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}