Rename AgValueType to MeasurementType

Just use plain enum instead of enum class
Remove unecessary legacy variables and function
This commit is contained in:
samuelbles07
2024-10-19 01:32:41 +07:00
parent 76a2f332d7
commit 0ccf46c219
3 changed files with 143 additions and 600 deletions

View File

@ -337,10 +337,10 @@ static void co2Update(void) {
int value = ag->s8.getCo2(); int value = ag->s8.getCo2();
if (utils::isValidCO2(value)) { if (utils::isValidCO2(value)) {
measurements.updateValue(Measurements::AgValueType::CO2, value); measurements.update(Measurements::CO2, value);
// Serial.printf("CO2 (ppm): %d\r\n", measurements.CO2); // Serial.printf("CO2 (ppm): %d\r\n", measurements.CO2);
} else { } else {
measurements.updateValue(Measurements::AgValueType::CO2, utils::getInvalidCO2()); measurements.update(Measurements::CO2, utils::getInvalidCO2());
} }
} }
@ -374,7 +374,7 @@ static void createMqttTask(void) {
/** Send data */ /** Send data */
if (mqttClient.isConnected()) { if (mqttClient.isConnected()) {
String payload = String payload =
measurements.toStringX(true, fwMode, wifiConnector.RSSI(), *ag, configuration); measurements.toString(true, fwMode, wifiConnector.RSSI(), *ag, configuration);
String topic = "airgradient/readings/" + ag->deviceId(); String topic = "airgradient/readings/" + ag->deviceId();
if (mqttClient.publish(topic.c_str(), payload.c_str(), if (mqttClient.publish(topic.c_str(), payload.c_str(),
@ -999,10 +999,10 @@ static void updateTvoc(void) {
return; return;
} }
measurements.updateValue(Measurements::AgValueType::TVOC, ag->sgp41.getTvocIndex()); measurements.update(Measurements::TVOC, ag->sgp41.getTvocIndex());
measurements.updateValue(Measurements::AgValueType::TVOCRaw, ag->sgp41.getTvocRaw()); measurements.update(Measurements::TVOCRaw, ag->sgp41.getTvocRaw());
measurements.updateValue(Measurements::AgValueType::NOx, ag->sgp41.getNoxIndex()); measurements.update(Measurements::NOx, ag->sgp41.getNoxIndex());
measurements.updateValue(Measurements::AgValueType::NOxRaw, ag->sgp41.getNoxRaw()); measurements.update(Measurements::NOxRaw, ag->sgp41.getNoxRaw());
// Serial.println(); // Serial.println();
// Serial.printf("TVOC index: %d\r\n", measurements.TVOC); // Serial.printf("TVOC index: %d\r\n", measurements.TVOC);
@ -1013,11 +1013,10 @@ static void updateTvoc(void) {
static void updatePMS5003() { static void updatePMS5003() {
if (ag->pms5003.connected()) { if (ag->pms5003.connected()) {
measurements.updateValue(Measurements::AgValueType::PM01, ag->pms5003.getPm01Ae()); measurements.update(Measurements::PM01, ag->pms5003.getPm01Ae());
measurements.updateValue(Measurements::AgValueType::PM25, ag->pms5003.getPm25Ae()); measurements.update(Measurements::PM25, ag->pms5003.getPm25Ae());
measurements.updateValue(Measurements::AgValueType::PM10, ag->pms5003.getPm10Ae()); measurements.update(Measurements::PM10, ag->pms5003.getPm10Ae());
measurements.updateValue(Measurements::AgValueType::PM03_PC, measurements.update(Measurements::PM03_PC, ag->pms5003.getPm03ParticleCount());
ag->pms5003.getPm03ParticleCount());
// Serial.println(); // Serial.println();
// Serial.printf("PM1 ug/m3: %d\r\n", measurements.pm01_1); // Serial.printf("PM1 ug/m3: %d\r\n", measurements.pm01_1);
@ -1026,10 +1025,10 @@ static void updatePMS5003() {
// Serial.printf("PM0.3 Count: %d\r\n", measurements.pm03PCount_1); // Serial.printf("PM0.3 Count: %d\r\n", measurements.pm03PCount_1);
// Serial.printf("PM firmware version: %d\r\n", ag->pms5003.getFirmwareVersion()); // Serial.printf("PM firmware version: %d\r\n", ag->pms5003.getFirmwareVersion());
} else { } else {
measurements.updateValue(Measurements::AgValueType::PM01, utils::getInvalidPmValue()); measurements.update(Measurements::PM01, utils::getInvalidPmValue());
measurements.updateValue(Measurements::AgValueType::PM25, utils::getInvalidPmValue()); measurements.update(Measurements::PM25, utils::getInvalidPmValue());
measurements.updateValue(Measurements::AgValueType::PM10, utils::getInvalidPmValue()); measurements.update(Measurements::PM10, utils::getInvalidPmValue());
measurements.updateValue(Measurements::AgValueType::PM03_PC, utils::getInvalidPmValue()); measurements.update(Measurements::PM03_PC, utils::getInvalidPmValue());
} }
} }
@ -1047,35 +1046,23 @@ static void updatePm(void) {
int channel = 1; int channel = 1;
if (configuration.hasSensorPMS1) { if (configuration.hasSensorPMS1) {
if (ag->pms5003t_1.connected()) { if (ag->pms5003t_1.connected()) {
measurements.updateValue(Measurements::AgValueType::PM01, ag->pms5003t_1.getPm01Ae(), measurements.update(Measurements::PM01, ag->pms5003t_1.getPm01Ae(), channel);
channel); measurements.update(Measurements::PM25, ag->pms5003t_1.getPm25Ae(), channel);
measurements.updateValue(Measurements::AgValueType::PM25, ag->pms5003t_1.getPm25Ae(), measurements.update(Measurements::PM10, ag->pms5003t_1.getPm10Ae(), channel);
channel); measurements.update(Measurements::PM03_PC, ag->pms5003t_1.getPm03ParticleCount(), channel);
measurements.updateValue(Measurements::AgValueType::PM10, ag->pms5003t_1.getPm10Ae(), measurements.update(Measurements::Temperature, ag->pms5003t_1.getTemperature(), channel);
channel); measurements.update(Measurements::Humidity, ag->pms5003t_1.getRelativeHumidity(), channel);
measurements.updateValue(Measurements::AgValueType::PM03_PC,
ag->pms5003t_1.getPm03ParticleCount(), channel);
measurements.updateValue(Measurements::AgValueType::Temperature,
ag->pms5003t_1.getTemperature(), channel);
measurements.updateValue(Measurements::AgValueType::Humidity,
ag->pms5003t_1.getRelativeHumidity(), channel);
// flag that new valid PMS value exists // flag that new valid PMS value exists
newPMS2Value = true; newPMS2Value = true;
} else { } else {
// PMS channel 1 now is not connected, update using invalid value // PMS channel 1 now is not connected, update using invalid value
measurements.updateValue(Measurements::AgValueType::PM01, utils::getInvalidPmValue(), measurements.update(Measurements::PM01, utils::getInvalidPmValue(), channel);
channel); measurements.update(Measurements::PM25, utils::getInvalidPmValue(), channel);
measurements.updateValue(Measurements::AgValueType::PM25, utils::getInvalidPmValue(), measurements.update(Measurements::PM10, utils::getInvalidPmValue(), channel);
channel); measurements.update(Measurements::PM03_PC, utils::getInvalidPmValue(), channel);
measurements.updateValue(Measurements::AgValueType::PM10, utils::getInvalidPmValue(), measurements.update(Measurements::Temperature, utils::getInvalidTemperature(), channel);
channel); measurements.update(Measurements::Humidity, utils::getInvalidHumidity(), channel);
measurements.updateValue(Measurements::AgValueType::PM03_PC, utils::getInvalidPmValue(),
channel);
measurements.updateValue(Measurements::AgValueType::Temperature,
utils::getInvalidTemperature(), channel);
measurements.updateValue(Measurements::AgValueType::Humidity, utils::getInvalidHumidity(),
channel);
} }
} }
@ -1083,35 +1070,23 @@ static void updatePm(void) {
channel = 2; channel = 2;
if (configuration.hasSensorPMS2) { if (configuration.hasSensorPMS2) {
if (ag->pms5003t_2.connected()) { if (ag->pms5003t_2.connected()) {
measurements.updateValue(Measurements::AgValueType::PM01, ag->pms5003t_2.getPm01Ae(), measurements.update(Measurements::PM01, ag->pms5003t_2.getPm01Ae(), channel);
channel); measurements.update(Measurements::PM25, ag->pms5003t_2.getPm25Ae(), channel);
measurements.updateValue(Measurements::AgValueType::PM25, ag->pms5003t_2.getPm25Ae(), measurements.update(Measurements::PM10, ag->pms5003t_2.getPm10Ae(), channel);
channel); measurements.update(Measurements::PM03_PC, ag->pms5003t_2.getPm03ParticleCount(), channel);
measurements.updateValue(Measurements::AgValueType::PM10, ag->pms5003t_2.getPm10Ae(), measurements.update(Measurements::Temperature, ag->pms5003t_2.getTemperature(), channel);
channel); measurements.update(Measurements::Humidity, ag->pms5003t_2.getRelativeHumidity(), channel);
measurements.updateValue(Measurements::AgValueType::PM03_PC,
ag->pms5003t_2.getPm03ParticleCount(), channel);
measurements.updateValue(Measurements::AgValueType::Temperature,
ag->pms5003t_2.getTemperature(), channel);
measurements.updateValue(Measurements::AgValueType::Humidity,
ag->pms5003t_2.getRelativeHumidity(), channel);
// flag that new valid PMS value exists // flag that new valid PMS value exists
newPMS2Value = true; newPMS2Value = true;
} else { } else {
// PMS channel channel now is not connected, update using invalid value // PMS channel channel now is not connected, update using invalid value
measurements.updateValue(Measurements::AgValueType::PM01, utils::getInvalidPmValue(), measurements.update(Measurements::PM01, utils::getInvalidPmValue(), channel);
channel); measurements.update(Measurements::PM25, utils::getInvalidPmValue(), channel);
measurements.updateValue(Measurements::AgValueType::PM25, utils::getInvalidPmValue(), measurements.update(Measurements::PM10, utils::getInvalidPmValue(), channel);
channel); measurements.update(Measurements::PM03_PC, utils::getInvalidPmValue(), channel);
measurements.updateValue(Measurements::AgValueType::PM10, utils::getInvalidPmValue(), measurements.update(Measurements::Temperature, utils::getInvalidTemperature(), channel);
channel); measurements.update(Measurements::Humidity, utils::getInvalidHumidity(), channel);
measurements.updateValue(Measurements::AgValueType::PM03_PC, utils::getInvalidPmValue(),
channel);
measurements.updateValue(Measurements::AgValueType::Temperature,
utils::getInvalidTemperature(), channel);
measurements.updateValue(Measurements::AgValueType::Humidity, utils::getInvalidHumidity(),
channel);
} }
} }
@ -1119,20 +1094,20 @@ static void updatePm(void) {
float temp, hum; float temp, hum;
if (newPMS1Value && newPMS2Value) { if (newPMS1Value && newPMS2Value) {
// Both PMS has new valid value // Both PMS has new valid value
temp = (measurements.getValueFloat(Measurements::AgValueType::Temperature, false, 1) + temp = (measurements.getFloat(Measurements::Temperature, false, 1) +
measurements.getValueFloat(Measurements::AgValueType::Temperature, false, 2)) / measurements.getFloat(Measurements::Temperature, false, 2)) /
2.0f; 2.0f;
hum = (measurements.getValueFloat(Measurements::AgValueType::Humidity, false, 1) + hum = (measurements.getFloat(Measurements::Humidity, false, 1) +
measurements.getValueFloat(Measurements::AgValueType::Humidity, false, 2)) / measurements.getFloat(Measurements::Humidity, false, 2)) /
2.0f; 2.0f;
} else if (newPMS1Value) { } else if (newPMS1Value) {
// Only PMS1 has new valid value // Only PMS1 has new valid value
temp = measurements.getValueFloat(Measurements::AgValueType::Temperature, false, 1); temp = measurements.getFloat(Measurements::Temperature, false, 1);
hum = measurements.getValueFloat(Measurements::AgValueType::Humidity, false, 1); hum = measurements.getFloat(Measurements::Humidity, false, 1);
} else { } else {
// Only PMS2 has new valid value // Only PMS2 has new valid value
temp = measurements.getValueFloat(Measurements::AgValueType::Temperature, false, 2); temp = measurements.getFloat(Measurements::Temperature, false, 2);
hum = measurements.getValueFloat(Measurements::AgValueType::Humidity, false, 2); hum = measurements.getFloat(Measurements::Humidity, false, 2);
} }
// Update compensation temperature and humidity for SGP41 // Update compensation temperature and humidity for SGP41
@ -1146,7 +1121,7 @@ static void sendDataToServer(void) {
return; return;
} }
String syncData = measurements.toStringX(false, fwMode, wifiConnector.RSSI(), *ag, configuration); String syncData = measurements.toString(false, fwMode, wifiConnector.RSSI(), *ag, configuration);
if (apiClient.postToServer(syncData)) { if (apiClient.postToServer(syncData)) {
Serial.println(); Serial.println();
Serial.println( Serial.println(
@ -1162,8 +1137,8 @@ static void tempHumUpdate(void) {
float temp = ag->sht.getTemperature(); float temp = ag->sht.getTemperature();
float rhum = ag->sht.getRelativeHumidity(); float rhum = ag->sht.getRelativeHumidity();
measurements.updateValue(Measurements::AgValueType::Temperature, temp); measurements.update(Measurements::Temperature, temp);
measurements.updateValue(Measurements::AgValueType::Humidity, rhum); measurements.update(Measurements::Humidity, rhum);
// Serial.printf("Temperature in C: %0.2f\n", temp); // Serial.printf("Temperature in C: %0.2f\n", temp);
// Serial.printf("Relative Humidity: %d\n", rhum); // Serial.printf("Relative Humidity: %d\n", rhum);
@ -1175,9 +1150,8 @@ static void tempHumUpdate(void) {
ag->sgp41.setCompensationTemperatureHumidity(temp, rhum); ag->sgp41.setCompensationTemperatureHumidity(temp, rhum);
} }
} else { } else {
measurements.updateValue(Measurements::AgValueType::Temperature, measurements.update(Measurements::Temperature, utils::getInvalidTemperature());
utils::getInvalidTemperature()); measurements.update(Measurements::Humidity, utils::getInvalidHumidity());
measurements.updateValue(Measurements::AgValueType::Humidity, utils::getInvalidHumidity());
Serial.println("SHT read failed"); Serial.println("SHT read failed");
} }
} }

View File

@ -1,55 +1,54 @@
#include "AgValue.h" #include "AgValue.h"
#include "AgConfigure.h" #include "AgConfigure.h"
#include "AirGradient.h" #include "AirGradient.h"
#include "Libraries/Arduino_JSON/src/Arduino_JSON.h"
#define json_prop_pmFirmware "firmware" #define json_prop_pmFirmware "firmware"
void Measurements::maxUpdate(AgValueType type, int max) { void Measurements::maxUpdate(MeasurementType type, int max) {
switch (type) { switch (type) {
case AgValueType::Temperature: case Temperature:
_temperature[0].update.max = max; _temperature[0].update.max = max;
_temperature[1].update.max = max; _temperature[1].update.max = max;
break; break;
case AgValueType::Humidity: case Humidity:
_humidity[0].update.max = max; _humidity[0].update.max = max;
_humidity[1].update.max = max; _humidity[1].update.max = max;
break; break;
case AgValueType::CO2: case CO2:
_co2.update.max = max; _co2.update.max = max;
break; break;
case AgValueType::TVOC: case TVOC:
_tvoc.update.max = max; _tvoc.update.max = max;
break; break;
case AgValueType::TVOCRaw: case TVOCRaw:
_tvoc_raw.update.max = max; _tvoc_raw.update.max = max;
break; break;
case AgValueType::NOx: case NOx:
_nox.update.max = max; _nox.update.max = max;
break; break;
case AgValueType::NOxRaw: case NOxRaw:
_nox_raw.update.max = max; _nox_raw.update.max = max;
break; break;
case AgValueType::PM25: case PM25:
_pm_25[0].update.max = max; _pm_25[0].update.max = max;
_pm_25[1].update.max = max; _pm_25[1].update.max = max;
break; break;
case AgValueType::PM01: case PM01:
_pm_01[0].update.max = max; _pm_01[0].update.max = max;
_pm_01[1].update.max = max; _pm_01[1].update.max = max;
break; break;
case AgValueType::PM10: case PM10:
_pm_10[0].update.max = max; _pm_10[0].update.max = max;
_pm_10[1].update.max = max; _pm_10[1].update.max = max;
break; break;
case AgValueType::PM03_PC: case PM03_PC:
_pm_03_pc[0].update.max = max; _pm_03_pc[0].update.max = max;
_pm_03_pc[1].update.max = max; _pm_03_pc[1].update.max = max;
break; break;
}; };
} }
bool Measurements::updateValue(AgValueType type, int val, int ch) { bool Measurements::update(MeasurementType type, int val, int ch) {
// Sanity check to validate channel, assert if invalid // Sanity check to validate channel, assert if invalid
validateChannel(ch); validateChannel(ch);
@ -60,39 +59,39 @@ bool Measurements::updateValue(AgValueType type, int val, int ch) {
IntegerValue *temporary = nullptr; IntegerValue *temporary = nullptr;
float invalidValue = 0; float invalidValue = 0;
switch (type) { switch (type) {
case AgValueType::CO2: case CO2:
temporary = &_co2; temporary = &_co2;
invalidValue = utils::getInvalidCO2(); invalidValue = utils::getInvalidCO2();
break; break;
case AgValueType::TVOC: case TVOC:
temporary = &_tvoc; temporary = &_tvoc;
invalidValue = utils::getInvalidVOC(); invalidValue = utils::getInvalidVOC();
break; break;
case AgValueType::TVOCRaw: case TVOCRaw:
temporary = &_tvoc_raw; temporary = &_tvoc_raw;
invalidValue = utils::getInvalidVOC(); invalidValue = utils::getInvalidVOC();
break; break;
case AgValueType::NOx: case NOx:
temporary = &_nox; temporary = &_nox;
invalidValue = utils::getInvalidNOx(); invalidValue = utils::getInvalidNOx();
break; break;
case AgValueType::NOxRaw: case NOxRaw:
temporary = &_nox_raw; temporary = &_nox_raw;
invalidValue = utils::getInvalidNOx(); invalidValue = utils::getInvalidNOx();
break; break;
case AgValueType::PM25: case PM25:
temporary = &_pm_25[ch]; temporary = &_pm_25[ch];
invalidValue = utils::getInvalidPmValue(); invalidValue = utils::getInvalidPmValue();
break; break;
case AgValueType::PM01: case PM01:
temporary = &_pm_01[ch]; temporary = &_pm_01[ch];
invalidValue = utils::getInvalidPmValue(); invalidValue = utils::getInvalidPmValue();
break; break;
case AgValueType::PM10: case PM10:
temporary = &_pm_10[ch]; temporary = &_pm_10[ch];
invalidValue = utils::getInvalidPmValue(); invalidValue = utils::getInvalidPmValue();
break; break;
case AgValueType::PM03_PC: case PM03_PC:
temporary = &_pm_03_pc[ch]; temporary = &_pm_03_pc[ch];
invalidValue = utils::getInvalidPmValue(); invalidValue = utils::getInvalidPmValue();
break; break;
@ -100,9 +99,9 @@ bool Measurements::updateValue(AgValueType type, int val, int ch) {
break; break;
}; };
// Sanity check if agvaluetype is defined for integer data type or not // Sanity check if measurement type is defined for integer data type or not
if (temporary == nullptr) { if (temporary == nullptr) {
Serial.printf("%s is not defined for integer data type\n", agValueTypeStr(type)); Serial.printf("%s is not defined for integer data type\n", measurementTypeStr(type));
// TODO: Just assert? // TODO: Just assert?
return false; return false;
} }
@ -125,13 +124,13 @@ bool Measurements::updateValue(AgValueType type, int val, int ch) {
// TODO: Need to check if SUCCESS == 0, what should we do? // TODO: Need to check if SUCCESS == 0, what should we do?
// Calculate the average // Calculate the average
temporary->avg = temporary->sumValues / temporary->update.success; temporary->avg = temporary->sumValues / temporary->update.success;
Serial.printf("%s{%d} count reached! Average value %d\n", agValueTypeStr(type), ch, Serial.printf("%s{%d} count reached! Average value %d\n", measurementTypeStr(type), ch,
temporary->avg); temporary->avg);
// Notify if there's are invalid value when updating // Notify if there's are invalid value when updating
int miss = temporary->update.max - temporary->update.success; int miss = temporary->update.max - temporary->update.success;
if (miss != 0) { if (miss != 0) {
Serial.printf("%s{%d} has %d invalid value out of %d update\n", agValueTypeStr(type), ch, Serial.printf("%s{%d} has %d invalid value out of %d update\n", measurementTypeStr(type), ch,
miss, temporary->update.max); miss, temporary->update.max);
} }
@ -145,7 +144,7 @@ bool Measurements::updateValue(AgValueType type, int val, int ch) {
return false; return false;
} }
bool Measurements::updateValue(AgValueType type, float val, int ch) { bool Measurements::update(MeasurementType type, float val, int ch) {
// Sanity check to validate channel, assert if invalid // Sanity check to validate channel, assert if invalid
validateChannel(ch); validateChannel(ch);
@ -156,11 +155,11 @@ bool Measurements::updateValue(AgValueType type, float val, int ch) {
FloatValue *temporary = nullptr; FloatValue *temporary = nullptr;
float invalidValue = 0; float invalidValue = 0;
switch (type) { switch (type) {
case AgValueType::Temperature: case Temperature:
temporary = &_temperature[ch]; temporary = &_temperature[ch];
invalidValue = utils::getInvalidTemperature(); invalidValue = utils::getInvalidTemperature();
break; break;
case AgValueType::Humidity: case Humidity:
temporary = &_humidity[ch]; temporary = &_humidity[ch];
invalidValue = utils::getInvalidHumidity(); invalidValue = utils::getInvalidHumidity();
break; break;
@ -168,9 +167,9 @@ bool Measurements::updateValue(AgValueType type, float val, int ch) {
break; break;
} }
// Sanity check if agvaluetype is defined for float data type or not // Sanity check if measurement type is defined for float data type or not
if (temporary == nullptr) { if (temporary == nullptr) {
Serial.printf("%s is not defined for float data type\n", agValueTypeStr(type)); Serial.printf("%s is not defined for float data type\n", measurementTypeStr(type));
// TODO: Just assert? // TODO: Just assert?
return false; return false;
} }
@ -193,13 +192,13 @@ bool Measurements::updateValue(AgValueType type, float val, int ch) {
// TODO: Need to check if SUCCESS == 0 // TODO: Need to check if SUCCESS == 0
// Calculate the average // Calculate the average
temporary->avg = temporary->sumValues / temporary->update.success; temporary->avg = temporary->sumValues / temporary->update.success;
Serial.printf("%s{%d} count reached! Average value %0.2f\n", agValueTypeStr(type), ch, Serial.printf("%s{%d} count reached! Average value %0.2f\n", measurementTypeStr(type), ch,
temporary->avg); temporary->avg);
// This is just for notifying // This is just for notifying
int miss = temporary->update.max - temporary->update.success; int miss = temporary->update.max - temporary->update.success;
if (miss != 0) { if (miss != 0) {
Serial.printf("%s{%d} has %d invalid value out of %d update\n", agValueTypeStr(type), ch, Serial.printf("%s{%d} has %d invalid value out of %d update\n", measurementTypeStr(type), ch,
miss, temporary->update.max); miss, temporary->update.max);
} }
@ -213,7 +212,7 @@ bool Measurements::updateValue(AgValueType type, float val, int ch) {
return false; return false;
} }
int Measurements::getValue(AgValueType type, bool average, int ch) { int Measurements::get(MeasurementType type, bool average, int ch) {
// Sanity check to validate channel, assert if invalid // Sanity check to validate channel, assert if invalid
validateChannel(ch); validateChannel(ch);
@ -224,40 +223,40 @@ int Measurements::getValue(AgValueType type, bool average, int ch) {
IntegerValue *temporary = nullptr; IntegerValue *temporary = nullptr;
float invalidValue = 0; float invalidValue = 0;
switch (type) { switch (type) {
case AgValueType::CO2: case CO2:
temporary = &_co2; temporary = &_co2;
break; break;
case AgValueType::TVOC: case TVOC:
temporary = &_tvoc; temporary = &_tvoc;
break; break;
case AgValueType::TVOCRaw: case TVOCRaw:
temporary = &_tvoc_raw; temporary = &_tvoc_raw;
break; break;
case AgValueType::NOx: case NOx:
temporary = &_nox; temporary = &_nox;
break; break;
case AgValueType::NOxRaw: case NOxRaw:
temporary = &_nox_raw; temporary = &_nox_raw;
break; break;
case AgValueType::PM25: case PM25:
temporary = &_pm_25[ch]; temporary = &_pm_25[ch];
break; break;
case AgValueType::PM01: case PM01:
temporary = &_pm_01[ch]; temporary = &_pm_01[ch];
break; break;
case AgValueType::PM10: case PM10:
temporary = &_pm_10[ch]; temporary = &_pm_10[ch];
break; break;
case AgValueType::PM03_PC: case PM03_PC:
temporary = &_pm_03_pc[ch]; temporary = &_pm_03_pc[ch];
break; break;
default: default:
break; break;
}; };
// Sanity check if agvaluetype is defined for integer data type or not // Sanity check if measurement type is defined for integer data type or not
if (temporary == nullptr) { if (temporary == nullptr) {
Serial.printf("%s is not defined for integer data type\n", agValueTypeStr(type)); Serial.printf("%s is not defined for integer data type\n", measurementTypeStr(type));
// TODO: Just assert? // TODO: Just assert?
return false; return false;
} }
@ -269,26 +268,29 @@ int Measurements::getValue(AgValueType type, bool average, int ch) {
return temporary->lastValue; return temporary->lastValue;
} }
float Measurements::getValueFloat(AgValueType type, bool average, int ch) { float Measurements::getFloat(MeasurementType type, bool average, int ch) {
// Sanity check to validate channel, assert if invalid // Sanity check to validate channel, assert if invalid
validateChannel(ch); validateChannel(ch);
// Follow array indexing just for get address of the value type
ch = ch - 1;
// Define data point source // Define data point source
FloatValue *temporary = nullptr; FloatValue *temporary = nullptr;
switch (type) { switch (type) {
case AgValueType::Temperature: case Temperature:
temporary = &_temperature[ch]; temporary = &_temperature[ch];
break; break;
case AgValueType::Humidity: case Humidity:
temporary = &_humidity[ch]; temporary = &_humidity[ch];
break; break;
default: default:
break; break;
} }
// Sanity check if agvaluetype is defined for float data type or not // Sanity check if measurement type is defined for float data type or not
if (temporary == nullptr) { if (temporary == nullptr) {
Serial.printf("%s is not defined for float data type\n", agValueTypeStr(type)); Serial.printf("%s is not defined for float data type\n", measurementTypeStr(type));
// TODO: Just assert? // TODO: Just assert?
return false; return false;
} }
@ -300,382 +302,6 @@ float Measurements::getValueFloat(AgValueType type, bool average, int ch) {
return temporary->lastValue; return temporary->lastValue;
} }
String Measurements::toString(bool localServer, AgFirmwareMode fwMode, int rssi, void *_ag,
void *_config) {
AirGradient *ag = (AirGradient *)_ag;
Configuration *config = (Configuration *)_config;
JSONVar root;
root["wifi"] = rssi;
if (localServer) {
root["serialno"] = ag->deviceId();
}
if (config->hasSensorS8 && utils::isValidCO2(this->CO2)) {
root["rco2"] = this->CO2;
}
if (ag->isOne() || (ag->isPro4_2()) || ag->isPro3_3() || ag->isBasic()) {
if (config->hasSensorPMS1) {
if (utils::isValidPm(this->pm01_1)) {
root["pm01"] = this->pm01_1;
}
if (utils::isValidPm(this->pm25_1)) {
root["pm02"] = this->pm25_1;
}
if (utils::isValidPm(this->pm10_1)) {
root["pm10"] = this->pm10_1;
}
if (utils::isValidPm03Count(this->pm03PCount_1)) {
root["pm003Count"] = this->pm03PCount_1;
}
if (!localServer) {
root[json_prop_pmFirmware] =
this->pms5003FirmwareVersion(ag->pms5003.getFirmwareVersion());
}
}
if (config->hasSensorSHT) {
if (utils::isValidTemperature(this->Temperature)) {
root["atmp"] = ag->round2(this->Temperature);
if (localServer) {
root["atmpCompensated"] = ag->round2(this->Temperature);
}
}
if (utils::isValidHumidity(this->Humidity)) {
root["rhum"] = this->Humidity;
if (localServer) {
root["rhumCompensated"] = this->Humidity;
}
}
}
if (config->hasSensorSHT && config->hasSensorPMS1) {
int pm25 = ag->pms5003.compensate(this->pm25_1, this->Humidity);
if (pm25 >= 0) {
root["pm02Compensated"] = pm25;
}
}
} else {
if (config->hasSensorPMS1 && config->hasSensorPMS2) {
if (utils::isValidPm(this->pm01_1) && utils::isValidPm(this->pm01_2)) {
root["pm01"] = ag->round2((this->pm01_1 + this->pm01_2) / 2.0f);
}
if (utils::isValidPm(this->pm25_1) && utils::isValidPm(this->pm25_2)) {
root["pm02"] = ag->round2((this->pm25_1 + this->pm25_2) / 2.0f);
}
if (utils::isValidPm(this->pm10_1) && utils::isValidPm(this->pm10_2)) {
root["pm10"] = ag->round2((this->pm10_1 + this->pm10_2) / 2.0f);
}
if (utils::isValidPm(this->pm03PCount_1) && utils::isValidPm(this->pm03PCount_2)) {
root["pm003Count"] = ag->round2((this->pm03PCount_1 + this->pm03PCount_2) / 2.0f);
}
float val;
if (utils::isValidTemperature(this->temp_1) && utils::isValidTemperature(this->temp_1)) {
root["atmp"] = ag->round2((this->temp_1 + this->temp_2) / 2.0f);
if (localServer) {
val = ag->pms5003t_2.compensateTemp((this->temp_1 + this->temp_2) / 2.0f);
if (utils::isValidTemperature(val)) {
root["atmpCompensated"] = ag->round2(val);
}
}
}
if (utils::isValidHumidity(this->hum_1) && utils::isValidHumidity(this->hum_1)) {
root["rhum"] = ag->round2((this->hum_1 + this->hum_2) / 2.0f);
if (localServer) {
val = ag->pms5003t_2.compensateHum((this->hum_1 + this->hum_2) / 2.0f);
if (utils::isValidHumidity(val)) {
root["rhumCompensated"] = (int)val;
}
}
}
int pm25 = (ag->pms5003t_1.compensate(this->pm25_1, this->hum_1) +
ag->pms5003t_2.compensate(this->pm25_2, this->hum_2)) /
2;
root["pm02Compensated"] = pm25;
}
if (fwMode == FW_MODE_O_1PS || fwMode == FW_MODE_O_1PST) {
float val;
if (config->hasSensorPMS1) {
if (utils::isValidPm(this->pm01_1)) {
root["pm01"] = this->pm01_1;
}
if (utils::isValidPm(this->pm25_1)) {
root["pm02"] = this->pm25_1;
}
if (utils::isValidPm(this->pm10_1)) {
root["pm10"] = this->pm10_1;
}
if (utils::isValidPm03Count(this->pm03PCount_1)) {
root["pm003Count"] = this->pm03PCount_1;
}
if (utils::isValidTemperature(this->temp_1)) {
root["atmp"] = ag->round2(this->temp_1);
if (localServer) {
val = ag->pms5003t_1.compensateTemp(this->temp_1);
if (utils::isValidTemperature(val)) {
root["atmpCompensated"] = ag->round2(val);
}
}
}
if (utils::isValidHumidity(this->hum_1)) {
root["rhum"] = this->hum_1;
if (localServer) {
val = ag->pms5003t_1.compensateHum(this->hum_1);
if (utils::isValidHumidity(val)) {
root["rhumCompensated"] = (int)val;
}
}
}
root["pm02Compensated"] = ag->pms5003t_1.compensate(this->pm25_1, this->hum_1);
if (!localServer) {
root[json_prop_pmFirmware] =
pms5003TFirmwareVersion(ag->pms5003t_1.getFirmwareVersion());
}
}
if (config->hasSensorPMS2) {
if(utils::isValidPm(this->pm01_2)) {
root["pm01"] = this->pm01_2;
}
if(utils::isValidPm(this->pm25_2)) {
root["pm02"] = this->pm25_2;
}
if(utils::isValidPm(this->pm10_2)) {
root["pm10"] = this->pm10_2;
}
if(utils::isValidPm03Count(this->pm03PCount_2)) {
root["pm003Count"] = this->pm03PCount_2;
}
float val;
if (utils::isValidTemperature(this->temp_2)) {
root["atmp"] = ag->round2(this->temp_2);
if (localServer) {
val = ag->pms5003t_2.compensateTemp(this->temp_2);
if (utils::isValidTemperature(val)) {
root["atmpCompensated"] = ag->round2(val);
}
}
}
if(utils::isValidHumidity(this->hum_2)) {
root["rhum"] = this->hum_2;
if (localServer) {
val = ag->pms5003t_2.compensateHum(this->hum_2);
if (utils::isValidHumidity(val)) {
root["rhumCompensated"] = (int)val;
}
}
}
root["pm02Compensated"] = ag->pms5003t_2.compensate(this->pm25_2, this->hum_2);
if(!localServer) {
root[json_prop_pmFirmware] =
pms5003TFirmwareVersion(ag->pms5003t_1.getFirmwareVersion());
}
}
} else {
if (fwMode == FW_MODE_O_1P) {
float val;
if (config->hasSensorPMS1) {
if (utils::isValidPm(this->pm01_1)) {
root["pm01"] = this->pm01_1;
}
if (utils::isValidPm(this->pm25_1)) {
root["pm02"] = this->pm25_1;
}
if (utils::isValidPm(this->pm10_1)) {
root["pm10"] = this->pm10_1;
}
if (utils::isValidPm03Count(this->pm03PCount_1)) {
root["pm003Count"] = this->pm03PCount_1;
}
if (utils::isValidTemperature(this->temp_1)) {
root["atmp"] = ag->round2(this->temp_1);
if (localServer) {
val = ag->pms5003t_1.compensateTemp(this->temp_1);
if (utils::isValidTemperature(val)) {
root["atmpCompensated"] = ag->round2(val);
}
}
}
if (utils::isValidHumidity(this->hum_1)) {
root["rhum"] = this->hum_1;
if(localServer) {
val = ag->pms5003t_1.compensateHum(this->hum_1);
if(utils::isValidHumidity(val)) {
root["rhumCompensated"] = (int)val;
}
}
}
root["pm02Compensated"] = ag->pms5003t_1.compensate(this->pm25_1, this->hum_1);
if(!localServer) {
root[json_prop_pmFirmware] =
pms5003TFirmwareVersion(ag->pms5003t_1.getFirmwareVersion());
}
} else if (config->hasSensorPMS2) {
if(utils::isValidPm(this->pm01_2)) {
root["pm01"] = this->pm01_2;
}
if(utils::isValidPm(this->pm25_2)) {
root["pm02"] = this->pm25_2;
}
if(utils::isValidPm(this->pm10_2)) {
root["pm10"] = this->pm10_2;
}
if(utils::isValidPm03Count(this->pm03PCount_2)) {
root["pm003Count"] = this->pm03PCount_2;
}
if (utils::isValidTemperature(this->temp_2)) {
root["atmp"] = ag->round2(this->temp_2);
if (localServer) {
val = ag->pms5003t_1.compensateTemp(this->temp_2);
if (utils::isValidTemperature(val)) {
root["atmpCompensated"] = ag->round2(val);
}
}
}
if (utils::isValidHumidity(this->hum_2)) {
root["rhum"] = this->hum_2;
if(localServer) {
val = ag->pms5003t_1.compensateHum(this->hum_2);
if(utils::isValidHumidity(val)) {
root["rhumCompensated"] = (int)val;
}
}
}
root["pm02Compensated"] = ag->pms5003t_1.compensate(this->pm25_1, this->hum_1);
if(!localServer) {
root[json_prop_pmFirmware] =
pms5003TFirmwareVersion(ag->pms5003t_2.getFirmwareVersion());
}
}
} else {
float val;
if (config->hasSensorPMS1) {
if(utils::isValidPm(this->pm01_1)) {
root["channels"]["1"]["pm01"] = this->pm01_1;
}
if(utils::isValidPm(this->pm25_1)) {
root["channels"]["1"]["pm02"] = this->pm25_1;
}
if(utils::isValidPm(this->pm10_1)) {
root["channels"]["1"]["pm10"] = this->pm10_1;
}
if (utils::isValidPm03Count(this->pm03PCount_1)) {
root["channels"]["1"]["pm003Count"] = this->pm03PCount_1;
}
if(utils::isValidTemperature(this->temp_1)) {
root["channels"]["1"]["atmp"] = ag->round2(this->temp_1);
if (localServer) {
val = ag->pms5003t_1.compensateTemp(this->temp_1);
if (utils::isValidTemperature(val)) {
root["channels"]["1"]["atmpCompensated"] = ag->round2(val);
}
}
}
if (utils::isValidHumidity(this->hum_1)) {
root["channels"]["1"]["rhum"] = this->hum_1;
if (localServer) {
val = ag->pms5003t_1.compensateHum(this->hum_1);
if (utils::isValidHumidity(val)) {
root["channels"]["1"]["rhumCompensated"] = (int)val;
}
}
}
root["channels"]["1"]["pm02Compensated"] = ag->pms5003t_1.compensate(this->pm25_1, this->hum_1);
// PMS5003T version
if(!localServer) {
root["channels"]["1"][json_prop_pmFirmware] =
pms5003TFirmwareVersion(ag->pms5003t_1.getFirmwareVersion());
}
}
if (config->hasSensorPMS2) {
float val;
if (utils::isValidPm(this->pm01_2)) {
root["channels"]["2"]["pm01"] = this->pm01_2;
}
if (utils::isValidPm(this->pm25_2)) {
root["channels"]["2"]["pm02"] = this->pm25_2;
}
if (utils::isValidPm(this->pm10_2)) {
root["channels"]["2"]["pm10"] = this->pm10_2;
}
if (utils::isValidPm03Count(this->pm03PCount_2)) {
root["channels"]["2"]["pm003Count"] = this->pm03PCount_2;
}
if (utils::isValidTemperature(this->temp_2)) {
root["channels"]["2"]["atmp"] = ag->round2(this->temp_2);
if (localServer) {
val = ag->pms5003t_1.compensateTemp(this->temp_2);
if (utils::isValidTemperature(val)) {
root["channels"]["2"]["atmpCompensated"] = ag->round2(val);
}
}
}
if (utils::isValidHumidity(this->hum_2)) {
root["channels"]["2"]["rhum"] = this->hum_2;
if (localServer) {
val = ag->pms5003t_1.compensateHum(this->hum_2);
if (utils::isValidHumidity(val)) {
root["channels"]["2"]["rhumCompensated"] = (int)val;
}
}
}
root["channels"]["2"]["pm02Compensated"] = ag->pms5003t_2.compensate(this->pm25_2, this->hum_2);
// PMS5003T version
if(!localServer) {
root["channels"]["2"][json_prop_pmFirmware] =
pms5003TFirmwareVersion(ag->pms5003t_2.getFirmwareVersion());
}
}
}
}
}
if (config->hasSensorSGP) {
if (utils::isValidVOC(this->TVOC)) {
root["tvocIndex"] = this->TVOC;
}
if (utils::isValidVOC(this->TVOCRaw)) {
root["tvocRaw"] = this->TVOCRaw;
}
if (utils::isValidNOx(this->NOx)) {
root["noxIndex"] = this->NOx;
}
if (utils::isValidNOx(this->NOxRaw)) {
root["noxRaw"] = this->NOxRaw;
}
}
root["boot"] = bootCount;
root["bootCount"] = bootCount;
if (localServer) {
if (ag->isOne()) {
root["ledMode"] = config->getLedBarModeName();
}
root["firmware"] = ag->getVersion();
root["model"] = AgFirmwareModeName(fwMode);
}
return JSON.stringify(root);
}
String Measurements::pms5003FirmwareVersion(int fwCode) { String Measurements::pms5003FirmwareVersion(int fwCode) {
return pms5003FirmwareVersionBase("PMS5003x", fwCode); return pms5003FirmwareVersionBase("PMS5003x", fwCode);
} }
@ -688,40 +314,40 @@ String Measurements::pms5003FirmwareVersionBase(String prefix, int fwCode) {
return prefix + String("-") + String(fwCode); return prefix + String("-") + String(fwCode);
} }
String Measurements::agValueTypeStr(AgValueType type) { String Measurements::measurementTypeStr(MeasurementType type) {
String str; String str;
switch (type) { switch (type) {
case AgValueType::Temperature: case Temperature:
str = "Temperature"; str = "Temperature";
break; break;
case AgValueType::Humidity: case Humidity:
str = "Humidity"; str = "Humidity";
break; break;
case AgValueType::CO2: case CO2:
str = "CO2"; str = "CO2";
break; break;
case AgValueType::TVOC: case TVOC:
str = "TVOC"; str = "TVOC";
break; break;
case AgValueType::TVOCRaw: case TVOCRaw:
str = "TVOCRaw"; str = "TVOCRaw";
break; break;
case AgValueType::NOx: case NOx:
str = "NOx"; str = "NOx";
break; break;
case AgValueType::NOxRaw: case NOxRaw:
str = "NOxRaw"; str = "NOxRaw";
break; break;
case AgValueType::PM25: case PM25:
str = "PM25"; str = "PM25";
break; break;
case AgValueType::PM01: case PM01:
str = "PM01"; str = "PM01";
break; break;
case AgValueType::PM10: case PM10:
str = "PM10"; str = "PM10";
break; break;
case AgValueType::PM03_PC: case PM03_PC:
str = "PM03"; str = "PM03";
break; break;
default: default:
@ -739,8 +365,8 @@ void Measurements::validateChannel(int ch) {
} }
} }
String Measurements::toStringX(bool localServer, AgFirmwareMode fwMode, int rssi, AirGradient &ag, String Measurements::toString(bool localServer, AgFirmwareMode fwMode, int rssi, AirGradient &ag,
Configuration &config) { Configuration &config) {
JSONVar root; JSONVar root;
if (ag.isOne() || (ag.isPro4_2()) || ag.isPro3_3() || ag.isBasic()) { if (ag.isOne() || (ag.isPro4_2()) || ag.isPro3_3() || ag.isBasic()) {

View File

@ -35,32 +35,11 @@ private:
}; };
public: public:
Measurements() { Measurements() {}
pm25_1 = -1;
pm01_1 = -1;
pm10_1 = -1;
pm03PCount_1 = -1;
temp_1 = -1001;
hum_1 = -1;
pm25_2 = -1;
pm01_2 = -1;
pm10_2 = -1;
pm03PCount_2 = -1;
temp_2 = -1001;
hum_2 = -1;
Temperature = -1001;
Humidity = -1;
CO2 = -1;
TVOC = -1;
TVOCRaw = -1;
NOx = -1;
NOxRaw = -1;
}
~Measurements() {} ~Measurements() {}
enum class AgValueType { // Enumeration for every AG measurements
enum MeasurementType {
Temperature, Temperature,
Humidity, Humidity,
CO2, CO2,
@ -75,40 +54,40 @@ public:
}; };
/** /**
* @brief Set each AgValueType maximum update for a value type before calculate the average * @brief Set each MeasurementType maximum update before calculate the average
* *
* @param type the target value type to set * @param type the target measurement type to set
* @param max the maximum counter * @param max the maximum counter
*/ */
void maxUpdate(AgValueType type, int max); void maxUpdate(MeasurementType, int max);
/** /**
* @brief update target type value with new value. * @brief update target measurement type with new value.
* Each AgValueType has last raw value and last average that are calculated based on max number of * Each MeasurementType has last raw value and last average that are calculated based on max
* update. This function is for AgValueType that use INT as the data type * number of update. This function is for MeasurementType that use INT as the data type
* *
* @param type (AgValueType) value type that will be updated * @param type measurement type that will be updated
* @param val (int) the new value * @param val (int) the new value
* @param ch (int) the AgValueType channel, not every AgValueType has more than 1 channel. * @param ch (int) the MeasurementType channel, not every MeasurementType has more than 1 channel.
* Currently maximum channel is 2. Default: 1 (channel 1) * Currently maximum channel is 2. Default: 1 (channel 1)
* @return true if update counter reached and new average value is calculated * @return true if update counter reached and new average value is calculated
* @return false otherwise * @return false otherwise
*/ */
bool updateValue(AgValueType type, int val, int ch = 1); bool update(MeasurementType type, int val, int ch = 1);
/** /**
* @brief update target type value with new value. * @brief update target measurement type with new value.
* Each AgValueType has last raw value and last average that are calculated based on max number of * Each MeasurementType has last raw value and last average that are calculated based on max
* update. This function is for AgValueType that use FLOAT as the data type * number of update. This function is for MeasurementType that use FLOAT as the data type
* *
* @param type (AgValueType) value type that will be updated * @param type measurement type that will be updated
* @param val (float) the new value * @param val (float) the new value
* @param ch (int) the AgValueType channel, not every AgValueType has more than 1 channel. * @param ch (int) the MeasurementType channel, not every MeasurementType has more than 1 channel.
* Currently maximum channel is 2. Default: 1 (channel 1) * Currently maximum channel is 2. Default: 1 (channel 1)
* @return true if update counter reached and new average value is calculated * @return true if update counter reached and new average value is calculated
* @return false otherwise * @return false otherwise
*/ */
bool updateValue(AgValueType type, float val, int ch = 1); bool update(MeasurementType type, float val, int ch = 1);
/** /**
* @brief Get the target measurement type value * @brief Get the target measurement type value
@ -118,7 +97,7 @@ public:
* @param ch target type value channel * @param ch target type value channel
* @return int measurement type value * @return int measurement type value
*/ */
int getValue(AgValueType type, bool average = true, int ch = 1); int get(MeasurementType type, bool average = true, int ch = 1);
/** /**
* @brief Get the target measurement type value * @brief Get the target measurement type value
@ -128,49 +107,13 @@ public:
* @param ch target type value channel * @param ch target type value channel
* @return float measurement type value * @return float measurement type value
*/ */
float getValueFloat(AgValueType type, bool average = true, int ch = 1); float getFloat(MeasurementType type, bool average = true, int ch = 1);
float Temperature; // TODO: update this to using setter
int Humidity;
int CO2;
int TVOC;
int TVOCRaw;
int NOx;
int NOxRaw;
int pm25_1;
int pm01_1;
int pm10_1;
int pm03PCount_1;
float temp_1;
int hum_1;
int pm25_2;
int pm01_2;
int pm10_2;
int pm03PCount_2;
float temp_2;
int hum_2;
int pm1Value01;
int pm1Value25;
int pm1Value10;
int pm1PCount;
int pm1temp;
int pm1hum;
int pm2Value01;
int pm2Value25;
int pm2Value10;
int pm2PCount;
int pm2temp;
int pm2hum;
int countPosition;
const int targetCount = 20;
int bootCount; int bootCount;
String toString(bool isLocal, AgFirmwareMode fwMode, int rssi, void *_ag, void *_config); String toString(bool localServer, AgFirmwareMode fwMode, int rssi, AirGradient &ag,
String toStringX(bool localServer, AgFirmwareMode fwMode, int rssi, AirGradient &ag, Configuration &config);
Configuration &config);
private: private:
// Some declared as an array (channel), because FW_MODE_O_1PPx has two PMS5003T // Some declared as an array (channel), because FW_MODE_O_1PPx has two PMS5003T
@ -212,7 +155,7 @@ private:
/** /**
* Convert AgValue Type to string representation of the value * Convert AgValue Type to string representation of the value
*/ */
String agValueTypeStr(AgValueType type); String measurementTypeStr(MeasurementType type);
/** /**
* @brief check if provided channel is a valid channel or not * @brief check if provided channel is a valid channel or not