updateValue return bool to indicate max average is set or not

Add more comments
Update naming
This commit is contained in:
samuelbles07
2024-10-11 22:05:03 +07:00
parent 9c09b82efd
commit 2a5cf78b68
2 changed files with 67 additions and 40 deletions

View File

@ -79,7 +79,7 @@ String Measurements::agValueTypeStr(AgValueType type) {
return str; return str;
} }
void Measurements::updateValue(AgValueType type, int val) { bool Measurements::updateValue(AgValueType type, int val) {
// Define data point source // Define data point source
IntegerValue *temporary = nullptr; IntegerValue *temporary = nullptr;
float invalidValue = 0; float invalidValue = 0;
@ -135,32 +135,35 @@ void Measurements::updateValue(AgValueType type, int val) {
if (val != invalidValue) { if (val != invalidValue) {
temporary->lastValue = val; temporary->lastValue = val;
temporary->sumValues = temporary->sumValues + val; temporary->sumValues = temporary->sumValues + val;
temporary->read.success = temporary->read.success + 1; temporary->update.success = temporary->update.success + 1;
} }
// Increment read counter // Increment update.counter
temporary->read.counter = temporary->read.counter + 1; temporary->update.counter = temporary->update.counter + 1;
// Calculate value average when maximum set is reached // Calculate value average when maximum set is reached
if (temporary->read.counter >= temporary->read.max) { if (temporary->update.counter >= temporary->update.max) {
// Calculate the average // Calculate the average
temporary->avg = temporary->sumValues / temporary->read.success; temporary->avg = temporary->sumValues / temporary->update.success;
// This is just for notifying // This is just for notifying
int miss = temporary->read.max - temporary->read.success; int miss = temporary->update.max - temporary->update.success;
if (miss != 0) { if (miss != 0) {
Serial.printf("%s reading miss %d out of %d update\n", agValueTypeStr(type), miss, Serial.printf("%s update.ng miss %d out of %d update\n", agValueTypeStr(type), miss,
temporary->read.max); temporary->update.max);
} }
// Resets the sum data and read variables // Resets average related variable calculation
temporary->sumValues = 0; temporary->sumValues = 0;
temporary->read.counter = 0; temporary->update.counter = 0;
temporary->read.success = 0; temporary->update.success = 0;
return true;
} }
return false;
} }
void Measurements::updateValue(AgValueType type, float val) { bool Measurements::updateValue(AgValueType type, float val) {
// Define data point source // Define data point source
FloatValue *temporary = nullptr; FloatValue *temporary = nullptr;
float invalidValue = 0; float invalidValue = 0;
@ -188,29 +191,32 @@ void Measurements::updateValue(AgValueType type, float val) {
if (val != invalidValue) { if (val != invalidValue) {
temporary->lastValue = val; temporary->lastValue = val;
temporary->sumValues = temporary->sumValues + val; temporary->sumValues = temporary->sumValues + val;
temporary->read.success = temporary->read.success + 1; temporary->update.success = temporary->update.success + 1;
} }
// Increment read counter // Increment update.counter
temporary->read.counter = temporary->read.counter + 1; temporary->update.counter = temporary->update.counter + 1;
// Calculate value average when maximum set is reached // Calculate value average when maximum set is reached
if (temporary->read.counter >= temporary->read.max) { if (temporary->update.counter >= temporary->update.max) {
// Calculate the average // Calculate the average
temporary->avg = temporary->sumValues / temporary->read.success; temporary->avg = temporary->sumValues / temporary->update.success;
// This is just for notifying // This is just for notifying
int miss = temporary->read.max - temporary->read.success; int miss = temporary->update.max - temporary->update.success;
if (miss != 0) { if (miss != 0) {
Serial.printf("%s reading miss %d out of %d update\n", agValueTypeStr(type), miss, Serial.printf("%s update.ng miss %d out of %d update\n", agValueTypeStr(type), miss,
temporary->read.max); temporary->update.max);
} }
// Resets the sum data and read variables // Resets average related variable calculation
temporary->sumValues = 0; temporary->sumValues = 0;
temporary->read.counter = 0; temporary->update.counter = 0;
temporary->read.success = 0; temporary->update.success = 0;
return true;
} }
return false;
} }
String Measurements::toString(bool localServer, AgFirmwareMode fwMode, int rssi, String Measurements::toString(bool localServer, AgFirmwareMode fwMode, int rssi,

View File

@ -7,28 +7,28 @@
class Measurements { class Measurements {
private: private:
// Generic struct for reading indication for respective value // Generic struct for update indication for respective value
// TODO: Reading naming is confusing, because its not actually reading but updating with new value struct Update {
struct Reading { int counter; // How many update attempts done
int counter; // How many reading attempts done int success; // How many update value that actually give valid value
int success; // How many reading that success from each attempts int max; // Maximum update before calculating average
int max; // Maximum reading attempt
}; };
// Reading type for sensor value that outputs float // Reading type for sensor value that outputs float
struct FloatValue { struct FloatValue {
float lastValue; // Last reading value float lastValue; // Last update value
float sumValues; // Total value of each reading float sumValues; // Total value from each update
float avg; // The last average calculation after maximum reading attempt reached float avg; // The last average calculation after maximum update attempt reached
Reading read; Update update;
}; };
// Reading type for sensor value that outputs integer // Reading type for sensor value that outputs integer
struct IntegerValue { struct IntegerValue {
int lastValue; // Last reading value int lastValue; // Last update value
unsigned long sumValues; // Total value of each reading // TODO: explain why unsigned long unsigned long sumValues; // Total value from each update; unsigned long to accomodate TVOx and
int avg; // The last average calculation after maximum reading attempt reached // NOx raw data
Reading read; int avg; // The last average calculation after maximum update attempt reached
Update update;
}; };
public: public:
@ -71,8 +71,29 @@ public:
PM03, PM03,
}; };
void updateValue(AgValueType type, int val); /**
void updateValue(AgValueType type, float val); * @brief update target type value with new value.
* Each AgValueType has last raw value and last average that are calculated based on max number of
* set This function is for AgValueType that use INT as the data type
*
* @param type (AgValueType) value type that will be updated
* @param val (int) the new value
* @return true if update counter reached and new average value is calculated
* @return false otherwise
*/
bool updateValue(AgValueType type, int val);
/**
* @brief update target type value with new value.
* Each AgValueType has last raw value and last average that are calculated based on max number of
* set This function is for AgValueType that use FLOAT as the data type
*
* @param type (AgValueType) value type that will be updated
* @param val (float) the new value
* @return true if update counter reached and new average value is calculated
* @return false otherwise
*/
bool updateValue(AgValueType type, float val);
float Temperature; float Temperature;
int Humidity; int Humidity;