Provide channel for neccessary ValueType

To support OA that have 2 PMS sensor
This commit is contained in:
samuelbles07
2024-10-13 23:30:03 +07:00
parent ea91cf9b6c
commit 6925b1ac9a
2 changed files with 72 additions and 29 deletions

View File

@@ -8,10 +8,12 @@
void Measurements::maxUpdate(AgValueType type, int max) { void Measurements::maxUpdate(AgValueType type, int max) {
switch (type) { switch (type) {
case AgValueType::Temperature: case AgValueType::Temperature:
_temperature.update.max = max; _temperature[0].update.max = max;
_temperature[1].update.max = max;
break; break;
case AgValueType::Humidity: case AgValueType::Humidity:
_humidity.update.max = max; _humidity[0].update.max = max;
_humidity[1].update.max = max;
break; break;
case AgValueType::CO2: case AgValueType::CO2:
_co2.update.max = max; _co2.update.max = max;
@@ -29,21 +31,35 @@ void Measurements::maxUpdate(AgValueType type, int max) {
_nox_raw.update.max = max; _nox_raw.update.max = max;
break; break;
case AgValueType::PM25: case AgValueType::PM25:
_pm_25.update.max = max; _pm_25[0].update.max = max;
_pm_25[1].update.max = max;
break; break;
case AgValueType::PM01: case AgValueType::PM01:
_pm_01.update.max = max; _pm_01[0].update.max = max;
_pm_01[1].update.max = max;
break; break;
case AgValueType::PM10: case AgValueType::PM10:
_pm_10.update.max = max; _pm_10[0].update.max = max;
_pm_10[1].update.max = max;
break; break;
case AgValueType::PM03_PC: case AgValueType::PM03_PC:
_pm_03_pc.update.max = max; _pm_03_pc[0].update.max = max;
_pm_03_pc[1].update.max = max;
break; break;
}; };
} }
bool Measurements::updateValue(AgValueType type, int val) { bool Measurements::updateValue(AgValueType type, int val, int ch) {
// Validate channel
if (ch != 1 || ch != 2) {
Serial.printf(" Channel %d is undefined. Only channel 1 or 2 is the optional value!", ch);
// TODO: Perhaps just do assert
return false;
}
// Follow array indexing just for get address of the value type
ch = ch - 1;
// Define data point source // Define data point source
IntegerValue *temporary = nullptr; IntegerValue *temporary = nullptr;
float invalidValue = 0; float invalidValue = 0;
@@ -69,19 +85,19 @@ bool Measurements::updateValue(AgValueType type, int val) {
invalidValue = utils::getInvalidNOx(); invalidValue = utils::getInvalidNOx();
break; break;
case AgValueType::PM25: case AgValueType::PM25:
temporary = &_pm_25; temporary = &_pm_25[ch];
invalidValue = utils::getInvalidPmValue(); invalidValue = utils::getInvalidPmValue();
break; break;
case AgValueType::PM01: case AgValueType::PM01:
temporary = &_pm_01; temporary = &_pm_01[ch];
invalidValue = utils::getInvalidPmValue(); invalidValue = utils::getInvalidPmValue();
break; break;
case AgValueType::PM10: case AgValueType::PM10:
temporary = &_pm_10; temporary = &_pm_10[ch];
invalidValue = utils::getInvalidPmValue(); invalidValue = utils::getInvalidPmValue();
break; break;
case AgValueType::PM03_PC: case AgValueType::PM03_PC:
temporary = &_pm_03_pc; temporary = &_pm_03_pc[ch];
invalidValue = utils::getInvalidPmValue(); invalidValue = utils::getInvalidPmValue();
break; break;
default: default:
@@ -92,9 +108,12 @@ bool Measurements::updateValue(AgValueType type, int val) {
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", agValueTypeStr(type));
// TODO: Just assert? // TODO: Just assert?
return; return false;
} }
// Restore channel value for debugging purpose
ch = ch + 1;
// Update new value when value provided is not the invalid one // Update new value when value provided is not the invalid one
if (val != invalidValue) { if (val != invalidValue) {
temporary->lastValue = val; temporary->lastValue = val;
@@ -107,14 +126,17 @@ bool Measurements::updateValue(AgValueType type, int val) {
// Calculate value average when maximum set is reached // Calculate value average when maximum set is reached
if (temporary->update.counter >= temporary->update.max) { if (temporary->update.counter >= temporary->update.max) {
// 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,
temporary->avg);
// This is just for notifying // 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 update.ng miss %d out of %d update\n", agValueTypeStr(type), miss, Serial.printf("%s{%d} has %d invalid value out of %d update\n", agValueTypeStr(type), ch,
temporary->update.max); miss, temporary->update.max);
} }
// Resets average related variable calculation // Resets average related variable calculation
@@ -127,17 +149,27 @@ bool Measurements::updateValue(AgValueType type, int val) {
return false; return false;
} }
bool Measurements::updateValue(AgValueType type, float val) { bool Measurements::updateValue(AgValueType type, float val, int ch) {
// Validate channel
if (ch != 1 || ch != 2) {
Serial.printf(" Channel %d is undefined. Only channel 1 or 2 is the optional value!", ch);
// TODO: Perhaps just do assert
return false;
}
// 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;
float invalidValue = 0; float invalidValue = 0;
switch (type) { switch (type) {
case AgValueType::Temperature: case AgValueType::Temperature:
temporary = &_temperature; temporary = &_temperature[ch];
invalidValue = utils::getInvalidTemperature(); invalidValue = utils::getInvalidTemperature();
break; break;
case AgValueType::Humidity: case AgValueType::Humidity:
temporary = &_humidity; temporary = &_humidity[ch];
invalidValue = utils::getInvalidHumidity(); invalidValue = utils::getInvalidHumidity();
break; break;
default: default:
@@ -148,9 +180,12 @@ bool Measurements::updateValue(AgValueType type, float val) {
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", agValueTypeStr(type));
// TODO: Just assert? // TODO: Just assert?
return; return false;
} }
// Restore channel value for debugging purpose
ch = ch + 1;
// Update new value when value provided is not the invalid one // Update new value when value provided is not the invalid one
if (val != invalidValue) { if (val != invalidValue) {
temporary->lastValue = val; temporary->lastValue = val;
@@ -163,14 +198,17 @@ bool Measurements::updateValue(AgValueType type, float val) {
// Calculate value average when maximum set is reached // Calculate value average when maximum set is reached
if (temporary->update.counter >= temporary->update.max) { if (temporary->update.counter >= temporary->update.max) {
// 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),
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 update.ng miss %d out of %d update\n", agValueTypeStr(type), miss, Serial.printf("%s{%d} has %d invalid value out of %d update\n", agValueTypeStr(type), ch,
temporary->update.max); miss, temporary->update.max);
} }
// Resets average related variable calculation // Resets average related variable calculation

View File

@@ -86,10 +86,12 @@ public:
* *
* @param type (AgValueType) value type that will be updated * @param type (AgValueType) value 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.
* 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); bool updateValue(AgValueType type, int val, int ch = 1);
/** /**
* @brief update target type value with new value. * @brief update target type value with new value.
@@ -98,10 +100,12 @@ public:
* *
* @param type (AgValueType) value type that will be updated * @param type (AgValueType) value 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.
* 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); bool updateValue(AgValueType type, float val, int ch = 1);
float Temperature; float Temperature;
int Humidity; int Humidity;
@@ -144,17 +148,18 @@ public:
String toString(bool isLocal, AgFirmwareMode fwMode, int rssi, void *_ag, void *_config); String toString(bool isLocal, AgFirmwareMode fwMode, int rssi, void *_ag, void *_config);
private: private:
FloatValue _temperature; // Some declared as an array (channel), because FW_MODE_O_1PPx has two PMS5003T
FloatValue _humidity; FloatValue _temperature[2];
FloatValue _humidity[2];
IntegerValue _co2; IntegerValue _co2;
IntegerValue _tvoc; IntegerValue _tvoc;
IntegerValue _tvoc_raw; IntegerValue _tvoc_raw;
IntegerValue _nox; IntegerValue _nox;
IntegerValue _nox_raw; IntegerValue _nox_raw;
IntegerValue _pm_25; IntegerValue _pm_25[2];
IntegerValue _pm_01; IntegerValue _pm_01[2];
IntegerValue _pm_10; IntegerValue _pm_10[2];
IntegerValue _pm_03_pc; // particle count 0.3 IntegerValue _pm_03_pc[2]; // particle count 0.3
/** /**
* @brief Get PMS5003 firmware version string * @brief Get PMS5003 firmware version string