diff options
author | Matthias Beyer <matthias.beyer@ifm.com> | 2022-05-17 14:29:12 +0200 |
---|---|---|
committer | Matthias Beyer <matthias.beyer@ifm.com> | 2022-05-17 14:29:12 +0200 |
commit | cd914a7efb480a9e867812680dfc3318d56dccbf (patch) | |
tree | 9134eb9e390143b770ca910e3160de8f12213dad | |
parent | a8b5751c4872f7e8cbe4b1ad1f77ea807f0e09b1 (diff) | |
parent | 02aebf91f9ccb195e13a11905e5233407fa4f99b (diff) |
Merge branch 'feature/add_tedge_api/showcase-plugin-c8y' into feature/add_tedge_api/showcase
-rw-r--r-- | Cargo.lock | 16 | ||||
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | plugins/plugin_c8y/Cargo.toml | 19 | ||||
-rw-r--r-- | plugins/plugin_c8y/README.md | 5 | ||||
-rw-r--r-- | plugins/plugin_c8y/src/builder.rs | 61 | ||||
-rw-r--r-- | plugins/plugin_c8y/src/config.rs | 5 | ||||
-rw-r--r-- | plugins/plugin_c8y/src/error.rs | 6 | ||||
-rw-r--r-- | plugins/plugin_c8y/src/lib.rs | 9 | ||||
-rw-r--r-- | plugins/plugin_c8y/src/plugin.rs | 66 | ||||
-rw-r--r-- | tedge/Cargo.toml | 2 | ||||
-rw-r--r-- | tedge/src/main.rs | 6 |
11 files changed, 196 insertions, 0 deletions
@@ -2216,6 +2216,21 @@ dependencies = [ ] [[package]] +name = "plugin_c8y" +version = "0.1.0" +dependencies = [ + "async-trait", + "miette", + "plugin_mqtt", + "serde", + "tedge_api", + "tedge_lib", + "thiserror", + "toml", + "tracing", +] + +[[package]] name = "plugin_collectd" version = "0.1.0" dependencies = [ @@ -3401,6 +3416,7 @@ dependencies = [ "miette", "nu-ansi-term", "plugin_avg", + "plugin_c8y", "plugin_collectd", "plugin_httpstop", "plugin_inotify", @@ -6,6 +6,7 @@ members = [ "crates/core/tedge_api/tedge_config_derive", "crates/tests/*", "plugins/plugin_avg", + "plugins/plugin_c8y", "plugins/plugin_collectd", "plugins/plugin_httpstop", "plugins/plugin_inotify", diff --git a/plugins/plugin_c8y/Cargo.toml b/plugins/plugin_c8y/Cargo.toml new file mode 100644 index 00000000..ae287b7e --- /dev/null +++ b/plugins/plugin_c8y/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "plugin_c8y" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +async-trait = "0.1" +miette = "4.4" +thiserror = "1" +serde = { version = "1", features = ["derive"] } +tracing = "0.1" +toml = "0.5" + +tedge_api = { path = "../../crates/core/tedge_api" } +tedge_lib = { path = "../../crates/core/tedge_lib" } +plugin_mqtt = { path = "../../plugins/plugin_mqtt" } + diff --git a/plugins/plugin_c8y/README.md b/plugins/plugin_c8y/README.md new file mode 100644 index 00000000..ef7caea4 --- /dev/null +++ b/plugins/plugin_c8y/README.md @@ -0,0 +1,5 @@ +# plugin_c8y + +This plugin implements the interfacing with the c8y cloud. + + diff --git a/plugins/plugin_c8y/src/builder.rs b/plugins/plugin_c8y/src/builder.rs new file mode 100644 index 00000000..68d683f1 --- /dev/null +++ b/plugins/plugin_c8y/src/builder.rs @@ -0,0 +1,61 @@ +use async_trait::async_trait; +use tedge_api::CancellationToken; +use tedge_api::PluginBuilder; +use tedge_api::PluginConfiguration; +use tedge_api::PluginDirectory; +use tedge_api::PluginError; +use tedge_api::plugin::BuiltPlugin; +use tedge_api::plugin::HandleTypes; +use tedge_api::plugin::PluginExt; + +use crate::config::C8yConfig; +use crate::plugin::C8yPlugin; + +pub struct C8yPluginBuilder; + +#[async_trait] +impl<PD> PluginBuilder<PD> for C8yPluginBuilder +where + PD: PluginDirectory, +{ + fn kind_name() -> &'static str { + "c8y" + } + + fn kind_configuration() -> Option<tedge_api::ConfigDescription> { + Some(<C8yConfig as tedge_api::AsConfig>::as_config()) + } + + fn kind_message_types() -> HandleTypes + where + Self: Sized, + { + C8yPlugin::get_handled_types() + } + + async fn verify_configuration( + &self, + config: &PluginConfiguration, + ) -> Result<(), tedge_api::error::PluginError> { + config + .clone() + .try_into() + .map(|_: C8yConfig| ()) + .map_err(crate::error::Error::ConfigParseFailed) + .map_err(PluginError::from) + } + + async fn instantiate( + &self, + config: PluginConfiguration, + _cancellation_token: CancellationToken, + _plugin_dir: &PD, + ) -> Result<BuiltPlugin, PluginError> { + let config = config + .try_into::<C8yConfig>() + .map_err(crate::error::Error::ConfigParseFailed)?; + + Ok(C8yPlugin::new(config).finish()) + } +} + diff --git a/plugins/plugin_c8y/src/config.rs b/plugins/plugin_c8y/src/config.rs new file mode 100644 index 00000000..7c9d2330 --- /dev/null +++ b/plugins/plugin_c8y/src/config.rs @@ -0,0 +1,5 @@ +#[derive(serde::Deserialize, Debug, tedge_api::Config)] +pub struct C8yConfig { + // currently empty +} + diff --git a/plugins/plugin_c8y/src/error.rs b/plugins/plugin_c8y/src/error.rs new file mode 100644 index 00000000..682ded6e --- /dev/null +++ b/plugins/plugin_c8y/src/error.rs @@ -0,0 +1,6 @@ +#[derive(Debug, miette::Diagnostic, thiserror::Error)] +pub(crate) enum Error { + #[error("Failed to parse configuration")] + ConfigParseFailed(toml::de::Error), +} + diff --git a/plugins/plugin_c8y/src/lib.rs b/plugins/plugin_c8y/src/lib.rs new file mode 100644 index 00000000..8d9f600a --- /dev/null +++ b/plugins/plugin_c8y/src/lib.rs @@ -0,0 +1,9 @@ +#![doc = include_str!("../README.md")] + +mod builder; +mod config; +mod error; +mod plugin; + +pub use crate::builder::C8yPluginBuilder; +pub use crate::plugin::C8yPlugin; diff --git a/plugins/plugin_c8y/src/plugin.rs b/plugins/plugin_c8y/src/plugin.rs new file mode 100644 index 00000000..31d104ca --- /dev/null +++ b/plugins/plugin_c8y/src/plugin.rs @@ -0,0 +1,66 @@ +use async_trait::async_trait; + +use tedge_api::address::ReplySenderFor; +use tedge_api::plugin::Handle; +use tedge_api::Plugin; +use tedge_api::PluginError; +use tedge_lib::measurement::Measurement; +use plugin_mqtt::IncomingMessage; + +use crate::config::C8yConfig; + +#[derive(Debug)] +pub struct C8yPlugin { + config: C8yConfig, +} + +impl C8yPlugin { + pub(crate) fn new(config: C8yConfig) -> Self { + Self { + config, + } + } +} + +impl tedge_api::plugin::PluginDeclaration for C8yPlugin { + type HandledMessages = (IncomingMessage, Measurement); +} + +#[async_trait] +impl Plugin for C8yPlugin { + #[tracing::instrument(name = "plugin.c8y.start", skip(self))] + async fn start(&mut self) -> Result<(), PluginError> { + Ok(()) + } + + #[tracing::instrument(name = "plugin.c8y.shutdown", skip(self))] + async fn shutdown(&mut self) -> Result<(), PluginError> { + Ok(()) + } +} + +#[async_trait] +impl Handle<Measurement> for C8yPlugin { + #[tracing::instrument(name = "plugin.c8y.handle_message", level = "trace")] + async fn handle_message( + &self, + _message: Measurement, + _sender: ReplySenderFor<Measurement>, + ) -> Result<(), PluginError> { + // TODO + Ok(()) + } +} + +#[async_trait] +impl Handle<IncomingMessage> for C8yPlugin { + #[tracing::instrument(name = "plugin.c8y.handle_message", level = "trace")] + async fn handle_message( + &self, + _message: IncomingMessage, + _sender: ReplySenderFor<IncomingMessage>, + ) -> Result<(), PluginError> { + // TODO + Ok(()) + } +} diff --git a/tedge/Cargo.toml b/tedge/Cargo.toml index 91b73c04..f0a4de27 100644 --- a/tedge/Cargo.toml +++ b/tedge/Cargo.toml @@ -41,6 +41,7 @@ plugin_thin_edge_json_to_measurement_mapper = { path = "../plugins/plugin_thin_e plugin_notification = { path = "../plugins/plugin_notification", optional = true } plugin_collectd = { path = "../plugins/plugin_collectd", optional = true } plugin_sm_apt = { path = "../plugins/plugin_sm_apt", optional = true } +plugin_c8y = { path = "../plugins/plugin_c8y", optional = true } [features] @@ -66,4 +67,5 @@ thin_edge_json = ["plugin_thin_edge_json", "plugin_thin_edge_json_to_measurement mqtt = ["plugin_mqtt", "plugin_mqtt_measurement_bridge"] collectd = ["plugin_collectd"] sm = ["plugin_sm_apt"] +c8y = ["plugin_c8y"] diff --git a/tedge/src/main.rs b/tedge/src/main.rs index b0ec952d..913c7049 100644 --- a/tedge/src/main.rs +++ b/tedge/src/main.rs @@ -186,6 +186,12 @@ async fn main() -> miette::Result<()> { plugin_sm_apt::SmAptPluginBuilder, plugin_sm_apt::SmAptPluginBuilder ); + let registry = register_plugin!( + registry, + "c8y", + plugin_c8y::C8yPluginBuilder, + plugin_c8y::C8yPluginBuilder + ); match args.command { cli::CliCommand::Run { config } => { |