Skip to content

Commit d5dd618

Browse files
Copilotdorkmo
andcommitted
Fix code review issues in calibration learning system
Co-authored-by: dorkmo <[email protected]>
1 parent bc85658 commit d5dd618

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

TankAlarm-112025-Server-BluesOpta/TankAlarm-112025-Server-BluesOpta.ino

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7157,7 +7157,11 @@ static TankCalibration *findOrCreateTankCalibration(const char *clientUid, uint8
71577157
// Simple linear regression using calibration log entries
71587158
// Returns true if enough data points exist for valid calibration
71597159
static void recalculateCalibration(TankCalibration *cal) {
7160-
if (!cal || cal->entryCount < 2) {
7160+
if (!cal) {
7161+
return;
7162+
}
7163+
7164+
if (cal->entryCount < 2) {
71617165
cal->hasLearnedCalibration = false;
71627166
return;
71637167
}
@@ -7362,7 +7366,7 @@ static void saveCalibrationEntry(const char *clientUid, uint8_t tankNumber, doub
73627366
// Update calibration for this tank
73637367
TankCalibration *cal = findOrCreateTankCalibration(clientUid, tankNumber);
73647368
if (cal) {
7365-
cal->entryCount++;
7369+
// recalculateCalibration will read the file and update entryCount
73667370
recalculateCalibration(cal);
73677371
saveCalibrationData();
73687372
}
@@ -7415,7 +7419,7 @@ static void loadCalibrationData() {
74157419
if (pos7 < 0) continue;
74167420
cal.hasLearnedCalibration = line.substring(pos6 + 1, pos7).toInt() == 1;
74177421

7418-
cal.lastCalibrationEpoch = line.substring(pos7 + 1).toFloat();
7422+
cal.lastCalibrationEpoch = atof(line.substring(pos7 + 1).c_str());
74197423

74207424
gTankCalibrationCount++;
74217425
}
@@ -7463,7 +7467,7 @@ static void loadCalibrationData() {
74637467
if (pos7 < 0) continue;
74647468
cal.hasLearnedCalibration = line.substring(pos6 + 1, pos7).toInt() == 1;
74657469

7466-
cal.lastCalibrationEpoch = line.substring(pos7 + 1).toFloat();
7470+
cal.lastCalibrationEpoch = atof(line.substring(pos7 + 1).c_str());
74677471

74687472
gTankCalibrationCount++;
74697473
}
@@ -7565,7 +7569,7 @@ static void handleCalibrationGet(EthernetClient &client) {
75657569

75667570
int pos3 = line.indexOf('\t', pos2 + 1);
75677571
if (pos3 < 0) continue;
7568-
double timestamp = line.substring(pos2 + 1, pos3).toFloat();
7572+
double timestamp = atof(line.substring(pos2 + 1, pos3).c_str());
75697573

75707574
int pos4 = line.indexOf('\t', pos3 + 1);
75717575
if (pos4 < 0) continue;
@@ -7613,7 +7617,7 @@ static void handleCalibrationGet(EthernetClient &client) {
76137617

76147618
int pos3 = line.indexOf('\t', pos2 + 1);
76157619
if (pos3 < 0) continue;
7616-
double timestamp = line.substring(pos2 + 1, pos3).toFloat();
7620+
double timestamp = atof(line.substring(pos2 + 1, pos3).c_str());
76177621

76187622
int pos4 = line.indexOf('\t', pos3 + 1);
76197623
if (pos4 < 0) continue;
@@ -7690,8 +7694,11 @@ static void handleCalibrationPost(EthernetClient &client, const String &body) {
76907694
gTankRecords[i].tankNumber == tankNumber) {
76917695
// Estimate mA from current level if we have height data
76927696
// This is approximate - better to have actual sensor reading
7693-
if (gTankRecords[i].heightInches > 0.1f) {
7697+
if (gTankRecords[i].heightInches > 1.0f) { // Require at least 1 inch height for safe division
76947698
float percent = gTankRecords[i].levelInches / gTankRecords[i].heightInches;
7699+
// Clamp percent to valid range
7700+
if (percent < 0.0f) percent = 0.0f;
7701+
if (percent > 1.0f) percent = 1.0f;
76957702
sensorReading = 4.0f + percent * 16.0f; // Estimate 4-20mA from percent
76967703
}
76977704
break;

0 commit comments

Comments
 (0)