cleanup
This commit is contained in:
58
server/src/telemetry/management_service.rs
Normal file
58
server/src/telemetry/management_service.rs
Normal file
@@ -0,0 +1,58 @@
|
||||
use crate::core::{TelemetryDefinitionRequest, Uuid};
|
||||
use crate::telemetry::data::TelemetryData;
|
||||
use crate::telemetry::definition::TelemetryDefinition;
|
||||
use papaya::HashMap;
|
||||
use std::error::Error;
|
||||
|
||||
pub struct TelemetryManagementService {
|
||||
uuid_index: HashMap<String, String>,
|
||||
tlm_data: HashMap<String, TelemetryData>,
|
||||
}
|
||||
|
||||
impl TelemetryManagementService {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
uuid_index: HashMap::new(),
|
||||
tlm_data: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn register(
|
||||
&self,
|
||||
telemetry_definition_request: TelemetryDefinitionRequest,
|
||||
) -> Result<String, Box<dyn Error>> {
|
||||
let uuid_index = self.uuid_index.pin();
|
||||
let tlm_data = self.tlm_data.pin();
|
||||
|
||||
let uuid = uuid_index
|
||||
.get_or_insert_with(telemetry_definition_request.name.clone(), || {
|
||||
Uuid::random().value
|
||||
})
|
||||
.clone();
|
||||
|
||||
let _ = tlm_data.try_insert(
|
||||
uuid.clone(),
|
||||
TelemetryData {
|
||||
definition: TelemetryDefinition {
|
||||
uuid: uuid.clone(),
|
||||
name: telemetry_definition_request.name.clone(),
|
||||
data_type: telemetry_definition_request.data_type(),
|
||||
},
|
||||
data: tokio::sync::watch::channel(None).0,
|
||||
},
|
||||
);
|
||||
|
||||
Ok(uuid)
|
||||
}
|
||||
|
||||
pub fn get_by_name(&self, name: &String) -> Option<TelemetryData> {
|
||||
let uuid_index = self.uuid_index.pin();
|
||||
let uuid = uuid_index.get(name)?;
|
||||
self.get_by_uuid(uuid)
|
||||
}
|
||||
|
||||
pub fn get_by_uuid(&self, uuid: &String) -> Option<TelemetryData> {
|
||||
let tlm_data = self.tlm_data.pin();
|
||||
tlm_data.get(uuid).cloned()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user