diff options
-rw-r--r-- | Cargo.lock | 1 | ||||
-rw-r--r-- | plugins/c8y_configuration_plugin/Cargo.toml | 5 | ||||
-rw-r--r-- | plugins/c8y_configuration_plugin/src/config.rs | 20 | ||||
-rw-r--r-- | plugins/c8y_configuration_plugin/src/main.rs | 12 |
4 files changed, 30 insertions, 8 deletions
@@ -396,6 +396,7 @@ dependencies = [ "mqtt_channel", "serde", "tedge_config", + "tedge_utils", "tempfile", "test-case", "thiserror", diff --git a/plugins/c8y_configuration_plugin/Cargo.toml b/plugins/c8y_configuration_plugin/Cargo.toml index 81dd3b97..17a463e7 100644 --- a/plugins/c8y_configuration_plugin/Cargo.toml +++ b/plugins/c8y_configuration_plugin/Cargo.toml @@ -10,13 +10,14 @@ description = "Thin.edge.io operation plugin for Cumulocity configuration manage [dependencies] anyhow = "1.0" c8y_smartrest = { path = "../../crates/core/c8y_smartrest" } +mqtt_channel = { path = "../../crates/common/mqtt_channel" } serde = { version = "1.0", features = ["derive"] } +tedge_config = { path = "../../crates/common/tedge_config" } +tedge_utils = { path = "../../crates/common/tedge_utils", features = ["logging"] } thiserror = "1.0" tokio = { version = "1.9", default_features = false, features = [ "fs", "io-util", "macros", "rt-multi-thread","signal"] } toml = "0.5" tracing = { version = "0.1", features = ["attributes", "log"] } -tedge_config = { path = "../../crates/common/tedge_config" } -mqtt_channel = { path = "../../crates/common/mqtt_channel" } [dev-dependencies] tempfile = "3.3" diff --git a/plugins/c8y_configuration_plugin/src/config.rs b/plugins/c8y_configuration_plugin/src/config.rs index ecd382ab..8e444ee6 100644 --- a/plugins/c8y_configuration_plugin/src/config.rs +++ b/plugins/c8y_configuration_plugin/src/config.rs @@ -1,6 +1,7 @@ use serde::Deserialize; use std::fs; use std::path::PathBuf; +use tracing::{info, warn}; pub const PLUGIN_CONFIG_FILE: &str = "c8y_configuration_plugin.toml"; @@ -24,12 +25,23 @@ impl PluginConfig { } fn read_config(path: PathBuf) -> Self { - match fs::read_to_string(path) { + let path_str = path.to_str().unwrap_or(PLUGIN_CONFIG_FILE); + info!("Reading the config file from {}", path_str); + match fs::read_to_string(path.clone()) { Ok(contents) => match toml::from_str(contents.as_str()) { Ok(config) => config, - _ => Self::default(), // The config file is ill-formed + _ => { + warn!("The config file {} is malformed.", path_str); + Self::default() + } }, - Err(_) => Self::default(), // The config file does not exist or is not readable + Err(_) => { + warn!( + "The config file {} does not exist or is not readable.", + path_str + ); + Self::default() + } } } @@ -108,7 +120,7 @@ mod tests { '/etc/tedge/mosquitto-conf/tedge-mosquitto.conf', '/etc/mosquitto/mosquitto.conf' ] - test = false + unsupported_key = false "#, PluginConfig { files: vec![] diff --git a/plugins/c8y_configuration_plugin/src/main.rs b/plugins/c8y_configuration_plugin/src/main.rs index c82100fd..ea98df54 100644 --- a/plugins/c8y_configuration_plugin/src/main.rs +++ b/plugins/c8y_configuration_plugin/src/main.rs @@ -10,6 +10,12 @@ use tracing::{debug, error, info, instrument, warn}; const CONFIG_ROOT_PATH: &str = "/etc/tedge/c8y"; +#[cfg(not(debug_assertions))] +const LOG_LEVEL_DEBUG: bool = false; + +#[cfg(debug_assertions)] +const LOG_LEVEL_DEBUG: bool = true; + async fn create_mqtt_client() -> Result<mqtt_channel::Connection, anyhow::Error> { let tedge_config = get_tedge_config()?; let mqtt_port = tedge_config.query(MqttPortSetting)?.into(); @@ -25,6 +31,8 @@ async fn create_mqtt_client() -> Result<mqtt_channel::Connection, anyhow::Error> #[tokio::main] async fn main() -> Result<(), anyhow::Error> { + tedge_utils::logging::initialise_tracing_subscriber(LOG_LEVEL_DEBUG); + // Create required clients let mut mqtt_client = create_mqtt_client().await?; @@ -34,10 +42,10 @@ async fn main() -> Result<(), anyhow::Error> { let msg = plugin_config.to_message()?; let () = mqtt_client.published.send(msg).await?; - // mqtt loop + // Mqtt message loop while let Some(message) = mqtt_client.received.next().await { debug!("Received {:?}", message); - match message.payload_str()?.split_at(3).0 { + match message.payload_str()?.split(',').nth(0).unwrap_or_default() { "524" => { debug!("{}", message.payload_str()?); todo!() // c8y_DownloadConfigFile |