added log feature on extension

This commit is contained in:
Valmo Trindade
2024-12-14 21:12:56 -03:00
parent f5e35683a1
commit cfffbdf8bd
6 changed files with 44 additions and 4 deletions

View File

@@ -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()

View File

@@ -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<LogPayload, FromArmaError> {
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()
}
}

View File

@@ -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;

View File

@@ -17,6 +17,10 @@ pub struct WsServer {
impl WsServer {
pub fn start(&self, rx: Receiver<WsCommand>) {
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);