Get value from each data type

This commit is contained in:
samuelbles07
2024-10-14 01:22:44 +07:00
parent 6925b1ac9a
commit 751d4e8380
2 changed files with 101 additions and 2 deletions

View File

@ -221,8 +221,87 @@ bool Measurements::updateValue(AgValueType type, float val, int ch) {
return false;
}
String Measurements::toString(bool localServer, AgFirmwareMode fwMode, int rssi,
void *_ag, void *_config) {
int Measurements::getValue(AgValueType type, bool average, int ch) {
// Follow array indexing just for get address of the value type
ch = ch - 1;
// Define data point source
IntegerValue *temporary = nullptr;
float invalidValue = 0;
switch (type) {
case AgValueType::CO2:
temporary = &_co2;
break;
case AgValueType::TVOC:
temporary = &_tvoc;
break;
case AgValueType::TVOCRaw:
temporary = &_tvoc_raw;
break;
case AgValueType::NOx:
temporary = &_nox;
break;
case AgValueType::NOxRaw:
temporary = &_nox_raw;
break;
case AgValueType::PM25:
temporary = &_pm_25[ch];
break;
case AgValueType::PM01:
temporary = &_pm_01[ch];
break;
case AgValueType::PM10:
temporary = &_pm_10[ch];
break;
case AgValueType::PM03_PC:
temporary = &_pm_03_pc[ch];
break;
default:
break;
};
// Sanity check if agvaluetype is defined for integer data type or not
if (temporary == nullptr) {
Serial.printf("%s is not defined for integer data type\n", agValueTypeStr(type));
// TODO: Just assert?
return false;
}
if (average) {
return temporary->avg;
}
return temporary->lastValue;
}
float Measurements::getValueFloat(AgValueType type, bool average, int ch) {
// Define data point source
FloatValue *temporary = nullptr;
switch (type) {
case AgValueType::Temperature:
temporary = &_temperature[ch];
break;
case AgValueType::Humidity:
temporary = &_humidity[ch];
break;
default:
break;
}
// Sanity check if agvaluetype is defined for float data type or not
if (temporary == nullptr) {
Serial.printf("%s is not defined for float data type\n", agValueTypeStr(type));
// TODO: Just assert?
return false;
}
if (average) {
return temporary->avg;
}
return temporary->lastValue;
}
AirGradient *ag = (AirGradient *)_ag;
Configuration *config = (Configuration *)_config;

View File

@ -107,6 +107,26 @@ public:
*/
bool updateValue(AgValueType type, float val, int ch = 1);
/**
* @brief Get the target measurement type value
*
* @param type measurement type that will be retrieve
* @param average true if expect last average value, false if expect last update value
* @param ch target type value channel
* @return int measurement type value
*/
int getValue(AgValueType type, bool average = true, int ch = 1);
/**
* @brief Get the target measurement type value
*
* @param type measurement type that will be retrieve
* @param average true if expect last average value, false if expect last update value
* @param ch target type value channel
* @return float measurement type value
*/
float getValueFloat(AgValueType type, bool average = true, int ch = 1);
float Temperature;
int Humidity;
int CO2;