summaryrefslogtreecommitdiffstats
path: root/crates/core/tedge_mapper/src/collectd/mapper.rs
blob: a4c6f4c56598fb0198ae0422205af3027360424e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use crate::{
    collectd::monitor::{DeviceMonitor, DeviceMonitorConfig},
    core::component::TEdgeComponent,
};
use async_trait::async_trait;
use mqtt_channel::TopicFilter;
use tedge_config::{ConfigSettingAccessor, MqttBindAddressSetting, MqttPortSetting, TEdgeConfig};
use tracing::{info, info_span, Instrument};

const COLLECTD_MAPPER_NAME: &str = "tedge-mapper-collectd";

pub struct CollectdMapper {}

impl CollectdMapper {
    pub fn new() -> Self {
        Self {}
    }
}

#[async_trait]
impl TEdgeComponent for CollectdMapper {
    fn session_name(&self) -> &str {
        COLLECTD_MAPPER_NAME
    }

    async fn init(&self) -> Result<(), anyhow::Error> {
        info!("Initialize tedge mapper collectd");
        self.init_session(TopicFilter::new(
            DeviceMonitorConfig::default().mqtt_source_topic,
        )?)
        .await?;
        Ok(())
    }

    async fn start(&self, tedge_config: TEdgeConfig) -> Result<(), anyhow::Error> {
        let mqtt_port = tedge_config.query(MqttPortSetting)?.into();
        let mqtt_host = tedge_config.query(MqttBindAddressSetting)?.to_string();

        let device_monitor_config = DeviceMonitorConfig::default()
            .with_port(mqtt_port)
            .with_host(mqtt_host);

        let device_monitor = DeviceMonitor::new(device_monitor_config);
        device_monitor
            .run()
            .instrument(info_span!(COLLECTD_MAPPER_NAME))
            .await?;

        Ok(())
    }
}