diff --git a/lib/CustomOpenTherm/CustomOpenTherm.h b/lib/CustomOpenTherm/CustomOpenTherm.h index 6fd6db1..3bef65f 100644 --- a/lib/CustomOpenTherm/CustomOpenTherm.h +++ b/lib/CustomOpenTherm/CustomOpenTherm.h @@ -70,50 +70,89 @@ public: } } - bool sendBoilerReset() { - unsigned int data = 1; - data <<= 8; - unsigned long response = this->sendRequest(buildRequest( - OpenThermMessageType::WRITE_DATA, - OpenThermMessageID::RemoteRequest, - data - )); - - return isValidResponse(response) && isValidResponseId(response, OpenThermMessageID::RemoteRequest); + inline auto sendBoilerReset() { + return this->sendRequestCode(1); } - bool sendServiceReset() { - unsigned int data = 10; - data <<= 8; - unsigned long response = this->sendRequest(buildRequest( - OpenThermMessageType::WRITE_DATA, - OpenThermMessageID::RemoteRequest, - data - )); - - return isValidResponse(response) && isValidResponseId(response, OpenThermMessageID::RemoteRequest); + inline auto sendServiceReset() { + return this->sendRequestCode(10); } - bool sendWaterFilling() { - unsigned int data = 2; - data <<= 8; + inline auto sendWaterFilling() { + return this->sendRequestCode(2); + } + + bool sendRequestCode(const uint8_t requestCode) { unsigned long response = this->sendRequest(buildRequest( OpenThermMessageType::WRITE_DATA, OpenThermMessageID::RemoteRequest, - data + static_cast(requestCode) << 8 )); - return isValidResponse(response) && isValidResponseId(response, OpenThermMessageID::RemoteRequest); + if (!isValidResponse(response) || !isValidResponseId(response, OpenThermMessageID::RemoteRequest)) { + return false; + } + + const uint8_t responseRequestCode = (response & 0xFFFF) >> 8; + const uint8_t responseCode = response & 0xFF; + if (responseRequestCode != requestCode || responseCode < 128) { + return false; + } + + // reset + this->sendRequest(buildRequest( + OpenThermMessageType::WRITE_DATA, + OpenThermMessageID::RemoteRequest, + 0u << 8 + )); + + return true; + } + + bool getStr(OpenThermMessageID id, char* buffer, uint16_t length = 50) { + if (buffer == nullptr || length == 0) { + return false; + } + + unsigned long response; + uint8_t index = 0; + uint8_t maxIndex = 255; + + while (index <= maxIndex && index < length) { + response = this->sendRequest(buildRequest( + OpenThermMessageType::READ_DATA, + id, + static_cast(index) << 8 + )); + + if (!isValidResponse(response) || !isValidResponseId(response, id)) { + break; + } + + const uint8_t character = response & 0xFF; + if (character == 0) { + break; + } + + if (index == 0) { + maxIndex = (response & 0xFFFF) >> 8; + } + + buffer[index++] = static_cast(character); + } + + buffer[index] = '\0'; + return index > 0; } static bool isCh2Active(unsigned long response) { - return response & 0x20; + return response & 0x20; } static bool isValidResponseId(unsigned long response, OpenThermMessageID id) { - uint8_t responseId = (response >> 16) & 0xFF; + const uint8_t responseId = (response >> 16) & 0xFF; - return (uint8_t)id == responseId; + return static_cast(id) == responseId; } static uint8_t getResponseMessageTypeId(unsigned long response) { @@ -124,10 +163,10 @@ public: uint8_t msgType = getResponseMessageTypeId(response); switch (msgType) { - case (uint8_t) OpenThermMessageType::READ_ACK: - case (uint8_t) OpenThermMessageType::WRITE_ACK: - case (uint8_t) OpenThermMessageType::DATA_INVALID: - case (uint8_t) OpenThermMessageType::UNKNOWN_DATA_ID: + case static_cast(OpenThermMessageType::READ_ACK): + case static_cast(OpenThermMessageType::WRITE_ACK): + case static_cast(OpenThermMessageType::DATA_INVALID): + case static_cast(OpenThermMessageType::UNKNOWN_DATA_ID): return CustomOpenTherm::messageTypeToString( static_cast(msgType) ); diff --git a/src/OpenThermTask.h b/src/OpenThermTask.h index 5101597..131f5ef 100644 --- a/src/OpenThermTask.h +++ b/src/OpenThermTask.h @@ -808,7 +808,7 @@ protected: bool result = this->updateDhwTemp(); if (result) { - float convertedDhwTemp = convertTemp( + const float convertedDhwTemp = convertTemp( vars.slave.dhw.currentTemp, settings.opentherm.unitSystem, settings.system.unitSystem @@ -832,7 +832,7 @@ protected: // Update DHW temp 2 if (settings.opentherm.options.dhwSupport && Sensors::getAmountByType(Sensors::Type::OT_DHW_TEMP2, true)) { if (this->updateDhwTemp2()) { - float convertedDhwTemp2 = convertTemp( + const float convertedDhwTemp2 = convertTemp( vars.slave.dhw.currentTemp2, settings.opentherm.unitSystem, settings.system.unitSystem @@ -856,7 +856,7 @@ protected: // Update DHW flow rate if (settings.opentherm.options.dhwSupport && Sensors::getAmountByType(Sensors::Type::OT_DHW_FLOW_RATE, true)) { if (this->updateDhwFlowRate()) { - float convertedDhwFlowRate = convertVolume( + const float convertedDhwFlowRate = convertVolume( vars.slave.dhw.flowRate, settings.opentherm.unitSystem, settings.system.unitSystem @@ -880,7 +880,7 @@ protected: // Update heating temp if (Sensors::getAmountByType(Sensors::Type::OT_HEATING_TEMP, true)) { if (this->updateHeatingTemp()) { - float convertedHeatingTemp = convertTemp( + const float convertedHeatingTemp = convertTemp( vars.slave.heating.currentTemp, settings.opentherm.unitSystem, settings.system.unitSystem @@ -904,7 +904,7 @@ protected: // Update heating return temp if (Sensors::getAmountByType(Sensors::Type::OT_HEATING_RETURN_TEMP, true)) { if (this->updateHeatingReturnTemp()) { - float convertedHeatingReturnTemp = convertTemp( + const float convertedHeatingReturnTemp = convertTemp( vars.slave.heating.returnTemp, settings.opentherm.unitSystem, settings.system.unitSystem @@ -929,7 +929,7 @@ protected: if (Sensors::getAmountByType(Sensors::Type::OT_CH2_TEMP, true)) { if (vars.master.ch2.enabled && !settings.opentherm.options.nativeOTC) { if (this->updateCh2Temp()) { - float convertedCh2Temp = convertTemp( + const float convertedCh2Temp = convertTemp( vars.slave.ch2.currentTemp, settings.opentherm.unitSystem, settings.system.unitSystem @@ -954,7 +954,7 @@ protected: // Update exhaust temp if (Sensors::getAmountByType(Sensors::Type::OT_EXHAUST_TEMP, true)) { if (this->updateExhaustTemp()) { - float convertedExhaustTemp = convertTemp( + const float convertedExhaustTemp = convertTemp( vars.slave.exhaust.temp, settings.opentherm.unitSystem, settings.system.unitSystem @@ -978,7 +978,7 @@ protected: // Update heat exchanger temp if (Sensors::getAmountByType(Sensors::Type::OT_HEAT_EXCHANGER_TEMP, true)) { if (this->updateHeatExchangerTemp()) { - float convertedHeatExchTemp = convertTemp( + const float convertedHeatExchTemp = convertTemp( vars.slave.heatExchangerTemp, settings.opentherm.unitSystem, settings.system.unitSystem @@ -1002,7 +1002,7 @@ protected: // Update outdoor temp if (Sensors::getAmountByType(Sensors::Type::OT_OUTDOOR_TEMP, true)) { if (this->updateOutdoorTemp()) { - float convertedOutdoorTemp = convertTemp( + const float convertedOutdoorTemp = convertTemp( vars.slave.heating.outdoorTemp, settings.opentherm.unitSystem, settings.system.unitSystem @@ -1026,7 +1026,7 @@ protected: // Update solar storage temp if (Sensors::getAmountByType(Sensors::Type::OT_SOLAR_STORAGE_TEMP, true)) { if (this->updateSolarStorageTemp()) { - float convertedSolarStorageTemp = convertTemp( + const float convertedSolarStorageTemp = convertTemp( vars.slave.solar.storage, settings.opentherm.unitSystem, settings.system.unitSystem @@ -1050,7 +1050,7 @@ protected: // Update solar collector temp if (Sensors::getAmountByType(Sensors::Type::OT_SOLAR_COLLECTOR_TEMP, true)) { if (this->updateSolarCollectorTemp()) { - float convertedSolarCollectorTemp = convertTemp( + const float convertedSolarCollectorTemp = convertTemp( vars.slave.solar.collector, settings.opentherm.unitSystem, settings.system.unitSystem @@ -1096,7 +1096,7 @@ protected: // Update pressure if (Sensors::getAmountByType(Sensors::Type::OT_PRESSURE, true)) { if (this->updatePressure()) { - float convertedPressure = convertPressure( + const float convertedPressure = convertPressure( vars.slave.pressure, settings.opentherm.unitSystem, settings.system.unitSystem @@ -1357,7 +1357,7 @@ protected: // Heating overheat control if (settings.heating.overheatProtection.highTemp > 0 && settings.heating.overheatProtection.lowTemp > 0) { - float highTemp = convertTemp( + const float highTemp = convertTemp( max({ vars.slave.heating.currentTemp, vars.slave.heating.returnTemp, @@ -1394,7 +1394,7 @@ protected: // DHW overheat control if (settings.dhw.overheatProtection.highTemp > 0 && settings.dhw.overheatProtection.lowTemp > 0) { - float highTemp = convertTemp( + const float highTemp = convertTemp( max({ vars.slave.heating.currentTemp, vars.slave.heating.returnTemp, @@ -1488,6 +1488,19 @@ protected: } else { Log.swarningln(FPSTR(L_OT), F("Failed set master config")); } + + /*char buf[100]; + if (this->instance->getStr(OpenThermMessageID::Brand, buf, sizeof(buf) - 1)) { + Log.snoticeln(FPSTR(L_OT), F("Slave brand: %s"), buf); + } + + if (this->instance->getStr(OpenThermMessageID::BrandVersion, buf, sizeof(buf) - 1)) { + Log.snoticeln(FPSTR(L_OT), F("Slave brand version: %s"), buf); + } + + if (this->instance->getStr(OpenThermMessageID::BrandSerialNumber, buf, sizeof(buf) - 1)) { + Log.snoticeln(FPSTR(L_OT), F("Slave brand s/n: %s"), buf); + }*/ } bool isReady() { @@ -1665,7 +1678,7 @@ protected: } - bool setRoomTemp(float temperature) { + bool setRoomTemp(const float temperature) { const unsigned int request = CustomOpenTherm::temperatureToData(temperature); const unsigned long response = this->instance->sendRequest(CustomOpenTherm::buildRequest( OpenThermMessageType::WRITE_DATA, @@ -1685,7 +1698,7 @@ protected: return CustomOpenTherm::getUInt(response) == request; } - bool setRoomTempCh2(float temperature) { + bool setRoomTempCh2(const float temperature) { const unsigned int request = CustomOpenTherm::temperatureToData(temperature); const unsigned long response = this->instance->sendRequest(CustomOpenTherm::buildRequest( OpenThermMessageType::WRITE_DATA, @@ -2220,7 +2233,7 @@ protected: return false; } - float value = CustomOpenTherm::getFloat(response); + const float value = CustomOpenTherm::getFloat(response); if (value < 0) { return false; } @@ -2244,7 +2257,7 @@ protected: return false; } - float value = CustomOpenTherm::getFloat(response); + const float value = CustomOpenTherm::getFloat(response); if (value <= 0) { return false; } @@ -2268,7 +2281,7 @@ protected: return false; } - float value = CustomOpenTherm::getFloat(response); + const float value = CustomOpenTherm::getFloat(response); if (value <= 0) { return false; } @@ -2322,7 +2335,7 @@ protected: return false; } - float value = CustomOpenTherm::getFloat(response); + const float value = CustomOpenTherm::getFloat(response); if (value <= 0) { return false; } @@ -2386,7 +2399,7 @@ protected: return false; } - float value = (float) CustomOpenTherm::getInt(response); + const float value = (float) CustomOpenTherm::getInt(response); if (!isValidTemp(value, settings.opentherm.unitSystem, -40, 500)) { return false; } @@ -2410,7 +2423,7 @@ protected: return false; } - float value = (float) CustomOpenTherm::getInt(response); + const float value = (float) CustomOpenTherm::getInt(response); if (value <= 0) { return false; } @@ -2511,7 +2524,7 @@ protected: return false; } - float value = CustomOpenTherm::getFloat(response); + const float value = CustomOpenTherm::getFloat(response); if (value < 0) { return false; }