Skip to main content

rsiot/components/cmp_plc/
component.rs

1use async_trait::async_trait;
2use serde::Serialize;
3
4use crate::{
5    executor::{Component, ComponentError, IComponentProcess, MsgBusLinker},
6    message::MsgDataBound,
7};
8
9use super::{
10    config::Config,
11    fn_process::fn_process,
12    plc::{FunctionBlockBase, IFunctionBlock},
13};
14
15/// Название компонента
16pub const COMPONENT_NAME: &str = "cmp_plc";
17
18#[cfg_attr(not(feature = "single-thread"), async_trait)]
19#[cfg_attr(feature = "single-thread", async_trait(? Send))]
20impl<TMsg, I, Q, S> IComponentProcess<Config<TMsg, I, Q, S>, TMsg>
21    for Component<Config<TMsg, I, Q, S>, TMsg>
22where
23    TMsg: MsgDataBound + 'static,
24    I: Clone + Default + Send + Serialize + 'static + Sync,
25    Q: Clone + Default + Send + Serialize + 'static + Sync,
26    S: Clone + Default + Send + Serialize + 'static + Sync,
27    FunctionBlockBase<I, Q, S>: IFunctionBlock<I, Q, S>,
28{
29    async fn process(
30        &self,
31        config: Config<TMsg, I, Q, S>,
32        msgbus_linker: MsgBusLinker<TMsg>,
33    ) -> Result<(), ComponentError> {
34        fn_process(msgbus_linker.init(COMPONENT_NAME), config)
35            .await
36            .map_err(|e| ComponentError::Execution(e.to_string()))
37    }
38}
39
40/// Компонент cmp_plc
41pub type Cmp<TMsg, I, Q, S> = Component<Config<TMsg, I, Q, S>, TMsg>;