diff --git a/components/panasonic_heatpump/helpers.cpp b/components/panasonic_heatpump/helpers.cpp index 81c00fb..6a6460a 100644 --- a/components/panasonic_heatpump/helpers.cpp +++ b/components/panasonic_heatpump/helpers.cpp @@ -34,13 +34,7 @@ namespace esphome ESP_LOGI(TAG, "%s %s[%i]", msgDir.c_str(), msgType.c_str(), length); delay(10); - char buffer[5]; - for (size_t i = 0; i < length; i++) - { - if (i > 0) logStr += separator; - sprintf(buffer, "%02X", data[i]); - logStr += buffer; - } + logStr += byte_array_to_hex_string(data, length, separator); for (size_t i = 0; i < logStr.length(); i += UART_LOG_CHUNK_SIZE) { @@ -48,5 +42,25 @@ namespace esphome delay(10); } } + + std::string PanasonicHelpers::byte_array_to_hex_string(const std::vector& data, const char separator) + { + return PanasonicHelpers::byte_array_to_hex_string(&data[0], data.size(), separator); + } + + std::string PanasonicHelpers::byte_array_to_hex_string(const uint8_t* data, const size_t length, const char separator) + { + std::string hexStr = ""; + char buffer[5]; + + for (size_t i = 0; i < length; i++) + { + if (i > 0) hexStr += separator; + sprintf(buffer, "%02X", data[i]); + hexStr += buffer; + } + + return hexStr; + } } // namespace panasonic_heatpump } // namespace esphome diff --git a/components/panasonic_heatpump/helpers.h b/components/panasonic_heatpump/helpers.h index fc868ab..478dfdd 100644 --- a/components/panasonic_heatpump/helpers.h +++ b/components/panasonic_heatpump/helpers.h @@ -25,6 +25,8 @@ namespace esphome public: static void log_uart_hex(UartLogDirection direction, const std::vector& data, const char separator); static void log_uart_hex(UartLogDirection direction, const uint8_t* data, const size_t length, const char separator); + static std::string byte_array_to_hex_string(const std::vector& data, const char separator); + static std::string byte_array_to_hex_string(const uint8_t* data, const size_t length, const char separator); }; } } \ No newline at end of file diff --git a/components/panasonic_heatpump/panasonic_heatpump.cpp b/components/panasonic_heatpump/panasonic_heatpump.cpp index 4561ce5..347f9bb 100644 --- a/components/panasonic_heatpump/panasonic_heatpump.cpp +++ b/components/panasonic_heatpump/panasonic_heatpump.cpp @@ -149,12 +149,19 @@ namespace esphome this->payload_length_ = byte_; } // Discard message if format is wrong - if ((this->response_message_.size() == 3 && byte_ != 0x01 && byte_ != 0x10) || - (this->response_message_.size() == 4 && byte_ != 0x10 && byte_ != 0x21)) + if (this->response_message_.size() == 3 && byte_ != 0x01 && byte_ != 0x10) { this->response_receiving_ = false; - ESP_LOGW(TAG, "Invalid response message: %d. byte is 0x%02X but expexted is 0x01, 0x10 or 0x21", - response_message_.size(), byte_); + ESP_LOGW(TAG, "Invalid request message: 0x%s. Expected last byte to be 0x01 or 0x10", + PanasonicHelpers::byte_array_to_hex_string(this->request_message_, ',')); + delay(10); // NOLINT + continue; + } + if (this->response_message_.size() == 4 && byte_ != 0x10 && byte_ != 0x21) + { + this->response_receiving_ = false; + ESP_LOGW(TAG, "Invalid request message: 0x%s. Expected last byte to be 0x10 or 0x21", + PanasonicHelpers::byte_array_to_hex_string(this->request_message_, ',')); delay(10); // NOLINT continue; } @@ -226,12 +233,19 @@ namespace esphome this->payload_length_ = byte_; } // Discard message if format is wrong - if ((this->request_message_.size() == 3 && byte_ != 0x01 && byte_ != 0x10) || - (this->request_message_.size() == 4 && byte_ != 0x10 && byte_ != 0x21)) + if (this->request_message_.size() == 3 && byte_ != 0x01 && byte_ != 0x10) { this->request_receiving_ = false; - ESP_LOGW(TAG, "Invalid request message: %d. byte is 0x%02X but expexted is 0x01, 0x10 or 0x21", - request_message_.size(), byte_); + ESP_LOGW(TAG, "Invalid request message: 0x%s. Expected last byte to be 0x01 or 0x10", + PanasonicHelpers::byte_array_to_hex_string(this->request_message_, ',')); + delay(10); // NOLINT + continue; + } + if (this->request_message_.size() == 4 && byte_ != 0x10 && byte_ != 0x21) + { + this->request_receiving_ = false; + ESP_LOGW(TAG, "Invalid request message: 0x%s. Expected last byte to be 0x10 or 0x21", + PanasonicHelpers::byte_array_to_hex_string(this->request_message_, ',')); delay(10); // NOLINT continue; }