summaryrefslogtreecommitdiffstats
path: root/plugins/plugin_notification/src/plugin.rs
blob: 09c8a93020f4dc8cdd5592405141144802ed7d93 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use async_trait::async_trait;

use tedge_api::plugin::Handle;
use tedge_api::Address;
use tedge_api::Plugin;
use tedge_api::PluginError;
use tedge_lib::measurement::Measurement;
use tracing::trace;

use crate::builder::MeasurementReceiver;
use crate::builder::NotificationReceiver;
use crate::config::NotificationType;

pub struct NotificationPlugin {
    target_addr: Address<MeasurementReceiver>,
    notify_addr: Address<NotificationReceiver>,

    raise: NotificationType,
    raise_msg: String,
}

impl tedge_api::plugin::PluginDeclaration for NotificationPlugin {
    type HandledMessages = (Measurement,);
}

impl NotificationPlugin {
    pub fn new(
        target_addr: Address<MeasurementReceiver>,
        notify_addr: Address<NotificationReceiver>,
        raise: NotificationType,
        raise_msg: String,
    ) -> Self {
        Self {
            target_addr,
            notify_addr,
            raise,
            raise_msg,
        }
    }
}

#[async_trait]
impl Plugin for NotificationPlugin {
    async fn start(&mut self) -> Result<(), PluginError> {
        Ok(())
    }

    async fn shutdown(&mut self) -> Result<(), PluginError> {
        trace!("Shutdown");
        Ok(())
    }
}

#[async_trait]
impl Handle<Measurement> for NotificationPlugin {
    async fn handle_message(
        &self,
        message: Measurement,
        _sender: tedge_api::address::ReplySenderFor<Measurement>,
    ) -> Result<(), PluginError> {
        trace!("Received measurement = {:?}", message);
        trace!("Sending notification for measurement = {:?}", message);
        let _ = self.notify_addr.send_and_wait(self.raise.clone().into_notification(self.raise_msg.to_string())).await;

        trace!("Forwarding measurement = {:?}", message);
        let _ = self.target_addr.send_and_wait(message).await;
        Ok(())
    }
}