Compare commits
6 Commits
main
...
a3aeff1d6f
| Author | SHA1 | Date | |
|---|---|---|---|
| a3aeff1d6f | |||
| 6a5e3e2b24 | |||
| 9228bca4eb | |||
| 6980b7f6aa | |||
| 29f7f6d83b | |||
| 8d737e8f33 |
905
Cargo.lock
generated
905
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
23
Cargo.toml
23
Cargo.toml
@@ -1,6 +1,27 @@
|
|||||||
[workspace]
|
[workspace]
|
||||||
members = ["server", "examples/simple_producer", "examples/simple_command"]
|
members = ["api", "server", "examples/simple_producer", "examples/simple_command"]
|
||||||
resolver = "2"
|
resolver = "2"
|
||||||
|
|
||||||
|
[workspace.dependencies]
|
||||||
|
actix-web = "4.12.1"
|
||||||
|
actix-ws = "0.3.0"
|
||||||
|
anyhow = "1.0.100"
|
||||||
|
chrono = { version = "0.4.42" }
|
||||||
|
derive_more = { version = "2.1.1" }
|
||||||
|
env_logger = "0.11.8"
|
||||||
|
fern = "0.7.1"
|
||||||
|
futures-util = "0.3.31"
|
||||||
|
log = "0.4.29"
|
||||||
|
num-traits = "0.2.19"
|
||||||
|
papaya = "0.2.3"
|
||||||
|
serde = { version = "1.0.228" }
|
||||||
|
serde_json = "1.0.148"
|
||||||
|
sqlx = "0.8.6"
|
||||||
|
thiserror = "2.0.17"
|
||||||
|
tokio = { version = "1.48.0" }
|
||||||
|
tokio-tungstenite = { version = "0.28.0" }
|
||||||
|
tokio-util = "0.7.17"
|
||||||
|
uuid = { version = "1.19.0", features = ["v4"] }
|
||||||
|
|
||||||
[profile.dev.package.sqlx-macros]
|
[profile.dev.package.sqlx-macros]
|
||||||
opt-level = 3
|
opt-level = 3
|
||||||
|
|||||||
19
api/Cargo.toml
Normal file
19
api/Cargo.toml
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
|
||||||
|
[package]
|
||||||
|
name = "api"
|
||||||
|
edition = "2021"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Sergey <me@sergeysav.com>"]
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
log = { workspace = true }
|
||||||
|
thiserror = { workspace = true }
|
||||||
|
serde = { workspace = true, features = ["derive"] }
|
||||||
|
derive_more = { workspace = true, features = ["from", "try_into"] }
|
||||||
|
uuid = { workspace = true, features = ["serde"] }
|
||||||
|
chrono = { workspace = true, features = ["serde"] }
|
||||||
|
tokio = { workspace = true, features = ["rt", "macros", "time"] }
|
||||||
|
tokio-tungstenite = { workspace = true, features = ["rustls-tls-native-roots"] }
|
||||||
|
tokio-util = { workspace = true }
|
||||||
|
futures-util = { workspace = true }
|
||||||
|
serde_json = { workspace = true }
|
||||||
31
api/src/client/error.rs
Normal file
31
api/src/client/error.rs
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
|
#[derive(Error, Debug)]
|
||||||
|
pub enum ConnectError {
|
||||||
|
#[error(transparent)]
|
||||||
|
TungsteniteError(#[from] tokio_tungstenite::tungstenite::Error),
|
||||||
|
#[error(transparent)]
|
||||||
|
IoError(#[from] std::io::Error),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Error, Debug)]
|
||||||
|
pub enum MessageError {
|
||||||
|
#[error(transparent)]
|
||||||
|
TokioSendError(#[from] tokio::sync::mpsc::error::SendError<()>),
|
||||||
|
#[error(transparent)]
|
||||||
|
TokioTrySendError(#[from] tokio::sync::mpsc::error::TrySendError<()>),
|
||||||
|
#[error(transparent)]
|
||||||
|
TokioLockError(#[from] tokio::sync::TryLockError),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Error, Debug)]
|
||||||
|
pub enum RequestError<E> {
|
||||||
|
#[error(transparent)]
|
||||||
|
TokioSendError(#[from] tokio::sync::mpsc::error::SendError<()>),
|
||||||
|
#[error(transparent)]
|
||||||
|
TokioLockError(#[from] tokio::sync::TryLockError),
|
||||||
|
#[error(transparent)]
|
||||||
|
RecvError(#[from] tokio::sync::oneshot::error::RecvError),
|
||||||
|
#[error(transparent)]
|
||||||
|
Inner(E),
|
||||||
|
}
|
||||||
483
api/src/client/mod.rs
Normal file
483
api/src/client/mod.rs
Normal file
@@ -0,0 +1,483 @@
|
|||||||
|
pub mod error;
|
||||||
|
|
||||||
|
use crate::client::error::{MessageError, RequestError};
|
||||||
|
use crate::messages::callback::GenericCallbackError;
|
||||||
|
use crate::messages::payload::RequestMessagePayload;
|
||||||
|
use crate::messages::payload::ResponseMessagePayload;
|
||||||
|
use crate::messages::{
|
||||||
|
ClientMessage, RegisterCallback, RequestMessage, RequestResponse, ResponseMessage,
|
||||||
|
};
|
||||||
|
use error::ConnectError;
|
||||||
|
use futures_util::stream::StreamExt;
|
||||||
|
use futures_util::SinkExt;
|
||||||
|
use log::{debug, error, info, trace, warn};
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::sync::mpsc::sync_channel;
|
||||||
|
use std::sync::Arc;
|
||||||
|
use std::thread;
|
||||||
|
use std::time::Duration;
|
||||||
|
use tokio::net::TcpStream;
|
||||||
|
use tokio::sync::{mpsc, oneshot, watch, RwLock, RwLockWriteGuard};
|
||||||
|
use tokio::time::sleep;
|
||||||
|
use tokio::{select, spawn};
|
||||||
|
use tokio_tungstenite::tungstenite::client::IntoClientRequest;
|
||||||
|
use tokio_tungstenite::tungstenite::handshake::client::Request;
|
||||||
|
use tokio_tungstenite::tungstenite::Message;
|
||||||
|
use tokio_tungstenite::{connect_async, MaybeTlsStream, WebSocketStream};
|
||||||
|
use tokio_util::sync::CancellationToken;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
type RegisteredCallback = mpsc::Sender<(ResponseMessage, oneshot::Sender<RequestMessagePayload>)>;
|
||||||
|
type ClientChannel = Arc<RwLock<mpsc::Sender<OutgoingMessage>>>;
|
||||||
|
|
||||||
|
enum Callback {
|
||||||
|
None,
|
||||||
|
Once(oneshot::Sender<ResponseMessage>),
|
||||||
|
Registered(RegisteredCallback),
|
||||||
|
}
|
||||||
|
|
||||||
|
struct OutgoingMessage {
|
||||||
|
msg: RequestMessage,
|
||||||
|
callback: Callback,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Client {
|
||||||
|
cancel: CancellationToken,
|
||||||
|
channel: ClientChannel,
|
||||||
|
connected_state_rx: watch::Receiver<bool>,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ClientContext {
|
||||||
|
cancel: CancellationToken,
|
||||||
|
request: Request,
|
||||||
|
connected_state_tx: watch::Sender<bool>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Client {
|
||||||
|
pub fn connect<R>(request: R) -> Result<Self, ConnectError>
|
||||||
|
where
|
||||||
|
R: IntoClientRequest,
|
||||||
|
{
|
||||||
|
let (tx, _rx) = mpsc::channel(1);
|
||||||
|
let cancel = CancellationToken::new();
|
||||||
|
let channel = Arc::new(RwLock::new(tx));
|
||||||
|
let (connected_state_tx, connected_state_rx) = watch::channel(false);
|
||||||
|
let context = ClientContext {
|
||||||
|
cancel: cancel.clone(),
|
||||||
|
request: request.into_client_request()?,
|
||||||
|
connected_state_tx,
|
||||||
|
};
|
||||||
|
|
||||||
|
context.start(channel.clone())?;
|
||||||
|
|
||||||
|
Ok(Self {
|
||||||
|
cancel,
|
||||||
|
channel,
|
||||||
|
connected_state_rx,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn send_message<M: ClientMessage>(&self, msg: M) -> Result<(), MessageError> {
|
||||||
|
let sender = self.channel.read().await;
|
||||||
|
let data = sender.reserve().await?;
|
||||||
|
data.send(OutgoingMessage {
|
||||||
|
msg: RequestMessage {
|
||||||
|
uuid: Uuid::new_v4(),
|
||||||
|
response: None,
|
||||||
|
payload: msg.into(),
|
||||||
|
},
|
||||||
|
callback: Callback::None,
|
||||||
|
});
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn send_message_if_connected<M: ClientMessage>(
|
||||||
|
&self,
|
||||||
|
msg: M,
|
||||||
|
) -> Result<(), MessageError> {
|
||||||
|
let sender = self.channel.try_read()?;
|
||||||
|
let data = sender.reserve().await?;
|
||||||
|
data.send(OutgoingMessage {
|
||||||
|
msg: RequestMessage {
|
||||||
|
uuid: Uuid::new_v4(),
|
||||||
|
response: None,
|
||||||
|
payload: msg.into(),
|
||||||
|
},
|
||||||
|
callback: Callback::None,
|
||||||
|
});
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn try_send_message<M: ClientMessage>(&self, msg: M) -> Result<(), MessageError> {
|
||||||
|
let sender = self.channel.try_read()?;
|
||||||
|
let data = sender.try_reserve()?;
|
||||||
|
data.send(OutgoingMessage {
|
||||||
|
msg: RequestMessage {
|
||||||
|
uuid: Uuid::new_v4(),
|
||||||
|
response: None,
|
||||||
|
payload: msg.into(),
|
||||||
|
},
|
||||||
|
callback: Callback::None,
|
||||||
|
});
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn send_request<M: RequestResponse>(
|
||||||
|
&self,
|
||||||
|
msg: M,
|
||||||
|
) -> Result<M::Response, RequestError<<M::Response as TryFrom<ResponseMessagePayload>>::Error>>
|
||||||
|
{
|
||||||
|
let sender = self.channel.read().await;
|
||||||
|
let data = sender.reserve().await?;
|
||||||
|
|
||||||
|
let (tx, rx) = oneshot::channel();
|
||||||
|
data.send(OutgoingMessage {
|
||||||
|
msg: RequestMessage {
|
||||||
|
uuid: Uuid::new_v4(),
|
||||||
|
response: None,
|
||||||
|
payload: msg.into(),
|
||||||
|
},
|
||||||
|
callback: Callback::Once(tx),
|
||||||
|
});
|
||||||
|
|
||||||
|
let response = rx.await?;
|
||||||
|
let response = M::Response::try_from(response.payload).map_err(RequestError::Inner)?;
|
||||||
|
|
||||||
|
Ok(response)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn register_callback_channel<M: RegisterCallback>(
|
||||||
|
&self,
|
||||||
|
msg: M,
|
||||||
|
) -> Result<mpsc::Receiver<(M::Callback, oneshot::Sender<M::Response>)>, MessageError>
|
||||||
|
where
|
||||||
|
<M as RegisterCallback>::Callback: Send + 'static,
|
||||||
|
<M as RegisterCallback>::Response: Send + 'static,
|
||||||
|
<<M as RegisterCallback>::Callback as TryFrom<ResponseMessagePayload>>::Error: Send,
|
||||||
|
{
|
||||||
|
let sender = self.channel.read().await;
|
||||||
|
let data = sender.reserve().await?;
|
||||||
|
|
||||||
|
let (inner_tx, mut inner_rx) = mpsc::channel(16);
|
||||||
|
let (outer_tx, outer_rx) = mpsc::channel(1);
|
||||||
|
|
||||||
|
data.send(OutgoingMessage {
|
||||||
|
msg: RequestMessage {
|
||||||
|
uuid: Uuid::new_v4(),
|
||||||
|
response: None,
|
||||||
|
payload: msg.into(),
|
||||||
|
},
|
||||||
|
callback: Callback::Registered(inner_tx),
|
||||||
|
});
|
||||||
|
|
||||||
|
spawn(async move {
|
||||||
|
// If the handler was unregistered we can stop
|
||||||
|
while let Some((msg, responder)) = inner_rx.recv().await {
|
||||||
|
let response: RequestMessagePayload = match M::Callback::try_from(msg.payload) {
|
||||||
|
Err(_) => GenericCallbackError::MismatchedType.into(),
|
||||||
|
Ok(o) => {
|
||||||
|
let (response_tx, response_rx) = oneshot::channel::<M::Response>();
|
||||||
|
match outer_tx.send((o, response_tx)).await {
|
||||||
|
Err(_) => GenericCallbackError::CallbackClosed.into(),
|
||||||
|
Ok(()) => response_rx
|
||||||
|
.await
|
||||||
|
.map(M::Response::into)
|
||||||
|
.unwrap_or_else(|_| GenericCallbackError::CallbackClosed.into()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if responder.send(response).is_err() {
|
||||||
|
// If the callback was unregistered we can stop
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Ok(outer_rx)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn register_callback_fn<M: RegisterCallback, F>(
|
||||||
|
&self,
|
||||||
|
msg: M,
|
||||||
|
mut f: F,
|
||||||
|
) -> Result<(), MessageError>
|
||||||
|
where
|
||||||
|
F: FnMut(M::Callback) -> M::Response + Send + 'static,
|
||||||
|
{
|
||||||
|
let sender = self.channel.read().await;
|
||||||
|
let data = sender.reserve().await?;
|
||||||
|
|
||||||
|
let (inner_tx, mut inner_rx) = mpsc::channel(16);
|
||||||
|
|
||||||
|
data.send(OutgoingMessage {
|
||||||
|
msg: RequestMessage {
|
||||||
|
uuid: Uuid::new_v4(),
|
||||||
|
response: None,
|
||||||
|
payload: msg.into(),
|
||||||
|
},
|
||||||
|
callback: Callback::Registered(inner_tx),
|
||||||
|
});
|
||||||
|
|
||||||
|
spawn(async move {
|
||||||
|
// If the handler was unregistered we can stop
|
||||||
|
while let Some((msg, responder)) = inner_rx.recv().await {
|
||||||
|
let response: RequestMessagePayload = match M::Callback::try_from(msg.payload) {
|
||||||
|
Err(_) => GenericCallbackError::MismatchedType.into(),
|
||||||
|
Ok(o) => f(o).into(),
|
||||||
|
};
|
||||||
|
|
||||||
|
if responder.send(response).is_err() {
|
||||||
|
// If the callback was unregistered we can stop
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn wait_connected(&self) {
|
||||||
|
let mut connected_rx = self.connected_state_rx.clone();
|
||||||
|
|
||||||
|
// If we aren't currently connected
|
||||||
|
if !*connected_rx.borrow_and_update() {
|
||||||
|
// Wait for a change notification
|
||||||
|
// If the channel is closed there is nothing we can do
|
||||||
|
let _ = connected_rx.changed().await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn wait_disconnected(&self) {
|
||||||
|
let mut connected_rx = self.connected_state_rx.clone();
|
||||||
|
|
||||||
|
// If we are currently connected
|
||||||
|
if *connected_rx.borrow_and_update() {
|
||||||
|
// Wait for a change notification
|
||||||
|
// If the channel is closed there is nothing we can do
|
||||||
|
let _ = connected_rx.changed().await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ClientContext {
|
||||||
|
fn start(mut self, channel: ClientChannel) -> Result<(), ConnectError> {
|
||||||
|
let runtime = tokio::runtime::Builder::new_current_thread()
|
||||||
|
.enable_all()
|
||||||
|
.build()?;
|
||||||
|
|
||||||
|
let (tx, rx) = sync_channel::<()>(1);
|
||||||
|
|
||||||
|
let _detached = thread::Builder::new()
|
||||||
|
.name("tlm-client".to_string())
|
||||||
|
.spawn(move || {
|
||||||
|
runtime.block_on(async {
|
||||||
|
let mut write_lock = channel.write().await;
|
||||||
|
|
||||||
|
// This cannot fail
|
||||||
|
let _ = tx.send(());
|
||||||
|
|
||||||
|
while !self.cancel.is_cancelled() {
|
||||||
|
write_lock = self.run_connection(write_lock, &channel).await;
|
||||||
|
}
|
||||||
|
drop(write_lock);
|
||||||
|
});
|
||||||
|
})?;
|
||||||
|
|
||||||
|
// This cannot fail
|
||||||
|
let _ = rx.recv();
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn run_connection<'a>(
|
||||||
|
&mut self,
|
||||||
|
mut write_lock: RwLockWriteGuard<'a, mpsc::Sender<OutgoingMessage>>,
|
||||||
|
channel: &'a ClientChannel,
|
||||||
|
) -> RwLockWriteGuard<'a, mpsc::Sender<OutgoingMessage>> {
|
||||||
|
debug!("Attempting to Connect to {}", self.request.uri());
|
||||||
|
let mut ws = match connect_async(self.request.clone()).await {
|
||||||
|
Ok((ws, _)) => ws,
|
||||||
|
Err(e) => {
|
||||||
|
info!("Failed to Connect: {e}");
|
||||||
|
sleep(Duration::from_secs(1)).await;
|
||||||
|
return write_lock;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
info!("Connected to {}", self.request.uri());
|
||||||
|
|
||||||
|
let (tx, rx) = mpsc::channel(128);
|
||||||
|
*write_lock = tx;
|
||||||
|
drop(write_lock);
|
||||||
|
|
||||||
|
// Don't care about the previous value
|
||||||
|
let _ = self.connected_state_tx.send_replace(true);
|
||||||
|
|
||||||
|
let close_connection = self.handle_connection(&mut ws, rx, channel).await;
|
||||||
|
|
||||||
|
let write_lock = channel.write().await;
|
||||||
|
// Send this after grabbing the lock - to prevent extra contention when others try to grab
|
||||||
|
// the lock to use that as a signal that we have reconnected
|
||||||
|
let _ = self.connected_state_tx.send_replace(false);
|
||||||
|
if close_connection {
|
||||||
|
if let Err(e) = ws.close(None).await {
|
||||||
|
error!("Failed to Close the Connection: {e}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
write_lock
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn handle_connection(
|
||||||
|
&mut self,
|
||||||
|
ws: &mut WebSocketStream<MaybeTlsStream<TcpStream>>,
|
||||||
|
mut rx: mpsc::Receiver<OutgoingMessage>,
|
||||||
|
channel: &ClientChannel,
|
||||||
|
) -> bool {
|
||||||
|
let mut callbacks = HashMap::<Uuid, Callback>::new();
|
||||||
|
loop {
|
||||||
|
select! {
|
||||||
|
_ = self.cancel.cancelled() => { break; },
|
||||||
|
Some(msg) = ws.next() => {
|
||||||
|
match msg {
|
||||||
|
Ok(msg) => {
|
||||||
|
match msg {
|
||||||
|
Message::Text(msg) => {
|
||||||
|
trace!("Incoming: {msg}");
|
||||||
|
let msg: ResponseMessage = match serde_json::from_str(&msg) {
|
||||||
|
Ok(m) => m,
|
||||||
|
Err(e) => {
|
||||||
|
error!("Failed to deserialize {e}");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
self.handle_incoming(msg, &mut callbacks, channel).await;
|
||||||
|
}
|
||||||
|
Message::Binary(_) => unimplemented!("Binary Data Not Implemented"),
|
||||||
|
Message::Ping(data) => {
|
||||||
|
if let Err(e) = ws.send(Message::Pong(data)).await {
|
||||||
|
error!("Failed to send Pong {e}");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Message::Pong(_) => {
|
||||||
|
// Intentionally Left Empty
|
||||||
|
}
|
||||||
|
Message::Close(_) => {
|
||||||
|
debug!("Websocket Closed");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
Message::Frame(_) => unreachable!("Not Possible"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
error!("Receive Error {e}");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some(msg) = rx.recv() => {
|
||||||
|
// Insert a callback if it isn't a None callback
|
||||||
|
if !matches!(msg.callback, Callback::None) {
|
||||||
|
callbacks.insert(msg.msg.uuid, msg.callback);
|
||||||
|
}
|
||||||
|
let msg = match serde_json::to_string(&msg.msg) {
|
||||||
|
Ok(m) => m,
|
||||||
|
Err(e) => {
|
||||||
|
error!("Encode Error {e}");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
trace!("Outgoing: {msg}");
|
||||||
|
if let Err(e) = ws.send(Message::Text(msg.into())).await {
|
||||||
|
error!("Send Error {e}");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else => { break; },
|
||||||
|
}
|
||||||
|
}
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn handle_incoming(
|
||||||
|
&mut self,
|
||||||
|
msg: ResponseMessage,
|
||||||
|
callbacks: &mut HashMap<Uuid, Callback>,
|
||||||
|
channel: &ClientChannel,
|
||||||
|
) {
|
||||||
|
if let Some(response_uuid) = msg.response {
|
||||||
|
match callbacks.get(&response_uuid) {
|
||||||
|
Some(Callback::None) => {
|
||||||
|
callbacks.remove(&response_uuid);
|
||||||
|
unreachable!("We skip registering callbacks of None type");
|
||||||
|
}
|
||||||
|
Some(Callback::Once(_)) => {
|
||||||
|
let Some(Callback::Once(callback)) = callbacks.remove(&response_uuid) else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
let _ = callback.send(msg);
|
||||||
|
}
|
||||||
|
Some(Callback::Registered(callback)) => {
|
||||||
|
let callback = callback.clone();
|
||||||
|
spawn(Self::handle_registered_callback(
|
||||||
|
callback,
|
||||||
|
msg,
|
||||||
|
channel.clone(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
warn!("No Callback Registered for {response_uuid}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn handle_registered_callback(
|
||||||
|
callback: RegisteredCallback,
|
||||||
|
msg: ResponseMessage,
|
||||||
|
channel: ClientChannel,
|
||||||
|
) {
|
||||||
|
let (tx, rx) = oneshot::channel();
|
||||||
|
|
||||||
|
let uuid = msg.uuid;
|
||||||
|
|
||||||
|
let response = match callback.send((msg, tx)).await {
|
||||||
|
Err(_) => GenericCallbackError::CallbackClosed.into(),
|
||||||
|
Ok(()) => rx
|
||||||
|
.await
|
||||||
|
.unwrap_or_else(|_| GenericCallbackError::CallbackClosed.into()),
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Err(e) = Self::send_response(channel, response, uuid).await {
|
||||||
|
error!("Failed to send response {e}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn send_response(
|
||||||
|
channel: ClientChannel,
|
||||||
|
payload: RequestMessagePayload,
|
||||||
|
response_uuid: Uuid,
|
||||||
|
) -> Result<(), MessageError> {
|
||||||
|
// If this failed that means we're in the middle of reconnecting, so our callbacks
|
||||||
|
// are all being cleaned up as-is. No response needed.
|
||||||
|
let sender = channel.try_read()?;
|
||||||
|
let data = sender.reserve().await?;
|
||||||
|
|
||||||
|
data.send(OutgoingMessage {
|
||||||
|
msg: RequestMessage {
|
||||||
|
uuid: Uuid::new_v4(),
|
||||||
|
response: Some(response_uuid),
|
||||||
|
payload,
|
||||||
|
},
|
||||||
|
callback: Callback::None,
|
||||||
|
});
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for Client {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
self.cancel.cancel();
|
||||||
|
}
|
||||||
|
}
|
||||||
8
api/src/data_type.rs
Normal file
8
api/src/data_type.rs
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
pub enum DataType {
|
||||||
|
Float32,
|
||||||
|
Float64,
|
||||||
|
Boolean,
|
||||||
|
}
|
||||||
9
api/src/data_value.rs
Normal file
9
api/src/data_value.rs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
use derive_more::TryInto;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, Serialize, Deserialize, TryInto)]
|
||||||
|
pub enum DataValue {
|
||||||
|
Float32(f32),
|
||||||
|
Float64(f64),
|
||||||
|
Boolean(bool),
|
||||||
|
}
|
||||||
4
api/src/lib.rs
Normal file
4
api/src/lib.rs
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
pub mod client;
|
||||||
|
pub mod data_type;
|
||||||
|
pub mod data_value;
|
||||||
|
pub mod messages;
|
||||||
@@ -1,8 +1,7 @@
|
|||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub enum TelemetryDataValue {
|
pub enum GenericCallbackError {
|
||||||
Float32(f32),
|
CallbackClosed,
|
||||||
Float64(f64),
|
MismatchedType,
|
||||||
Boolean(bool),
|
|
||||||
}
|
}
|
||||||
35
api/src/messages/command.rs
Normal file
35
api/src/messages/command.rs
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
use crate::data_type::DataType;
|
||||||
|
use crate::data_value::DataValue;
|
||||||
|
use crate::messages::RegisterCallback;
|
||||||
|
use chrono::{DateTime, Utc};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct CommandParameterDefinition {
|
||||||
|
pub name: String,
|
||||||
|
pub data_type: DataType,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct CommandDefinition {
|
||||||
|
pub name: String,
|
||||||
|
pub parameters: Vec<CommandParameterDefinition>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RegisterCallback for CommandDefinition {
|
||||||
|
type Callback = Command;
|
||||||
|
type Response = CommandResponse;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct Command {
|
||||||
|
pub timestamp: DateTime<Utc>,
|
||||||
|
pub parameters: HashMap<String, DataValue>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct CommandResponse {
|
||||||
|
pub success: bool,
|
||||||
|
pub response: String,
|
||||||
|
}
|
||||||
40
api/src/messages/mod.rs
Normal file
40
api/src/messages/mod.rs
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
pub mod callback;
|
||||||
|
pub mod command;
|
||||||
|
pub mod payload;
|
||||||
|
pub mod telemetry_definition;
|
||||||
|
pub mod telemetry_entry;
|
||||||
|
|
||||||
|
use payload::{RequestMessagePayload, ResponseMessagePayload};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct RequestMessage {
|
||||||
|
pub uuid: Uuid,
|
||||||
|
#[serde(default)]
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub response: Option<Uuid>,
|
||||||
|
#[serde(flatten)]
|
||||||
|
pub payload: RequestMessagePayload,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct ResponseMessage {
|
||||||
|
pub uuid: Uuid,
|
||||||
|
#[serde(default)]
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub response: Option<Uuid>,
|
||||||
|
#[serde(flatten)]
|
||||||
|
pub payload: ResponseMessagePayload,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait ClientMessage: Into<RequestMessagePayload> {}
|
||||||
|
|
||||||
|
pub trait RequestResponse: Into<RequestMessagePayload> {
|
||||||
|
type Response: TryFrom<ResponseMessagePayload>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait RegisterCallback: Into<RequestMessagePayload> {
|
||||||
|
type Callback: TryFrom<ResponseMessagePayload>;
|
||||||
|
type Response: Into<RequestMessagePayload>;
|
||||||
|
}
|
||||||
23
api/src/messages/payload.rs
Normal file
23
api/src/messages/payload.rs
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
use crate::messages::callback::GenericCallbackError;
|
||||||
|
use crate::messages::command::{Command, CommandDefinition, CommandResponse};
|
||||||
|
use crate::messages::telemetry_definition::{
|
||||||
|
TelemetryDefinitionRequest, TelemetryDefinitionResponse,
|
||||||
|
};
|
||||||
|
use crate::messages::telemetry_entry::TelemetryEntry;
|
||||||
|
use derive_more::{From, TryInto};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize, From)]
|
||||||
|
pub enum RequestMessagePayload {
|
||||||
|
TelemetryDefinitionRequest(TelemetryDefinitionRequest),
|
||||||
|
TelemetryEntry(TelemetryEntry),
|
||||||
|
GenericCallbackError(GenericCallbackError),
|
||||||
|
CommandDefinition(CommandDefinition),
|
||||||
|
CommandResponse(CommandResponse),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize, From, TryInto)]
|
||||||
|
pub enum ResponseMessagePayload {
|
||||||
|
TelemetryDefinitionResponse(TelemetryDefinitionResponse),
|
||||||
|
Command(Command),
|
||||||
|
}
|
||||||
19
api/src/messages/telemetry_definition.rs
Normal file
19
api/src/messages/telemetry_definition.rs
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
use crate::data_type::DataType;
|
||||||
|
use crate::messages::RequestResponse;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct TelemetryDefinitionRequest {
|
||||||
|
pub name: String,
|
||||||
|
pub data_type: DataType,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct TelemetryDefinitionResponse {
|
||||||
|
pub uuid: Uuid,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RequestResponse for TelemetryDefinitionRequest {
|
||||||
|
type Response = TelemetryDefinitionResponse;
|
||||||
|
}
|
||||||
14
api/src/messages/telemetry_entry.rs
Normal file
14
api/src/messages/telemetry_entry.rs
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
use crate::data_value::DataValue;
|
||||||
|
use crate::messages::ClientMessage;
|
||||||
|
use chrono::{DateTime, Utc};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct TelemetryEntry {
|
||||||
|
pub uuid: Uuid,
|
||||||
|
pub value: DataValue,
|
||||||
|
pub timestamp: DateTime<Utc>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ClientMessage for TelemetryEntry {}
|
||||||
@@ -4,11 +4,9 @@ name = "simple_command"
|
|||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
server = { path = "../../server" }
|
anyhow = { workspace = true }
|
||||||
tonic = "0.12.3"
|
api = { path = "../../api" }
|
||||||
tokio = { version = "1.43.0", features = ["rt-multi-thread", "signal"] }
|
env_logger = { workspace = true }
|
||||||
chrono = "0.4.39"
|
log = { workspace = true }
|
||||||
tokio-util = "0.7.13"
|
tokio = { workspace = true, features = ["rt-multi-thread", "signal"] }
|
||||||
num-traits = "0.2.19"
|
tokio-util = { workspace = true }
|
||||||
log = "0.4.29"
|
|
||||||
anyhow = "1.0.100"
|
|
||||||
|
|||||||
@@ -1,82 +1,96 @@
|
|||||||
use chrono::DateTime;
|
use anyhow::anyhow;
|
||||||
use server::core::client_side_command::Inner;
|
use api::client::Client;
|
||||||
use server::core::command_service_client::CommandServiceClient;
|
use api::data_type::DataType;
|
||||||
use server::core::telemetry_value::Value;
|
use api::messages::command::{
|
||||||
use server::core::{
|
Command, CommandDefinition, CommandParameterDefinition, CommandResponse,
|
||||||
ClientSideCommand, Command, CommandDefinitionRequest, CommandParameterDefinition,
|
|
||||||
CommandResponse, TelemetryDataType,
|
|
||||||
};
|
};
|
||||||
|
use log::info;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use tokio::select;
|
use std::sync::Arc;
|
||||||
use tokio::sync::mpsc;
|
|
||||||
use tokio::task::JoinHandle;
|
|
||||||
use tokio_util::sync::CancellationToken;
|
use tokio_util::sync::CancellationToken;
|
||||||
use tonic::codegen::tokio_stream::wrappers::ReceiverStream;
|
|
||||||
use tonic::codegen::tokio_stream::StreamExt;
|
|
||||||
use tonic::transport::Channel;
|
|
||||||
|
|
||||||
struct CommandHandler {
|
fn handle_command(command: Command) -> anyhow::Result<String> {
|
||||||
handle: JoinHandle<()>,
|
let timestamp = command.timestamp;
|
||||||
|
let a: f32 = (*command
|
||||||
|
.parameters
|
||||||
|
.get("a")
|
||||||
|
.ok_or(anyhow!("Parameter 'a' Missing"))?)
|
||||||
|
.try_into()?;
|
||||||
|
let b: f64 = (*command
|
||||||
|
.parameters
|
||||||
|
.get("b")
|
||||||
|
.ok_or(anyhow!("Parameter 'b' Missing"))?)
|
||||||
|
.try_into()?;
|
||||||
|
let c: bool = (*command
|
||||||
|
.parameters
|
||||||
|
.get("c")
|
||||||
|
.ok_or(anyhow!("Parameter 'c' Missing"))?)
|
||||||
|
.try_into()?;
|
||||||
|
|
||||||
|
info!("Command Received:\n timestamp: {timestamp}\n a: {a}\n b: {b}\n c: {c}");
|
||||||
|
|
||||||
|
Ok(format!(
|
||||||
|
"Successfully Received Command! timestamp: {timestamp} a: {a} b: {b} c: {c}"
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CommandHandler {
|
struct CommandHandle {
|
||||||
pub async fn new<F: Fn(Command) -> CommandResponse + Send + 'static>(
|
|
||||||
cancellation_token: CancellationToken,
|
cancellation_token: CancellationToken,
|
||||||
client: &mut CommandServiceClient<Channel>,
|
}
|
||||||
command_definition_request: CommandDefinitionRequest,
|
|
||||||
handler: F,
|
impl CommandHandle {
|
||||||
|
pub async fn register(
|
||||||
|
client: Arc<Client>,
|
||||||
|
command_definition: CommandDefinition,
|
||||||
|
mut callback: impl FnMut(Command) -> anyhow::Result<String> + Send + 'static,
|
||||||
) -> anyhow::Result<Self> {
|
) -> anyhow::Result<Self> {
|
||||||
let (tx, rx) = mpsc::channel(4);
|
let cancellation_token = CancellationToken::new();
|
||||||
|
let result = Self {
|
||||||
|
cancellation_token: cancellation_token.clone(),
|
||||||
|
};
|
||||||
|
|
||||||
// The buffer size of 4 means this is safe to send immediately
|
tokio::spawn(async move {
|
||||||
tx.send(ClientSideCommand {
|
while !cancellation_token.is_cancelled() {
|
||||||
inner: Some(Inner::Request(command_definition_request)),
|
// This would only fail if the sender closed while trying to insert data
|
||||||
})
|
// It would wait until space is made
|
||||||
.await?;
|
let Ok(mut rx) = client
|
||||||
let response = client.new_command(ReceiverStream::new(rx)).await?;
|
.register_callback_channel(command_definition.clone())
|
||||||
let mut cmd_stream = response.into_inner();
|
.await
|
||||||
|
else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
|
||||||
let handle = tokio::spawn(async move {
|
while let Some((cmd, responder)) = rx.recv().await {
|
||||||
loop {
|
let response = match callback(cmd) {
|
||||||
select! {
|
Ok(response) => CommandResponse {
|
||||||
_ = cancellation_token.cancelled() => break,
|
success: true,
|
||||||
Some(msg) = cmd_stream.next() => {
|
response,
|
||||||
match msg {
|
|
||||||
Ok(cmd) => {
|
|
||||||
let uuid = cmd.uuid.clone();
|
|
||||||
let mut response = handler(cmd);
|
|
||||||
response.uuid = uuid;
|
|
||||||
match tx.send(ClientSideCommand {
|
|
||||||
inner: Some(Inner::Response(response))
|
|
||||||
}).await {
|
|
||||||
Ok(()) => {},
|
|
||||||
Err(e) => {
|
|
||||||
println!("SendError: {e}");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
println!("Error: {e}");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
else => break,
|
Err(err) => CommandResponse {
|
||||||
|
success: false,
|
||||||
|
response: err.to_string(),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
// This should only err if we had an error elsewhere
|
||||||
|
let _ = responder.send(response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
Ok(Self { handle })
|
Ok(result)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn join(self) -> anyhow::Result<()> {
|
impl Drop for CommandHandle {
|
||||||
Ok(self.handle.await?)
|
fn drop(&mut self) {
|
||||||
|
self.cancellation_token.cancel();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn Error>> {
|
async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
|
env_logger::init();
|
||||||
|
|
||||||
let cancellation_token = CancellationToken::new();
|
let cancellation_token = CancellationToken::new();
|
||||||
{
|
{
|
||||||
let cancellation_token = cancellation_token.clone();
|
let cancellation_token = cancellation_token.clone();
|
||||||
@@ -86,56 +100,34 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut client = CommandServiceClient::connect("http://[::1]:50051").await?;
|
let client = Arc::new(Client::connect("ws://[::1]:8080/backend")?);
|
||||||
|
|
||||||
let cmd_handler = CommandHandler::new(
|
let handle = CommandHandle::register(
|
||||||
cancellation_token,
|
client,
|
||||||
&mut client,
|
CommandDefinition {
|
||||||
CommandDefinitionRequest {
|
|
||||||
name: "simple_command/a".to_string(),
|
name: "simple_command/a".to_string(),
|
||||||
parameters: vec![
|
parameters: vec![
|
||||||
CommandParameterDefinition {
|
CommandParameterDefinition {
|
||||||
name: "a".to_string(),
|
name: "a".to_string(),
|
||||||
data_type: TelemetryDataType::Float32.into(),
|
data_type: DataType::Float32,
|
||||||
},
|
},
|
||||||
CommandParameterDefinition {
|
CommandParameterDefinition {
|
||||||
name: "b".to_string(),
|
name: "b".to_string(),
|
||||||
data_type: TelemetryDataType::Float64.into(),
|
data_type: DataType::Float64,
|
||||||
},
|
},
|
||||||
CommandParameterDefinition {
|
CommandParameterDefinition {
|
||||||
name: "c".to_string(),
|
name: "c".to_string(),
|
||||||
data_type: TelemetryDataType::Boolean.into(),
|
data_type: DataType::Boolean,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|command| {
|
handle_command,
|
||||||
let timestamp = command.timestamp.expect("Missing Timestamp");
|
|
||||||
let timestamp = DateTime::from_timestamp(timestamp.secs, timestamp.nanos as u32)
|
|
||||||
.expect("Could not construct date time");
|
|
||||||
let Value::Float32(a) = command.parameters["a"].value.expect("Missing Value a") else {
|
|
||||||
panic!("Wrong Type a");
|
|
||||||
};
|
|
||||||
let Value::Float64(b) = command.parameters["b"].value.expect("Missing Value b") else {
|
|
||||||
panic!("Wrong Type b");
|
|
||||||
};
|
|
||||||
let Value::Boolean(c) = command.parameters["c"].value.expect("Missing Value c") else {
|
|
||||||
panic!("Wrong Type c");
|
|
||||||
};
|
|
||||||
|
|
||||||
println!("Command Received:\n timestamp: {timestamp}\n a: {a}\n b: {b}\n c: {c}");
|
|
||||||
|
|
||||||
CommandResponse {
|
|
||||||
uuid: command.uuid.clone(),
|
|
||||||
success: true,
|
|
||||||
response: format!(
|
|
||||||
"Successfully Received Command! timestamp: {timestamp} a: {a} b: {b} c: {c}"
|
|
||||||
),
|
|
||||||
}
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
cmd_handler.join().await?;
|
cancellation_token.cancelled().await;
|
||||||
|
|
||||||
|
drop(handle);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,9 +4,13 @@ name = "simple_producer"
|
|||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
server = { path = "../../server" }
|
anyhow = { workspace = true }
|
||||||
tonic = "0.12.3"
|
api = { path = "../../api" }
|
||||||
tokio = { version = "1.43.0", features = ["rt-multi-thread", "signal"] }
|
chrono = { workspace = true }
|
||||||
chrono = "0.4.39"
|
env_logger = { workspace = true }
|
||||||
tokio-util = "0.7.13"
|
futures-util = { workspace = true }
|
||||||
num-traits = "0.2.19"
|
num-traits = { workspace = true }
|
||||||
|
tokio = { workspace = true, features = ["rt-multi-thread", "signal", "time", "macros"] }
|
||||||
|
tokio-util = { workspace = true }
|
||||||
|
uuid = { workspace = true }
|
||||||
|
log = "0.4.29"
|
||||||
|
|||||||
@@ -1,212 +1,179 @@
|
|||||||
use chrono::DateTime;
|
use api::client::error::MessageError;
|
||||||
use num_traits::float::FloatConst;
|
use api::client::Client;
|
||||||
use server::core::telemetry_service_client::TelemetryServiceClient;
|
use api::data_type::DataType;
|
||||||
use server::core::telemetry_value::Value;
|
use api::data_value::DataValue;
|
||||||
use server::core::{
|
use api::messages::telemetry_definition::TelemetryDefinitionRequest;
|
||||||
TelemetryDataType, TelemetryDefinitionRequest, TelemetryItem, TelemetryValue, Timestamp, Uuid,
|
use api::messages::telemetry_entry::TelemetryEntry;
|
||||||
};
|
use chrono::{DateTime, TimeDelta, Utc};
|
||||||
use std::error::Error;
|
use futures_util::future::join_all;
|
||||||
|
use num_traits::FloatConst;
|
||||||
|
use std::sync::Arc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use tokio::select;
|
use tokio::sync::{oneshot, RwLock};
|
||||||
use tokio::sync::mpsc;
|
use tokio::time::{sleep_until, Instant};
|
||||||
use tokio::sync::mpsc::Sender;
|
|
||||||
use tokio::time::Instant;
|
|
||||||
use tokio_util::sync::CancellationToken;
|
use tokio_util::sync::CancellationToken;
|
||||||
use tonic::codegen::tokio_stream::wrappers::ReceiverStream;
|
use uuid::Uuid;
|
||||||
use tonic::codegen::tokio_stream::StreamExt;
|
|
||||||
use tonic::codegen::StdError;
|
|
||||||
use tonic::transport::Channel;
|
|
||||||
use tonic::Request;
|
|
||||||
|
|
||||||
struct Telemetry {
|
struct Telemetry {
|
||||||
client: TelemetryServiceClient<Channel>,
|
client: Arc<Client>,
|
||||||
tx: Sender<TelemetryItem>,
|
|
||||||
cancel: CancellationToken,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct TelemetryItemHandle {
|
struct TelemetryItemHandle {
|
||||||
uuid: String,
|
cancellation_token: CancellationToken,
|
||||||
tx: Sender<TelemetryItem>,
|
uuid: Arc<RwLock<Uuid>>,
|
||||||
|
client: Arc<Client>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Telemetry {
|
impl Telemetry {
|
||||||
pub async fn new<D>(dst: D) -> Result<Self, Box<dyn Error>>
|
pub fn new(client: Arc<Client>) -> Self {
|
||||||
where
|
Self { client }
|
||||||
D: TryInto<tonic::transport::Endpoint>,
|
|
||||||
D::Error: Into<StdError>,
|
|
||||||
{
|
|
||||||
let mut client = TelemetryServiceClient::connect(dst).await?;
|
|
||||||
let client_stored = client.clone();
|
|
||||||
let cancel = CancellationToken::new();
|
|
||||||
let cancel_stored = cancel.clone();
|
|
||||||
let (local_tx, mut local_rx) = mpsc::channel(16);
|
|
||||||
|
|
||||||
tokio::spawn(async move {
|
|
||||||
while !cancel.is_cancelled() {
|
|
||||||
let (server_tx, server_rx) = mpsc::channel(16);
|
|
||||||
let response_stream = client
|
|
||||||
.insert_telemetry(ReceiverStream::new(server_rx))
|
|
||||||
.await;
|
|
||||||
if let Ok(response_stream) = response_stream {
|
|
||||||
let mut response_stream = response_stream.into_inner();
|
|
||||||
loop {
|
|
||||||
select! {
|
|
||||||
_ = cancel.cancelled() => {
|
|
||||||
break;
|
|
||||||
},
|
|
||||||
Some(item) = local_rx.recv() => {
|
|
||||||
match server_tx.send(item).await {
|
|
||||||
Ok(_) => {}
|
|
||||||
Err(_) => break,
|
|
||||||
}
|
|
||||||
},
|
|
||||||
Some(response) = response_stream.next() => {
|
|
||||||
match response {
|
|
||||||
Ok(_) => {}
|
|
||||||
Err(_) => {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
else => break,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
tokio::time::sleep(Duration::from_secs(1)).await;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
Ok(Self {
|
|
||||||
client: client_stored,
|
|
||||||
tx: local_tx,
|
|
||||||
cancel: cancel_stored,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn register(
|
pub async fn register(
|
||||||
&mut self,
|
&self,
|
||||||
name: String,
|
name: String,
|
||||||
data_type: TelemetryDataType,
|
data_type: DataType,
|
||||||
) -> Result<TelemetryItemHandle, Box<dyn Error>> {
|
) -> anyhow::Result<TelemetryItemHandle> {
|
||||||
let response = self
|
let cancellation_token = CancellationToken::new();
|
||||||
.client
|
let cancel_token = cancellation_token.clone();
|
||||||
.new_telemetry(Request::new(TelemetryDefinitionRequest {
|
let client = self.client.clone();
|
||||||
name,
|
|
||||||
data_type: data_type.into(),
|
|
||||||
}))
|
|
||||||
.await?
|
|
||||||
.into_inner();
|
|
||||||
|
|
||||||
let Some(uuid) = response.uuid else {
|
let response_uuid = Arc::new(RwLock::new(Uuid::nil()));
|
||||||
return Err("UUID Missing".into());
|
|
||||||
|
let response_uuid_inner = response_uuid.clone();
|
||||||
|
let (tx, rx) = oneshot::channel();
|
||||||
|
|
||||||
|
tokio::spawn(async move {
|
||||||
|
let mut write_lock = Some(response_uuid_inner.write().await);
|
||||||
|
let _ = tx.send(());
|
||||||
|
while !cancel_token.is_cancelled() {
|
||||||
|
if let Ok(response) = client
|
||||||
|
.send_request(TelemetryDefinitionRequest {
|
||||||
|
name: name.clone(),
|
||||||
|
data_type,
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
let mut lock = match write_lock {
|
||||||
|
None => response_uuid_inner.write().await,
|
||||||
|
Some(lock) => lock,
|
||||||
};
|
};
|
||||||
|
// Update the value in the lock
|
||||||
|
*lock = response.uuid;
|
||||||
|
// Set this value so the loop works
|
||||||
|
write_lock = None;
|
||||||
|
}
|
||||||
|
|
||||||
|
client.wait_disconnected().await;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Wait until the write lock is acquired
|
||||||
|
let _ = rx.await;
|
||||||
|
// Wait until the write lock is released for the first time
|
||||||
|
drop(response_uuid.read().await);
|
||||||
|
|
||||||
Ok(TelemetryItemHandle {
|
Ok(TelemetryItemHandle {
|
||||||
uuid: uuid.value,
|
cancellation_token,
|
||||||
tx: self.tx.clone(),
|
uuid: response_uuid,
|
||||||
|
client: self.client.clone(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drop for Telemetry {
|
|
||||||
fn drop(&mut self) {
|
|
||||||
self.cancel.cancel();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TelemetryItemHandle {
|
impl TelemetryItemHandle {
|
||||||
pub async fn publish(
|
pub async fn publish(&self, value: DataValue, timestamp: DateTime<Utc>) -> anyhow::Result<()> {
|
||||||
&self,
|
let Ok(lock) = self.uuid.try_read() else {
|
||||||
value: Value,
|
return Ok(());
|
||||||
timestamp: DateTime<chrono::Utc>,
|
};
|
||||||
) -> Result<(), Box<dyn Error>> {
|
let uuid = *lock;
|
||||||
let offset_from_unix_epoch =
|
drop(lock);
|
||||||
timestamp - DateTime::from_timestamp(0, 0).expect("Could not get Unix epoch");
|
|
||||||
self.tx
|
self.client
|
||||||
.send(TelemetryItem {
|
.send_message_if_connected(TelemetryEntry {
|
||||||
uuid: Some(Uuid {
|
uuid,
|
||||||
value: self.uuid.clone(),
|
value,
|
||||||
}),
|
timestamp,
|
||||||
value: Some(TelemetryValue { value: Some(value) }),
|
|
||||||
timestamp: Some(Timestamp {
|
|
||||||
secs: offset_from_unix_epoch.num_seconds(),
|
|
||||||
nanos: offset_from_unix_epoch.subsec_nanos(),
|
|
||||||
}),
|
|
||||||
})
|
})
|
||||||
.await?;
|
.await
|
||||||
|
.or_else(|e| match e {
|
||||||
|
MessageError::TokioLockError(_) => Ok(()),
|
||||||
|
e => Err(e),
|
||||||
|
})?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
impl Drop for TelemetryItemHandle {
|
||||||
async fn main() -> Result<(), Box<dyn Error>> {
|
fn drop(&mut self) {
|
||||||
let mut tlm = Telemetry::new("http://[::1]:50051").await?;
|
self.cancellation_token.cancel();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let index_handle = tlm
|
#[tokio::main]
|
||||||
.register(
|
async fn main() -> anyhow::Result<()> {
|
||||||
"simple_producer/time_offset".into(),
|
env_logger::init();
|
||||||
TelemetryDataType::Float64,
|
|
||||||
)
|
let client = Arc::new(Client::connect("ws://[::1]:8080/backend")?);
|
||||||
|
let tlm = Telemetry::new(client);
|
||||||
|
|
||||||
|
{
|
||||||
|
let time_offset = tlm
|
||||||
|
.register("simple_producer/time_offset".into(), DataType::Float64)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let publish_offset = tlm
|
let publish_offset = tlm
|
||||||
.register(
|
.register("simple_producer/publish_offset".into(), DataType::Float64)
|
||||||
"simple_producer/publish_offset".into(),
|
|
||||||
TelemetryDataType::Float64,
|
|
||||||
)
|
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let await_offset = tlm
|
let await_offset = tlm
|
||||||
.register(
|
.register("simple_producer/await_offset".into(), DataType::Float64)
|
||||||
"simple_producer/await_offset".into(),
|
|
||||||
TelemetryDataType::Float64,
|
|
||||||
)
|
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let sin_tlm_handle = tlm
|
let sin_tlm_handle = tlm
|
||||||
.register("simple_producer/sin".into(), TelemetryDataType::Float32)
|
.register("simple_producer/sin".into(), DataType::Float32)
|
||||||
.await?;
|
.await?;
|
||||||
let cos_tlm_handle = tlm
|
let cos_tlm_handle = tlm
|
||||||
.register("simple_producer/cos".into(), TelemetryDataType::Float64)
|
.register("simple_producer/cos".into(), DataType::Float64)
|
||||||
.await?;
|
.await?;
|
||||||
let bool_tlm_handle = tlm
|
let bool_tlm_handle = tlm
|
||||||
.register("simple_producer/bool".into(), TelemetryDataType::Boolean)
|
.register("simple_producer/bool".into(), DataType::Boolean)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let sin2_tlm_handle = tlm
|
let sin2_tlm_handle = tlm
|
||||||
.register("simple_producer/sin2".into(), TelemetryDataType::Float32)
|
.register("simple_producer/sin2".into(), DataType::Float32)
|
||||||
.await?;
|
.await?;
|
||||||
let cos2_tlm_handle = tlm
|
let cos2_tlm_handle = tlm
|
||||||
.register("simple_producer/cos2".into(), TelemetryDataType::Float64)
|
.register("simple_producer/cos2".into(), DataType::Float64)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let sin3_tlm_handle = tlm
|
let sin3_tlm_handle = tlm
|
||||||
.register("simple_producer/sin3".into(), TelemetryDataType::Float32)
|
.register("simple_producer/sin3".into(), DataType::Float32)
|
||||||
.await?;
|
.await?;
|
||||||
let cos3_tlm_handle = tlm
|
let cos3_tlm_handle = tlm
|
||||||
.register("simple_producer/cos3".into(), TelemetryDataType::Float64)
|
.register("simple_producer/cos3".into(), DataType::Float64)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let sin4_tlm_handle = tlm
|
let sin4_tlm_handle = tlm
|
||||||
.register("simple_producer/sin4".into(), TelemetryDataType::Float32)
|
.register("simple_producer/sin4".into(), DataType::Float32)
|
||||||
.await?;
|
.await?;
|
||||||
let cos4_tlm_handle = tlm
|
let cos4_tlm_handle = tlm
|
||||||
.register("simple_producer/cos4".into(), TelemetryDataType::Float64)
|
.register("simple_producer/cos4".into(), DataType::Float64)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let sin5_tlm_handle = tlm
|
let sin5_tlm_handle = tlm
|
||||||
.register("simple_producer/sin5".into(), TelemetryDataType::Float32)
|
.register("simple_producer/sin5".into(), DataType::Float32)
|
||||||
.await?;
|
.await?;
|
||||||
let cos5_tlm_handle = tlm
|
let cos5_tlm_handle = tlm
|
||||||
.register("simple_producer/cos5".into(), TelemetryDataType::Float64)
|
.register("simple_producer/cos5".into(), DataType::Float64)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let sin6_tlm_handle = tlm
|
let sin6_tlm_handle = tlm
|
||||||
.register("simple_producer/sin6".into(), TelemetryDataType::Float32)
|
.register("simple_producer/sin6".into(), DataType::Float32)
|
||||||
.await?;
|
.await?;
|
||||||
let cos6_tlm_handle = tlm
|
let cos6_tlm_handle = tlm
|
||||||
.register("simple_producer/cos6".into(), TelemetryDataType::Float64)
|
.register("simple_producer/cos6".into(), DataType::Float64)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let cancellation_token = CancellationToken::new();
|
let cancellation_token = CancellationToken::new();
|
||||||
@@ -215,89 +182,95 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
let _ = tokio::signal::ctrl_c().await;
|
let _ = tokio::signal::ctrl_c().await;
|
||||||
cancellation_token.cancel();
|
cancellation_token.cancel();
|
||||||
|
println!("Cancellation Token Cancelled");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
let start_time = chrono::Utc::now();
|
let start_time = Utc::now();
|
||||||
let start_instant = Instant::now();
|
let start_instant = Instant::now();
|
||||||
let mut next_time = start_instant;
|
let mut next_time = start_instant;
|
||||||
let mut index = 0;
|
let mut index = 0;
|
||||||
let mut tasks = vec![];
|
let mut tasks = vec![];
|
||||||
while !cancellation_token.is_cancelled() {
|
while !cancellation_token.is_cancelled() {
|
||||||
next_time += Duration::from_millis(10);
|
next_time += Duration::from_millis(1000);
|
||||||
index += 1;
|
index += 1;
|
||||||
tokio::time::sleep_until(next_time).await;
|
sleep_until(next_time).await;
|
||||||
let publish_time =
|
let publish_time = start_time + TimeDelta::from_std(next_time - start_instant).unwrap();
|
||||||
start_time + chrono::TimeDelta::from_std(next_time - start_instant).unwrap();
|
|
||||||
let actual_time = Instant::now();
|
let actual_time = Instant::now();
|
||||||
tasks.push(index_handle.publish(
|
tasks.push(time_offset.publish(
|
||||||
Value::Float64((actual_time - next_time).as_secs_f64()),
|
DataValue::Float64((actual_time - next_time).as_secs_f64()),
|
||||||
chrono::Utc::now(),
|
Utc::now(),
|
||||||
));
|
));
|
||||||
tasks.push(sin_tlm_handle.publish(
|
tasks.push(sin_tlm_handle.publish(
|
||||||
Value::Float32((f32::TAU() * (index as f32) / (1000.0_f32)).sin()),
|
DataValue::Float32((f32::TAU() * (index as f32) / (1000.0_f32)).sin()),
|
||||||
publish_time,
|
publish_time,
|
||||||
));
|
));
|
||||||
tasks.push(cos_tlm_handle.publish(
|
tasks.push(cos_tlm_handle.publish(
|
||||||
Value::Float64((f64::TAU() * (index as f64) / (1000.0_f64)).cos()),
|
DataValue::Float64((f64::TAU() * (index as f64) / (1000.0_f64)).cos()),
|
||||||
publish_time,
|
publish_time,
|
||||||
));
|
));
|
||||||
tasks.push(bool_tlm_handle.publish(Value::Boolean(index % 1000 > 500), publish_time));
|
tasks.push(
|
||||||
|
bool_tlm_handle.publish(DataValue::Boolean(index % 1000 > 500), publish_time),
|
||||||
|
);
|
||||||
tasks.push(sin2_tlm_handle.publish(
|
tasks.push(sin2_tlm_handle.publish(
|
||||||
Value::Float32((f32::TAU() * (index as f32) / (500.0_f32)).sin()),
|
DataValue::Float32((f32::TAU() * (index as f32) / (500.0_f32)).sin()),
|
||||||
publish_time,
|
publish_time,
|
||||||
));
|
));
|
||||||
tasks.push(cos2_tlm_handle.publish(
|
tasks.push(cos2_tlm_handle.publish(
|
||||||
Value::Float64((f64::TAU() * (index as f64) / (500.0_f64)).cos()),
|
DataValue::Float64((f64::TAU() * (index as f64) / (500.0_f64)).cos()),
|
||||||
publish_time,
|
publish_time,
|
||||||
));
|
));
|
||||||
tasks.push(sin3_tlm_handle.publish(
|
tasks.push(sin3_tlm_handle.publish(
|
||||||
Value::Float32((f32::TAU() * (index as f32) / (333.0_f32)).sin()),
|
DataValue::Float32((f32::TAU() * (index as f32) / (333.0_f32)).sin()),
|
||||||
publish_time,
|
publish_time,
|
||||||
));
|
));
|
||||||
tasks.push(cos3_tlm_handle.publish(
|
tasks.push(cos3_tlm_handle.publish(
|
||||||
Value::Float64((f64::TAU() * (index as f64) / (333.0_f64)).cos()),
|
DataValue::Float64((f64::TAU() * (index as f64) / (333.0_f64)).cos()),
|
||||||
publish_time,
|
publish_time,
|
||||||
));
|
));
|
||||||
tasks.push(sin4_tlm_handle.publish(
|
tasks.push(sin4_tlm_handle.publish(
|
||||||
Value::Float32((f32::TAU() * (index as f32) / (250.0_f32)).sin()),
|
DataValue::Float32((f32::TAU() * (index as f32) / (250.0_f32)).sin()),
|
||||||
publish_time,
|
publish_time,
|
||||||
));
|
));
|
||||||
tasks.push(cos4_tlm_handle.publish(
|
tasks.push(cos4_tlm_handle.publish(
|
||||||
Value::Float64((f64::TAU() * (index as f64) / (250.0_f64)).cos()),
|
DataValue::Float64((f64::TAU() * (index as f64) / (250.0_f64)).cos()),
|
||||||
publish_time,
|
publish_time,
|
||||||
));
|
));
|
||||||
tasks.push(sin5_tlm_handle.publish(
|
tasks.push(sin5_tlm_handle.publish(
|
||||||
Value::Float32((f32::TAU() * (index as f32) / (200.0_f32)).sin()),
|
DataValue::Float32((f32::TAU() * (index as f32) / (200.0_f32)).sin()),
|
||||||
publish_time,
|
publish_time,
|
||||||
));
|
));
|
||||||
tasks.push(cos5_tlm_handle.publish(
|
tasks.push(cos5_tlm_handle.publish(
|
||||||
Value::Float64((f64::TAU() * (index as f64) / (200.0_f64)).cos()),
|
DataValue::Float64((f64::TAU() * (index as f64) / (200.0_f64)).cos()),
|
||||||
publish_time,
|
publish_time,
|
||||||
));
|
));
|
||||||
tasks.push(sin6_tlm_handle.publish(
|
tasks.push(sin6_tlm_handle.publish(
|
||||||
Value::Float32((f32::TAU() * (index as f32) / (166.0_f32)).sin()),
|
DataValue::Float32((f32::TAU() * (index as f32) / (166.0_f32)).sin()),
|
||||||
publish_time,
|
publish_time,
|
||||||
));
|
));
|
||||||
tasks.push(cos6_tlm_handle.publish(
|
tasks.push(cos6_tlm_handle.publish(
|
||||||
Value::Float64((f64::TAU() * (index as f64) / (166.0_f64)).cos()),
|
DataValue::Float64((f64::TAU() * (index as f64) / (166.0_f64)).cos()),
|
||||||
publish_time,
|
publish_time,
|
||||||
));
|
));
|
||||||
|
|
||||||
tasks.push(publish_offset.publish(
|
tasks.push(publish_offset.publish(
|
||||||
Value::Float64((Instant::now() - actual_time).as_secs_f64()),
|
DataValue::Float64((Instant::now() - actual_time).as_secs_f64()),
|
||||||
chrono::Utc::now(),
|
Utc::now(),
|
||||||
));
|
));
|
||||||
|
|
||||||
for task in tasks.drain(..) {
|
// Join the tasks so they all run in parallel
|
||||||
task.await?;
|
for task in join_all(tasks.drain(..)).await {
|
||||||
|
task?;
|
||||||
}
|
}
|
||||||
|
|
||||||
tasks.push(await_offset.publish(
|
tasks.push(await_offset.publish(
|
||||||
Value::Float64((Instant::now() - actual_time).as_secs_f64()),
|
DataValue::Float64((Instant::now() - actual_time).as_secs_f64()),
|
||||||
chrono::Utc::now(),
|
Utc::now(),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
println!("Exiting Loop");
|
||||||
|
}
|
||||||
|
drop(tlm);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,25 +6,20 @@ version = "0.1.0"
|
|||||||
authors = ["Sergey <me@sergeysav.com>"]
|
authors = ["Sergey <me@sergeysav.com>"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
fern = "0.7.1"
|
actix-web = { workspace = true, features = [ ] }
|
||||||
log = "0.4.29"
|
actix-ws = { workspace = true }
|
||||||
prost = "0.13.5"
|
anyhow = { workspace = true }
|
||||||
rand = "0.9.0"
|
api = { path = "../api" }
|
||||||
tonic = { version = "0.12.3" }
|
chrono = { workspace = true }
|
||||||
tokio = { version = "1.43.0", features = ["rt-multi-thread", "signal", "fs"] }
|
derive_more = { workspace = true, features = ["from"] }
|
||||||
chrono = "0.4.42"
|
fern = { workspace = true }
|
||||||
actix-web = { version = "4.12.1", features = [ ] }
|
futures-util = { workspace = true }
|
||||||
actix-ws = "0.3.0"
|
log = { workspace = true }
|
||||||
tokio-util = "0.7.17"
|
papaya = { workspace = true }
|
||||||
serde = { version = "1.0.228", features = ["derive"] }
|
serde = { workspace = true, features = ["derive"] }
|
||||||
serde_json = "1.0.145"
|
serde_json = { workspace = true }
|
||||||
hex = "0.4.3"
|
sqlx = { workspace = true, features = [ "runtime-tokio", "tls-rustls-ring-native-roots", "sqlite" ] }
|
||||||
papaya = "0.2.3"
|
thiserror = { workspace = true }
|
||||||
thiserror = "2.0.17"
|
tokio = { workspace = true, features = ["rt-multi-thread", "signal", "fs"] }
|
||||||
derive_more = { version = "2.1.0", features = ["from"] }
|
tokio-util = { workspace = true }
|
||||||
anyhow = "1.0.100"
|
uuid = { workspace = true }
|
||||||
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"
|
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
println!("cargo:rerun-if-changed=migrations");
|
println!("cargo:rerun-if-changed=migrations");
|
||||||
tonic_build::compile_protos("proto/core.proto")?;
|
|
||||||
Ok(())
|
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::command::service::RegisteredCommand;
|
||||||
use crate::core::TelemetryDataType;
|
use api::messages::command::{CommandDefinition, CommandParameterDefinition};
|
||||||
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>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<RegisteredCommand> for CommandDefinition {
|
impl From<RegisteredCommand> for CommandDefinition {
|
||||||
fn from(value: RegisteredCommand) -> Self {
|
fn from(value: RegisteredCommand) -> Self {
|
||||||
@@ -27,7 +10,7 @@ impl From<RegisteredCommand> for CommandDefinition {
|
|||||||
.parameters
|
.parameters
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|param| CommandParameterDefinition {
|
.map(|param| CommandParameterDefinition {
|
||||||
data_type: param.data_type(),
|
data_type: param.data_type,
|
||||||
name: param.name,
|
name: param.name,
|
||||||
})
|
})
|
||||||
.collect(),
|
.collect(),
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use crate::core::TelemetryDataType;
|
|
||||||
use actix_web::http::StatusCode;
|
use actix_web::http::StatusCode;
|
||||||
use actix_web::ResponseError;
|
use actix_web::ResponseError;
|
||||||
|
use api::data_type::DataType;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
#[derive(Error, Debug)]
|
#[derive(Error, Debug)]
|
||||||
@@ -14,7 +14,7 @@ pub enum Error {
|
|||||||
#[error("Incorrect Parameter Type for {name}. {expected_type:?} expected.")]
|
#[error("Incorrect Parameter Type for {name}. {expected_type:?} expected.")]
|
||||||
WrongParameterType {
|
WrongParameterType {
|
||||||
name: String,
|
name: String,
|
||||||
expected_type: TelemetryDataType,
|
expected_type: DataType,
|
||||||
},
|
},
|
||||||
#[error("No Command Receiver")]
|
#[error("No Command Receiver")]
|
||||||
NoCommandReceiver,
|
NoCommandReceiver,
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
pub mod command_handle;
|
||||||
mod definition;
|
mod definition;
|
||||||
pub mod error;
|
pub mod error;
|
||||||
pub mod service;
|
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 as CmdError;
|
||||||
use crate::command::error::Error::{
|
use crate::command::error::Error::{
|
||||||
CommandFailure, CommandNotFound, FailedToReceiveResponse, FailedToSend,
|
CommandFailure, CommandNotFound, FailedToReceiveResponse, FailedToSend,
|
||||||
IncorrectParameterCount, MisingParameter, NoCommandReceiver, WrongParameterType,
|
IncorrectParameterCount, MisingParameter, NoCommandReceiver, WrongParameterType,
|
||||||
};
|
};
|
||||||
use crate::core::telemetry_value::Value;
|
use anyhow::bail;
|
||||||
use crate::core::{
|
use api::data_type::DataType;
|
||||||
Command, CommandDefinitionRequest, CommandResponse, TelemetryDataType, TelemetryValue,
|
use api::data_value::DataValue;
|
||||||
Timestamp, Uuid,
|
use api::messages::command::{Command, CommandDefinition, CommandResponse};
|
||||||
};
|
use api::messages::ResponseMessage;
|
||||||
use chrono::{DateTime, Utc};
|
use chrono::Utc;
|
||||||
use log::error;
|
use log::error;
|
||||||
use papaya::HashMap;
|
use papaya::HashMap;
|
||||||
use std::collections::HashMap as StdHashMap;
|
use std::collections::HashMap as StdHashMap;
|
||||||
use tokio::sync::mpsc;
|
|
||||||
use tokio::sync::oneshot;
|
use tokio::sync::oneshot;
|
||||||
|
use tokio::sync::{mpsc, RwLock};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub(super) struct RegisteredCommand {
|
pub(super) struct RegisteredCommand {
|
||||||
pub(super) name: String,
|
pub(super) name: String,
|
||||||
pub(super) definition: CommandDefinitionRequest,
|
pub(super) definition: CommandDefinition,
|
||||||
tx: mpsc::Sender<Option<(Command, oneshot::Sender<CommandResponse>)>>,
|
response_uuid: Uuid,
|
||||||
|
tx: mpsc::Sender<ResponseMessage>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct CommandManagementService {
|
pub struct CommandManagementService {
|
||||||
registered_commands: HashMap<String, RegisteredCommand>,
|
registered_commands: HashMap<String, RegisteredCommand>,
|
||||||
|
outstanding_responses: RwLock<StdHashMap<Uuid, oneshot::Sender<CommandResponse>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CommandManagementService {
|
impl CommandManagementService {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
registered_commands: HashMap::new(),
|
registered_commands: HashMap::new(),
|
||||||
|
outstanding_responses: RwLock::new(StdHashMap::new()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -52,26 +56,26 @@ impl CommandManagementService {
|
|||||||
.map(|registration| registration.clone().into())
|
.map(|registration| registration.clone().into())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn register_command(
|
pub fn register_command(
|
||||||
&self,
|
&self,
|
||||||
command: CommandDefinitionRequest,
|
uuid: Uuid,
|
||||||
) -> anyhow::Result<mpsc::Receiver<Option<(Command, oneshot::Sender<CommandResponse>)>>> {
|
command: CommandDefinition,
|
||||||
let (tx, rx) = mpsc::channel(1);
|
tx: mpsc::Sender<ResponseMessage>,
|
||||||
|
) -> anyhow::Result<CommandHandle> {
|
||||||
let registered_commands = self.registered_commands.pin_owned();
|
let registered_commands = self.registered_commands.pin();
|
||||||
if let Some(previous) = registered_commands.insert(
|
// We don't care about the previously registered command
|
||||||
command.name.clone(),
|
let name = command.name.clone();
|
||||||
|
let _ = registered_commands.insert(
|
||||||
|
name.clone(),
|
||||||
RegisteredCommand {
|
RegisteredCommand {
|
||||||
name: command.name.clone(),
|
response_uuid: uuid,
|
||||||
|
name: name.clone(),
|
||||||
definition: command,
|
definition: command,
|
||||||
tx,
|
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(
|
pub async fn send_command(
|
||||||
@@ -80,8 +84,6 @@ impl CommandManagementService {
|
|||||||
parameters: serde_json::Map<String, serde_json::Value>,
|
parameters: serde_json::Map<String, serde_json::Value>,
|
||||||
) -> Result<String, CmdError> {
|
) -> Result<String, CmdError> {
|
||||||
let timestamp = Utc::now();
|
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 name = name.into();
|
||||||
let registered_commands = self.registered_commands.pin();
|
let registered_commands = self.registered_commands.pin();
|
||||||
@@ -100,27 +102,21 @@ impl CommandManagementService {
|
|||||||
let Some(param_value) = parameters.get(¶meter.name) else {
|
let Some(param_value) = parameters.get(¶meter.name) else {
|
||||||
return Err(MisingParameter(parameter.name.clone()));
|
return Err(MisingParameter(parameter.name.clone()));
|
||||||
};
|
};
|
||||||
let Some(param_value) = (match parameter.data_type() {
|
let Some(param_value) = (match parameter.data_type {
|
||||||
TelemetryDataType::Float32 => {
|
DataType::Float32 => param_value.as_f64().map(|v| DataValue::Float32(v as f32)),
|
||||||
param_value.as_f64().map(|v| Value::Float32(v as f32))
|
DataType::Float64 => param_value.as_f64().map(DataValue::Float64),
|
||||||
}
|
DataType::Boolean => param_value.as_bool().map(DataValue::Boolean),
|
||||||
TelemetryDataType::Float64 => param_value.as_f64().map(Value::Float64),
|
|
||||||
TelemetryDataType::Boolean => param_value.as_bool().map(Value::Boolean),
|
|
||||||
}) else {
|
}) else {
|
||||||
return Err(WrongParameterType {
|
return Err(WrongParameterType {
|
||||||
name: parameter.name.clone(),
|
name: parameter.name.clone(),
|
||||||
expected_type: parameter.data_type(),
|
expected_type: parameter.data_type,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
result_parameters.insert(
|
result_parameters.insert(parameter.name.clone(), param_value);
|
||||||
parameter.name.clone(),
|
|
||||||
TelemetryValue {
|
|
||||||
value: Some(param_value),
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clone & Drop lets us use a standard pin instead of an owned pin
|
// Clone & Drop lets us use a standard pin instead of an owned pin
|
||||||
|
let response_uuid = registration.response_uuid;
|
||||||
let tx = registration.tx.clone();
|
let tx = registration.tx.clone();
|
||||||
drop(registered_commands);
|
drop(registered_commands);
|
||||||
|
|
||||||
@@ -128,23 +124,27 @@ impl CommandManagementService {
|
|||||||
return Err(NoCommandReceiver);
|
return Err(NoCommandReceiver);
|
||||||
}
|
}
|
||||||
|
|
||||||
let uuid = Uuid::random();
|
let uuid = Uuid::new_v4();
|
||||||
let (response_tx, response_rx) = oneshot::channel();
|
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
|
if let Err(e) = tx
|
||||||
.send(Some((
|
.send(ResponseMessage {
|
||||||
Command {
|
uuid,
|
||||||
uuid: Some(uuid),
|
response: Some(response_uuid),
|
||||||
timestamp: Some(Timestamp {
|
payload: Command {
|
||||||
secs: offset_from_unix_epoch.num_seconds(),
|
timestamp,
|
||||||
nanos: offset_from_unix_epoch.subsec_nanos(),
|
|
||||||
}),
|
|
||||||
parameters: result_parameters,
|
parameters: result_parameters,
|
||||||
},
|
}
|
||||||
response_tx,
|
.into(),
|
||||||
)))
|
})
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
error!("Failed to Send Command: {e}");
|
error!("Failed to Send Command {e}");
|
||||||
return Err(FailedToSend);
|
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() {
|
for (key, sender) in in_progress.drain() {
|
||||||
if sender.send(CommandResponse {
|
if sender
|
||||||
|
.send(CommandResponse {
|
||||||
uuid: Some(Uuid::from(key)),
|
uuid: Some(Uuid::from(key)),
|
||||||
success: false,
|
success: false,
|
||||||
response: "Command Handler Shut Down".to_string(),
|
response: "Command Handler Shut Down".to_string(),
|
||||||
}).is_err() {
|
})
|
||||||
|
.is_err()
|
||||||
|
{
|
||||||
error!("Failed to send command response on shutdown");
|
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 actix_web::{delete, get, post, put, web, Responder};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct CreateParam {
|
struct CreateParam {
|
||||||
@@ -13,7 +14,7 @@ struct CreateParam {
|
|||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct IdParam {
|
struct IdParam {
|
||||||
id: String,
|
id: Uuid,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("/panel")]
|
#[post("/panel")]
|
||||||
@@ -22,7 +23,7 @@ pub(super) async fn new(
|
|||||||
data: web::Json<CreateParam>,
|
data: web::Json<CreateParam>,
|
||||||
) -> Result<impl Responder, HttpServerResultError> {
|
) -> Result<impl Responder, HttpServerResultError> {
|
||||||
let uuid = panels.create(&data.name, &data.data).await?;
|
let uuid = panels.create(&data.name, &data.data).await?;
|
||||||
Ok(web::Json(uuid.value))
|
Ok(web::Json(uuid))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/panel")]
|
#[get("/panel")]
|
||||||
@@ -38,12 +39,10 @@ pub(super) async fn get_one(
|
|||||||
panels: web::Data<Arc<PanelService>>,
|
panels: web::Data<Arc<PanelService>>,
|
||||||
path: web::Path<IdParam>,
|
path: web::Path<IdParam>,
|
||||||
) -> Result<impl Responder, HttpServerResultError> {
|
) -> Result<impl Responder, HttpServerResultError> {
|
||||||
let result = panels.read(path.id.clone().into()).await?;
|
let result = panels.read(path.id).await?;
|
||||||
match result {
|
match result {
|
||||||
Some(result) => Ok(web::Json(result)),
|
Some(result) => Ok(web::Json(result)),
|
||||||
None => Err(HttpServerResultError::PanelUuidNotFound {
|
None => Err(HttpServerResultError::PanelUuidNotFound { uuid: path.id }),
|
||||||
uuid: path.id.clone(),
|
|
||||||
}),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,7 +52,7 @@ pub(super) async fn set(
|
|||||||
path: web::Path<IdParam>,
|
path: web::Path<IdParam>,
|
||||||
data: web::Json<PanelUpdate>,
|
data: web::Json<PanelUpdate>,
|
||||||
) -> Result<impl Responder, HttpServerResultError> {
|
) -> Result<impl Responder, HttpServerResultError> {
|
||||||
panels.update(path.id.clone().into(), data.0).await?;
|
panels.update(path.id, data.0).await?;
|
||||||
Ok(web::Json(()))
|
Ok(web::Json(()))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,6 +61,6 @@ pub(super) async fn delete(
|
|||||||
panels: web::Data<Arc<PanelService>>,
|
panels: web::Data<Arc<PanelService>>,
|
||||||
path: web::Path<IdParam>,
|
path: web::Path<IdParam>,
|
||||||
) -> Result<impl Responder, HttpServerResultError> {
|
) -> Result<impl Responder, HttpServerResultError> {
|
||||||
panels.delete(path.id.clone().into()).await?;
|
panels.delete(path.id).await?;
|
||||||
Ok(web::Json(()))
|
Ok(web::Json(()))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ use serde::Deserialize;
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use tokio::time::timeout;
|
use tokio::time::timeout;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
#[get("/tlm/info/{name:[\\w\\d/_-]+}")]
|
#[get("/tlm/info/{name:[\\w\\d/_-]+}")]
|
||||||
pub(super) async fn get_tlm_definition(
|
pub(super) async fn get_tlm_definition(
|
||||||
@@ -36,13 +37,17 @@ struct HistoryQuery {
|
|||||||
resolution: i64,
|
resolution: i64,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/tlm/history/{uuid:[0-9a-f]+}")]
|
#[get("/tlm/history/{uuid:[0-9a-f-]+}")]
|
||||||
pub(super) async fn get_tlm_history(
|
pub(super) async fn get_tlm_history(
|
||||||
data_arc: web::Data<Arc<TelemetryManagementService>>,
|
data_arc: web::Data<Arc<TelemetryManagementService>>,
|
||||||
uuid: web::Path<String>,
|
uuid: web::Path<String>,
|
||||||
info: web::Query<HistoryQuery>,
|
info: web::Query<HistoryQuery>,
|
||||||
) -> Result<impl Responder, HttpServerResultError> {
|
) -> 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!(
|
trace!(
|
||||||
"get_tlm_history {} from {} to {} resolution {}",
|
"get_tlm_history {} from {} to {} resolution {}",
|
||||||
uuid,
|
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::http::StatusCode;
|
||||||
use actix_web::HttpResponse;
|
use actix_web::HttpResponse;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
#[derive(Error, Debug)]
|
#[derive(Error, Debug)]
|
||||||
pub enum HttpServerResultError {
|
pub enum HttpServerResultError {
|
||||||
#[error("Telemetry Name Not Found: {tlm}")]
|
#[error("Telemetry Name Not Found: {tlm}")]
|
||||||
TlmNameNotFound { tlm: String },
|
TlmNameNotFound { tlm: String },
|
||||||
|
#[error("Invalid Uuid: {uuid}")]
|
||||||
|
InvalidUuid { uuid: String },
|
||||||
#[error("Telemetry Uuid Not Found: {uuid}")]
|
#[error("Telemetry Uuid Not Found: {uuid}")]
|
||||||
TlmUuidNotFound { uuid: String },
|
TlmUuidNotFound { uuid: Uuid },
|
||||||
#[error("DateTime Parsing Error: {date_time}")]
|
#[error("DateTime Parsing Error: {date_time}")]
|
||||||
InvalidDateTime { date_time: String },
|
InvalidDateTime { date_time: String },
|
||||||
#[error("Timed out")]
|
#[error("Timed out")]
|
||||||
@@ -17,7 +20,7 @@ pub enum HttpServerResultError {
|
|||||||
#[error("Internal Error")]
|
#[error("Internal Error")]
|
||||||
InternalError(#[from] anyhow::Error),
|
InternalError(#[from] anyhow::Error),
|
||||||
#[error("Panel Uuid Not Found: {uuid}")]
|
#[error("Panel Uuid Not Found: {uuid}")]
|
||||||
PanelUuidNotFound { uuid: String },
|
PanelUuidNotFound { uuid: Uuid },
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
Command(#[from] crate::command::error::Error),
|
Command(#[from] crate::command::error::Error),
|
||||||
}
|
}
|
||||||
@@ -26,6 +29,7 @@ impl ResponseError for HttpServerResultError {
|
|||||||
fn status_code(&self) -> StatusCode {
|
fn status_code(&self) -> StatusCode {
|
||||||
match self {
|
match self {
|
||||||
HttpServerResultError::TlmNameNotFound { .. } => StatusCode::NOT_FOUND,
|
HttpServerResultError::TlmNameNotFound { .. } => StatusCode::NOT_FOUND,
|
||||||
|
HttpServerResultError::InvalidUuid { .. } => StatusCode::BAD_REQUEST,
|
||||||
HttpServerResultError::TlmUuidNotFound { .. } => StatusCode::NOT_FOUND,
|
HttpServerResultError::TlmUuidNotFound { .. } => StatusCode::NOT_FOUND,
|
||||||
HttpServerResultError::InvalidDateTime { .. } => StatusCode::BAD_REQUEST,
|
HttpServerResultError::InvalidDateTime { .. } => StatusCode::BAD_REQUEST,
|
||||||
HttpServerResultError::Timeout => StatusCode::GATEWAY_TIMEOUT,
|
HttpServerResultError::Timeout => StatusCode::GATEWAY_TIMEOUT,
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
mod api;
|
mod api;
|
||||||
|
mod backend;
|
||||||
mod error;
|
mod error;
|
||||||
mod websocket;
|
mod websocket;
|
||||||
|
|
||||||
use crate::command::service::CommandManagementService;
|
use crate::command::service::CommandManagementService;
|
||||||
use crate::http::api::setup_api;
|
use crate::http::api::setup_api;
|
||||||
|
use crate::http::backend::setup_backend;
|
||||||
use crate::http::websocket::setup_websocket;
|
use crate::http::websocket::setup_websocket;
|
||||||
use crate::panels::PanelService;
|
use crate::panels::PanelService;
|
||||||
use crate::telemetry::management_service::TelemetryManagementService;
|
use crate::telemetry::management_service::TelemetryManagementService;
|
||||||
@@ -31,6 +33,7 @@ pub async fn setup(
|
|||||||
.app_data(cancel_token.clone())
|
.app_data(cancel_token.clone())
|
||||||
.app_data(panel_service.clone())
|
.app_data(panel_service.clone())
|
||||||
.app_data(command_service.clone())
|
.app_data(command_service.clone())
|
||||||
|
.service(web::scope("/backend").configure(setup_backend))
|
||||||
.service(web::scope("/ws").configure(setup_websocket))
|
.service(web::scope("/ws").configure(setup_websocket))
|
||||||
.service(web::scope("/api").configure(setup_api))
|
.service(web::scope("/api").configure(setup_api))
|
||||||
.wrap(Logger::default())
|
.wrap(Logger::default())
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ use crate::telemetry::management_service::TelemetryManagementService;
|
|||||||
use actix_web::{rt, web, HttpRequest, HttpResponse};
|
use actix_web::{rt, web, HttpRequest, HttpResponse};
|
||||||
use actix_ws::{AggregatedMessage, ProtocolError, Session};
|
use actix_ws::{AggregatedMessage, ProtocolError, Session};
|
||||||
use anyhow::anyhow;
|
use anyhow::anyhow;
|
||||||
|
use futures_util::StreamExt;
|
||||||
use log::{error, trace};
|
use log::{error, trace};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
@@ -14,7 +15,7 @@ use tokio::select;
|
|||||||
use tokio::sync::mpsc::Sender;
|
use tokio::sync::mpsc::Sender;
|
||||||
use tokio::time::{sleep_until, Instant};
|
use tokio::time::{sleep_until, Instant};
|
||||||
use tokio_util::sync::CancellationToken;
|
use tokio_util::sync::CancellationToken;
|
||||||
use tonic::codegen::tokio_stream::StreamExt;
|
use uuid::Uuid;
|
||||||
|
|
||||||
pub mod request;
|
pub mod request;
|
||||||
pub mod response;
|
pub mod response;
|
||||||
@@ -23,11 +24,11 @@ fn handle_register_tlm_listener(
|
|||||||
data: &Arc<TelemetryManagementService>,
|
data: &Arc<TelemetryManagementService>,
|
||||||
request: RegisterTlmListenerRequest,
|
request: RegisterTlmListenerRequest,
|
||||||
tx: &Sender<WebsocketResponse>,
|
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) {
|
if let Some(tlm_data) = data.get_by_uuid(&request.uuid) {
|
||||||
let token = CancellationToken::new();
|
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();
|
token.cancel();
|
||||||
}
|
}
|
||||||
let minimum_separation = Duration::from_millis(request.minimum_separation_ms as u64);
|
let minimum_separation = Duration::from_millis(request.minimum_separation_ms as u64);
|
||||||
@@ -46,7 +47,7 @@ fn handle_register_tlm_listener(
|
|||||||
ref_val.clone()
|
ref_val.clone()
|
||||||
};
|
};
|
||||||
let _ = tx.send(TlmValueResponse {
|
let _ = tx.send(TlmValueResponse {
|
||||||
uuid: request.uuid.clone(),
|
uuid: request.uuid,
|
||||||
value,
|
value,
|
||||||
}.into()).await;
|
}.into()).await;
|
||||||
now
|
now
|
||||||
@@ -65,7 +66,7 @@ fn handle_register_tlm_listener(
|
|||||||
|
|
||||||
fn handle_unregister_tlm_listener(
|
fn handle_unregister_tlm_listener(
|
||||||
request: UnregisterTlmListenerRequest,
|
request: UnregisterTlmListenerRequest,
|
||||||
tlm_listeners: &mut HashMap<String, CancellationToken>,
|
tlm_listeners: &mut HashMap<Uuid, CancellationToken>,
|
||||||
) {
|
) {
|
||||||
if let Some(token) = tlm_listeners.remove(&request.uuid) {
|
if let Some(token) = tlm_listeners.remove(&request.uuid) {
|
||||||
token.cancel();
|
token.cancel();
|
||||||
@@ -76,7 +77,7 @@ async fn handle_websocket_message(
|
|||||||
data: &Arc<TelemetryManagementService>,
|
data: &Arc<TelemetryManagementService>,
|
||||||
request: WebsocketRequest,
|
request: WebsocketRequest,
|
||||||
tx: &Sender<WebsocketResponse>,
|
tx: &Sender<WebsocketResponse>,
|
||||||
tlm_listeners: &mut HashMap<String, CancellationToken>,
|
tlm_listeners: &mut HashMap<Uuid, CancellationToken>,
|
||||||
) {
|
) {
|
||||||
match request {
|
match request {
|
||||||
WebsocketRequest::RegisterTlmListener(request) => {
|
WebsocketRequest::RegisterTlmListener(request) => {
|
||||||
@@ -110,7 +111,7 @@ async fn handle_websocket_incoming(
|
|||||||
data: &Arc<TelemetryManagementService>,
|
data: &Arc<TelemetryManagementService>,
|
||||||
session: &mut Session,
|
session: &mut Session,
|
||||||
tx: &Sender<WebsocketResponse>,
|
tx: &Sender<WebsocketResponse>,
|
||||||
tlm_listeners: &mut HashMap<String, CancellationToken>,
|
tlm_listeners: &mut HashMap<Uuid, CancellationToken>,
|
||||||
) -> anyhow::Result<bool> {
|
) -> anyhow::Result<bool> {
|
||||||
match msg {
|
match msg {
|
||||||
Ok(AggregatedMessage::Close(_)) => Ok(false),
|
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,
|
req: HttpRequest,
|
||||||
stream: web::Payload,
|
stream: web::Payload,
|
||||||
data: web::Data<Arc<TelemetryManagementService>>,
|
data: web::Data<Arc<TelemetryManagementService>>,
|
||||||
|
|||||||
@@ -1,15 +1,16 @@
|
|||||||
use derive_more::From;
|
use derive_more::From;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct RegisterTlmListenerRequest {
|
pub struct RegisterTlmListenerRequest {
|
||||||
pub uuid: String,
|
pub uuid: Uuid,
|
||||||
pub minimum_separation_ms: u32,
|
pub minimum_separation_ms: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct UnregisterTlmListenerRequest {
|
pub struct UnregisterTlmListenerRequest {
|
||||||
pub uuid: String,
|
pub uuid: Uuid,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize, From)]
|
#[derive(Debug, Clone, Serialize, Deserialize, From)]
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
use crate::telemetry::data_item::TelemetryDataItem;
|
use crate::telemetry::data_item::TelemetryDataItem;
|
||||||
use derive_more::From;
|
use derive_more::From;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct TlmValueResponse {
|
pub struct TlmValueResponse {
|
||||||
pub uuid: String,
|
pub uuid: Uuid,
|
||||||
pub value: Option<TelemetryDataItem>,
|
pub value: Option<TelemetryDataItem>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,14 +1,8 @@
|
|||||||
mod command;
|
mod command;
|
||||||
mod grpc;
|
|
||||||
mod http;
|
mod http;
|
||||||
mod panels;
|
mod panels;
|
||||||
mod serialization;
|
mod serialization;
|
||||||
mod telemetry;
|
mod telemetry;
|
||||||
mod uuid;
|
|
||||||
|
|
||||||
pub mod core {
|
|
||||||
tonic::include_proto!("core");
|
|
||||||
}
|
|
||||||
|
|
||||||
use crate::command::service::CommandManagementService;
|
use crate::command::service::CommandManagementService;
|
||||||
use crate::panels::PanelService;
|
use crate::panels::PanelService;
|
||||||
@@ -53,14 +47,11 @@ pub async fn setup() -> anyhow::Result<()> {
|
|||||||
|
|
||||||
let cmd = Arc::new(CommandManagementService::new());
|
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 panel_service = PanelService::new(sqlite.clone());
|
||||||
|
|
||||||
let result = http::setup(cancellation_token.clone(), tlm.clone(), panel_service, cmd).await;
|
let result = http::setup(cancellation_token.clone(), tlm.clone(), panel_service, cmd).await;
|
||||||
cancellation_token.cancel();
|
cancellation_token.cancel();
|
||||||
result?; // result is dropped
|
result?; // result is dropped
|
||||||
grpc_server.await?; //grpc server is dropped
|
|
||||||
drop(cancellation_token); // All cancellation tokens are now dropped
|
drop(cancellation_token); // All cancellation tokens are now dropped
|
||||||
|
|
||||||
sqlite.close().await;
|
sqlite.close().await;
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
pub mod panel;
|
pub mod panel;
|
||||||
|
|
||||||
use crate::core::Uuid;
|
|
||||||
use crate::panels::panel::{PanelRequired, PanelUpdate};
|
use crate::panels::panel::{PanelRequired, PanelUpdate};
|
||||||
use panel::Panel;
|
use panel::Panel;
|
||||||
use sqlx::SqlitePool;
|
use sqlx::SqlitePool;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
pub struct PanelService {
|
pub struct PanelService {
|
||||||
pool: SqlitePool,
|
pool: SqlitePool,
|
||||||
@@ -15,7 +15,8 @@ impl PanelService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn create(&self, name: &str, data: &str) -> anyhow::Result<Uuid> {
|
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?;
|
let mut transaction = self.pool.begin().await?;
|
||||||
|
|
||||||
@@ -24,7 +25,7 @@ impl PanelService {
|
|||||||
INSERT INTO PANELS (id, name, data, deleted)
|
INSERT INTO PANELS (id, name, data, deleted)
|
||||||
VALUES ($1, $2, $3, FALSE);
|
VALUES ($1, $2, $3, FALSE);
|
||||||
"#,
|
"#,
|
||||||
id.value,
|
id_string,
|
||||||
name,
|
name,
|
||||||
data
|
data
|
||||||
)
|
)
|
||||||
@@ -65,7 +66,7 @@ impl PanelService {
|
|||||||
WHERE id = $1 AND deleted = FALSE
|
WHERE id = $1 AND deleted = FALSE
|
||||||
"#,
|
"#,
|
||||||
)
|
)
|
||||||
.bind(id.value)
|
.bind(id.to_string())
|
||||||
.fetch_optional(&mut *transaction)
|
.fetch_optional(&mut *transaction)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
@@ -75,6 +76,7 @@ impl PanelService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn update(&self, id: Uuid, data: PanelUpdate) -> anyhow::Result<()> {
|
pub async fn update(&self, id: Uuid, data: PanelUpdate) -> anyhow::Result<()> {
|
||||||
|
let id = id.to_string();
|
||||||
let mut transaction = self.pool.begin().await?;
|
let mut transaction = self.pool.begin().await?;
|
||||||
|
|
||||||
if let Some(name) = data.name {
|
if let Some(name) = data.name {
|
||||||
@@ -84,7 +86,7 @@ impl PanelService {
|
|||||||
SET name = $2
|
SET name = $2
|
||||||
WHERE id = $1;
|
WHERE id = $1;
|
||||||
"#,
|
"#,
|
||||||
id.value,
|
id,
|
||||||
name
|
name
|
||||||
)
|
)
|
||||||
.execute(&mut *transaction)
|
.execute(&mut *transaction)
|
||||||
@@ -97,7 +99,7 @@ impl PanelService {
|
|||||||
SET data = $2
|
SET data = $2
|
||||||
WHERE id = $1;
|
WHERE id = $1;
|
||||||
"#,
|
"#,
|
||||||
id.value,
|
id,
|
||||||
data
|
data
|
||||||
)
|
)
|
||||||
.execute(&mut *transaction)
|
.execute(&mut *transaction)
|
||||||
@@ -110,6 +112,7 @@ impl PanelService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn delete(&self, id: Uuid) -> anyhow::Result<()> {
|
pub async fn delete(&self, id: Uuid) -> anyhow::Result<()> {
|
||||||
|
let id = id.to_string();
|
||||||
let mut transaction = self.pool.begin().await?;
|
let mut transaction = self.pool.begin().await?;
|
||||||
|
|
||||||
let _ = sqlx::query!(
|
let _ = sqlx::query!(
|
||||||
@@ -118,7 +121,7 @@ impl PanelService {
|
|||||||
SET deleted = TRUE
|
SET deleted = TRUE
|
||||||
WHERE id = $1;
|
WHERE id = $1;
|
||||||
"#,
|
"#,
|
||||||
id.value,
|
id,
|
||||||
)
|
)
|
||||||
.execute(&mut *transaction)
|
.execute(&mut *transaction)
|
||||||
.await?;
|
.await?;
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
use crate::telemetry::data_value::TelemetryDataValue;
|
use api::data_value::DataValue;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct TelemetryDataItem {
|
pub struct TelemetryDataItem {
|
||||||
pub value: TelemetryDataValue,
|
pub value: DataValue,
|
||||||
pub timestamp: String,
|
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,13 +1,10 @@
|
|||||||
use crate::core::TelemetryDataType;
|
use api::data_type::DataType;
|
||||||
use crate::telemetry::data_type::tlm_data_type_deserializer;
|
|
||||||
use crate::telemetry::data_type::tlm_data_type_serializer;
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct TelemetryDefinition {
|
pub struct TelemetryDefinition {
|
||||||
pub uuid: String,
|
pub uuid: Uuid,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
#[serde(serialize_with = "tlm_data_type_serializer")]
|
pub data_type: DataType,
|
||||||
#[serde(deserialize_with = "tlm_data_type_deserializer")]
|
|
||||||
pub data_type: TelemetryDataType,
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
use crate::core::TelemetryDataType;
|
|
||||||
use crate::serialization::file_ext::{ReadExt, WriteExt};
|
use crate::serialization::file_ext::{ReadExt, WriteExt};
|
||||||
use crate::telemetry::data::TelemetryData;
|
use crate::telemetry::data::TelemetryData;
|
||||||
use crate::telemetry::data_item::TelemetryDataItem;
|
use crate::telemetry::data_item::TelemetryDataItem;
|
||||||
use crate::telemetry::data_value::TelemetryDataValue;
|
|
||||||
use crate::telemetry::definition::TelemetryDefinition;
|
use crate::telemetry::definition::TelemetryDefinition;
|
||||||
use anyhow::{anyhow, ensure, Context};
|
use anyhow::{anyhow, ensure, Context};
|
||||||
|
use api::data_type::DataType;
|
||||||
|
use api::data_value::DataValue;
|
||||||
use chrono::{DateTime, DurationRound, SecondsFormat, TimeDelta, Utc};
|
use chrono::{DateTime, DurationRound, SecondsFormat, TimeDelta, Utc};
|
||||||
use log::{error, info};
|
use log::{error, info};
|
||||||
use std::cmp::min;
|
use std::cmp::min;
|
||||||
@@ -44,7 +44,7 @@ fn update_next_from(
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct SegmentData {
|
struct SegmentData {
|
||||||
values: Vec<TelemetryDataValue>,
|
values: Vec<DataValue>,
|
||||||
timestamps: Vec<DateTime<Utc>>,
|
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 {
|
if timestamp < self.start || timestamp >= self.end {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -121,7 +121,7 @@ impl HistorySegmentRam {
|
|||||||
next_from,
|
next_from,
|
||||||
);
|
);
|
||||||
result.push(TelemetryDataItem {
|
result.push(TelemetryDataItem {
|
||||||
value: data.values[i].clone(),
|
value: data.values[i],
|
||||||
timestamp: t.to_rfc3339_opts(SecondsFormat::Millis, true),
|
timestamp: t.to_rfc3339_opts(SecondsFormat::Millis, true),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -196,9 +196,9 @@ impl HistorySegmentFile {
|
|||||||
// Write all the values
|
// Write all the values
|
||||||
for value in &data.values {
|
for value in &data.values {
|
||||||
match value {
|
match value {
|
||||||
TelemetryDataValue::Float32(value) => file.write_data::<f32>(*value)?,
|
DataValue::Float32(value) => file.write_data::<f32>(*value)?,
|
||||||
TelemetryDataValue::Float64(value) => file.write_data::<f64>(*value)?,
|
DataValue::Float64(value) => file.write_data::<f64>(*value)?,
|
||||||
TelemetryDataValue::Boolean(value) => file.write_data::<bool>(*value)?,
|
DataValue::Boolean(value) => file.write_data::<bool>(*value)?,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -215,10 +215,7 @@ impl HistorySegmentFile {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn load_to_ram(
|
fn load_to_ram(mut self, telemetry_data_type: DataType) -> anyhow::Result<HistorySegmentRam> {
|
||||||
mut self,
|
|
||||||
telemetry_data_type: TelemetryDataType,
|
|
||||||
) -> anyhow::Result<HistorySegmentRam> {
|
|
||||||
let mut segment_data = SegmentData {
|
let mut segment_data = SegmentData {
|
||||||
values: Vec::with_capacity(self.length as usize),
|
values: Vec::with_capacity(self.length as usize),
|
||||||
timestamps: Vec::with_capacity(self.length as usize),
|
timestamps: Vec::with_capacity(self.length as usize),
|
||||||
@@ -281,7 +278,7 @@ impl HistorySegmentFile {
|
|||||||
from: DateTime<Utc>,
|
from: DateTime<Utc>,
|
||||||
to: DateTime<Utc>,
|
to: DateTime<Utc>,
|
||||||
maximum_resolution: TimeDelta,
|
maximum_resolution: TimeDelta,
|
||||||
telemetry_data_type: TelemetryDataType,
|
telemetry_data_type: DataType,
|
||||||
) -> anyhow::Result<(DateTime<Utc>, Vec<TelemetryDataItem>)> {
|
) -> anyhow::Result<(DateTime<Utc>, Vec<TelemetryDataItem>)> {
|
||||||
self.file_position = 0;
|
self.file_position = 0;
|
||||||
self.file.seek(SeekFrom::Start(0))?;
|
self.file.seek(SeekFrom::Start(0))?;
|
||||||
@@ -334,22 +331,19 @@ impl HistorySegmentFile {
|
|||||||
self.read_date_time()
|
self.read_date_time()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_telemetry_item(
|
fn read_telemetry_item(&mut self, telemetry_data_type: DataType) -> anyhow::Result<DataValue> {
|
||||||
&mut self,
|
|
||||||
telemetry_data_type: TelemetryDataType,
|
|
||||||
) -> anyhow::Result<TelemetryDataValue> {
|
|
||||||
match telemetry_data_type {
|
match telemetry_data_type {
|
||||||
TelemetryDataType::Float32 => {
|
DataType::Float32 => {
|
||||||
self.file_position += 4;
|
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;
|
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;
|
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(
|
fn get_telemetry_item(
|
||||||
&mut self,
|
&mut self,
|
||||||
index: u64,
|
index: u64,
|
||||||
telemetry_data_type: TelemetryDataType,
|
telemetry_data_type: DataType,
|
||||||
) -> anyhow::Result<TelemetryDataValue> {
|
) -> anyhow::Result<DataValue> {
|
||||||
let item_length = match telemetry_data_type {
|
let item_length = match telemetry_data_type {
|
||||||
TelemetryDataType::Float32 => 4,
|
DataType::Float32 => 4,
|
||||||
TelemetryDataType::Float64 => 8,
|
DataType::Float64 => 8,
|
||||||
TelemetryDataType::Boolean => 1,
|
DataType::Boolean => 1,
|
||||||
};
|
};
|
||||||
let desired_position =
|
let desired_position =
|
||||||
Self::HEADER_LENGTH + self.length * Self::TIMESTAMP_LENGTH + index * item_length;
|
Self::HEADER_LENGTH + self.length * Self::TIMESTAMP_LENGTH + index * item_length;
|
||||||
@@ -429,7 +423,7 @@ impl TelemetryHistory {
|
|||||||
history_segment_ram: HistorySegmentRam,
|
history_segment_ram: HistorySegmentRam,
|
||||||
) -> JoinHandle<()> {
|
) -> JoinHandle<()> {
|
||||||
let mut path = service.data_root_folder.clone();
|
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 || {
|
spawn_blocking(move || {
|
||||||
match HistorySegmentFile::save_to_disk(path, history_segment_ram) {
|
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
|
// 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>,
|
start: DateTime<Utc>,
|
||||||
) -> JoinHandle<anyhow::Result<HistorySegmentFile>> {
|
) -> JoinHandle<anyhow::Result<HistorySegmentFile>> {
|
||||||
let mut path = service.data_root_folder.clone();
|
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))
|
spawn_blocking(move || HistorySegmentFile::open(path, start))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -458,7 +452,7 @@ impl TelemetryHistory {
|
|||||||
&self,
|
&self,
|
||||||
start: DateTime<Utc>,
|
start: DateTime<Utc>,
|
||||||
service: &TelemetryHistoryService,
|
service: &TelemetryHistoryService,
|
||||||
telemetry_data_type: TelemetryDataType,
|
telemetry_data_type: DataType,
|
||||||
) -> HistorySegmentRam {
|
) -> HistorySegmentRam {
|
||||||
let ram = self
|
let ram = self
|
||||||
.get_disk_segment(service, start)
|
.get_disk_segment(service, start)
|
||||||
@@ -480,7 +474,7 @@ impl TelemetryHistory {
|
|||||||
pub async fn insert(
|
pub async fn insert(
|
||||||
&self,
|
&self,
|
||||||
service: &TelemetryHistoryService,
|
service: &TelemetryHistoryService,
|
||||||
value: TelemetryDataValue,
|
value: DataValue,
|
||||||
timestamp: DateTime<Utc>,
|
timestamp: DateTime<Utc>,
|
||||||
) {
|
) {
|
||||||
let segments = self.segments.read().await;
|
let segments = self.segments.read().await;
|
||||||
@@ -531,7 +525,7 @@ impl TelemetryHistory {
|
|||||||
pub fn insert_sync(
|
pub fn insert_sync(
|
||||||
history: Arc<Self>,
|
history: Arc<Self>,
|
||||||
service: Arc<TelemetryHistoryService>,
|
service: Arc<TelemetryHistoryService>,
|
||||||
value: TelemetryDataValue,
|
value: DataValue,
|
||||||
timestamp: DateTime<Utc>,
|
timestamp: DateTime<Utc>,
|
||||||
) {
|
) {
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
@@ -579,7 +573,7 @@ impl TelemetryHistory {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let mut path = telemetry_history_service.data_root_folder.clone();
|
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;
|
let mut start = start;
|
||||||
while start < end {
|
while start < end {
|
||||||
|
|||||||
@@ -1,7 +1,15 @@
|
|||||||
use crate::core::{TelemetryDefinitionRequest, Uuid};
|
|
||||||
use crate::telemetry::data::TelemetryData;
|
use crate::telemetry::data::TelemetryData;
|
||||||
|
use crate::telemetry::data_item::TelemetryDataItem;
|
||||||
use crate::telemetry::definition::TelemetryDefinition;
|
use crate::telemetry::definition::TelemetryDefinition;
|
||||||
use crate::telemetry::history::{TelemetryHistory, TelemetryHistoryService};
|
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 log::{error, info, warn};
|
||||||
use papaya::{HashMap, HashMapRef, LocalGuard};
|
use papaya::{HashMap, HashMapRef, LocalGuard};
|
||||||
use std::fs;
|
use std::fs;
|
||||||
@@ -12,12 +20,13 @@ use std::sync::Arc;
|
|||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use tokio::sync::Mutex;
|
use tokio::sync::Mutex;
|
||||||
use tokio::time::sleep;
|
use tokio::time::sleep;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
const RELEASED_ATTEMPTS: usize = 5;
|
const RELEASED_ATTEMPTS: usize = 5;
|
||||||
|
|
||||||
pub struct TelemetryManagementService {
|
pub struct TelemetryManagementService {
|
||||||
uuid_index: HashMap<String, String>,
|
uuid_index: HashMap<String, Uuid>,
|
||||||
tlm_data: HashMap<String, Arc<TelemetryHistory>>,
|
tlm_data: HashMap<Uuid, Arc<TelemetryHistory>>,
|
||||||
telemetry_history_service: Arc<TelemetryHistoryService>,
|
telemetry_history_service: Arc<TelemetryHistoryService>,
|
||||||
metadata_file: Arc<Mutex<File>>,
|
metadata_file: Arc<Mutex<File>>,
|
||||||
}
|
}
|
||||||
@@ -49,8 +58,8 @@ impl TelemetryManagementService {
|
|||||||
// Skip invalid entries
|
// Skip invalid entries
|
||||||
match serde_json::from_str::<TelemetryDefinition>(line) {
|
match serde_json::from_str::<TelemetryDefinition>(line) {
|
||||||
Ok(tlm_def) => {
|
Ok(tlm_def) => {
|
||||||
let _ = uuid_index.insert(tlm_def.name.clone(), tlm_def.uuid.clone());
|
let _ = uuid_index.insert(tlm_def.name.clone(), tlm_def.uuid);
|
||||||
let _ = tlm_data.insert(tlm_def.uuid.clone(), Arc::new(tlm_def.into()));
|
let _ = tlm_data.insert(tlm_def.uuid, Arc::new(tlm_def.into()));
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
error!("Failed to parse metadata entry {err}");
|
error!("Failed to parse metadata entry {err}");
|
||||||
@@ -79,23 +88,20 @@ impl TelemetryManagementService {
|
|||||||
pub fn register(
|
pub fn register(
|
||||||
&self,
|
&self,
|
||||||
telemetry_definition_request: TelemetryDefinitionRequest,
|
telemetry_definition_request: TelemetryDefinitionRequest,
|
||||||
) -> anyhow::Result<String> {
|
) -> anyhow::Result<TelemetryDefinitionResponse> {
|
||||||
let uuid_index = self.uuid_index.pin();
|
let uuid_index = self.uuid_index.pin();
|
||||||
let tlm_data = self.tlm_data.pin();
|
let tlm_data = self.tlm_data.pin();
|
||||||
|
|
||||||
let uuid = uuid_index
|
let uuid =
|
||||||
.get_or_insert_with(telemetry_definition_request.name.clone(), || {
|
*uuid_index.get_or_insert_with(telemetry_definition_request.name.clone(), Uuid::new_v4);
|
||||||
Uuid::random().value
|
|
||||||
})
|
|
||||||
.clone();
|
|
||||||
|
|
||||||
let inserted = tlm_data.try_insert(
|
let inserted = tlm_data.try_insert(
|
||||||
uuid.clone(),
|
uuid,
|
||||||
Arc::new(
|
Arc::new(
|
||||||
TelemetryDefinition {
|
TelemetryDefinition {
|
||||||
uuid: uuid.clone(),
|
uuid,
|
||||||
name: telemetry_definition_request.name.clone(),
|
name: telemetry_definition_request.name.clone(),
|
||||||
data_type: telemetry_definition_request.data_type(),
|
data_type: telemetry_definition_request.data_type,
|
||||||
}
|
}
|
||||||
.into(),
|
.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> {
|
pub fn get_by_name(&self, name: &String) -> Option<TelemetryData> {
|
||||||
@@ -138,7 +175,7 @@ impl TelemetryManagementService {
|
|||||||
self.get_by_uuid(uuid)
|
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();
|
let tlm_data = self.tlm_data.pin();
|
||||||
tlm_data
|
tlm_data
|
||||||
.get(uuid)
|
.get(uuid)
|
||||||
@@ -200,11 +237,11 @@ impl TelemetryManagementService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct TelemetryManagementServicePin<'a> {
|
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> {
|
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)
|
self.tlm_data.get(uuid)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
pub mod data;
|
pub mod data;
|
||||||
pub mod data_item;
|
pub mod data_item;
|
||||||
pub mod data_type;
|
|
||||||
pub mod data_value;
|
|
||||||
pub mod definition;
|
pub mod definition;
|
||||||
pub mod history;
|
pub mod history;
|
||||||
pub mod management_service;
|
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