using a class for http request.

This commit is contained in:
Martin Brodbeck 2023-01-02 14:28:51 +01:00
parent 225f9eb989
commit 4c87dfe188
4 changed files with 95 additions and 45 deletions

View file

@ -2,6 +2,7 @@
set(SOURCES set(SOURCES
ws2812.cpp ws2812.cpp
http_client.cpp
ntp_client.cpp ntp_client.cpp
abfall.cpp abfall.cpp
) )

View file

@ -10,9 +10,10 @@
#include "pico/stdlib.h" #include "pico/stdlib.h"
#include "pico/util/datetime.h" #include "pico/util/datetime.h"
#include "lwip/apps/http_client.h" //#include "lwip/apps/http_client.h"
#include "ntp_client.h" #include "ntp_client.h"
#include "http_client.h"
#include "ws2812.h" #include "ws2812.h"
using std::string; using std::string;
@ -65,40 +66,6 @@ int wifi_setup(uint32_t country, const string &ssid, const string &pw, bool firs
return status; return status;
} }
char myBuffer[2048];
void result_callback(void *arg, httpc_result_t httpc_result, u32_t rx_content_len, u32_t srv_res,
err_t err) {
printf("Transfer complete\n");
printf("Local result=%d\n", httpc_result);
printf("Http result=%d\n", srv_res);
}
err_t headers_callback(httpc_state_t *connection, void *arg, struct pbuf *hdr, u16_t hdr_len,
u32_t content_len) {
printf("Headers received\n");
printf("Content length=%d\n", content_len);
printf("Header length=%d\n", hdr_len);
pbuf_copy_partial(hdr, myBuffer, hdr->tot_len, 0);
printf("Headers \n");
printf("%s", myBuffer);
return ERR_OK;
}
err_t body_callback(void *arg, struct altcp_pcb *conn, struct pbuf *p, err_t err) {
// printf("Body\n");
pbuf_copy_partial(p, myBuffer, p->tot_len, 0);
// printf("%s", myBuffer);
// TODO: Parse CSV
return ERR_OK;
}
int main() { int main() {
const string ssid{"Apis cerana"}; const string ssid{"Apis cerana"};
const string pw{"2JkJEh2vptVT"}; const string pw{"2JkJEh2vptVT"};
@ -123,16 +90,10 @@ int main() {
} while (res != CYW43_LINK_UP); } while (res != CYW43_LINK_UP);
uint16_t port = 80;
httpc_connection_t settings;
settings.result_fn = nullptr;
settings.headers_done_fn = nullptr;
err_t err = httpc_get_file_dns("beenas.brodbeck-online.de", port, "/abfall/abfall.csv",
&settings, body_callback, nullptr, nullptr);
// printf("Status %d\n", err);
NtpClient::setDateTime(); NtpClient::setDateTime();
HttpClient client;
std::string test = client.retrieveWasteDatesAsCsv();
printf("%s\n", test.c_str());
char datetime_buf[256]; char datetime_buf[256];
char *datetime_str = &datetime_buf[0]; char *datetime_str = &datetime_buf[0];
@ -142,7 +103,7 @@ int main() {
rtc_get_datetime(&dt); rtc_get_datetime(&dt);
datetime_to_str(datetime_str, sizeof(datetime_buf), &dt); datetime_to_str(datetime_str, sizeof(datetime_buf), &dt);
// printf("DateTime: %s\n", datetime_str); // printf("DateTime: %s\n", datetime_str);
led.switchColor(Color::RED); led.switchColor(Color::OFF);
sleep_ms(5000); sleep_ms(5000);
} }

61
src/http_client.cpp Normal file
View file

@ -0,0 +1,61 @@
#include "http_client.h"
void result_callback(void *arg, httpc_result_t httpc_result, u32_t rx_content_len, u32_t srv_res,
err_t err) {
printf("Transfer complete\n");
printf("Local result=%d\n", httpc_result);
printf("Http result=%d\n", srv_res);
}
err_t headers_callback(httpc_state_t *connection, void *arg, struct pbuf *hdr, u16_t hdr_len,
u32_t content_len) {
printf("Headers received\n");
printf("Content length=%d\n", content_len);
printf("Header length=%d\n", hdr_len);
pbuf_copy_partial(hdr, myBuffer, hdr->tot_len, 0);
printf("Headers \n");
printf("%s", myBuffer);
return ERR_OK;
}
err_t body_callback(void *arg, struct altcp_pcb *conn, struct pbuf *p, err_t err) {
bool *test = (bool *)arg;
printf("...");
pbuf_copy_partial(p, myBuffer, p->tot_len, 0);
// printf("%s", myBuffer);
// TODO: Parse CSV
*test = true;
return ERR_OK;
}
HttpClient::HttpClient() {
m_settings.use_proxy = false;
m_settings.result_fn = nullptr; // result_callback;
m_settings.headers_done_fn = nullptr; // headers_callback;
}
std::string HttpClient::retrieveWasteDatesAsCsv() {
uint16_t port = 80;
std::string test("");
err_t err = httpc_get_file_dns("beenas.brodbeck-online.de", port, "/abfall/abfall.csv",
&m_settings, body_callback, &m_received, nullptr);
// printf("Status %d\n", err);
printf("Waiting for waste dates ");
while (m_received == false) {
sleep_ms(100);
}
printf(" received.\n");
test.append(myBuffer);
return test;
}

27
src/http_client.h Normal file
View file

@ -0,0 +1,27 @@
#pragma once
#include <string>
#include "lwip/apps/http_client.h"
#include "pico/cyw43_arch.h"
#include "pico/stdlib.h"
static char myBuffer[2048];
extern "C" void result_callback(void *arg, httpc_result_t httpc_result, u32_t rx_content_len,
u32_t srv_res, err_t err);
extern "C" err_t headers_callback(httpc_state_t *connection, void *arg, struct pbuf *hdr,
u16_t hdr_len, u32_t content_len);
extern "C" err_t body_callback(void *arg, struct altcp_pcb *conn, struct pbuf *p, err_t err);
class HttpClient {
public:
HttpClient();
std::string retrieveWasteDatesAsCsv();
bool m_received{false};
private:
httpc_connection_t m_settings;
};