Devices can request current time with a precision to the millisecond
This commit is contained in:
parent
8918547375
commit
378c296e71
1
central_backend/src/server/devices_api/mod.rs
Normal file
1
central_backend/src/server/devices_api/mod.rs
Normal file
@ -0,0 +1 @@
|
|||||||
|
pub mod utils_controller;
|
15
central_backend/src/server/devices_api/utils_controller.rs
Normal file
15
central_backend/src/server/devices_api/utils_controller.rs
Normal 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(),
|
||||||
|
}))
|
||||||
|
}
|
@ -4,6 +4,7 @@ use crate::energy::energy_actor::EnergyActorAddr;
|
|||||||
|
|
||||||
pub mod auth_middleware;
|
pub mod auth_middleware;
|
||||||
pub mod custom_error;
|
pub mod custom_error;
|
||||||
|
pub mod devices_api;
|
||||||
pub mod servers;
|
pub mod servers;
|
||||||
pub mod unsecure_server;
|
pub mod unsecure_server;
|
||||||
pub mod web_api;
|
pub mod web_api;
|
||||||
|
@ -3,6 +3,7 @@ use crate::constants;
|
|||||||
use crate::crypto::pki;
|
use crate::crypto::pki;
|
||||||
use crate::energy::energy_actor::EnergyActorAddr;
|
use crate::energy::energy_actor::EnergyActorAddr;
|
||||||
use crate::server::auth_middleware::AuthChecker;
|
use crate::server::auth_middleware::AuthChecker;
|
||||||
|
use crate::server::devices_api::utils_controller;
|
||||||
use crate::server::unsecure_server::*;
|
use crate::server::unsecure_server::*;
|
||||||
use crate::server::web_api::*;
|
use crate::server::web_api::*;
|
||||||
use actix_cors::Cors;
|
use actix_cors::Cors;
|
||||||
@ -105,6 +106,7 @@ pub async fn secure_server(energy_actor: EnergyActorAddr) -> anyhow::Result<()>
|
|||||||
proxy: AppConfig::get().proxy_ip.clone(),
|
proxy: AppConfig::get().proxy_ip.clone(),
|
||||||
}))
|
}))
|
||||||
.route("/", web::get().to(server_controller::secure_home))
|
.route("/", web::get().to(server_controller::secure_home))
|
||||||
|
// Web API
|
||||||
.route(
|
.route(
|
||||||
"/web_api/server/config",
|
"/web_api/server/config",
|
||||||
web::get().to(server_controller::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_api/energy/cached_consumption",
|
||||||
web::get().to(energy_controller::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)?
|
.bind_openssl(&AppConfig::get().listen_address, builder)?
|
||||||
.run()
|
.run()
|
||||||
|
@ -1 +1,2 @@
|
|||||||
pub mod files_utils;
|
pub mod files_utils;
|
||||||
|
pub mod time_utils;
|
||||||
|
10
central_backend/src/utils/time_utils.rs
Normal file
10
central_backend/src/utils/time_utils.rs
Normal 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()
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user