2024-09-17 20:31:51 +00:00
|
|
|
use crate::devices::device::{DeviceId, DeviceRelayID};
|
2024-10-05 13:50:46 +00:00
|
|
|
use crate::ota::ota_update::OTAPlatform;
|
2024-06-29 08:11:31 +00:00
|
|
|
use clap::{Parser, Subcommand};
|
2024-06-27 23:05:02 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2024-06-27 16:55:09 +00:00
|
|
|
|
2024-09-25 17:05:54 +00:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
pub enum ConsumptionHistoryType {
|
|
|
|
GridConsumption,
|
|
|
|
RelayConsumption,
|
|
|
|
}
|
|
|
|
|
2024-06-29 08:11:31 +00:00
|
|
|
/// Electrical consumption fetcher backend
|
|
|
|
#[derive(Subcommand, Debug, Clone)]
|
|
|
|
pub enum ConsumptionBackend {
|
|
|
|
/// Constant consumption value
|
|
|
|
Constant {
|
2024-06-30 20:54:23 +00:00
|
|
|
/// The constant value to use
|
2024-06-30 18:14:23 +00:00
|
|
|
#[clap(short, long, default_value_t = 500)]
|
2024-06-29 08:11:31 +00:00
|
|
|
value: i32,
|
|
|
|
},
|
|
|
|
|
|
|
|
/// Generate random consumption value
|
|
|
|
Random {
|
2024-06-30 20:54:23 +00:00
|
|
|
/// Minimum acceptable generated value
|
2024-06-29 08:11:31 +00:00
|
|
|
#[clap(long, default_value_t = -5000)]
|
|
|
|
min: i32,
|
2024-06-30 20:54:23 +00:00
|
|
|
/// Maximum acceptable generated value
|
2024-06-29 08:11:31 +00:00
|
|
|
#[clap(long, default_value_t = 20000)]
|
|
|
|
max: i32,
|
|
|
|
},
|
2024-06-30 18:14:23 +00:00
|
|
|
|
|
|
|
/// Read consumption value in a file, on the filesystem
|
|
|
|
File {
|
2024-06-30 20:54:23 +00:00
|
|
|
/// The path to the file that will be read to process consumption values
|
2024-06-30 18:14:23 +00:00
|
|
|
#[clap(short, long, default_value = "/dev/shm/consumption.txt")]
|
|
|
|
path: String,
|
|
|
|
},
|
2024-10-12 15:28:49 +00:00
|
|
|
|
|
|
|
/// Fronius inverter consumption
|
|
|
|
Fronius {
|
|
|
|
/// The origin of the domain where the webserver of the Fronius Symo can be reacher
|
|
|
|
#[clap(short, long)]
|
|
|
|
origin: String,
|
|
|
|
},
|
2024-06-29 08:11:31 +00:00
|
|
|
}
|
|
|
|
|
2024-06-27 16:55:09 +00:00
|
|
|
/// Solar system central backend
|
|
|
|
#[derive(Parser, Debug)]
|
|
|
|
#[command(version, about, long_about = None)]
|
|
|
|
pub struct AppConfig {
|
2024-10-18 17:57:06 +00:00
|
|
|
/// Read arguments from env file
|
|
|
|
#[clap(short, long, env)]
|
|
|
|
pub config: Option<String>,
|
|
|
|
|
2024-06-29 12:43:56 +00:00
|
|
|
/// Proxy IP, might end with a star "*"
|
|
|
|
#[clap(short, long, env)]
|
|
|
|
pub proxy_ip: Option<String>,
|
|
|
|
|
|
|
|
/// Secret key, used to sign some resources. Must be randomly generated
|
|
|
|
#[clap(short = 'S', long, env, default_value = "")]
|
|
|
|
secret: String,
|
|
|
|
|
|
|
|
/// Specify whether the cookie should be transmitted only over secure connections
|
|
|
|
///
|
|
|
|
/// This should be always true when running in production mode
|
|
|
|
#[clap(long, env)]
|
|
|
|
pub cookie_secure: bool,
|
|
|
|
|
|
|
|
/// Unsecure : for development, bypass authentication
|
|
|
|
#[clap(long, env)]
|
|
|
|
pub unsecure_disable_login: bool,
|
|
|
|
|
|
|
|
/// Admin username
|
|
|
|
#[clap(long, env, default_value = "admin")]
|
|
|
|
pub admin_username: String,
|
|
|
|
|
|
|
|
/// Admin password
|
|
|
|
#[clap(long, env, default_value = "admin")]
|
|
|
|
pub admin_password: String,
|
|
|
|
|
2024-06-27 16:55:09 +00:00
|
|
|
/// The port the server will listen to (using HTTPS)
|
|
|
|
#[arg(short, long, env, default_value = "0.0.0.0:8443")]
|
2024-06-28 19:34:18 +00:00
|
|
|
pub listen_address: String,
|
2024-06-27 16:55:09 +00:00
|
|
|
|
2024-06-27 23:05:02 +00:00
|
|
|
/// The port the server will listen to (using HTTP, for unsecure connections)
|
|
|
|
#[arg(short, long, env, default_value = "0.0.0.0:8080")]
|
2024-06-28 19:34:18 +00:00
|
|
|
pub unsecure_listen_address: String,
|
2024-06-27 23:05:02 +00:00
|
|
|
|
|
|
|
/// Public server hostname (assuming that the ports used are the same for listen address)
|
|
|
|
#[arg(short('H'), long, env, default_value = "localhost")]
|
2024-06-28 19:34:18 +00:00
|
|
|
pub hostname: String,
|
2024-06-27 23:05:02 +00:00
|
|
|
|
2024-06-27 16:55:09 +00:00
|
|
|
/// Server storage path
|
|
|
|
#[arg(short, long, env, default_value = "storage")]
|
|
|
|
storage: String,
|
2024-06-29 08:11:31 +00:00
|
|
|
|
2024-09-16 20:27:43 +00:00
|
|
|
/// The minimal production that must be excluded when selecting relays to turn on
|
|
|
|
#[arg(short('m'), long, env, default_value_t = -500)]
|
|
|
|
pub production_margin: i32,
|
|
|
|
|
|
|
|
/// Energy refresh operations interval, in seconds
|
2024-09-23 18:30:34 +00:00
|
|
|
#[arg(short('i'), long, env, default_value_t = 25)]
|
2024-09-16 20:27:43 +00:00
|
|
|
pub refresh_interval: u64,
|
|
|
|
|
2024-09-23 18:30:34 +00:00
|
|
|
/// Energy refresh operations interval, in seconds
|
|
|
|
#[arg(short('f'), long, env, default_value_t = 5)]
|
|
|
|
pub energy_fetch_interval: u64,
|
|
|
|
|
2024-06-29 08:11:31 +00:00
|
|
|
/// Consumption backend provider
|
|
|
|
#[clap(subcommand)]
|
|
|
|
pub consumption_backend: Option<ConsumptionBackend>,
|
2024-06-27 16:55:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lazy_static::lazy_static! {
|
|
|
|
static ref ARGS: AppConfig = {
|
|
|
|
AppConfig::parse()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AppConfig {
|
2024-10-18 17:57:06 +00:00
|
|
|
/// Parse environment variables from file, if requedst
|
|
|
|
pub fn parse_env_file() -> anyhow::Result<()> {
|
|
|
|
if let Some(c) = Self::parse().config {
|
|
|
|
log::info!("Load additional environment variables from {c}");
|
|
|
|
let conf_file = Path::new(&c);
|
|
|
|
|
|
|
|
if !conf_file.is_file() {
|
|
|
|
panic!("Specified configuration is not a file!");
|
|
|
|
}
|
|
|
|
dotenvy::from_path(conf_file)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-06-27 16:55:09 +00:00
|
|
|
/// Get parsed command line arguments
|
|
|
|
pub fn get() -> &'static AppConfig {
|
|
|
|
&ARGS
|
|
|
|
}
|
|
|
|
|
2024-06-29 12:43:56 +00:00
|
|
|
/// Get app secret
|
|
|
|
pub fn secret(&self) -> &str {
|
|
|
|
let mut secret = self.secret.as_str();
|
|
|
|
|
|
|
|
if cfg!(debug_assertions) && secret.is_empty() {
|
|
|
|
secret = "DEBUGKEYDEBUGKEYDEBUGKEYDEBUGKEYDEBUGKEYDEBUGKEYDEBUGKEYDEBUGKEY";
|
|
|
|
}
|
|
|
|
|
|
|
|
if secret.is_empty() {
|
|
|
|
panic!("SECRET is undefined or too short (min 64 chars)!")
|
|
|
|
}
|
|
|
|
|
|
|
|
secret
|
|
|
|
}
|
|
|
|
|
2024-06-27 23:05:02 +00:00
|
|
|
/// URL for unsecure connections
|
|
|
|
pub fn unsecure_origin(&self) -> String {
|
|
|
|
format!(
|
|
|
|
"http://{}:{}",
|
|
|
|
self.hostname,
|
|
|
|
self.unsecure_listen_address.split_once(':').unwrap().1
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// URL for secure connections
|
|
|
|
pub fn secure_origin(&self) -> String {
|
|
|
|
format!(
|
|
|
|
"https://{}:{}",
|
|
|
|
self.hostname,
|
|
|
|
self.listen_address.split_once(':').unwrap().1
|
|
|
|
)
|
|
|
|
}
|
2024-06-27 16:55:09 +00:00
|
|
|
|
2024-06-29 12:43:56 +00:00
|
|
|
/// Get auth cookie domain
|
|
|
|
pub fn cookie_domain(&self) -> Option<String> {
|
|
|
|
if cfg!(debug_assertions) {
|
|
|
|
let domain = self.secure_origin().split_once("://")?.1.to_string();
|
|
|
|
Some(
|
|
|
|
domain
|
|
|
|
.split_once(':')
|
|
|
|
.map(|s| s.0)
|
|
|
|
.unwrap_or(&domain)
|
|
|
|
.to_string(),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
// In release mode, the web app is hosted on the same origin as the API
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-27 16:55:09 +00:00
|
|
|
/// Get storage path
|
|
|
|
pub fn storage_path(&self) -> PathBuf {
|
|
|
|
Path::new(&self.storage).to_path_buf()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get PKI storage path
|
|
|
|
pub fn pki_path(&self) -> PathBuf {
|
|
|
|
self.storage_path().join("pki")
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get PKI root CA cert path
|
|
|
|
pub fn root_ca_cert_path(&self) -> PathBuf {
|
2024-07-01 19:10:45 +00:00
|
|
|
self.pki_path().join("root_ca.crt")
|
2024-06-27 16:55:09 +00:00
|
|
|
}
|
|
|
|
|
2024-06-28 15:21:40 +00:00
|
|
|
/// Get PKI root CA CRL path
|
|
|
|
pub fn root_ca_crl_path(&self) -> PathBuf {
|
|
|
|
self.pki_path().join("root_ca.crl")
|
|
|
|
}
|
|
|
|
|
2024-06-27 16:55:09 +00:00
|
|
|
/// Get PKI root CA private key path
|
|
|
|
pub fn root_ca_priv_key_path(&self) -> PathBuf {
|
|
|
|
self.pki_path().join("root_ca.key")
|
|
|
|
}
|
2024-06-27 23:05:02 +00:00
|
|
|
|
|
|
|
/// Get PKI web CA cert path
|
|
|
|
pub fn web_ca_cert_path(&self) -> PathBuf {
|
2024-07-01 19:10:45 +00:00
|
|
|
self.pki_path().join("web_ca.crt")
|
2024-06-27 23:05:02 +00:00
|
|
|
}
|
|
|
|
|
2024-06-28 15:21:40 +00:00
|
|
|
/// Get PKI web CA CRL path
|
|
|
|
pub fn web_ca_crl_path(&self) -> PathBuf {
|
|
|
|
self.pki_path().join("web_ca.crl")
|
|
|
|
}
|
|
|
|
|
2024-06-27 23:05:02 +00:00
|
|
|
/// Get PKI web CA private key path
|
|
|
|
pub fn web_ca_priv_key_path(&self) -> PathBuf {
|
|
|
|
self.pki_path().join("web_ca.key")
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get PKI devices CA cert path
|
|
|
|
pub fn devices_ca_cert_path(&self) -> PathBuf {
|
2024-07-01 19:10:45 +00:00
|
|
|
self.pki_path().join("devices_ca.crt")
|
2024-06-27 23:05:02 +00:00
|
|
|
}
|
|
|
|
|
2024-06-28 15:21:40 +00:00
|
|
|
/// Get PKI devices CA CRL path
|
|
|
|
pub fn devices_ca_crl_path(&self) -> PathBuf {
|
|
|
|
self.pki_path().join("devices_ca.crl")
|
|
|
|
}
|
|
|
|
|
2024-06-27 23:05:02 +00:00
|
|
|
/// Get PKI devices CA private key path
|
|
|
|
pub fn devices_ca_priv_key_path(&self) -> PathBuf {
|
|
|
|
self.pki_path().join("devices_ca.key")
|
|
|
|
}
|
2024-06-28 19:34:18 +00:00
|
|
|
|
|
|
|
/// Get PKI server cert path
|
|
|
|
pub fn server_cert_path(&self) -> PathBuf {
|
2024-07-01 19:10:45 +00:00
|
|
|
self.pki_path().join("server.crt")
|
2024-06-28 19:34:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get PKI server private key path
|
|
|
|
pub fn server_priv_key_path(&self) -> PathBuf {
|
|
|
|
self.pki_path().join("server.key")
|
|
|
|
}
|
2024-07-01 19:10:45 +00:00
|
|
|
|
|
|
|
/// Get devices configuration storage path
|
|
|
|
pub fn devices_config_path(&self) -> PathBuf {
|
|
|
|
self.storage_path().join("devices")
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get device configuration path
|
|
|
|
pub fn device_config_path(&self, id: &DeviceId) -> PathBuf {
|
|
|
|
self.devices_config_path().join(format!("{}.conf", id.0))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get device certificate path
|
|
|
|
pub fn device_cert_path(&self, id: &DeviceId) -> PathBuf {
|
|
|
|
self.devices_config_path().join(format!("{}.crt", id.0))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get device CSR path
|
|
|
|
pub fn device_csr_path(&self, id: &DeviceId) -> PathBuf {
|
|
|
|
self.devices_config_path().join(format!("{}.csr", id.0))
|
|
|
|
}
|
2024-09-17 20:31:51 +00:00
|
|
|
|
|
|
|
/// Get relays runtime storage path
|
|
|
|
pub fn relays_runtime_stats_storage_path(&self) -> PathBuf {
|
|
|
|
self.storage_path().join("relays_runtime")
|
|
|
|
}
|
|
|
|
|
2024-09-17 20:42:24 +00:00
|
|
|
/// Get relay runtime stats path for a given relay
|
|
|
|
pub fn relay_runtime_stats_dir(&self, relay_id: DeviceRelayID) -> PathBuf {
|
2024-09-17 20:31:51 +00:00
|
|
|
self.relays_runtime_stats_storage_path()
|
|
|
|
.join(relay_id.0.to_string())
|
2024-09-17 20:42:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get relay runtime stats path for a given relay for a given day
|
|
|
|
pub fn relay_runtime_day_file_path(&self, relay_id: DeviceRelayID, day: u64) -> PathBuf {
|
|
|
|
self.relay_runtime_stats_dir(relay_id).join(day.to_string())
|
2024-09-17 20:31:51 +00:00
|
|
|
}
|
2024-09-23 19:47:25 +00:00
|
|
|
|
|
|
|
/// Get energy consumption history path
|
|
|
|
pub fn energy_consumption_history(&self) -> PathBuf {
|
|
|
|
self.storage_path().join("consumption_history")
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get energy consumption history file path for a given day
|
2024-09-25 17:05:54 +00:00
|
|
|
pub fn energy_consumption_history_day(
|
|
|
|
&self,
|
|
|
|
number: u64,
|
|
|
|
r#type: ConsumptionHistoryType,
|
|
|
|
) -> PathBuf {
|
2024-09-23 19:47:25 +00:00
|
|
|
self.storage_path()
|
|
|
|
.join("consumption_history")
|
2024-09-25 17:05:54 +00:00
|
|
|
.join(format!(
|
|
|
|
"{number}-{}",
|
|
|
|
match r#type {
|
|
|
|
ConsumptionHistoryType::GridConsumption => "grid",
|
|
|
|
ConsumptionHistoryType::RelayConsumption => "relay-consumption",
|
|
|
|
}
|
|
|
|
))
|
2024-09-23 19:47:25 +00:00
|
|
|
}
|
2024-09-30 20:11:48 +00:00
|
|
|
|
|
|
|
/// Get logs directory
|
|
|
|
pub fn logs_dir(&self) -> PathBuf {
|
|
|
|
self.storage_path().join("logs")
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the logs for a given day
|
|
|
|
pub fn log_of_day(&self, day: u64) -> PathBuf {
|
|
|
|
self.logs_dir().join(format!("{day}.log"))
|
|
|
|
}
|
2024-10-03 19:16:51 +00:00
|
|
|
|
|
|
|
/// Get the directory that will store OTA updates
|
|
|
|
pub fn ota_dir(&self) -> PathBuf {
|
2024-10-05 13:50:46 +00:00
|
|
|
self.storage_path().join("ota")
|
2024-10-03 19:16:51 +00:00
|
|
|
}
|
|
|
|
|
2024-10-05 17:35:23 +00:00
|
|
|
/// Get the directory that will store OTA updates for a given platform
|
|
|
|
pub fn ota_platform_dir(&self, platform: OTAPlatform) -> PathBuf {
|
|
|
|
self.ota_dir().join(platform.to_string())
|
|
|
|
}
|
|
|
|
|
2024-10-05 13:50:46 +00:00
|
|
|
/// Get the path to the file that will contain an OTA update
|
|
|
|
pub fn path_ota_update(&self, platform: OTAPlatform, version: &semver::Version) -> PathBuf {
|
2024-10-05 17:35:23 +00:00
|
|
|
self.ota_platform_dir(platform).join(version.to_string())
|
2024-10-03 19:16:51 +00:00
|
|
|
}
|
2024-06-27 16:55:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use crate::app_config::AppConfig;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn verify_cli() {
|
|
|
|
use clap::CommandFactory;
|
|
|
|
AppConfig::command().debug_assert()
|
|
|
|
}
|
2024-06-27 23:05:02 +00:00
|
|
|
}
|