Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
timmo001 committed Jun 7, 2024
1 parent 1f8d6d7 commit 3d4d173
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 44 deletions.
5 changes: 3 additions & 2 deletions src-tauri/src/modules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ mod sensors;
mod system;

use crate::{
event::EventType, shared::get_data_path, websocket::client::WebSocketClient,
websocket::server::WebsocketRequest,
event::EventType,
shared::get_data_path,
websocket::{client::WebSocketClient, WebsocketRequest},
};
use log::{error, info};
use serde::{Deserialize, Serialize};
Expand Down
6 changes: 2 additions & 4 deletions src-tauri/src/websocket/client.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::{
settings::{get_settings, Settings},
websocket::server::WebsocketRequest,
};
use super::WebsocketRequest;
use crate::settings::{get_settings, Settings};
use futures_util::SinkExt;
use log::{debug, info};
use serde_json::json;
Expand Down
28 changes: 28 additions & 0 deletions src-tauri/src/websocket/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,30 @@
pub(crate) mod client;
pub(crate) mod server;

use serde::{Deserialize, Serialize};
use serde_json::Value;

#[derive(Debug, Serialize, Deserialize)]
pub struct WebsocketRequest {
pub id: String,
pub token: String,
pub event: String,
pub data: Value,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct WebsocketResponse {
pub id: String,
#[serde(rename = "type")]
pub type_: String,
pub data: Value,
pub subtype: Option<String>,
pub message: Option<String>,
pub module: Option<String>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct DataListener {
pub id: String,
pub modules: Vec<String>,
}
50 changes: 13 additions & 37 deletions src-tauri/src/websocket/server.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::{DataListener, WebsocketRequest, WebsocketResponse};
use crate::{
event::{EventSubtype, EventType},
modules::{get_module_data, Module, ModuleUpdate, RequestModules},
Expand All @@ -6,43 +7,17 @@ use crate::{
use log::{debug, info, warn};
use rocket::get;
use rocket_ws::{Message, Stream, WebSocket};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::str::FromStr;
use std::sync::Mutex;

#[derive(Debug, Serialize, Deserialize)]
pub struct WebsocketRequest {
pub id: String,
pub token: String,
pub event: String,
pub data: Value,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct WebsocketResponse {
pub id: String,
#[serde(rename = "type")]
pub type_: String,
pub data: Value,
pub subtype: Option<String>,
pub message: Option<String>,
pub module: Option<String>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct DataListener {
pub id: String,
pub modules: Vec<String>,
}

static REGISTERED_LISTENERS: Mutex<Vec<DataListener>> = Mutex::new(vec![]);

#[get("/api/websocket")]
pub async fn websocket(ws: WebSocket) -> Stream!['static] {
Stream! { ws =>
for await msg in ws {

// Get the message
let message = msg?.to_string();
debug!("Received message: {:?}", message);

Expand Down Expand Up @@ -204,19 +179,20 @@ pub async fn websocket(ws: WebSocket) -> Stream!['static] {
// TODO: Open the application
}
Ok(EventType::RegisterDataListener) => {
info!("RegisterDataListener event");
let request_data_result: Result<RequestModules, _> =
serde_json::from_value(request.data.clone());
if let Err(e) = request_data_result {
warn!("Invalid data: {:?}", e);
continue;
}

let request_data = request_data_result.unwrap();
info!("Register data listener for modules: {:?}", request_data.modules);

// Register data listener
let listener_id = uuid::Uuid::new_v4().to_string();
REGISTERED_LISTENERS.lock().unwrap().push(DataListener {
id: listener_id.clone(),
modules: request
.data
.as_array()
.unwrap()
.iter()
.map(|m| m.as_str().unwrap().to_string())
.collect(),
id: uuid::Uuid::new_v4().to_string(),
modules: request_data.modules,
});

yield Message::text(serde_json::to_string(&WebsocketResponse {
Expand Down
1 change: 0 additions & 1 deletion src/components/data/items.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ export default function DataItemsComponent({
enableClipboard
name={null}
shouldCollapse={(field: CollapsedFieldProps): boolean => {
console.log(field);
return field.name &&
field.type === "array" &&
Array.isArray(field.src) &&
Expand Down

0 comments on commit 3d4d173

Please sign in to comment.