Alex Mikhalev
4 years ago
9 changed files with 123 additions and 18 deletions
@ -0,0 +1,12 @@
@@ -0,0 +1,12 @@
|
||||
{ |
||||
"mqtt": { |
||||
"broker_host": "localhost", |
||||
"broker_port": 1883, |
||||
"client_id": "sprinklers_rs-0001", |
||||
"device_id": "sprinklers_rs-0001" |
||||
}, |
||||
"section_interface": { |
||||
"provider": "Mock", |
||||
"num_sections": 6 |
||||
} |
||||
} |
@ -0,0 +1,37 @@
@@ -0,0 +1,37 @@
|
||||
use sprinklers_core::section_interface::{MockSectionInterface, SecId, SectionInterface}; |
||||
|
||||
#[cfg(feature = "sprinklers_linux")] |
||||
use sprinklers_linux::LinuxGpioConfig; |
||||
|
||||
use serde::{Deserialize, Serialize}; |
||||
use std::sync::Arc; |
||||
|
||||
#[derive(Debug, Serialize, Deserialize)] |
||||
#[serde(tag = "provider")] |
||||
pub enum SectionInterfaceConfig { |
||||
Mock { |
||||
num_sections: SecId, |
||||
}, |
||||
#[cfg(feature = "sprinklers_linux")] |
||||
LinuxGpio(LinuxGpioConfig), |
||||
} |
||||
|
||||
impl Default for SectionInterfaceConfig { |
||||
fn default() -> Self { |
||||
SectionInterfaceConfig::Mock { num_sections: 6 } |
||||
} |
||||
} |
||||
|
||||
impl SectionInterfaceConfig { |
||||
pub fn build(self) -> eyre::Result<Arc<dyn SectionInterface>> { |
||||
Ok(match self { |
||||
SectionInterfaceConfig::Mock { num_sections } => { |
||||
Arc::new(MockSectionInterface::new(num_sections)) |
||||
} |
||||
#[cfg(feature = "sprinklers_linux")] |
||||
SectionInterfaceConfig::LinuxGpio(config) => { |
||||
Arc::new(config.build()?) |
||||
} |
||||
}) |
||||
} |
||||
} |
@ -0,0 +1,44 @@
@@ -0,0 +1,44 @@
|
||||
use crate::section_interface::SectionInterfaceConfig; |
||||
|
||||
use serde::{Deserialize, Serialize}; |
||||
use tracing::trace; |
||||
|
||||
#[derive(Debug, Serialize, Deserialize)] |
||||
#[serde(remote = "sprinklers_mqtt::Options")] |
||||
struct MqttOptions { |
||||
pub broker_host: String, |
||||
pub broker_port: u16, |
||||
pub device_id: String, |
||||
pub client_id: String, |
||||
} |
||||
|
||||
#[derive(Debug, Serialize, Deserialize)] |
||||
pub struct Settings { |
||||
#[serde(with = "MqttOptions")] |
||||
pub mqtt: sprinklers_mqtt::Options, |
||||
#[serde(default)] |
||||
pub section_interface: SectionInterfaceConfig, |
||||
} |
||||
|
||||
impl Settings { |
||||
pub fn new() -> eyre::Result<Self> { |
||||
let mut s = config::Config::new(); |
||||
|
||||
let default_config = config::File::from_str( |
||||
include_str!("../sprinklers_rs.default.json"), |
||||
config::FileFormat::Json, |
||||
); |
||||
s.merge(default_config)?; |
||||
|
||||
// TODO: specify configuration path from arguments or env
|
||||
s.merge(config::File::with_name("sprinklers_rs").required(false))?; |
||||
|
||||
s.merge(config::Environment::with_prefix("SPRINKLERS").separator("__"))?; |
||||
|
||||
let settings: Settings = s.try_into()?; |
||||
|
||||
trace!("settings: {:#?}", settings); |
||||
|
||||
Ok(settings) |
||||
} |
||||
} |
Loading…
Reference in new issue