2022-04-29 14:29:37 +00:00
|
|
|
/*
|
|
|
|
* api.c
|
|
|
|
*
|
|
|
|
* Created on: Apr 24, 2022
|
|
|
|
* Author: mateusz
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* 19 maja, Krszystof Binek, miedy 12 a 13
|
|
|
|
*/
|
|
|
|
|
2022-05-02 21:07:08 +00:00
|
|
|
#include <api/api_content.h>
|
2022-04-30 11:58:23 +00:00
|
|
|
#include "etc/api_configuration.h"
|
2022-04-29 14:29:37 +00:00
|
|
|
|
|
|
|
#include "http_client/http_client.h"
|
|
|
|
|
|
|
|
#include "stdint.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Buffers for generating JSON and URL
|
|
|
|
*/
|
2022-05-02 21:07:08 +00:00
|
|
|
#define API_BUFFER_LN 512
|
2022-04-29 14:29:37 +00:00
|
|
|
char api_buffer[API_BUFFER_LN];
|
|
|
|
#define URL_BUFFER_LN 96
|
|
|
|
char api_url_buffer[URL_BUFFER_LN];
|
|
|
|
|
2022-04-30 11:58:23 +00:00
|
|
|
/**
|
|
|
|
* Index to move around a buffer with request body
|
|
|
|
*/
|
|
|
|
uint32_t api_buffer_idx = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Value returned from http client
|
|
|
|
*/
|
2022-04-29 14:29:37 +00:00
|
|
|
uint8_t api_retval = 0xFF;
|
|
|
|
|
2022-04-30 11:58:23 +00:00
|
|
|
/**
|
|
|
|
* Cycle counter to control the frequency of api calls
|
|
|
|
*/
|
|
|
|
int8_t api_cycle_counter = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is used to retrigger specific api communication in case of any request
|
|
|
|
* appears on the same call to `api_pooler` function. For code simplicity only one
|
|
|
|
* API request is possible in the single 'api_pooler' cycle
|
|
|
|
*/
|
|
|
|
#define API_TRIGGER_STATUS (1 << 1)
|
|
|
|
#define API_TRIGGER_MEASUREMENTS (1 << 2)
|
|
|
|
int8_t api_retrigger_api_call = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pointers to base url and station name (stored within flash)
|
|
|
|
*/
|
2022-04-29 14:29:37 +00:00
|
|
|
const char * api_base_url;
|
|
|
|
const char * api_station_name;
|
|
|
|
|
|
|
|
typedef enum api_endpoint{
|
|
|
|
|
|
|
|
PARAMETEO_STATUS,
|
|
|
|
PARAMETEO_WX
|
|
|
|
} api_endpoint_t;
|
|
|
|
|
2022-04-30 11:58:23 +00:00
|
|
|
#define LN api_buffer_idx
|
|
|
|
#define OUT api_buffer
|
2022-04-29 14:29:37 +00:00
|
|
|
|
|
|
|
static void api_construct_url_status(api_endpoint_t endpoint) {
|
|
|
|
|
|
|
|
if (api_base_url != 0x00 && api_station_name != 0x00) {
|
|
|
|
memset(api_url_buffer, 0x00, URL_BUFFER_LN);
|
|
|
|
|
|
|
|
switch (endpoint) {
|
|
|
|
case PARAMETEO_STATUS:
|
2022-04-30 11:58:23 +00:00
|
|
|
snprintf(api_url_buffer, URL_BUFFER_LN - 1, "%s/parameteo/%s/status", api_base_url, api_station_name);
|
2022-04-29 14:29:37 +00:00
|
|
|
break;
|
|
|
|
case PARAMETEO_WX:
|
2022-04-30 13:23:24 +00:00
|
|
|
snprintf(api_url_buffer, URL_BUFFER_LN - 1, "%s/parameteo/%s/measurements", api_base_url, api_station_name);
|
2022-04-29 14:29:37 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void api_callback(uint16_t http_code, char * content, uint16_t content_lenght) {
|
|
|
|
|
2022-04-30 11:58:23 +00:00
|
|
|
|
2022-04-29 14:29:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void api_init(const char * api_base, const char * station_name) {
|
|
|
|
api_base_url = api_base;
|
|
|
|
api_station_name = station_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
void api_send_json_status(void) {
|
|
|
|
BEGIN
|
|
|
|
PRINT_ALL_STATUS
|
|
|
|
END
|
|
|
|
|
2022-04-30 11:58:23 +00:00
|
|
|
if (api_buffer_idx < API_BUFFER_LN) {
|
2022-04-29 14:29:37 +00:00
|
|
|
api_construct_url_status(PARAMETEO_STATUS);
|
|
|
|
|
2022-04-30 13:23:24 +00:00
|
|
|
api_retval = http_client_async_post(api_url_buffer, strlen(api_url_buffer), OUT, strlen(OUT), 0, api_callback);
|
2022-04-29 14:29:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-30 11:58:23 +00:00
|
|
|
void api_send_json_measuremenets(void) {
|
|
|
|
BEGIN
|
|
|
|
PRINT_ALL_MEASUREMENTS
|
|
|
|
END
|
|
|
|
|
|
|
|
if (api_buffer_idx < API_BUFFER_LN) {
|
2022-04-30 13:23:24 +00:00
|
|
|
api_construct_url_status(PARAMETEO_WX);
|
2022-04-30 11:58:23 +00:00
|
|
|
|
2022-04-30 13:23:24 +00:00
|
|
|
api_retval = http_client_async_post(api_url_buffer, strlen(api_url_buffer), OUT, strlen(OUT), 0, api_callback);
|
2022-04-30 11:58:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|