rsiot/components/cmp_inject_periodic/
mod.rs

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
70
71
72
73
74
75
76
77
78
79
//! Компонент для периодического генерирования сообщений

use async_trait::async_trait;
use tokio::time::{sleep, Duration, Instant};

use crate::{
    executor::{CmpInOut, Component, ComponentError, IComponentProcess},
    message::{AuthPermissions, Message, MsgDataBound, ServiceBound},
};

/// Конфигурация cmp_inject_periodic
#[derive(Clone, Debug)]
pub struct Config<TMsg, TFnPeriodic>
where
    TMsg: Clone,
    TFnPeriodic: FnMut() -> Vec<Message<TMsg>> + Send + Sync,
{
    /// Период вызова
    pub period: Duration,

    /// Функция для генерирования сообщений
    ///
    /// Тип данных - `FnMut() -> Vec<Message<TMsg>>`
    pub fn_periodic: TFnPeriodic,
}

#[cfg_attr(not(feature = "single-thread"), async_trait)]
#[cfg_attr(feature = "single-thread", async_trait(?Send))]
impl<TMsg, TFnPeriodic, TService> IComponentProcess<Config<TMsg, TFnPeriodic>, TMsg, TService>
    for Component<Config<TMsg, TFnPeriodic>, TMsg, TService>
where
    TMsg: MsgDataBound,
    TFnPeriodic: FnMut() -> Vec<Message<TMsg>> + Send + Sync,
    TService: ServiceBound,
{
    async fn process(
        &self,
        config: Config<TMsg, TFnPeriodic>,
        in_out: CmpInOut<TMsg, TService>,
    ) -> Result<(), ComponentError> {
        fn_process(
            config,
            in_out.clone_with_new_id("cmp_inject_periodic", AuthPermissions::FullAccess),
        )
        .await
    }
}

async fn fn_process<TMsg, TFnPeriodic, TService>(
    mut config: Config<TMsg, TFnPeriodic>,
    in_out: CmpInOut<TMsg, TService>,
) -> Result<(), ComponentError>
where
    TMsg: MsgDataBound,
    TFnPeriodic: FnMut() -> Vec<Message<TMsg>> + Send + Sync,
    TService: ServiceBound,
{
    loop {
        let begin = Instant::now();
        let msgs = (config.fn_periodic)();
        for msg in msgs {
            in_out
                .send_output(msg)
                .await
                .map_err(|err| ComponentError::Execution(err.to_string()))?;
        }
        let elapsed = begin.elapsed();
        let sleep_time = if config.period <= elapsed {
            Duration::from_millis(10)
        } else {
            config.period - elapsed
        };
        sleep(sleep_time).await;
    }
}

/// Компонент cmp_inject_periodic
pub type Cmp<TMessage, TFnPeriodic, TService> =
    Component<Config<TMessage, TFnPeriodic>, TMessage, TService>;