SolarEnergy/central_backend/src/constants.rs

80 lines
2.5 KiB
Rust
Raw Normal View History

2024-06-29 09:45:39 +00:00
use std::time::Duration;
2024-06-29 12:43:56 +00:00
/// Name of the cookie that contains session information
pub const SESSION_COOKIE_NAME: &str = "X-session-cookie";
2024-06-29 09:45:39 +00:00
/// Energy refresh operations interval
pub const ENERGY_REFRESH_INTERVAL: Duration = Duration::from_secs(30);
/// Fallback value to use if production cannot be fetched
pub const FALLBACK_PRODUCTION_VALUE: i32 = 5000;
2024-06-29 12:43:56 +00:00
/// Maximum session duration after inactivity, in seconds
pub const MAX_INACTIVITY_DURATION: u64 = 3600;
/// Maximum session duration (1 day)
pub const MAX_SESSION_DURATION: u64 = 3600 * 24;
/// List of routes that do not require authentication
pub const ROUTES_WITHOUT_AUTH: [&str; 2] =
["/web_api/server/config", "/web_api/auth/password_auth"];
2024-07-22 20:19:48 +00:00
#[derive(serde::Serialize)]
pub struct SizeConstraint {
/// Minimal string length
min: usize,
/// Maximal string length
max: usize,
}
impl SizeConstraint {
pub fn new(min: usize, max: usize) -> Self {
Self { min, max }
}
pub fn validate(&self, val: &str) -> bool {
let len = val.trim().len();
len >= self.min && len <= self.max
}
pub fn validate_usize(&self, val: usize) -> bool {
val >= self.min && val <= self.max
}
2024-07-22 20:19:48 +00:00
}
/// Backend static constraints
#[derive(serde::Serialize)]
pub struct StaticConstraints {
/// Device name constraint
pub dev_name_len: SizeConstraint,
/// Device description constraint
pub dev_description_len: SizeConstraint,
2024-07-29 20:11:13 +00:00
/// Relay name constraint
pub relay_name_len: SizeConstraint,
/// Relay priority constraint
pub relay_priority: SizeConstraint,
/// Relay consumption constraint
pub relay_consumption: SizeConstraint,
/// Relay minimal uptime
pub relay_minimal_uptime: SizeConstraint,
/// Relay minimal downtime
pub relay_minimal_downtime: SizeConstraint,
/// Relay daily minimal uptime
pub relay_daily_minimal_runtime: SizeConstraint,
2024-07-22 20:19:48 +00:00
}
impl Default for StaticConstraints {
fn default() -> Self {
Self {
dev_name_len: SizeConstraint::new(1, 50),
dev_description_len: SizeConstraint::new(0, 100),
2024-07-29 20:11:13 +00:00
relay_name_len: SizeConstraint::new(1, 100),
relay_priority: SizeConstraint::new(0, 999999),
relay_consumption: SizeConstraint::new(0, 999999),
relay_minimal_uptime: SizeConstraint::new(0, 9999999),
relay_minimal_downtime: SizeConstraint::new(0, 9999999),
relay_daily_minimal_runtime: SizeConstraint::new(0, 3600 * 24),
2024-07-22 20:19:48 +00:00
}
}
}