added location sender dll command

This commit is contained in:
Valmo Trindade
2024-10-21 02:14:46 -03:00
parent 059b749cab
commit acb3a49f85
3 changed files with 30 additions and 1 deletions

View File

@@ -5,6 +5,8 @@ use log::info;
use ws::{listen, Message, Result as WsResult}; use ws::{listen, Message, Result as WsResult};
use lazy_static::lazy_static; use lazy_static::lazy_static;
use crate::structs::{IntoMessage, LocationPayload};
enum WsCommand { enum WsCommand {
SendMessage(String), SendMessage(String),
Stop, Stop,
@@ -60,7 +62,8 @@ impl WsServer {
}); });
} }
fn send_message(&self, message: String) { fn send_message<T: IntoMessage>(&self, payload: T) {
let message = payload.into_message(); // Convert the payload to a String
self.tx.send(WsCommand::SendMessage(message)).unwrap(); self.tx.send(WsCommand::SendMessage(message)).unwrap();
} }
@@ -97,6 +100,15 @@ pub fn message(payload: String) -> &'static str {
"Sending message to all WebSocket clients" "Sending message to all WebSocket clients"
} }
pub fn location(payload: LocationPayload) -> &'static str {
if let Some(ref server) = *WEBSOCKET_SERVER.lock().unwrap() {
server.send_message(payload);
} else {
info!("WebSocket server is not running.");
}
"sending location to all WebSocket clients"
}
pub fn stop() -> &'static str { pub fn stop() -> &'static str {
if let Some(ref server) = *WEBSOCKET_SERVER.lock().unwrap() { if let Some(ref server) = *WEBSOCKET_SERVER.lock().unwrap() {
server.stop(); server.stop();

View File

@@ -30,5 +30,6 @@ pub fn init() -> Extension {
.command("start", commands::start) .command("start", commands::start)
.command("stop", commands::stop) .command("stop", commands::stop)
.command("message", commands::message) .command("message", commands::message)
.command("location", commands::location)
.finish() .finish()
} }

View File

@@ -9,6 +9,22 @@ pub struct LocationPayload {
pub bearing: f32, pub bearing: f32,
} }
pub trait IntoMessage {
fn into_message(self) -> String;
}
impl IntoMessage for String {
fn into_message(self) -> String {
self
}
}
impl IntoMessage for LocationPayload {
fn into_message(self) -> String {
serde_json::to_string(&self).unwrap() // Convert struct to JSON
}
}
impl FromArma for LocationPayload { impl FromArma for LocationPayload {
fn from_arma(data: String) -> Result<LocationPayload, FromArmaError> { fn from_arma(data: String) -> Result<LocationPayload, FromArmaError> {
let (latitude, longitude, altitude, bearing) = <(f32, f32, f32, f32)>::from_arma(data)?; let (latitude, longitude, altitude, bearing) = <(f32, f32, f32, f32)>::from_arma(data)?;