Skip to content

Commit

Permalink
refactor: added ID validation for opentherm response
Browse files Browse the repository at this point in the history
  • Loading branch information
Laxilef committed Oct 27, 2024
1 parent f62e687 commit 11b1277
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 12 deletions.
24 changes: 15 additions & 9 deletions lib/CustomOpenTherm/CustomOpenTherm.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class CustomOpenTherm : public OpenTherm {
temperatureToData(temperature)
));

return isValidResponse(response);
return isValidResponse(response) && isValidResponseId(response, OpenThermMessageID::TSet);
}

bool setHeatingCh2Temp(float temperature) {
Expand All @@ -115,7 +115,7 @@ class CustomOpenTherm : public OpenTherm {
temperatureToData(temperature)
));

return isValidResponse(response);
return isValidResponse(response) && isValidResponseId(response, OpenThermMessageID::TsetCH2);
}

bool setDhwTemp(float temperature) {
Expand All @@ -125,7 +125,7 @@ class CustomOpenTherm : public OpenTherm {
temperatureToData(temperature)
));

return isValidResponse(response);
return isValidResponse(response) && isValidResponseId(response, OpenThermMessageID::TdhwSet);
}

bool setRoomSetpoint(float temperature) {
Expand All @@ -135,7 +135,7 @@ class CustomOpenTherm : public OpenTherm {
temperatureToData(temperature)
));

return isValidResponse(response);
return isValidResponse(response) && isValidResponseId(response, OpenThermMessageID::TrSet);
}

bool setRoomSetpointCh2(float temperature) {
Expand All @@ -145,7 +145,7 @@ class CustomOpenTherm : public OpenTherm {
temperatureToData(temperature)
));

return isValidResponse(response);
return isValidResponse(response) && isValidResponseId(response, OpenThermMessageID::TrSetCH2);
}

bool setRoomTemp(float temperature) {
Expand All @@ -155,7 +155,7 @@ class CustomOpenTherm : public OpenTherm {
temperatureToData(temperature)
));

return isValidResponse(response);
return isValidResponse(response) && isValidResponseId(response, OpenThermMessageID::Tr);
}

bool sendBoilerReset() {
Expand All @@ -167,7 +167,7 @@ class CustomOpenTherm : public OpenTherm {
data
));

return isValidResponse(response);
return isValidResponse(response) && isValidResponseId(response, OpenThermMessageID::RemoteRequest);
}

bool sendServiceReset() {
Expand All @@ -179,7 +179,7 @@ class CustomOpenTherm : public OpenTherm {
data
));

return isValidResponse(response);
return isValidResponse(response) && isValidResponseId(response, OpenThermMessageID::RemoteRequest);
}

bool sendWaterFilling() {
Expand All @@ -191,7 +191,13 @@ class CustomOpenTherm : public OpenTherm {
data
));

return isValidResponse(response);
return isValidResponse(response) && isValidResponseId(response, OpenThermMessageID::RemoteRequest);
}

static bool isValidResponseId(unsigned long response, OpenThermMessageID id) {
byte responseId = (response >> 16) & 0xFF;

return (byte)id == responseId;
}

// converters
Expand Down
63 changes: 60 additions & 3 deletions src/OpenThermTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ class OpenThermTask : public Task {
statusLb
);

