Skip to content

Commit

Permalink
Restore config (#207)
Browse files Browse the repository at this point in the history
  • Loading branch information
XdoctorwhoZ authored Dec 24, 2024
1 parent 5ebb346 commit 9a1374e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
11 changes: 9 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use panduza_platform_core::env::system_default_config_dir;
use panduza_platform_core::log_warn;
use panduza_platform_core::Logger;
use serde::Deserialize;
use serde::Serialize;

Expand Down Expand Up @@ -42,15 +44,20 @@ impl Default for Config {

/// Get the platform configuration from the default config file
///
pub fn get_platform_config() -> Config {
pub fn get_platform_config(logger: Logger) -> Config {

let file_path = system_default_config_dir().unwrap().join("platform.toml");
let config_content = if file_path.exists() {
std::fs::read_to_string(&file_path).expect("Failed to read config file")
} else {
let default_config = Config::default();
let toml_content =
toml::to_string(&default_config).expect("Failed to serialize default config");
std::fs::write(&file_path, &toml_content).expect("Failed to write default config file");

if let Err(e) = std::fs::write(&file_path, &toml_content) {
log_warn!(logger, "Failed to write default config file: {}", e);
}

toml_content
};
toml::from_str(&config_content).expect("Failed to parse config file")
Expand Down
33 changes: 26 additions & 7 deletions src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ use crate::underscore_device::store::data::SharedStore;
use crate::underscore_device::UnderscoreDevice;
use futures::FutureExt;
use panduza_platform_core::{
create_task_channel, env, log_debug, log_warn, Factory, InstanceMonitor, Notification,
create_task_channel, env, log_debug, log_warn, Factory, InstanceMonitor, Logger, Notification,
NotificationGroup, ProductionOrder, Runtime, Store, TaskReceiver, TaskResult, TaskSender,
};
use panduza_platform_core::{PlatformLogger, Reactor, ReactorSettings};
use panduza_platform_core::{Reactor, ReactorSettings};
use rumqttd::Broker;
use rumqttd::Config;
use std::fs::File;
Expand Down Expand Up @@ -51,7 +51,11 @@ pub enum ServiceRequest {
///
pub struct Platform {
/// Main logger
logger: PlatformLogger,
logger: Logger,

///
///
config: crate::config::Config,

///
///
Expand Down Expand Up @@ -134,7 +138,8 @@ impl Platform {
//
// Create object
return Self {
logger: PlatformLogger::new(),
logger: Logger::new_for_platform(),

config: crate::config::Config::default(),

keep_alive: Arc::new(AtomicBool::new(true)),
Expand Down Expand Up @@ -390,7 +395,8 @@ impl Platform {
// info
self.logger.info("----- SERVICE : READ CONFIG -----");

self.config = crate::config::get_platform_config();
self.config = crate::config::get_platform_config(self.logger.clone());

}

/// -------------------------------------------------------------
Expand All @@ -407,6 +413,16 @@ impl Platform {
.and_then(|b| b.addr.clone())
.unwrap_or("127.0.0.1".to_string());

let port = self
.config
.broker
.as_ref()
.and_then(|b| b.port.clone())
.unwrap_or(1883);

let listen_addr = format!("{}:{}", addr, port);


let mut router: std::collections::HashMap<String, config::Value> = config::Map::new();
router.insert("id".to_string(), config::Value::new(None, 0));
router.insert(
Expand Down Expand Up @@ -449,7 +465,7 @@ impl Platform {
server.insert("name".to_string(), config::Value::new(None, "v4-1"));
server.insert(
"listen".to_string(),
config::Value::new(None, "0.0.0.0:1883"),
config::Value::new(None, listen_addr.clone()),
);
server.insert(
"next_connection_delay_ms".to_string(),
Expand All @@ -471,11 +487,14 @@ impl Platform {
.build()
.unwrap();

//
// this is where we deserialize it into Config
let rumqttd_config: Config = config.try_deserialize().unwrap();
let mut broker = Broker::new(rumqttd_config);

self.logger.info("Start Broker");
//
// start broker
log_info!(self.logger, "Broker listen on: {}", listen_addr);
let _jh = std::thread::spawn(move || {
broker.start().unwrap();
println!("BROKER STOPPPED !!!!!!!!!!!!!!!!!");
Expand Down
6 changes: 3 additions & 3 deletions src/plugins_manager.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use panduza_platform_core::env;
use panduza_platform_core::Error;
use panduza_platform_core::Logger;
use panduza_platform_core::Notification;
use panduza_platform_core::PlatformLogger;
use panduza_platform_core::Plugin;
use panduza_platform_core::ProductionOrder;
use panduza_platform_core::Store;
Expand Down Expand Up @@ -172,7 +172,7 @@ impl PluginHandler {
///
pub struct PluginsManager {
/// logger
logger: PlatformLogger,
logger: Logger,

///
/// Plugin handlers
Expand All @@ -190,7 +190,7 @@ impl PluginsManager {
///
pub fn new(enable_stdout: bool, debug: bool, trace: bool) -> Self {
Self {
logger: PlatformLogger::new(),
logger: Logger::new_for_platform(),

handlers: Vec::new(),

Expand Down

0 comments on commit 9a1374e

Please sign in to comment.