rsiot/components/cmp_http_server/tasks/
cmp_plc_data.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
use crate::{
    executor::CmpInOut,
    message::{Message, MsgDataBound, ServiceBound},
};

use super::super::{shared_state::TSharedState, ConfigCmpPlcData};

pub struct CmpPlcData<TMsg, TService>
where
    TMsg: MsgDataBound,
    TService: ServiceBound,
{
    pub input: CmpInOut<TMsg, TService>,
    pub shared_state: TSharedState<TMsg, TService>,
    pub fn_input: fn(&Message<TMsg>) -> ConfigCmpPlcData,
}

impl<TMsg, TService> CmpPlcData<TMsg, TService>
where
    TMsg: MsgDataBound,
    TService: ServiceBound,
{
    pub async fn spawn(mut self) -> super::Result<()> {
        while let Ok(msg) = self.input.recv_input().await {
            let data = (self.fn_input)(&msg);

            // Выходим из блока, чтобы не блокировать SharedState
            if matches!(data, ConfigCmpPlcData::NoData) {
                continue;
            }

            let mut shared_state = self.shared_state.lock().await;
            match data {
                ConfigCmpPlcData::NoData => continue,
                ConfigCmpPlcData::Input(data) => shared_state.cmp_plc_input = data,
                ConfigCmpPlcData::Output(data) => shared_state.cmp_plc_output = data,
                ConfigCmpPlcData::Static(data) => shared_state.cmp_plc_static = data,
            }
        }

        Err(super::Error::TaskEndCmpPlcInput)
    }
}