diff --git a/addons/main/functions/api/fn_log_message.sqf b/addons/main/functions/api/fn_log_message.sqf new file mode 100644 index 0000000..9cc3a71 --- /dev/null +++ b/addons/main/functions/api/fn_log_message.sqf @@ -0,0 +1,3 @@ +params[_type, _message]; + +"armatak" callExtension ["log", [_type, _message]] \ No newline at end of file diff --git a/addons/main/initPlayerLocal.sqf b/addons/main/initPlayerLocal.sqf index 1064744..f77d5e9 100644 --- a/addons/main/initPlayerLocal.sqf +++ b/addons/main/initPlayerLocal.sqf @@ -36,4 +36,4 @@ onPlayerDisconnected "'armatak' callExtension ['websocket:stop',[]];"; [{ "armatak" callExtension ["websocket:location",[player call armatak_fnc_extract_position]]; -}, 1, []] call CBA_fnc_addPerFrameHandler; \ No newline at end of file +}, 1, []] call CBA_fnc_addPerFrameHandler; diff --git a/src/lib.rs b/src/lib.rs index b3ea4dc..0e64e80 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,6 +39,7 @@ pub fn init() -> Extension { ) .command("local_ip", util::get_local_address) .command("uuid", util::get_uuid) + .command("log", util::log_info) .group( "ots_api", Group::new() diff --git a/src/structs.rs b/src/structs.rs index 8460e5a..64c5e69 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -1,6 +1,28 @@ use arma_rs::{FromArma, FromArmaError}; use serde::{Deserialize, Serialize}; +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct LogPayload { + pub status: String, + pub message: String, +} + +impl IntoMessage for LogPayload { + fn into_message(self) -> String { + serde_json::to_string(&self).unwrap() + } +} + +impl FromArma for LogPayload { + fn from_arma(data: String) -> Result { + let (status, message) = <(String, String)>::from_arma(data)?; + Ok(Self { + status, + message + }) + } +} + #[derive(Debug, Serialize, Deserialize, Clone)] pub struct LocationPayload { pub latitude: f64, @@ -21,7 +43,7 @@ impl IntoMessage for String { impl IntoMessage for LocationPayload { fn into_message(self) -> String { - serde_json::to_string(&self).unwrap() // Convert struct to JSON + serde_json::to_string(&self).unwrap() } } diff --git a/src/util.rs b/src/util.rs index 1eca44d..6ea8d47 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,7 +1,17 @@ -use log::{error, info}; +use log::{error, info, warn}; use reqwest::Client; use std::net::{IpAddr, UdpSocket}; -use crate::structs::{LoginInfo, LoginPayload, Marker, MarkerPayload}; +use crate::structs::{LogPayload, LoginInfo, LoginPayload, Marker, MarkerPayload}; + +pub fn log_info(data: LogPayload) -> String { + match data.status.as_str() { + "info" => info!("{}", data.message), + "warn" => warn!("{}", data.message), + "error" => error!("{}", data.message), + _ => error!("{}","Wrong log call") + } + "logged".to_string() +} pub fn get_uuid() -> String { use uuid::Uuid; diff --git a/src/websocket.rs b/src/websocket.rs index 01cc214..2cfd7a9 100644 --- a/src/websocket.rs +++ b/src/websocket.rs @@ -17,6 +17,10 @@ pub struct WsServer { impl WsServer { pub fn start(&self, rx: Receiver) { + if let Some(ref server) = *WEBSOCKET_SERVER.lock().unwrap() { + server.stop(); + } + let clients = Arc::new(Mutex::new(Vec::new())); let clients_clone = Arc::clone(&clients);