if (!CustomOpenTherm::isValidResponse(response)) {
if (!CustomOpenTherm::isValidResponse(response) || !CustomOpenTherm::isValidResponseId(response, OpenThermMessageID::Status)) {
Log.swarningln(
FPSTR(L_OT),
F("Failed receive boiler status: %s"),
Expand Down Expand Up @@ -717,6 +717,9 @@ class OpenThermTask : public Task {

if (!CustomOpenTherm::isValidResponse(response)) {
return false;

} else if (!CustomOpenTherm::isValidResponseId(response, OpenThermMessageID::SConfigSMemberIDcode)) {
return false;
}

vars.parameters.slaveMemberId = response & 0xFF;
Expand Down Expand Up @@ -776,7 +779,7 @@ class OpenThermTask : public Task {
request
));

return CustomOpenTherm::isValidResponse(response);
return CustomOpenTherm::isValidResponse(response) && CustomOpenTherm::isValidResponseId(response, OpenThermMessageID::MConfigMMemberIDcode);
}

bool setMaxModulationLevel(byte value) {
Expand All @@ -788,6 +791,9 @@ class OpenThermTask : public Task {

if (!CustomOpenTherm::isValidResponse(response)) {
return false;

} else if (!CustomOpenTherm::isValidResponseId(response, OpenThermMessageID::MaxRelModLevelSetting)) {
return false;
}

vars.parameters.maxModulation = CustomOpenTherm::getFloat(response);
Expand All @@ -803,6 +809,9 @@ class OpenThermTask : public Task {

if (!CustomOpenTherm::isValidResponse(response)) {
return false;

} else if (!CustomOpenTherm::isValidResponseId(response, OpenThermMessageID::OpenThermVersionSlave)) {
return false;
}

vars.parameters.slaveOtVersion = CustomOpenTherm::getFloat(response);
Expand All @@ -818,6 +827,9 @@ class OpenThermTask : public Task {

if (!CustomOpenTherm::isValidResponse(response)) {
return false;

} else if (!CustomOpenTherm::isValidResponseId(response, OpenThermMessageID::OpenThermVersionMaster)) {
return false;
}

vars.parameters.masterOtVersion = CustomOpenTherm::getFloat(response);
Expand All @@ -834,6 +846,9 @@ class OpenThermTask : public Task {

if (!CustomOpenTherm::isValidResponse(response)) {
return false;

} else if (!CustomOpenTherm::isValidResponseId(response, OpenThermMessageID::SlaveVersion)) {
return false;
}

vars.parameters.slaveVersion = response & 0xFF;
Expand All @@ -851,6 +866,9 @@ class OpenThermTask : public Task {

if (!CustomOpenTherm::isValidResponse(response)) {
return false;

} else if (!CustomOpenTherm::isValidResponseId(response, OpenThermMessageID::MasterVersion)) {
return false;
}

vars.parameters.masterVersion = response & 0xFF;
Expand All @@ -868,6 +886,9 @@ class OpenThermTask : public Task {

if (!CustomOpenTherm::isValidResponse(response)) {
return false;

} else if (!CustomOpenTherm::isValidResponseId(response, OpenThermMessageID::TdhwSetUBTdhwSetLB)) {
return false;
}

byte minTemp = response & 0xFF;
Expand All @@ -892,6 +913,9 @@ class OpenThermTask : public Task {

if (!CustomOpenTherm::isValidResponse(response)) {
return false;

} else if (!CustomOpenTherm::isValidResponseId(response, OpenThermMessageID::MaxTSetUBMaxTSetLB)) {
return false;
}

byte minTemp = response & 0xFF;
Expand All @@ -913,7 +937,7 @@ class OpenThermTask : public Task {
CustomOpenTherm::temperatureToData(value)
));

return CustomOpenTherm::isValidResponse(response);
return CustomOpenTherm::isValidResponse(response) && CustomOpenTherm::isValidResponseId(response, OpenThermMessageID::MaxTSet);
}

bool updateOutdoorTemp() {
Expand All @@ -925,6 +949,9 @@ class OpenThermTask : public Task {

if (!CustomOpenTherm::isValidResponse(response)) {
return false;

} else if (!CustomOpenTherm::isValidResponseId(response, OpenThermMessageID::Toutside)) {
return false;
}

float value = settings.sensors.outdoor.offset + convertTemp(
Expand Down Expand Up @@ -952,6 +979,9 @@ class OpenThermTask : public Task {

if (!CustomOpenTherm::isValidResponse(response)) {
return false;

} else if (!CustomOpenTherm::isValidResponseId(response, OpenThermMessageID::Texhaust)) {
return false;
}

float value = (float) CustomOpenTherm::getInt(response);
Expand Down Expand Up @@ -984,6 +1014,9 @@ class OpenThermTask : public Task {

if (!CustomOpenTherm::isValidResponse(response)) {
return false;

} else if (!CustomOpenTherm::isValidResponseId(response, OpenThermMessageID::Tboiler)) {
return false;
}

float value = CustomOpenTherm::getFloat(response);
Expand Down Expand Up @@ -1016,6 +1049,9 @@ class OpenThermTask : public Task {

if (!CustomOpenTherm::isValidResponse(response)) {
return false;

} else if (!CustomOpenTherm::isValidResponseId(response, OpenThermMessageID::Tret)) {
return false;
}

float value = convertTemp(
Expand Down Expand Up @@ -1045,6 +1081,9 @@ class OpenThermTask : public Task {

if (!CustomOpenTherm::isValidResponse(response)) {
return false;

} else if (!CustomOpenTherm::isValidResponseId(response, OpenThermMessageID::Tdhw)) {
return false;
}

float value = CustomOpenTherm::getFloat(response);
Expand Down Expand Up @@ -1077,6 +1116,9 @@ class OpenThermTask : public Task {

if (!CustomOpenTherm::isValidResponse(response)) {
return false;

} else if (!CustomOpenTherm::isValidResponseId(response, OpenThermMessageID::DHWFlowRate)) {
return false;
}

float value = CustomOpenTherm::getFloat(response);
Expand Down Expand Up @@ -1116,6 +1158,9 @@ class OpenThermTask : public Task {

if (!CustomOpenTherm::isValidResponse(response)) {
return false;

} else if (!CustomOpenTherm::isValidResponseId(response, OpenThermMessageID::ASFflags)) {
return false;
}

vars.sensors.faultCode = response & 0xFF;
Expand All @@ -1131,6 +1176,9 @@ class OpenThermTask : public Task {

if (!CustomOpenTherm::isValidResponse(response)) {
return false;

} else if (!CustomOpenTherm::isValidResponseId(response, OpenThermMessageID::OEMDiagnosticCode)) {
return false;
}

vars.sensors.diagnosticCode = CustomOpenTherm::getUInt(response);
Expand All @@ -1146,6 +1194,9 @@ class OpenThermTask : public Task {

if (!CustomOpenTherm::isValidResponse(response)) {
return false;

} else if (!CustomOpenTherm::isValidResponseId(response, OpenThermMessageID::RelModLevel)) {
return false;
}

float value = CustomOpenTherm::getFloat(response);
Expand All @@ -1172,6 +1223,9 @@ class OpenThermTask : public Task {

if (!CustomOpenTherm::isValidResponse(response)) {
return false;

} else if (!CustomOpenTherm::isValidResponseId(response, OpenThermMessageID::MaxCapacityMinModLevel)) {
return false;
}

vars.parameters.minModulation = response & 0xFF;
Expand All @@ -1189,6 +1243,9 @@ class OpenThermTask : public Task {

if (!CustomOpenTherm::isValidResponse(response)) {
return false;

} else if (!CustomOpenTherm::isValidResponseId(response, OpenThermMessageID::CHPressure)) {
return false;
}

float value = CustomOpenTherm::getFloat(response);
Expand Down

0 comments on commit 11b1277

Please sign in to comment.