Files
arduino/examples/BASIC/OpenMetrics.cpp

205 lines
7.3 KiB
C++
Raw Normal View History

2024-06-24 18:34:24 +07:00
#include "OpenMetrics.h"
OpenMetrics::OpenMetrics(Measurements &measure, Configuration &config,
WifiConnector &wifiConnector, AgApiClient &apiClient)
: measure(measure), config(config), wifiConnector(wifiConnector),
apiClient(apiClient) {}
OpenMetrics::~OpenMetrics() {}
void OpenMetrics::setAirGradient(AirGradient *ag) { this->ag = ag; }
const char *OpenMetrics::getApiContentType(void) {
return "application/openmetrics-text; version=1.0.0; charset=utf-8";
}
const char *OpenMetrics::getApi(void) { return "/metrics"; }
String OpenMetrics::getPayload(void) {
String response;
String current_metric_name;
const auto add_metric = [&](const String &name, const String &help,
const String &type, const String &unit = "") {
current_metric_name = "airgradient_" + name;
if (!unit.isEmpty())
current_metric_name += "_" + unit;
response += "# HELP " + current_metric_name + " " + help + "\n";
response += "# TYPE " + current_metric_name + " " + type + "\n";
if (!unit.isEmpty())
response += "# UNIT " + current_metric_name + " " + unit + "\n";
};
const auto add_metric_point = [&](const String &labels, const String &value) {
response += current_metric_name + "{" + labels + "} " + value + "\n";
};
add_metric("info", "AirGradient device information", "info");
add_metric_point("airgradient_serial_number=\"" + ag->deviceId() +
"\",airgradient_device_type=\"" + ag->getBoardName() +
"\",airgradient_library_version=\"" + ag->getVersion() +
"\"",
"1");
add_metric("config_ok",
"1 if the AirGradient device was able to successfully fetch its "
"configuration from the server",
"gauge");
add_metric_point("", apiClient.isFetchConfigureFailed() ? "0" : "1");
add_metric(
"post_ok",
"1 if the AirGradient device was able to successfully send to the server",
"gauge");
add_metric_point("", apiClient.isPostToServerFailed() ? "0" : "1");
add_metric(
"wifi_rssi",
"WiFi signal strength from the AirGradient device perspective, in dBm",
"gauge", "dbm");
add_metric_point("", String(wifiConnector.RSSI()));
2024-11-27 00:43:03 +07:00
// Initialize default invalid value for each measurements
2024-07-24 09:05:57 +07:00
float _temp = utils::getInvalidTemperature();
float _hum = utils::getInvalidHumidity();
2024-08-26 14:14:42 +07:00
int pm01 = utils::getInvalidPmValue();
int pm25 = utils::getInvalidPmValue();
int pm10 = utils::getInvalidPmValue();
int pm03PCount = utils::getInvalidPmValue();
2024-11-27 00:43:03 +07:00
int co2 = utils::getInvalidCO2();
2024-07-24 09:05:57 +07:00
int atmpCompensated = utils::getInvalidTemperature();
int ahumCompensated = utils::getInvalidHumidity();
2024-10-22 00:11:58 +07:00
int tvoc = utils::getInvalidVOC();
2024-11-27 00:43:03 +07:00
int tvocRaw = utils::getInvalidVOC();
2024-10-22 00:11:58 +07:00
int nox = utils::getInvalidNOx();
2024-11-27 00:43:03 +07:00
int noxRaw = utils::getInvalidNOx();
2024-06-24 18:34:24 +07:00
if (config.hasSensorSHT) {
2024-10-22 00:11:58 +07:00
_temp = measure.getFloat(Measurements::Temperature);
_hum = measure.getFloat(Measurements::Humidity);
2024-06-24 18:34:24 +07:00
atmpCompensated = _temp;
ahumCompensated = _hum;
}
if (config.hasSensorPMS1) {
2024-10-22 00:11:58 +07:00
pm01 = measure.get(Measurements::PM01);
2024-12-03 01:55:11 +07:00
float correctedPm = measure.getCorrectedPM25(*ag, config, false, 1);
pm25 = round(correctedPm);
2024-10-22 00:11:58 +07:00
pm10 = measure.get(Measurements::PM10);
pm03PCount = measure.get(Measurements::PM03_PC);
}
if (config.hasSensorSGP) {
tvoc = measure.get(Measurements::TVOC);
2024-11-27 00:43:03 +07:00
tvocRaw = measure.get(Measurements::TVOCRaw);
2024-10-22 00:11:58 +07:00
nox = measure.get(Measurements::NOx);
2024-11-27 00:43:03 +07:00
noxRaw = measure.get(Measurements::NOxRaw);
}
if (config.hasSensorS8) {
co2 = measure.get(Measurements::CO2);
2024-06-24 18:34:24 +07:00
}
if (config.hasSensorPMS1) {
2024-08-30 19:07:31 +07:00
if (utils::isValidPm(pm01)) {
2024-06-24 18:34:24 +07:00
add_metric("pm1",
"PM1.0 concentration as measured by the AirGradient PMS "
"sensor, in micrograms per cubic meter",
"gauge", "ugm3");
add_metric_point("", String(pm01));
}
2024-08-30 19:07:31 +07:00
if (utils::isValidPm(pm25)) {
2024-06-24 18:34:24 +07:00
add_metric("pm2d5",
"PM2.5 concentration as measured by the AirGradient PMS "
"sensor, in micrograms per cubic meter",
"gauge", "ugm3");
add_metric_point("", String(pm25));
}
2024-08-30 19:07:31 +07:00
if (utils::isValidPm(pm10)) {
2024-06-24 18:34:24 +07:00
add_metric("pm10",
"PM10 concentration as measured by the AirGradient PMS "
"sensor, in micrograms per cubic meter",
"gauge", "ugm3");
add_metric_point("", String(pm10));
}
2024-08-30 19:07:31 +07:00
if (utils::isValidPm03Count(pm03PCount)) {
2024-06-24 18:34:24 +07:00
add_metric("pm0d3",
"PM0.3 concentration as measured by the AirGradient PMS "
"sensor, in number of particules per 100 milliliters",
"gauge", "p100ml");
add_metric_point("", String(pm03PCount));
}
}
if (config.hasSensorSGP) {
2024-10-22 00:11:58 +07:00
if (utils::isValidVOC(tvoc)) {
2024-06-24 18:34:24 +07:00
add_metric("tvoc_index",
"The processed Total Volatile Organic Compounds (TVOC) index "
"as measured by the AirGradient SGP sensor",
"gauge");
2024-10-22 00:11:58 +07:00
add_metric_point("", String(tvoc));
2024-06-24 18:34:24 +07:00
}
2024-11-27 00:43:03 +07:00
if (utils::isValidVOC(tvocRaw)) {
2024-06-24 18:34:24 +07:00
add_metric("tvoc_raw",
"The raw input value to the Total Volatile Organic Compounds "
"(TVOC) index as measured by the AirGradient SGP sensor",
"gauge");
2024-11-27 00:43:03 +07:00
add_metric_point("", String(tvocRaw));
2024-06-24 18:34:24 +07:00
}
2024-10-22 00:11:58 +07:00
if (utils::isValidNOx(nox)) {
2024-06-24 18:34:24 +07:00
add_metric("nox_index",
"The processed Nitrous Oxide (NOx) index as measured by the "
"AirGradient SGP sensor",
"gauge");
2024-10-22 00:11:58 +07:00
add_metric_point("", String(nox));
2024-06-24 18:34:24 +07:00
}
2024-11-27 00:43:03 +07:00
if (utils::isValidNOx(noxRaw)) {
2024-06-24 18:34:24 +07:00
add_metric("nox_raw",
"The raw input value to the Nitrous Oxide (NOx) index as "
"measured by the AirGradient SGP sensor",
"gauge");
2024-11-27 00:43:03 +07:00
add_metric_point("", String(noxRaw));
2024-06-24 18:34:24 +07:00
}
}
2024-11-27 00:43:03 +07:00
if (utils::isValidCO2(co2)) {
add_metric("co2",
"Carbon dioxide concentration as measured by the AirGradient S8 "
"sensor, in parts per million",
"gauge", "ppm");
add_metric_point("", String(co2));
}
2024-07-24 09:05:57 +07:00
if (utils::isValidTemperature(_temp)) {
2024-06-24 18:34:24 +07:00
add_metric(
"temperature",
"The ambient temperature as measured by the AirGradient SHT / PMS "
"sensor, in degrees Celsius",
"gauge", "celsius");
add_metric_point("", String(_temp));
}
2024-07-24 09:05:57 +07:00
if (utils::isValidTemperature(atmpCompensated)) {
2024-06-24 18:34:24 +07:00
add_metric("temperature_compensated",
"The compensated ambient temperature as measured by the "
"AirGradient SHT / PMS "
"sensor, in degrees Celsius",
"gauge", "celsius");
add_metric_point("", String(atmpCompensated));
}
2024-07-24 09:05:57 +07:00
if (utils::isValidHumidity(_hum)) {
2024-06-24 18:34:24 +07:00
add_metric(
"humidity",
"The relative humidity as measured by the AirGradient SHT sensor",
"gauge", "percent");
add_metric_point("", String(_hum));
}
2024-07-24 09:05:57 +07:00
if (utils::isValidHumidity(ahumCompensated)) {
2024-06-24 18:34:24 +07:00
add_metric("humidity_compensated",
"The compensated relative humidity as measured by the "
"AirGradient SHT / PMS sensor",
"gauge", "percent");
add_metric_point("", String(ahumCompensated));
}
response += "# EOF\n";
return response;
}