Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
/// @file
/// @brief API implementation for communication with server
/* noisemeter-device - Firmware for CivicTechTO's Noisemeter Device
* Copyright (C) 2024 Clyne Sullivan
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef API_H
#define API_H
#include "data-packet.h"
#include "UUID/UUID.h"
#include <ArduinoJson.h>
#include <WString.h>
#include <list>
#include <optional>
class HTTPClient;
/**
* @brief Provides interface for API calls.
*/
class API
{
/** API host / base URL. */
constexpr static const char Base[] = "https://api.tracket.info/v1/";
/** String builder to create URLs for API endpoints and requests. */
struct Request {
/**
* Initializes a new request with zero parameters.
* @param endpoint Endpoint for the API request.
*/
Request(const char endpoint[]);
/**
* Adds a parameter to the given request.
* @param param Name of parameter to add.
* @param value Value of the added parameter.
* @return *this
*/
Request& addParam(const char param[], String value);
/** Stores the resulting request URL. */
String url;
String params;
};
public:
/** Results of the software update endpoint. */
struct LatestSoftware {
/** Version number of latest available software. */
String version;
/** Download URL for latest software. */
String url;
};
struct DeviceConfig {
/** Timestamp for when this config was last updated. */
String timestamp;
/** Specifies how frequently to create a packet w/ min/mean/max stats. */
int measurementFrequency;
/** Specifies how frequently to upload measurements. */
int sendFrequency;
};
/**
* Creates a new API interface for the given device (ID).
* @param id_ Device UUID
* @param token_ API token (required for authorized API calls)
*/
API(UUID id_, String token_ = {});
/**
* Sends a DataPacket (dB measurement) to the server.
* This request requires authentication.
* @param packet Packet to be sent.
* @return If successful, the timestamp of the last DeviceConfig modification
*/
std::optional<String> sendMeasurement(const DataPacket& packet);
std::optional<String> sendMeasurements(const std::list<DataPacket>& packets,
const std::list<DataPacket>::const_iterator from);
/**
* Sends diagnostic/analytic data to the server along with a DataPacket.
* This request requires authentication.
* @param packet Packet to be sent.
* @param version Device's software version number.
* @param boottime Timestamp of last connection to the internet.
* @return If successful, the timestamp of the last DeviceConfig modification
*/
std::optional<String> sendMeasurementWithDiagnostics(const DataPacket& packet, String version, String boottime);
/**
* Attempts to register the device with the given email.
* @param email Email to register with.
* @return Device's new API token if successful.
*/
std::optional<String> sendRegister(String email);
/**
* Attempts to get the latest available software info (for OTA).
* @return LatestSoftware if successful.
*/
std::optional<LatestSoftware> getLatestSoftware();
std::optional<DeviceConfig> getDeviceConfig();
/**
* Provides the server's root certificate for non-API HTTPS requests.
*/
static const char *rootCertificate();
private:
/** Device's UUID. */
UUID id;
/** Device's API token for authorized requests. */
String token;
/** Converts response string into JSON. */
std::optional<JsonDocument> responseToJson(const String& response);
/** Attempts the given request and returns the JSON response on success. */
std::optional<JsonDocument> sendAuthorizedRequest(
const Request& req, String ctype = "application/x-www-form-urlencoded");
/** Attempts the given request and returns the JSON response on success. */
std::optional<JsonDocument> sendNonauthorizedRequest(const Request& req);
std::optional<JsonDocument> sendHttpGET(HTTPClient& https);
std::optional<JsonDocument> sendHttpPOST(HTTPClient& https, const String& payload);
std::optional<JsonDocument> handleHttpResponse(HTTPClient& https);
};
#endif // API_H
b8134799cThis file is pinned to the selected release. Download the original to open it in your design tool.