rsiot/components/cmp_plc/
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use std::sync::Arc;

use futures::TryFutureExt;
use serde::Serialize;
use tokio::{sync::mpsc, sync::Mutex, task::JoinSet};
use tracing::info;

use crate::{
    components::shared_tasks,
    executor::{join_set_spawn, Cache, CmpInOut},
    message::{MsgDataBound, ServiceBound},
};

use super::{
    config::Config,
    plc::{FunctionBlockBase, IFunctionBlock},
    tasks, Error,
};

pub async fn fn_process<TMsg, TService, I, Q, S>(
    in_out: CmpInOut<TMsg, TService>,
    config: Config<TMsg, I, Q, S>,
) -> super::Result<()>
where
    TMsg: MsgDataBound + 'static,
    TService: ServiceBound + 'static,
    I: Clone + Default + Send + Serialize + 'static + Sync,
    Q: Clone + Default + Send + Serialize + 'static + Sync,
    S: Clone + Default + Send + Serialize + 'static + Sync,
    FunctionBlockBase<I, Q, S>: IFunctionBlock<I, Q, S>,
{
    info!("PLC mode: STOPPED");

    let input_msg_cache = Cache::<TMsg>::new();

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

    let buffer_size = in_out.max_capacity();
    let (channel_plc_to_filter_send, channel_plc_to_filter_recv) = mpsc::channel(buffer_size);
    let (channel_filter_to_output_send, channel_filter_to_output_recv) = mpsc::channel(buffer_size);

    // Ожидаем данные для восстановления памяти
    let fb_main = tasks::Retention {
        cmp_in_out: in_out.clone(),
        config_retention: config.retention.clone(),
        fb_main: config.fb_main.clone(),
    }
    .spawn()
    .await?;
    let fb_main = Arc::new(Mutex::new(fb_main));

    // Сохранение входных сообщений в кеше
    let task = tasks::SaveInputInCache {
        in_out: in_out.clone(),
        input_msg_cache: input_msg_cache.clone(),
    };
    join_set_spawn(&mut task_set, task.spawn());

    // Выполнение цикла ПЛК
    let task = tasks::PlcLoop {
        input_msg_cache,
        output: channel_plc_to_filter_send,
        config: config.clone(),
        fb_main: fb_main.clone(),
    };
    join_set_spawn(&mut task_set, task.spawn());

    // Фильтрация исходящих сообщений
    let task = shared_tasks::filter_identical_data::FilterIdenticalData {
        input: channel_plc_to_filter_recv,
        output: channel_filter_to_output_send,
    };
    join_set_spawn(
        &mut task_set,
        task.spawn().map_err(Error::FilterMsgsWithSameData),
    );

    // Пересылка сообщений на выход компонента
    let task = shared_tasks::mpsc_to_msgbus::MpscToMsgBus {
        input: channel_filter_to_output_recv,
        msg_bus: in_out.clone(),
    };
    join_set_spawn(&mut task_set, task.spawn().map_err(Error::ToCmpOutput));

    // Периодический экспорт состояния
    let task = tasks::ExportCurrentState {
        in_out: in_out.clone(),
        config_retention: config.retention,
        fb_main: fb_main.clone(),
    };
    join_set_spawn(&mut task_set, task.spawn());

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