rsiot/components/cmp_http_client_esp/
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
use std::time::Duration;

use embedded_svc::http::{client::Client as HttpClient, Method};
use esp_idf_svc::http::client::EspHttpConnection;
use tokio::{
    task::JoinSet,
    time::{sleep, Instant},
};
use tracing::{error, info, warn};
use url::Url;

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

use super::{config, Error};

pub async fn fn_process<TMsg, TService>(
    config: config::Config<TMsg>,
    in_out: CmpInOut<TMsg, TService>,
) -> super::Result<()>
where
    TMsg: MsgDataBound + 'static,
    TService: ServiceBound + 'static,
{
    // Необходимо подождать, пока поднимется Wi-Fi
    sleep(Duration::from_secs(2)).await;

    info!("Starting http-client, configuration: {:?}", config);

    loop {
        let res = task_main(in_out.clone(), config.clone()).await;
        match res {
            Ok(_) => (),
            Err(err) => {
                error!("Error in http-client: {:?}", err);
            }
        }
        info!("Restarting...");
        sleep(Duration::from_secs(2)).await;
    }
}

/// Основная задача
async fn task_main<TMsg, TService>(
    in_out: CmpInOut<TMsg, TService>,
    config: config::Config<TMsg>,
) -> super::Result<()>
where
    TMsg: MsgDataBound + 'static,
    TService: ServiceBound + 'static,
{
    let mut set = JoinSet::<super::Result<()>>::new();

    // Парсим url
    let url = Url::parse(&config.connection_config.base_url);
    let url = match url {
        Ok(val) => val,
        Err(err) => {
            let err = err.to_string();
            let err = format!("Cannot parse url: {}", err);
            return Err(Error::Configuration(err));
        }
    };

    // запускаем периодические запросы
    for req in config.requests_periodic {
        let future = task_periodic_request(in_out.clone(), req, url.clone());
        set.spawn_local(future);
    }
    // Запускаем задачи запросов на основе входного потока сообщений
    // for item in config.requests_input {
    //     let future = task_input_request(
    //         in_out.clone(),
    //         config.connection_config.base_url.clone(),
    //         item,
    //     );
    //     set.spawn(future);
    // }
    while let Some(res) = set.join_next().await {
        res??
    }
    Ok(())
}

/// Задача обработки периодического запроса
async fn task_periodic_request<TMsg, TService>(
    in_out: CmpInOut<TMsg, TService>,
    config: config::RequestPeriodic<TMsg>,
    url: Url,
) -> super::Result<()>
where
    TMsg: MsgDataBound,
    TService: ServiceBound,
{
    loop {
        let begin = Instant::now();

        let msgs = process_request_and_response(
            &url,
            &config.http_param,
            config.on_success,
            config.on_failure,
        )
        .await?;
        for msg in msgs {
            in_out.send_output(msg).await?;
        }
        let elapsed = begin.elapsed();
        let sleep_time = if config.period <= elapsed {
            Duration::from_millis(10)
        } else {
            config.period - elapsed
        };

        sleep(sleep_time).await;
    }
}

/// Выполнение запроса и вызов коллбеков при ответе
async fn process_request_and_response<TMsg>(
    url: &Url,
    request_param: &config::HttpParam,
    on_success: config::CbkOnSuccess<TMsg>,
    on_failure: config::CbkOnFailure<TMsg>,
) -> super::Result<Vec<Message<TMsg>>> {
    info!("Call http client");
    let response = send_request(url.clone(), request_param).await;
    // let response = match response {
    //     Ok(val) => val,
    //     Err(err) => match err {
    //         Error::Reqwest(source) => {
    //             error!("{:?}", source);
    //             let msgs = (on_failure)();
    //             return Ok(msgs);
    //         }
    //         _ => return Err(err),
    //     },
    // };
    // let status = response.status();
    // let text = response.text().await?;
    // if status != StatusCode::OK {
    //     let msgs = (on_failure)();
    //     error!(
    //         "Error on request.\nRequest params: {:?}\nResponse text: {:?}",
    //         request_param, text
    //     );
    //     return Ok(msgs);
    // }
    // let msgs = (on_success)(&text)?;

    let msgs = vec![];
    Ok(msgs)
}

/// Выполнение HTTP запроса
async fn send_request(url: Url, req: &config::HttpParam) -> super::Result<()> {
    let endpoint = match req {
        config::HttpParam::Get { endpoint } => endpoint,
        config::HttpParam::Put { endpoint, body: _ } => endpoint,
        config::HttpParam::Post { endpoint, body: _ } => endpoint,
    };
    let url = url.join(endpoint).map_err(|err| {
        let err = err.to_string();
        Error::Configuration(err)
    })?;
    let url = url.to_string();
    info!("Url: {}", url);

    let mut client = HttpClient::wrap(EspHttpConnection::new(&Default::default()).unwrap());

    let headers = [("accept", "text/plain")];
    let response = match req {
        config::HttpParam::Get { endpoint: _ } => {
            let request = client.request(Method::Get, url.as_ref(), &headers);
            let request = match request {
                Ok(val) => val,
                Err(err) => {
                    let err = err.to_string();
                    warn!("{}", err);
                    return Ok(());
                }
            };
            request.submit().unwrap()
            // client.get(&url).unwrap().submit().unwrap()
        }
        // config::HttpParam::Put { endpoint: _, body } => {
        //     client.put(url).body(body.to_string()).send().await?
        // }
        // config::HttpParam::Post { endpoint: _, body } => {
        //     client.post(url).body(body.to_string()).send().await?
        // }
        _ => todo!(),
    };
    let status = response.status();
    info!("<- {}", status);
    Ok(())
}