pyrinas_server/
settings.rs

1use crate::Error;
2use serde::Deserialize;
3use std::fs;
4use std::path::Path;
5use toml;
6
7#[derive(Debug, Deserialize, Clone)]
8pub struct Mqtt {
9    pub name: String,
10    pub topics: Vec<String>,
11    pub rumqtt: librumqttd::Config,
12}
13
14#[derive(Debug, Deserialize, Clone)]
15pub struct Influx {
16    pub database: String,
17    pub host: String,
18    pub password: String,
19    pub port: u16,
20    pub user: String,
21}
22
23/// Struct for Admin interface
24#[derive(Debug, Deserialize, Clone)]
25pub struct Admin {
26    /// Port for the Websocket admin interface
27    pub port: u16,
28    /// Api key for Admin interface
29    pub api_key: String,
30}
31
32#[derive(Debug, Deserialize, Clone)]
33pub struct Ota {
34    pub url: String,
35    pub db_path: String,
36    pub http_port: u16,
37    pub image_path: String,
38}
39
40#[derive(Debug, Deserialize, Clone)]
41pub struct PyrinasSettings {
42    pub influx: Option<Influx>,
43    pub mqtt: Mqtt,
44    pub admin: Option<Admin>,
45    pub ota: Ota,
46}
47
48impl PyrinasSettings {
49    pub fn new(config: String) -> Result<Self, Error> {
50        // Get the path
51        let path = Path::new(&config);
52
53        // Get it as a string first
54        let config = fs::read_to_string(path)?;
55
56        // Get the actual config
57        match toml::from_str(&config) {
58            Ok(settings) => Ok(settings),
59            Err(e) => Err(Error::CustomError(format!(
60                "Unable to deserialize TOML: {}",
61                e
62            ))),
63        }
64    }
65}