rsiot/components/cmp_mqtt_client/
fn_process.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
80
use std::time::Duration;

use rumqttc::mqttbytes::QoS;
use rumqttc::{AsyncClient, MqttOptions};
use tokio::{task::JoinSet, time::sleep};
use tracing::{error, info};

use crate::message::ServiceBound;
use crate::{executor::CmpInOut, message::MsgDataBound};

use super::{tasks, Config};

pub async fn fn_process<TMsg, TService>(
    config: Config<TMsg>,
    in_out: CmpInOut<TMsg, TService>,
) -> super::Result<()>
where
    TMsg: MsgDataBound + 'static,
    TService: ServiceBound + 'static,
{
    loop {
        info!("Starting");
        let res = main(config.clone(), in_out.clone()).await;
        match res {
            Ok(_) => (),
            Err(err) => {
                error!("Error in cmp_mqtt_client: {}", err);
            }
        }
        info!("Restarting...");
        sleep(Duration::from_secs(2)).await;
    }
}

async fn main<TMsg, TService>(
    config: Config<TMsg>,
    in_out: CmpInOut<TMsg, TService>,
) -> super::Result<()>
where
    TMsg: MsgDataBound + 'static,
    TService: ServiceBound + 'static,
{
    let mut mqttoptions = MqttOptions::new(config.client_id, config.host, config.port);
    mqttoptions.set_keep_alive(Duration::from_secs(50000)); // TODO - прерывает обмен

    let (client, eventloop) = AsyncClient::new(mqttoptions, 10);
    client.subscribe("rsiot/#", QoS::ExactlyOnce).await?;

    let mut task_set: JoinSet<super::Result<()>> = JoinSet::new();

    // Отправление сообщений из кеша на MQTT-брокер
    let task = tasks::SendCache {
        in_out: in_out.clone(),
        config_fn_input: config.fn_input,
        client: client.clone(),
    };
    task_set.spawn(task.spawn());

    // Отправление входящих сообщений на MQTT-брокер
    let task = tasks::Input {
        in_out: in_out.clone(),
        config_fn_input: config.fn_input,
        client,
    };
    task_set.spawn(task.spawn());

    // Получение сообщения от MQTT-брокера
    let task = tasks::Output {
        in_out,
        config_fn_output: config.fn_output,
        eventloop,
    };
    task_set.spawn(task.spawn());

    while let Some(res) = task_set.join_next().await {
        res??
    }

    Ok(())
}