Devices can request current time with a precision to the millisecond

This commit is contained in:
Pierre HUBERT 2024-07-01 17:56:10 +02:00
parent 8918547375
commit 378c296e71
6 changed files with 35 additions and 0 deletions

View File

@ -0,0 +1 @@
pub mod utils_controller;

View File

@ -0,0 +1,15 @@
use crate::server::custom_error::HttpResult;
use crate::utils::time_utils::time_millis;
use actix_web::HttpResponse;
#[derive(serde::Serialize)]
pub struct CurrTime {
time_ms: u128,
}
/// Get current time
pub async fn curr_time() -> HttpResult {
Ok(HttpResponse::Ok().json(CurrTime {
time_ms: time_millis(),
}))
}

View File

@ -4,6 +4,7 @@ use crate::energy::energy_actor::EnergyActorAddr;
pub mod auth_middleware;
pub mod custom_error;
pub mod devices_api;
pub mod servers;
pub mod unsecure_server;
pub mod web_api;

View File

@ -3,6 +3,7 @@ use crate::constants;
use crate::crypto::pki;
use crate::energy::energy_actor::EnergyActorAddr;
use crate::server::auth_middleware::AuthChecker;
use crate::server::devices_api::utils_controller;
use crate::server::unsecure_server::*;
use crate::server::web_api::*;
use actix_cors::Cors;
@ -105,6 +106,7 @@ pub async fn secure_server(energy_actor: EnergyActorAddr) -> anyhow::Result<()>
proxy: AppConfig::get().proxy_ip.clone(),
}))
.route("/", web::get().to(server_controller::secure_home))
// Web API
.route(
"/web_api/server/config",
web::get().to(server_controller::config),
@ -129,6 +131,11 @@ pub async fn secure_server(energy_actor: EnergyActorAddr) -> anyhow::Result<()>
"/web_api/energy/cached_consumption",
web::get().to(energy_controller::cached_consumption),
)
// Devices API
.route(
"/devices_api/utils/time",
web::get().to(utils_controller::curr_time),
)
})
.bind_openssl(&AppConfig::get().listen_address, builder)?
.run()

View File

@ -1 +1,2 @@
pub mod files_utils;
pub mod time_utils;

View File

@ -0,0 +1,10 @@
use std::time::{SystemTime, UNIX_EPOCH};
/// Get the current time since epoch
pub fn time_millis() -> u128 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_millis()
}