Replace gRPC Backend (#10)
**Rationale:** Having two separate servers and communication methods resulted in additional maintenance & the need to convert often between backend & frontend data types. By moving the backend communication off of gRPC and to just use websockets it both gives more control & allows for simplification of the implementation. #8 **Changes:** - Replaces gRPC backend. - New implementation automatically handles reconnect logic - Implements an api layer - Migrates examples to the api layer - Implements a proc macro to make command handling easier - Implements unit tests for the api layer (90+% coverage) - Implements integration tests for the proc macro (90+% coverage) Reviewed-on: #10 Co-authored-by: Sergey Savelyev <sergeysav.nn@gmail.com> Co-committed-by: Sergey Savelyev <sergeysav.nn@gmail.com>
This commit was merged in pull request #10.
This commit is contained in:
@@ -6,25 +6,20 @@ version = "0.1.0"
|
||||
authors = ["Sergey <me@sergeysav.com>"]
|
||||
|
||||
[dependencies]
|
||||
fern = "0.7.1"
|
||||
log = "0.4.29"
|
||||
prost = "0.13.5"
|
||||
rand = "0.9.0"
|
||||
tonic = { version = "0.12.3" }
|
||||
tokio = { version = "1.43.0", features = ["rt-multi-thread", "signal", "fs"] }
|
||||
chrono = "0.4.42"
|
||||
actix-web = { version = "4.12.1", features = [ ] }
|
||||
actix-ws = "0.3.0"
|
||||
tokio-util = "0.7.17"
|
||||
serde = { version = "1.0.228", features = ["derive"] }
|
||||
serde_json = "1.0.145"
|
||||
hex = "0.4.3"
|
||||
papaya = "0.2.3"
|
||||
thiserror = "2.0.17"
|
||||
derive_more = { version = "2.1.0", features = ["from"] }
|
||||
anyhow = "1.0.100"
|
||||
sqlx = { version = "0.8.6", features = [ "runtime-tokio", "tls-native-tls", "sqlite" ] }
|
||||
uuid = { version = "1.19.0", features = ["v4"] }
|
||||
|
||||
[build-dependencies]
|
||||
tonic-build = "0.12.3"
|
||||
actix-web = { workspace = true, features = [ ] }
|
||||
actix-ws = { workspace = true }
|
||||
anyhow = { workspace = true }
|
||||
api = { path = "../api" }
|
||||
chrono = { workspace = true }
|
||||
derive_more = { workspace = true, features = ["from"] }
|
||||
fern = { workspace = true, features = ["colored"] }
|
||||
futures-util = { workspace = true }
|
||||
log = { workspace = true }
|
||||
papaya = { workspace = true }
|
||||
serde = { workspace = true, features = ["derive"] }
|
||||
serde_json = { workspace = true }
|
||||
sqlx = { workspace = true, features = [ "runtime-tokio", "tls-rustls-ring-native-roots", "sqlite" ] }
|
||||
thiserror = { workspace = true }
|
||||
tokio = { workspace = true, features = ["rt-multi-thread", "signal", "fs"] }
|
||||
tokio-util = { workspace = true }
|
||||
uuid = { workspace = true }
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
println!("cargo:rerun-if-changed=migrations");
|
||||
tonic_build::compile_protos("proto/core.proto")?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1,83 +0,0 @@
|
||||
|
||||
syntax = "proto3";
|
||||
package core;
|
||||
|
||||
enum TelemetryDataType {
|
||||
Float32 = 0;
|
||||
Float64 = 1;
|
||||
Boolean = 2;
|
||||
}
|
||||
|
||||
message TelemetryValue {
|
||||
oneof value {
|
||||
float float_32 = 1;
|
||||
double float_64 = 2;
|
||||
bool boolean = 3;
|
||||
}
|
||||
}
|
||||
|
||||
message UUID {
|
||||
string value = 1;
|
||||
}
|
||||
|
||||
// UTC since UNIX
|
||||
message Timestamp {
|
||||
sfixed64 secs = 1;
|
||||
sfixed32 nanos = 2;
|
||||
}
|
||||
|
||||
message TelemetryDefinitionRequest {
|
||||
string name = 1;
|
||||
TelemetryDataType data_type = 2;
|
||||
}
|
||||
|
||||
message TelemetryDefinitionResponse {
|
||||
UUID uuid = 1;
|
||||
}
|
||||
|
||||
message TelemetryItem {
|
||||
UUID uuid = 1;
|
||||
TelemetryValue value = 2;
|
||||
Timestamp timestamp = 3;
|
||||
}
|
||||
|
||||
message TelemetryInsertResponse {
|
||||
}
|
||||
|
||||
service TelemetryService {
|
||||
rpc NewTelemetry (TelemetryDefinitionRequest) returns (TelemetryDefinitionResponse);
|
||||
rpc InsertTelemetry (stream TelemetryItem) returns (stream TelemetryInsertResponse);
|
||||
}
|
||||
|
||||
message CommandParameterDefinition {
|
||||
string name = 1;
|
||||
TelemetryDataType data_type = 2;
|
||||
}
|
||||
|
||||
message CommandDefinitionRequest {
|
||||
string name = 1;
|
||||
repeated CommandParameterDefinition parameters = 2;
|
||||
}
|
||||
|
||||
message Command {
|
||||
UUID uuid = 1;
|
||||
Timestamp timestamp = 2;
|
||||
map<string, TelemetryValue> parameters = 3;
|
||||
}
|
||||
|
||||
message CommandResponse {
|
||||
UUID uuid = 1;
|
||||
bool success = 2;
|
||||
string response = 3;
|
||||
}
|
||||
|
||||
message ClientSideCommand {
|
||||
oneof inner {
|
||||
CommandDefinitionRequest request = 1;
|
||||
CommandResponse response = 2;
|
||||
}
|
||||
}
|
||||
|
||||
service CommandService {
|
||||
rpc NewCommand (stream ClientSideCommand) returns (stream Command);
|
||||
}
|
||||
20
server/src/command/command_handle.rs
Normal file
20
server/src/command/command_handle.rs
Normal file
@@ -0,0 +1,20 @@
|
||||
use uuid::Uuid;
|
||||
|
||||
pub struct CommandHandle {
|
||||
name: String,
|
||||
uuid: Uuid,
|
||||
}
|
||||
|
||||
impl CommandHandle {
|
||||
pub fn new(name: String, uuid: Uuid) -> Self {
|
||||
Self { name, uuid }
|
||||
}
|
||||
|
||||
pub fn name(&self) -> &str {
|
||||
&self.name
|
||||
}
|
||||
|
||||
pub fn uuid(&self) -> &Uuid {
|
||||
&self.uuid
|
||||
}
|
||||
}
|
||||
@@ -1,22 +1,5 @@
|
||||
use crate::command::service::RegisteredCommand;
|
||||
use crate::core::TelemetryDataType;
|
||||
use crate::telemetry::data_type::tlm_data_type_deserializer;
|
||||
use crate::telemetry::data_type::tlm_data_type_serializer;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct CommandParameterDefinition {
|
||||
pub name: String,
|
||||
#[serde(serialize_with = "tlm_data_type_serializer")]
|
||||
#[serde(deserialize_with = "tlm_data_type_deserializer")]
|
||||
pub data_type: TelemetryDataType,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct CommandDefinition {
|
||||
pub name: String,
|
||||
pub parameters: Vec<CommandParameterDefinition>,
|
||||
}
|
||||
use api::messages::command::{CommandDefinition, CommandParameterDefinition};
|
||||
|
||||
impl From<RegisteredCommand> for CommandDefinition {
|
||||
fn from(value: RegisteredCommand) -> Self {
|
||||
@@ -27,7 +10,7 @@ impl From<RegisteredCommand> for CommandDefinition {
|
||||
.parameters
|
||||
.into_iter()
|
||||
.map(|param| CommandParameterDefinition {
|
||||
data_type: param.data_type(),
|
||||
data_type: param.data_type,
|
||||
name: param.name,
|
||||
})
|
||||
.collect(),
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use crate::core::TelemetryDataType;
|
||||
use actix_web::http::StatusCode;
|
||||
use actix_web::ResponseError;
|
||||
use api::data_type::DataType;
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
@@ -14,7 +14,7 @@ pub enum Error {
|
||||
#[error("Incorrect Parameter Type for {name}. {expected_type:?} expected.")]
|
||||
WrongParameterType {
|
||||
name: String,
|
||||
expected_type: TelemetryDataType,
|
||||
expected_type: DataType,
|
||||
},
|
||||
#[error("No Command Receiver")]
|
||||
NoCommandReceiver,
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
pub mod command_handle;
|
||||
mod definition;
|
||||
pub mod error;
|
||||
pub mod service;
|
||||
|
||||
@@ -1,36 +1,40 @@
|
||||
use crate::command::definition::CommandDefinition;
|
||||
use crate::command::command_handle::CommandHandle;
|
||||
use crate::command::error::Error as CmdError;
|
||||
use crate::command::error::Error::{
|
||||
CommandFailure, CommandNotFound, FailedToReceiveResponse, FailedToSend,
|
||||
IncorrectParameterCount, MisingParameter, NoCommandReceiver, WrongParameterType,
|
||||
};
|
||||
use crate::core::telemetry_value::Value;
|
||||
use crate::core::{
|
||||
Command, CommandDefinitionRequest, CommandResponse, TelemetryDataType, TelemetryValue,
|
||||
Timestamp, Uuid,
|
||||
};
|
||||
use chrono::{DateTime, Utc};
|
||||
use anyhow::bail;
|
||||
use api::data_type::DataType;
|
||||
use api::data_value::DataValue;
|
||||
use api::messages::command::{Command, CommandDefinition, CommandHeader, CommandResponse};
|
||||
use api::messages::ResponseMessage;
|
||||
use chrono::Utc;
|
||||
use log::error;
|
||||
use papaya::HashMap;
|
||||
use std::collections::HashMap as StdHashMap;
|
||||
use tokio::sync::mpsc;
|
||||
use tokio::sync::oneshot;
|
||||
use tokio::sync::{mpsc, RwLock};
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(super) struct RegisteredCommand {
|
||||
pub(super) name: String,
|
||||
pub(super) definition: CommandDefinitionRequest,
|
||||
tx: mpsc::Sender<Option<(Command, oneshot::Sender<CommandResponse>)>>,
|
||||
pub(super) definition: CommandDefinition,
|
||||
response_uuid: Uuid,
|
||||
tx: mpsc::Sender<ResponseMessage>,
|
||||
}
|
||||
|
||||
pub struct CommandManagementService {
|
||||
registered_commands: HashMap<String, RegisteredCommand>,
|
||||
outstanding_responses: RwLock<StdHashMap<Uuid, oneshot::Sender<CommandResponse>>>,
|
||||
}
|
||||
|
||||
impl CommandManagementService {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
registered_commands: HashMap::new(),
|
||||
outstanding_responses: RwLock::new(StdHashMap::new()),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,26 +56,26 @@ impl CommandManagementService {
|
||||
.map(|registration| registration.clone().into())
|
||||
}
|
||||
|
||||
pub async fn register_command(
|
||||
pub fn register_command(
|
||||
&self,
|
||||
command: CommandDefinitionRequest,
|
||||
) -> anyhow::Result<mpsc::Receiver<Option<(Command, oneshot::Sender<CommandResponse>)>>> {
|
||||
let (tx, rx) = mpsc::channel(1);
|
||||
|
||||
let registered_commands = self.registered_commands.pin_owned();
|
||||
if let Some(previous) = registered_commands.insert(
|
||||
command.name.clone(),
|
||||
uuid: Uuid,
|
||||
command: CommandDefinition,
|
||||
tx: mpsc::Sender<ResponseMessage>,
|
||||
) -> anyhow::Result<CommandHandle> {
|
||||
let registered_commands = self.registered_commands.pin();
|
||||
// We don't care about the previously registered command
|
||||
let name = command.name.clone();
|
||||
let _ = registered_commands.insert(
|
||||
name.clone(),
|
||||
RegisteredCommand {
|
||||
name: command.name.clone(),
|
||||
response_uuid: uuid,
|
||||
name: name.clone(),
|
||||
definition: command,
|
||||
tx,
|
||||
},
|
||||
) {
|
||||
// If the receiver was already closed, we don't care (ignore error)
|
||||
let _ = previous.tx.send(None).await;
|
||||
}
|
||||
);
|
||||
|
||||
Ok(rx)
|
||||
Ok(CommandHandle::new(name, uuid))
|
||||
}
|
||||
|
||||
pub async fn send_command(
|
||||
@@ -80,8 +84,6 @@ impl CommandManagementService {
|
||||
parameters: serde_json::Map<String, serde_json::Value>,
|
||||
) -> Result<String, CmdError> {
|
||||
let timestamp = Utc::now();
|
||||
let offset_from_unix_epoch =
|
||||
timestamp - DateTime::from_timestamp(0, 0).expect("Could not get Unix epoch");
|
||||
|
||||
let name = name.into();
|
||||
let registered_commands = self.registered_commands.pin();
|
||||
@@ -100,27 +102,21 @@ impl CommandManagementService {
|
||||
let Some(param_value) = parameters.get(¶meter.name) else {
|
||||
return Err(MisingParameter(parameter.name.clone()));
|
||||
};
|
||||
let Some(param_value) = (match parameter.data_type() {
|
||||
TelemetryDataType::Float32 => {
|
||||
param_value.as_f64().map(|v| Value::Float32(v as f32))
|
||||
}
|
||||
TelemetryDataType::Float64 => param_value.as_f64().map(Value::Float64),
|
||||
TelemetryDataType::Boolean => param_value.as_bool().map(Value::Boolean),
|
||||
let Some(param_value) = (match parameter.data_type {
|
||||
DataType::Float32 => param_value.as_f64().map(|v| DataValue::Float32(v as f32)),
|
||||
DataType::Float64 => param_value.as_f64().map(DataValue::Float64),
|
||||
DataType::Boolean => param_value.as_bool().map(DataValue::Boolean),
|
||||
}) else {
|
||||
return Err(WrongParameterType {
|
||||
name: parameter.name.clone(),
|
||||
expected_type: parameter.data_type(),
|
||||
expected_type: parameter.data_type,
|
||||
});
|
||||
};
|
||||
result_parameters.insert(
|
||||
parameter.name.clone(),
|
||||
TelemetryValue {
|
||||
value: Some(param_value),
|
||||
},
|
||||
);
|
||||
result_parameters.insert(parameter.name.clone(), param_value);
|
||||
}
|
||||
|
||||
// Clone & Drop lets us use a standard pin instead of an owned pin
|
||||
let response_uuid = registration.response_uuid;
|
||||
let tx = registration.tx.clone();
|
||||
drop(registered_commands);
|
||||
|
||||
@@ -128,23 +124,27 @@ impl CommandManagementService {
|
||||
return Err(NoCommandReceiver);
|
||||
}
|
||||
|
||||
let uuid = Uuid::random();
|
||||
let uuid = Uuid::new_v4();
|
||||
let (response_tx, response_rx) = oneshot::channel();
|
||||
|
||||
{
|
||||
let mut outstanding_responses = self.outstanding_responses.write().await;
|
||||
outstanding_responses.insert(uuid, response_tx);
|
||||
}
|
||||
|
||||
if let Err(e) = tx
|
||||
.send(Some((
|
||||
Command {
|
||||
uuid: Some(uuid),
|
||||
timestamp: Some(Timestamp {
|
||||
secs: offset_from_unix_epoch.num_seconds(),
|
||||
nanos: offset_from_unix_epoch.subsec_nanos(),
|
||||
}),
|
||||
.send(ResponseMessage {
|
||||
uuid,
|
||||
response: Some(response_uuid),
|
||||
payload: Command {
|
||||
header: CommandHeader { timestamp },
|
||||
parameters: result_parameters,
|
||||
},
|
||||
response_tx,
|
||||
)))
|
||||
}
|
||||
.into(),
|
||||
})
|
||||
.await
|
||||
{
|
||||
error!("Failed to Send Command: {e}");
|
||||
error!("Failed to Send Command {e}");
|
||||
return Err(FailedToSend);
|
||||
}
|
||||
|
||||
@@ -162,4 +162,33 @@ impl CommandManagementService {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn handle_command_response(
|
||||
&self,
|
||||
uuid: Uuid,
|
||||
response: CommandResponse,
|
||||
) -> anyhow::Result<()> {
|
||||
let responder = {
|
||||
let mut outstanding_responses = self.outstanding_responses.write().await;
|
||||
outstanding_responses.remove(&uuid)
|
||||
};
|
||||
match responder {
|
||||
None => bail!("Unexpected Command Response for Command {uuid}"),
|
||||
Some(response_tx) => {
|
||||
if let Err(e) = response_tx.send(response) {
|
||||
bail!("Failed to send Command Response {e:?}");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn unregister(&self, command_handle: CommandHandle) {
|
||||
let registered_commands = self.registered_commands.pin();
|
||||
// We don't care if this succeeded
|
||||
let _ = registered_commands.remove_if(command_handle.name(), |_, registration| {
|
||||
registration.response_uuid == *command_handle.uuid()
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -135,11 +135,14 @@ impl CommandService for CoreCommandService {
|
||||
}
|
||||
}
|
||||
for (key, sender) in in_progress.drain() {
|
||||
if sender.send(CommandResponse {
|
||||
uuid: Some(Uuid::from(key)),
|
||||
success: false,
|
||||
response: "Command Handler Shut Down".to_string(),
|
||||
}).is_err() {
|
||||
if sender
|
||||
.send(CommandResponse {
|
||||
uuid: Some(Uuid::from(key)),
|
||||
success: false,
|
||||
response: "Command Handler Shut Down".to_string(),
|
||||
})
|
||||
.is_err()
|
||||
{
|
||||
error!("Failed to send command response on shutdown");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
mod cmd;
|
||||
mod tlm;
|
||||
|
||||
use crate::command::service::CommandManagementService;
|
||||
use crate::core::command_service_server::CommandServiceServer;
|
||||
use crate::core::telemetry_service_server::TelemetryServiceServer;
|
||||
use crate::grpc::cmd::CoreCommandService;
|
||||
use crate::grpc::tlm::CoreTelemetryService;
|
||||
use crate::telemetry::management_service::TelemetryManagementService;
|
||||
use log::{error, info};
|
||||
use std::sync::Arc;
|
||||
use tokio::task::JoinHandle;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
use tonic::transport::Server;
|
||||
|
||||
pub fn setup(
|
||||
token: CancellationToken,
|
||||
telemetry_management_service: Arc<TelemetryManagementService>,
|
||||
command_service: Arc<CommandManagementService>,
|
||||
) -> anyhow::Result<JoinHandle<()>> {
|
||||
let addr = "[::1]:50051".parse()?;
|
||||
Ok(tokio::spawn(async move {
|
||||
let tlm_service = CoreTelemetryService {
|
||||
tlm_management: telemetry_management_service,
|
||||
cancellation_token: token.clone(),
|
||||
};
|
||||
|
||||
let cmd_service = CoreCommandService {
|
||||
command_service,
|
||||
cancellation_token: token.clone(),
|
||||
};
|
||||
|
||||
info!("Starting gRPC Server");
|
||||
let result = Server::builder()
|
||||
.add_service(TelemetryServiceServer::new(tlm_service))
|
||||
.add_service(CommandServiceServer::new(cmd_service))
|
||||
.serve_with_shutdown(addr, token.cancelled_owned())
|
||||
.await;
|
||||
|
||||
if let Err(err) = result {
|
||||
error!("gRPC Server Encountered An Error: {err}");
|
||||
}
|
||||
}))
|
||||
}
|
||||
@@ -1,141 +0,0 @@
|
||||
use crate::core::telemetry_service_server::TelemetryService;
|
||||
use crate::core::telemetry_value::Value;
|
||||
use crate::core::{
|
||||
TelemetryDataType, TelemetryDefinitionRequest, TelemetryDefinitionResponse,
|
||||
TelemetryInsertResponse, TelemetryItem, TelemetryValue, Uuid,
|
||||
};
|
||||
use crate::telemetry::data_item::TelemetryDataItem;
|
||||
use crate::telemetry::data_value::TelemetryDataValue;
|
||||
use crate::telemetry::history::TelemetryHistory;
|
||||
use crate::telemetry::management_service::TelemetryManagementService;
|
||||
use chrono::{DateTime, SecondsFormat};
|
||||
use log::trace;
|
||||
use std::pin::Pin;
|
||||
use std::sync::Arc;
|
||||
use tokio::select;
|
||||
use tokio::sync::mpsc;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
use tonic::codegen::tokio_stream::wrappers::ReceiverStream;
|
||||
use tonic::codegen::tokio_stream::{Stream, StreamExt};
|
||||
use tonic::{Request, Response, Status, Streaming};
|
||||
|
||||
pub struct CoreTelemetryService {
|
||||
pub tlm_management: Arc<TelemetryManagementService>,
|
||||
pub cancellation_token: CancellationToken,
|
||||
}
|
||||
|
||||
#[tonic::async_trait]
|
||||
impl TelemetryService for CoreTelemetryService {
|
||||
async fn new_telemetry(
|
||||
&self,
|
||||
request: Request<TelemetryDefinitionRequest>,
|
||||
) -> Result<Response<TelemetryDefinitionResponse>, Status> {
|
||||
trace!("CoreTelemetryService::new_telemetry");
|
||||
self.tlm_management
|
||||
.register(request.into_inner())
|
||||
.map(|uuid| {
|
||||
Response::new(TelemetryDefinitionResponse {
|
||||
uuid: Some(Uuid { value: uuid }),
|
||||
})
|
||||
})
|
||||
.map_err(|err| Status::already_exists(err.to_string()))
|
||||
}
|
||||
|
||||
type InsertTelemetryStream =
|
||||
Pin<Box<dyn Stream<Item = Result<TelemetryInsertResponse, Status>> + Send>>;
|
||||
|
||||
async fn insert_telemetry(
|
||||
&self,
|
||||
request: Request<Streaming<TelemetryItem>>,
|
||||
) -> Result<Response<Self::InsertTelemetryStream>, Status> {
|
||||
trace!("CoreTelemetryService::insert_telemetry");
|
||||
|
||||
let cancel_token = self.cancellation_token.clone();
|
||||
let tlm_management = self.tlm_management.clone();
|
||||
let mut in_stream = request.into_inner();
|
||||
let (tx, rx) = mpsc::channel(128);
|
||||
|
||||
tokio::spawn(async move {
|
||||
loop {
|
||||
select! {
|
||||
_ = cancel_token.cancelled() => {
|
||||
break;
|
||||
},
|
||||
Some(message) = in_stream.next() => {
|
||||
match message {
|
||||
Ok(tlm_item) => {
|
||||
tx
|
||||
.send(Self::handle_new_tlm_item(&tlm_management, &tlm_item))
|
||||
.await
|
||||
.expect("working rx");
|
||||
}
|
||||
Err(err) => {
|
||||
let _ = tx.send(Err(err)).await;
|
||||
}
|
||||
}
|
||||
},
|
||||
else => break,
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Ok(Response::new(Box::pin(ReceiverStream::new(rx))))
|
||||
}
|
||||
}
|
||||
|
||||
impl CoreTelemetryService {
|
||||
#[allow(clippy::result_large_err)]
|
||||
fn handle_new_tlm_item(
|
||||
tlm_management: &Arc<TelemetryManagementService>,
|
||||
tlm_item: &TelemetryItem,
|
||||
) -> Result<TelemetryInsertResponse, Status> {
|
||||
trace!("CoreTelemetryService::handle_new_tlm_item {:?}", tlm_item);
|
||||
let Some(ref uuid) = tlm_item.uuid else {
|
||||
return Err(Status::failed_precondition("UUID Missing"));
|
||||
};
|
||||
let tlm_management_pin = tlm_management.pin();
|
||||
let Some(tlm_data) = tlm_management_pin.get_by_uuid(&uuid.value) else {
|
||||
return Err(Status::not_found("Telemetry Item Not Found"));
|
||||
};
|
||||
|
||||
let Some(TelemetryValue { value: Some(value) }) = tlm_item.value else {
|
||||
return Err(Status::failed_precondition("Value Missing"));
|
||||
};
|
||||
|
||||
let Some(timestamp) = tlm_item.timestamp else {
|
||||
return Err(Status::failed_precondition("Timestamp Missing"));
|
||||
};
|
||||
|
||||
let expected_type = match value {
|
||||
Value::Float32(_) => TelemetryDataType::Float32,
|
||||
Value::Float64(_) => TelemetryDataType::Float64,
|
||||
Value::Boolean(_) => TelemetryDataType::Boolean,
|
||||
};
|
||||
if expected_type != tlm_data.data.definition.data_type {
|
||||
return Err(Status::failed_precondition("Data Type Mismatch"));
|
||||
};
|
||||
|
||||
let Some(timestamp) = DateTime::from_timestamp(timestamp.secs, timestamp.nanos as u32)
|
||||
else {
|
||||
return Err(Status::invalid_argument("Failed to construct UTC DateTime"));
|
||||
};
|
||||
|
||||
let value = match value {
|
||||
Value::Float32(x) => TelemetryDataValue::Float32(x),
|
||||
Value::Float64(x) => TelemetryDataValue::Float64(x),
|
||||
Value::Boolean(x) => TelemetryDataValue::Boolean(x),
|
||||
};
|
||||
let _ = tlm_data.data.data.send_replace(Some(TelemetryDataItem {
|
||||
value: value.clone(),
|
||||
timestamp: timestamp.to_rfc3339_opts(SecondsFormat::Millis, true),
|
||||
}));
|
||||
TelemetryHistory::insert_sync(
|
||||
tlm_data.clone(),
|
||||
tlm_management.history_service(),
|
||||
value,
|
||||
timestamp,
|
||||
);
|
||||
|
||||
Ok(TelemetryInsertResponse {})
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ use crate::panels::PanelService;
|
||||
use actix_web::{delete, get, post, put, web, Responder};
|
||||
use serde::Deserialize;
|
||||
use std::sync::Arc;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct CreateParam {
|
||||
@@ -13,7 +14,7 @@ struct CreateParam {
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct IdParam {
|
||||
id: String,
|
||||
id: Uuid,
|
||||
}
|
||||
|
||||
#[post("/panel")]
|
||||
@@ -22,7 +23,7 @@ pub(super) async fn new(
|
||||
data: web::Json<CreateParam>,
|
||||
) -> Result<impl Responder, HttpServerResultError> {
|
||||
let uuid = panels.create(&data.name, &data.data).await?;
|
||||
Ok(web::Json(uuid.value))
|
||||
Ok(web::Json(uuid))
|
||||
}
|
||||
|
||||
#[get("/panel")]
|
||||
@@ -38,12 +39,10 @@ pub(super) async fn get_one(
|
||||
panels: web::Data<Arc<PanelService>>,
|
||||
path: web::Path<IdParam>,
|
||||
) -> Result<impl Responder, HttpServerResultError> {
|
||||
let result = panels.read(path.id.clone().into()).await?;
|
||||
let result = panels.read(path.id).await?;
|
||||
match result {
|
||||
Some(result) => Ok(web::Json(result)),
|
||||
None => Err(HttpServerResultError::PanelUuidNotFound {
|
||||
uuid: path.id.clone(),
|
||||
}),
|
||||
None => Err(HttpServerResultError::PanelUuidNotFound { uuid: path.id }),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,7 +52,7 @@ pub(super) async fn set(
|
||||
path: web::Path<IdParam>,
|
||||
data: web::Json<PanelUpdate>,
|
||||
) -> Result<impl Responder, HttpServerResultError> {
|
||||
panels.update(path.id.clone().into(), data.0).await?;
|
||||
panels.update(path.id, data.0).await?;
|
||||
Ok(web::Json(()))
|
||||
}
|
||||
|
||||
@@ -62,6 +61,6 @@ pub(super) async fn delete(
|
||||
panels: web::Data<Arc<PanelService>>,
|
||||
path: web::Path<IdParam>,
|
||||
) -> Result<impl Responder, HttpServerResultError> {
|
||||
panels.delete(path.id.clone().into()).await?;
|
||||
panels.delete(path.id).await?;
|
||||
Ok(web::Json(()))
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ use serde::Deserialize;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use tokio::time::timeout;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[get("/tlm/info/{name:[\\w\\d/_-]+}")]
|
||||
pub(super) async fn get_tlm_definition(
|
||||
@@ -36,13 +37,17 @@ struct HistoryQuery {
|
||||
resolution: i64,
|
||||
}
|
||||
|
||||
#[get("/tlm/history/{uuid:[0-9a-f]+}")]
|
||||
#[get("/tlm/history/{uuid:[0-9a-f-]+}")]
|
||||
pub(super) async fn get_tlm_history(
|
||||
data_arc: web::Data<Arc<TelemetryManagementService>>,
|
||||
uuid: web::Path<String>,
|
||||
info: web::Query<HistoryQuery>,
|
||||
) -> Result<impl Responder, HttpServerResultError> {
|
||||
let uuid = uuid.to_string();
|
||||
let Ok(uuid) = Uuid::parse_str(&uuid) else {
|
||||
return Err(HttpServerResultError::InvalidUuid {
|
||||
uuid: uuid.to_string(),
|
||||
});
|
||||
};
|
||||
trace!(
|
||||
"get_tlm_history {} from {} to {} resolution {}",
|
||||
uuid,
|
||||
|
||||
117
server/src/http/backend/connection.rs
Normal file
117
server/src/http/backend/connection.rs
Normal file
@@ -0,0 +1,117 @@
|
||||
use crate::command::command_handle::CommandHandle;
|
||||
use crate::command::service::CommandManagementService;
|
||||
use crate::telemetry::management_service::TelemetryManagementService;
|
||||
use actix_ws::{AggregatedMessage, ProtocolError, Session};
|
||||
use anyhow::bail;
|
||||
use api::messages::payload::RequestMessagePayload;
|
||||
use api::messages::{RequestMessage, ResponseMessage};
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::mpsc::{Receiver, Sender};
|
||||
use uuid::Uuid;
|
||||
|
||||
pub(super) struct BackendConnection {
|
||||
session: Session,
|
||||
tlm_management: Arc<TelemetryManagementService>,
|
||||
cmd_management: Arc<CommandManagementService>,
|
||||
tx: Sender<ResponseMessage>,
|
||||
commands: Vec<CommandHandle>,
|
||||
pub rx: Receiver<ResponseMessage>,
|
||||
pub should_close: bool,
|
||||
}
|
||||
|
||||
impl BackendConnection {
|
||||
pub fn new(
|
||||
session: Session,
|
||||
tlm_management: Arc<TelemetryManagementService>,
|
||||
cmd_management: Arc<CommandManagementService>,
|
||||
) -> Self {
|
||||
let (tx, rx) = tokio::sync::mpsc::channel::<ResponseMessage>(128);
|
||||
Self {
|
||||
session,
|
||||
tlm_management,
|
||||
cmd_management,
|
||||
tx,
|
||||
commands: vec![],
|
||||
rx,
|
||||
should_close: false,
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_request(&mut self, msg: RequestMessage) -> anyhow::Result<()> {
|
||||
match msg.payload {
|
||||
RequestMessagePayload::TelemetryDefinitionRequest(tlm_def) => {
|
||||
self.tx
|
||||
.send(ResponseMessage {
|
||||
uuid: Uuid::new_v4(),
|
||||
response: Some(msg.uuid),
|
||||
payload: self.tlm_management.register(tlm_def)?.into(),
|
||||
})
|
||||
.await?;
|
||||
}
|
||||
RequestMessagePayload::TelemetryEntry(tlm_entry) => {
|
||||
self.tlm_management.add_tlm_item(tlm_entry)?;
|
||||
}
|
||||
RequestMessagePayload::GenericCallbackError(_) => todo!(),
|
||||
RequestMessagePayload::CommandDefinition(def) => {
|
||||
let cmd = self
|
||||
.cmd_management
|
||||
.register_command(msg.uuid, def, self.tx.clone())?;
|
||||
self.commands.push(cmd);
|
||||
}
|
||||
RequestMessagePayload::CommandResponse(response) => match msg.response {
|
||||
None => bail!("Command Response Payload Must Respond to a Command"),
|
||||
Some(uuid) => {
|
||||
self.cmd_management
|
||||
.handle_command_response(uuid, response)
|
||||
.await?;
|
||||
}
|
||||
},
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn handle_request_message(
|
||||
&mut self,
|
||||
msg: Result<AggregatedMessage, ProtocolError>,
|
||||
) -> anyhow::Result<()> {
|
||||
let msg = msg?;
|
||||
match msg {
|
||||
AggregatedMessage::Text(data) => {
|
||||
self.handle_request(serde_json::from_str(&data)?).await?;
|
||||
}
|
||||
AggregatedMessage::Binary(_) => {
|
||||
bail!("Binary Messages Unsupported");
|
||||
}
|
||||
AggregatedMessage::Ping(bytes) => {
|
||||
self.session.pong(&bytes).await?;
|
||||
}
|
||||
AggregatedMessage::Pong(_) => {
|
||||
// Intentionally Ignore
|
||||
}
|
||||
AggregatedMessage::Close(_) => {
|
||||
self.should_close = true;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn handle_response(&mut self, msg: ResponseMessage) -> anyhow::Result<()> {
|
||||
let msg_json = serde_json::to_string(&msg)?;
|
||||
self.session.text(msg_json).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn cleanup(mut self) {
|
||||
self.rx.close();
|
||||
// Clone here to prevent conflict with the Drop trait
|
||||
let _ = self.session.clone().close(None).await;
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for BackendConnection {
|
||||
fn drop(&mut self) {
|
||||
for command in self.commands.drain(..) {
|
||||
self.cmd_management.unregister(command);
|
||||
}
|
||||
}
|
||||
}
|
||||
60
server/src/http/backend/mod.rs
Normal file
60
server/src/http/backend/mod.rs
Normal file
@@ -0,0 +1,60 @@
|
||||
use futures_util::stream::StreamExt;
|
||||
mod connection;
|
||||
|
||||
use crate::command::service::CommandManagementService;
|
||||
use crate::http::backend::connection::BackendConnection;
|
||||
use crate::telemetry::management_service::TelemetryManagementService;
|
||||
use actix_web::{rt, web, HttpRequest, HttpResponse};
|
||||
use log::{error, trace};
|
||||
use std::sync::Arc;
|
||||
use tokio::select;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
|
||||
async fn backend_connect(
|
||||
req: HttpRequest,
|
||||
stream: web::Payload,
|
||||
cancel_token: web::Data<CancellationToken>,
|
||||
telemetry_management_service: web::Data<Arc<TelemetryManagementService>>,
|
||||
command_management_service: web::Data<Arc<CommandManagementService>>,
|
||||
) -> Result<HttpResponse, actix_web::Error> {
|
||||
trace!("backend_connect");
|
||||
let (res, session, stream) = actix_ws::handle(&req, stream)?;
|
||||
|
||||
let mut stream = stream
|
||||
.aggregate_continuations()
|
||||
// up to 1 MiB
|
||||
.max_continuation_size(2_usize.pow(20));
|
||||
|
||||
let cancel_token = cancel_token.get_ref().clone();
|
||||
let tlm_management = telemetry_management_service.get_ref().clone();
|
||||
let cmd_management = command_management_service.get_ref().clone();
|
||||
|
||||
rt::spawn(async move {
|
||||
let mut connection = BackendConnection::new(session, tlm_management, cmd_management);
|
||||
while !connection.should_close {
|
||||
let result = select! {
|
||||
_ = cancel_token.cancelled() => {
|
||||
connection.should_close = true;
|
||||
Ok(())
|
||||
},
|
||||
Some(msg) = connection.rx.recv() => connection.handle_response(msg).await,
|
||||
Some(msg) = stream.next() => connection.handle_request_message(msg).await,
|
||||
else => {
|
||||
connection.should_close = true;
|
||||
Ok(())
|
||||
},
|
||||
};
|
||||
if let Err(e) = result {
|
||||
error!("backend socket error: {e}");
|
||||
connection.should_close = true;
|
||||
}
|
||||
}
|
||||
connection.cleanup().await;
|
||||
});
|
||||
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
pub fn setup_backend(cfg: &mut web::ServiceConfig) {
|
||||
cfg.route("", web::get().to(backend_connect));
|
||||
}
|
||||
@@ -3,13 +3,16 @@ use actix_web::http::header::ContentType;
|
||||
use actix_web::http::StatusCode;
|
||||
use actix_web::HttpResponse;
|
||||
use thiserror::Error;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum HttpServerResultError {
|
||||
#[error("Telemetry Name Not Found: {tlm}")]
|
||||
TlmNameNotFound { tlm: String },
|
||||
#[error("Invalid Uuid: {uuid}")]
|
||||
InvalidUuid { uuid: String },
|
||||
#[error("Telemetry Uuid Not Found: {uuid}")]
|
||||
TlmUuidNotFound { uuid: String },
|
||||
TlmUuidNotFound { uuid: Uuid },
|
||||
#[error("DateTime Parsing Error: {date_time}")]
|
||||
InvalidDateTime { date_time: String },
|
||||
#[error("Timed out")]
|
||||
@@ -17,7 +20,7 @@ pub enum HttpServerResultError {
|
||||
#[error("Internal Error")]
|
||||
InternalError(#[from] anyhow::Error),
|
||||
#[error("Panel Uuid Not Found: {uuid}")]
|
||||
PanelUuidNotFound { uuid: String },
|
||||
PanelUuidNotFound { uuid: Uuid },
|
||||
#[error(transparent)]
|
||||
Command(#[from] crate::command::error::Error),
|
||||
}
|
||||
@@ -26,6 +29,7 @@ impl ResponseError for HttpServerResultError {
|
||||
fn status_code(&self) -> StatusCode {
|
||||
match self {
|
||||
HttpServerResultError::TlmNameNotFound { .. } => StatusCode::NOT_FOUND,
|
||||
HttpServerResultError::InvalidUuid { .. } => StatusCode::BAD_REQUEST,
|
||||
HttpServerResultError::TlmUuidNotFound { .. } => StatusCode::NOT_FOUND,
|
||||
HttpServerResultError::InvalidDateTime { .. } => StatusCode::BAD_REQUEST,
|
||||
HttpServerResultError::Timeout => StatusCode::GATEWAY_TIMEOUT,
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
mod api;
|
||||
mod backend;
|
||||
mod error;
|
||||
mod websocket;
|
||||
|
||||
use crate::command::service::CommandManagementService;
|
||||
use crate::http::api::setup_api;
|
||||
use crate::http::backend::setup_backend;
|
||||
use crate::http::websocket::setup_websocket;
|
||||
use crate::panels::PanelService;
|
||||
use crate::telemetry::management_service::TelemetryManagementService;
|
||||
@@ -31,6 +33,7 @@ pub async fn setup(
|
||||
.app_data(cancel_token.clone())
|
||||
.app_data(panel_service.clone())
|
||||
.app_data(command_service.clone())
|
||||
.service(web::scope("/backend").configure(setup_backend))
|
||||
.service(web::scope("/ws").configure(setup_websocket))
|
||||
.service(web::scope("/api").configure(setup_api))
|
||||
.wrap(Logger::default())
|
||||
|
||||
@@ -6,6 +6,7 @@ use crate::telemetry::management_service::TelemetryManagementService;
|
||||
use actix_web::{rt, web, HttpRequest, HttpResponse};
|
||||
use actix_ws::{AggregatedMessage, ProtocolError, Session};
|
||||
use anyhow::anyhow;
|
||||
use futures_util::StreamExt;
|
||||
use log::{error, trace};
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
@@ -14,7 +15,7 @@ use tokio::select;
|
||||
use tokio::sync::mpsc::Sender;
|
||||
use tokio::time::{sleep_until, Instant};
|
||||
use tokio_util::sync::CancellationToken;
|
||||
use tonic::codegen::tokio_stream::StreamExt;
|
||||
use uuid::Uuid;
|
||||
|
||||
pub mod request;
|
||||
pub mod response;
|
||||
@@ -23,11 +24,11 @@ fn handle_register_tlm_listener(
|
||||
data: &Arc<TelemetryManagementService>,
|
||||
request: RegisterTlmListenerRequest,
|
||||
tx: &Sender<WebsocketResponse>,
|
||||
tlm_listeners: &mut HashMap<String, CancellationToken>,
|
||||
tlm_listeners: &mut HashMap<Uuid, CancellationToken>,
|
||||
) {
|
||||
if let Some(tlm_data) = data.get_by_uuid(&request.uuid) {
|
||||
let token = CancellationToken::new();
|
||||
if let Some(token) = tlm_listeners.insert(tlm_data.definition.uuid.clone(), token.clone()) {
|
||||
if let Some(token) = tlm_listeners.insert(tlm_data.definition.uuid, token.clone()) {
|
||||
token.cancel();
|
||||
}
|
||||
let minimum_separation = Duration::from_millis(request.minimum_separation_ms as u64);
|
||||
@@ -46,7 +47,7 @@ fn handle_register_tlm_listener(
|
||||
ref_val.clone()
|
||||
};
|
||||
let _ = tx.send(TlmValueResponse {
|
||||
uuid: request.uuid.clone(),
|
||||
uuid: request.uuid,
|
||||
value,
|
||||
}.into()).await;
|
||||
now
|
||||
@@ -65,7 +66,7 @@ fn handle_register_tlm_listener(
|
||||
|
||||
fn handle_unregister_tlm_listener(
|
||||
request: UnregisterTlmListenerRequest,
|
||||
tlm_listeners: &mut HashMap<String, CancellationToken>,
|
||||
tlm_listeners: &mut HashMap<Uuid, CancellationToken>,
|
||||
) {
|
||||
if let Some(token) = tlm_listeners.remove(&request.uuid) {
|
||||
token.cancel();
|
||||
@@ -76,7 +77,7 @@ async fn handle_websocket_message(
|
||||
data: &Arc<TelemetryManagementService>,
|
||||
request: WebsocketRequest,
|
||||
tx: &Sender<WebsocketResponse>,
|
||||
tlm_listeners: &mut HashMap<String, CancellationToken>,
|
||||
tlm_listeners: &mut HashMap<Uuid, CancellationToken>,
|
||||
) {
|
||||
match request {
|
||||
WebsocketRequest::RegisterTlmListener(request) => {
|
||||
@@ -110,7 +111,7 @@ async fn handle_websocket_incoming(
|
||||
data: &Arc<TelemetryManagementService>,
|
||||
session: &mut Session,
|
||||
tx: &Sender<WebsocketResponse>,
|
||||
tlm_listeners: &mut HashMap<String, CancellationToken>,
|
||||
tlm_listeners: &mut HashMap<Uuid, CancellationToken>,
|
||||
) -> anyhow::Result<bool> {
|
||||
match msg {
|
||||
Ok(AggregatedMessage::Close(_)) => Ok(false),
|
||||
@@ -130,7 +131,7 @@ async fn handle_websocket_incoming(
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn websocket_connect(
|
||||
async fn websocket_connect(
|
||||
req: HttpRequest,
|
||||
stream: web::Payload,
|
||||
data: web::Data<Arc<TelemetryManagementService>>,
|
||||
|
||||
@@ -1,15 +1,16 @@
|
||||
use derive_more::From;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct RegisterTlmListenerRequest {
|
||||
pub uuid: String,
|
||||
pub uuid: Uuid,
|
||||
pub minimum_separation_ms: u32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct UnregisterTlmListenerRequest {
|
||||
pub uuid: String,
|
||||
pub uuid: Uuid,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, From)]
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
use crate::telemetry::data_item::TelemetryDataItem;
|
||||
use derive_more::From;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct TlmValueResponse {
|
||||
pub uuid: String,
|
||||
pub uuid: Uuid,
|
||||
pub value: Option<TelemetryDataItem>,
|
||||
}
|
||||
|
||||
|
||||
@@ -1,14 +1,8 @@
|
||||
mod command;
|
||||
mod grpc;
|
||||
mod http;
|
||||
mod panels;
|
||||
mod serialization;
|
||||
mod telemetry;
|
||||
mod uuid;
|
||||
|
||||
pub mod core {
|
||||
tonic::include_proto!("core");
|
||||
}
|
||||
|
||||
use crate::command::service::CommandManagementService;
|
||||
use crate::panels::PanelService;
|
||||
@@ -53,14 +47,11 @@ pub async fn setup() -> anyhow::Result<()> {
|
||||
|
||||
let cmd = Arc::new(CommandManagementService::new());
|
||||
|
||||
let grpc_server = grpc::setup(cancellation_token.clone(), tlm.clone(), cmd.clone())?;
|
||||
|
||||
let panel_service = PanelService::new(sqlite.clone());
|
||||
|
||||
let result = http::setup(cancellation_token.clone(), tlm.clone(), panel_service, cmd).await;
|
||||
cancellation_token.cancel();
|
||||
result?; // result is dropped
|
||||
grpc_server.await?; //grpc server is dropped
|
||||
drop(cancellation_token); // All cancellation tokens are now dropped
|
||||
|
||||
sqlite.close().await;
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use fern::colors::{Color, ColoredLevelConfig};
|
||||
use std::env;
|
||||
use std::str::FromStr;
|
||||
|
||||
@@ -9,14 +10,18 @@ async fn main() -> anyhow::Result<()> {
|
||||
Err(_) => log::LevelFilter::Info,
|
||||
};
|
||||
|
||||
let colors = ColoredLevelConfig::new()
|
||||
.info(Color::Green)
|
||||
.debug(Color::Blue);
|
||||
|
||||
let mut log_config = fern::Dispatch::new()
|
||||
.format(|out, message, record| {
|
||||
.format(move |out, message, record| {
|
||||
out.finish(format_args!(
|
||||
"[{}][{}][{}] {}",
|
||||
"[{} {} {}] {}",
|
||||
chrono::Utc::now().format("%Y-%m-%d %H:%M:%S"),
|
||||
colors.color(record.level()),
|
||||
record.target(),
|
||||
record.level(),
|
||||
message
|
||||
message,
|
||||
))
|
||||
})
|
||||
.level(log::LevelFilter::Warn)
|
||||
@@ -27,5 +32,6 @@ async fn main() -> anyhow::Result<()> {
|
||||
log_config = log_config.chain(fern::log_file(log_file)?)
|
||||
}
|
||||
log_config.apply()?;
|
||||
|
||||
server::setup().await
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
pub mod panel;
|
||||
|
||||
use crate::core::Uuid;
|
||||
use crate::panels::panel::{PanelRequired, PanelUpdate};
|
||||
use panel::Panel;
|
||||
use sqlx::SqlitePool;
|
||||
use uuid::Uuid;
|
||||
|
||||
pub struct PanelService {
|
||||
pool: SqlitePool,
|
||||
@@ -15,7 +15,8 @@ impl PanelService {
|
||||
}
|
||||
|
||||
pub async fn create(&self, name: &str, data: &str) -> anyhow::Result<Uuid> {
|
||||
let id = Uuid::random();
|
||||
let id = Uuid::new_v4();
|
||||
let id_string = id.to_string();
|
||||
|
||||
let mut transaction = self.pool.begin().await?;
|
||||
|
||||
@@ -24,7 +25,7 @@ impl PanelService {
|
||||
INSERT INTO PANELS (id, name, data, deleted)
|
||||
VALUES ($1, $2, $3, FALSE);
|
||||
"#,
|
||||
id.value,
|
||||
id_string,
|
||||
name,
|
||||
data
|
||||
)
|
||||
@@ -65,7 +66,7 @@ impl PanelService {
|
||||
WHERE id = $1 AND deleted = FALSE
|
||||
"#,
|
||||
)
|
||||
.bind(id.value)
|
||||
.bind(id.to_string())
|
||||
.fetch_optional(&mut *transaction)
|
||||
.await?;
|
||||
|
||||
@@ -75,6 +76,7 @@ impl PanelService {
|
||||
}
|
||||
|
||||
pub async fn update(&self, id: Uuid, data: PanelUpdate) -> anyhow::Result<()> {
|
||||
let id = id.to_string();
|
||||
let mut transaction = self.pool.begin().await?;
|
||||
|
||||
if let Some(name) = data.name {
|
||||
@@ -84,7 +86,7 @@ impl PanelService {
|
||||
SET name = $2
|
||||
WHERE id = $1;
|
||||
"#,
|
||||
id.value,
|
||||
id,
|
||||
name
|
||||
)
|
||||
.execute(&mut *transaction)
|
||||
@@ -97,7 +99,7 @@ impl PanelService {
|
||||
SET data = $2
|
||||
WHERE id = $1;
|
||||
"#,
|
||||
id.value,
|
||||
id,
|
||||
data
|
||||
)
|
||||
.execute(&mut *transaction)
|
||||
@@ -110,6 +112,7 @@ impl PanelService {
|
||||
}
|
||||
|
||||
pub async fn delete(&self, id: Uuid) -> anyhow::Result<()> {
|
||||
let id = id.to_string();
|
||||
let mut transaction = self.pool.begin().await?;
|
||||
|
||||
let _ = sqlx::query!(
|
||||
@@ -118,7 +121,7 @@ impl PanelService {
|
||||
SET deleted = TRUE
|
||||
WHERE id = $1;
|
||||
"#,
|
||||
id.value,
|
||||
id,
|
||||
)
|
||||
.execute(&mut *transaction)
|
||||
.await?;
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
use crate::telemetry::data_value::TelemetryDataValue;
|
||||
use api::data_value::DataValue;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct TelemetryDataItem {
|
||||
pub value: TelemetryDataValue,
|
||||
pub value: DataValue,
|
||||
pub timestamp: String,
|
||||
}
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
use crate::core::TelemetryDataType;
|
||||
use serde::de::Visitor;
|
||||
use serde::{Deserializer, Serializer};
|
||||
use std::fmt::Formatter;
|
||||
|
||||
pub fn tlm_data_type_serializer<S>(
|
||||
tlm_data_type: &TelemetryDataType,
|
||||
serializer: S,
|
||||
) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
serializer.serialize_str(tlm_data_type.as_str_name())
|
||||
}
|
||||
|
||||
struct TlmDataTypeVisitor;
|
||||
|
||||
impl Visitor<'_> for TlmDataTypeVisitor {
|
||||
type Value = TelemetryDataType;
|
||||
|
||||
fn expecting(&self, formatter: &mut Formatter) -> std::fmt::Result {
|
||||
formatter.write_str("A &str")
|
||||
}
|
||||
|
||||
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
|
||||
where
|
||||
E: serde::de::Error,
|
||||
{
|
||||
TelemetryDataType::from_str_name(v).ok_or(E::custom("Invalid TelemetryDataType"))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn tlm_data_type_deserializer<'de, D>(deserializer: D) -> Result<TelemetryDataType, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
deserializer.deserialize_str(TlmDataTypeVisitor)
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub enum TelemetryDataValue {
|
||||
Float32(f32),
|
||||
Float64(f64),
|
||||
Boolean(bool),
|
||||
}
|
||||
@@ -1,13 +1,10 @@
|
||||
use crate::core::TelemetryDataType;
|
||||
use crate::telemetry::data_type::tlm_data_type_deserializer;
|
||||
use crate::telemetry::data_type::tlm_data_type_serializer;
|
||||
use api::data_type::DataType;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct TelemetryDefinition {
|
||||
pub uuid: String,
|
||||
pub uuid: Uuid,
|
||||
pub name: String,
|
||||
#[serde(serialize_with = "tlm_data_type_serializer")]
|
||||
#[serde(deserialize_with = "tlm_data_type_deserializer")]
|
||||
pub data_type: TelemetryDataType,
|
||||
pub data_type: DataType,
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
use crate::core::TelemetryDataType;
|
||||
use crate::serialization::file_ext::{ReadExt, WriteExt};
|
||||
use crate::telemetry::data::TelemetryData;
|
||||
use crate::telemetry::data_item::TelemetryDataItem;
|
||||
use crate::telemetry::data_value::TelemetryDataValue;
|
||||
use crate::telemetry::definition::TelemetryDefinition;
|
||||
use anyhow::{anyhow, ensure, Context};
|
||||
use api::data_type::DataType;
|
||||
use api::data_value::DataValue;
|
||||
use chrono::{DateTime, DurationRound, SecondsFormat, TimeDelta, Utc};
|
||||
use log::{error, info};
|
||||
use std::cmp::min;
|
||||
@@ -44,7 +44,7 @@ fn update_next_from(
|
||||
}
|
||||
|
||||
struct SegmentData {
|
||||
values: Vec<TelemetryDataValue>,
|
||||
values: Vec<DataValue>,
|
||||
timestamps: Vec<DateTime<Utc>>,
|
||||
}
|
||||
|
||||
@@ -66,7 +66,7 @@ impl HistorySegmentRam {
|
||||
}
|
||||
}
|
||||
|
||||
fn insert(&self, value: TelemetryDataValue, timestamp: DateTime<Utc>) {
|
||||
fn insert(&self, value: DataValue, timestamp: DateTime<Utc>) {
|
||||
if timestamp < self.start || timestamp >= self.end {
|
||||
return;
|
||||
}
|
||||
@@ -121,7 +121,7 @@ impl HistorySegmentRam {
|
||||
next_from,
|
||||
);
|
||||
result.push(TelemetryDataItem {
|
||||
value: data.values[i].clone(),
|
||||
value: data.values[i],
|
||||
timestamp: t.to_rfc3339_opts(SecondsFormat::Millis, true),
|
||||
});
|
||||
}
|
||||
@@ -196,9 +196,9 @@ impl HistorySegmentFile {
|
||||
// Write all the values
|
||||
for value in &data.values {
|
||||
match value {
|
||||
TelemetryDataValue::Float32(value) => file.write_data::<f32>(*value)?,
|
||||
TelemetryDataValue::Float64(value) => file.write_data::<f64>(*value)?,
|
||||
TelemetryDataValue::Boolean(value) => file.write_data::<bool>(*value)?,
|
||||
DataValue::Float32(value) => file.write_data::<f32>(*value)?,
|
||||
DataValue::Float64(value) => file.write_data::<f64>(*value)?,
|
||||
DataValue::Boolean(value) => file.write_data::<bool>(*value)?,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -215,10 +215,7 @@ impl HistorySegmentFile {
|
||||
})
|
||||
}
|
||||
|
||||
fn load_to_ram(
|
||||
mut self,
|
||||
telemetry_data_type: TelemetryDataType,
|
||||
) -> anyhow::Result<HistorySegmentRam> {
|
||||
fn load_to_ram(mut self, telemetry_data_type: DataType) -> anyhow::Result<HistorySegmentRam> {
|
||||
let mut segment_data = SegmentData {
|
||||
values: Vec::with_capacity(self.length as usize),
|
||||
timestamps: Vec::with_capacity(self.length as usize),
|
||||
@@ -281,7 +278,7 @@ impl HistorySegmentFile {
|
||||
from: DateTime<Utc>,
|
||||
to: DateTime<Utc>,
|
||||
maximum_resolution: TimeDelta,
|
||||
telemetry_data_type: TelemetryDataType,
|
||||
telemetry_data_type: DataType,
|
||||
) -> anyhow::Result<(DateTime<Utc>, Vec<TelemetryDataItem>)> {
|
||||
self.file_position = 0;
|
||||
self.file.seek(SeekFrom::Start(0))?;
|
||||
@@ -334,22 +331,19 @@ impl HistorySegmentFile {
|
||||
self.read_date_time()
|
||||
}
|
||||
|
||||
fn read_telemetry_item(
|
||||
&mut self,
|
||||
telemetry_data_type: TelemetryDataType,
|
||||
) -> anyhow::Result<TelemetryDataValue> {
|
||||
fn read_telemetry_item(&mut self, telemetry_data_type: DataType) -> anyhow::Result<DataValue> {
|
||||
match telemetry_data_type {
|
||||
TelemetryDataType::Float32 => {
|
||||
DataType::Float32 => {
|
||||
self.file_position += 4;
|
||||
Ok(TelemetryDataValue::Float32(self.file.read_data::<f32>()?))
|
||||
Ok(DataValue::Float32(self.file.read_data::<f32>()?))
|
||||
}
|
||||
TelemetryDataType::Float64 => {
|
||||
DataType::Float64 => {
|
||||
self.file_position += 8;
|
||||
Ok(TelemetryDataValue::Float64(self.file.read_data::<f64>()?))
|
||||
Ok(DataValue::Float64(self.file.read_data::<f64>()?))
|
||||
}
|
||||
TelemetryDataType::Boolean => {
|
||||
DataType::Boolean => {
|
||||
self.file_position += 1;
|
||||
Ok(TelemetryDataValue::Boolean(self.file.read_data::<bool>()?))
|
||||
Ok(DataValue::Boolean(self.file.read_data::<bool>()?))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -357,12 +351,12 @@ impl HistorySegmentFile {
|
||||
fn get_telemetry_item(
|
||||
&mut self,
|
||||
index: u64,
|
||||
telemetry_data_type: TelemetryDataType,
|
||||
) -> anyhow::Result<TelemetryDataValue> {
|
||||
telemetry_data_type: DataType,
|
||||
) -> anyhow::Result<DataValue> {
|
||||
let item_length = match telemetry_data_type {
|
||||
TelemetryDataType::Float32 => 4,
|
||||
TelemetryDataType::Float64 => 8,
|
||||
TelemetryDataType::Boolean => 1,
|
||||
DataType::Float32 => 4,
|
||||
DataType::Float64 => 8,
|
||||
DataType::Boolean => 1,
|
||||
};
|
||||
let desired_position =
|
||||
Self::HEADER_LENGTH + self.length * Self::TIMESTAMP_LENGTH + index * item_length;
|
||||
@@ -429,7 +423,7 @@ impl TelemetryHistory {
|
||||
history_segment_ram: HistorySegmentRam,
|
||||
) -> JoinHandle<()> {
|
||||
let mut path = service.data_root_folder.clone();
|
||||
path.push(&self.data.definition.uuid);
|
||||
path.push(self.data.definition.uuid.as_hyphenated().to_string());
|
||||
spawn_blocking(move || {
|
||||
match HistorySegmentFile::save_to_disk(path, history_segment_ram) {
|
||||
// Immediately drop the segment - now that we've saved it to disk we don't need to keep it in memory
|
||||
@@ -450,7 +444,7 @@ impl TelemetryHistory {
|
||||
start: DateTime<Utc>,
|
||||
) -> JoinHandle<anyhow::Result<HistorySegmentFile>> {
|
||||
let mut path = service.data_root_folder.clone();
|
||||
path.push(&self.data.definition.uuid);
|
||||
path.push(self.data.definition.uuid.as_hyphenated().to_string());
|
||||
spawn_blocking(move || HistorySegmentFile::open(path, start))
|
||||
}
|
||||
|
||||
@@ -458,7 +452,7 @@ impl TelemetryHistory {
|
||||
&self,
|
||||
start: DateTime<Utc>,
|
||||
service: &TelemetryHistoryService,
|
||||
telemetry_data_type: TelemetryDataType,
|
||||
telemetry_data_type: DataType,
|
||||
) -> HistorySegmentRam {
|
||||
let ram = self
|
||||
.get_disk_segment(service, start)
|
||||
@@ -480,7 +474,7 @@ impl TelemetryHistory {
|
||||
pub async fn insert(
|
||||
&self,
|
||||
service: &TelemetryHistoryService,
|
||||
value: TelemetryDataValue,
|
||||
value: DataValue,
|
||||
timestamp: DateTime<Utc>,
|
||||
) {
|
||||
let segments = self.segments.read().await;
|
||||
@@ -531,7 +525,7 @@ impl TelemetryHistory {
|
||||
pub fn insert_sync(
|
||||
history: Arc<Self>,
|
||||
service: Arc<TelemetryHistoryService>,
|
||||
value: TelemetryDataValue,
|
||||
value: DataValue,
|
||||
timestamp: DateTime<Utc>,
|
||||
) {
|
||||
tokio::spawn(async move {
|
||||
@@ -579,7 +573,7 @@ impl TelemetryHistory {
|
||||
.unwrap();
|
||||
|
||||
let mut path = telemetry_history_service.data_root_folder.clone();
|
||||
path.push(&self.data.definition.uuid);
|
||||
path.push(self.data.definition.uuid.as_hyphenated().to_string());
|
||||
|
||||
let mut start = start;
|
||||
while start < end {
|
||||
|
||||
@@ -1,7 +1,15 @@
|
||||
use crate::core::{TelemetryDefinitionRequest, Uuid};
|
||||
use crate::telemetry::data::TelemetryData;
|
||||
use crate::telemetry::data_item::TelemetryDataItem;
|
||||
use crate::telemetry::definition::TelemetryDefinition;
|
||||
use crate::telemetry::history::{TelemetryHistory, TelemetryHistoryService};
|
||||
use anyhow::bail;
|
||||
use api::data_type::DataType;
|
||||
use api::data_value::DataValue;
|
||||
use api::messages::telemetry_definition::{
|
||||
TelemetryDefinitionRequest, TelemetryDefinitionResponse,
|
||||
};
|
||||
use api::messages::telemetry_entry::TelemetryEntry;
|
||||
use chrono::SecondsFormat;
|
||||
use log::{error, info, warn};
|
||||
use papaya::{HashMap, HashMapRef, LocalGuard};
|
||||
use std::fs;
|
||||
@@ -12,12 +20,13 @@ use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use tokio::sync::Mutex;
|
||||
use tokio::time::sleep;
|
||||
use uuid::Uuid;
|
||||
|
||||
const RELEASED_ATTEMPTS: usize = 5;
|
||||
|
||||
pub struct TelemetryManagementService {
|
||||
uuid_index: HashMap<String, String>,
|
||||
tlm_data: HashMap<String, Arc<TelemetryHistory>>,
|
||||
uuid_index: HashMap<String, Uuid>,
|
||||
tlm_data: HashMap<Uuid, Arc<TelemetryHistory>>,
|
||||
telemetry_history_service: Arc<TelemetryHistoryService>,
|
||||
metadata_file: Arc<Mutex<File>>,
|
||||
}
|
||||
@@ -49,8 +58,8 @@ impl TelemetryManagementService {
|
||||
// Skip invalid entries
|
||||
match serde_json::from_str::<TelemetryDefinition>(line) {
|
||||
Ok(tlm_def) => {
|
||||
let _ = uuid_index.insert(tlm_def.name.clone(), tlm_def.uuid.clone());
|
||||
let _ = tlm_data.insert(tlm_def.uuid.clone(), Arc::new(tlm_def.into()));
|
||||
let _ = uuid_index.insert(tlm_def.name.clone(), tlm_def.uuid);
|
||||
let _ = tlm_data.insert(tlm_def.uuid, Arc::new(tlm_def.into()));
|
||||
}
|
||||
Err(err) => {
|
||||
error!("Failed to parse metadata entry {err}");
|
||||
@@ -79,23 +88,20 @@ impl TelemetryManagementService {
|
||||
pub fn register(
|
||||
&self,
|
||||
telemetry_definition_request: TelemetryDefinitionRequest,
|
||||
) -> anyhow::Result<String> {
|
||||
) -> anyhow::Result<TelemetryDefinitionResponse> {
|
||||
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 uuid =
|
||||
*uuid_index.get_or_insert_with(telemetry_definition_request.name.clone(), Uuid::new_v4);
|
||||
|
||||
let inserted = tlm_data.try_insert(
|
||||
uuid.clone(),
|
||||
uuid,
|
||||
Arc::new(
|
||||
TelemetryDefinition {
|
||||
uuid: uuid.clone(),
|
||||
uuid,
|
||||
name: telemetry_definition_request.name.clone(),
|
||||
data_type: telemetry_definition_request.data_type(),
|
||||
data_type: telemetry_definition_request.data_type,
|
||||
}
|
||||
.into(),
|
||||
),
|
||||
@@ -129,7 +135,38 @@ impl TelemetryManagementService {
|
||||
});
|
||||
}
|
||||
|
||||
Ok(uuid)
|
||||
Ok(TelemetryDefinitionResponse { uuid })
|
||||
}
|
||||
|
||||
pub fn add_tlm_item(&self, tlm_item: TelemetryEntry) -> anyhow::Result<()> {
|
||||
let tlm_management_pin = self.pin();
|
||||
let Some(tlm_data) = tlm_management_pin.get_by_uuid(&tlm_item.uuid) else {
|
||||
bail!("Telemetry Item Not Found");
|
||||
};
|
||||
|
||||
let expected_type = match &tlm_item.value {
|
||||
DataValue::Float32(_) => DataType::Float32,
|
||||
DataValue::Float64(_) => DataType::Float64,
|
||||
DataValue::Boolean(_) => DataType::Boolean,
|
||||
};
|
||||
if expected_type != tlm_data.data.definition.data_type {
|
||||
bail!("Data Type Mismatch");
|
||||
};
|
||||
|
||||
let _ = tlm_data.data.data.send_replace(Some(TelemetryDataItem {
|
||||
value: tlm_item.value,
|
||||
timestamp: tlm_item
|
||||
.timestamp
|
||||
.to_rfc3339_opts(SecondsFormat::Millis, true),
|
||||
}));
|
||||
TelemetryHistory::insert_sync(
|
||||
tlm_data.clone(),
|
||||
self.history_service(),
|
||||
tlm_item.value,
|
||||
tlm_item.timestamp,
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get_by_name(&self, name: &String) -> Option<TelemetryData> {
|
||||
@@ -138,7 +175,7 @@ impl TelemetryManagementService {
|
||||
self.get_by_uuid(uuid)
|
||||
}
|
||||
|
||||
pub fn get_by_uuid(&self, uuid: &String) -> Option<TelemetryData> {
|
||||
pub fn get_by_uuid(&self, uuid: &Uuid) -> Option<TelemetryData> {
|
||||
let tlm_data = self.tlm_data.pin();
|
||||
tlm_data
|
||||
.get(uuid)
|
||||
@@ -200,11 +237,11 @@ impl TelemetryManagementService {
|
||||
}
|
||||
|
||||
pub struct TelemetryManagementServicePin<'a> {
|
||||
tlm_data: HashMapRef<'a, String, Arc<TelemetryHistory>, RandomState, LocalGuard<'a>>,
|
||||
tlm_data: HashMapRef<'a, Uuid, Arc<TelemetryHistory>, RandomState, LocalGuard<'a>>,
|
||||
}
|
||||
|
||||
impl<'a> TelemetryManagementServicePin<'a> {
|
||||
pub fn get_by_uuid(&'a self, uuid: &String) -> Option<&'a Arc<TelemetryHistory>> {
|
||||
pub fn get_by_uuid(&'a self, uuid: &Uuid) -> Option<&'a Arc<TelemetryHistory>> {
|
||||
self.tlm_data.get(uuid)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
pub mod data;
|
||||
pub mod data_item;
|
||||
pub mod data_type;
|
||||
pub mod data_value;
|
||||
pub mod definition;
|
||||
pub mod history;
|
||||
pub mod management_service;
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
use crate::core::Uuid;
|
||||
use rand::RngCore;
|
||||
|
||||
impl Uuid {
|
||||
pub fn random() -> Self {
|
||||
let mut uuid = [0u8; 16];
|
||||
rand::rng().fill_bytes(&mut uuid);
|
||||
Self {
|
||||
value: hex::encode(uuid),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for Uuid {
|
||||
fn from(value: String) -> Self {
|
||||
Self { value }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user