Updated warning text for invalid message (fixes #13)

This commit is contained in:
ElVit 2025-11-24 18:44:09 +01:00
parent 04b6f5c044
commit c7212af657
3 changed files with 45 additions and 15 deletions

View File

@ -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<uint8_t>& 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

View File

@ -25,6 +25,8 @@ namespace esphome
public:
static void log_uart_hex(UartLogDirection direction, const std::vector<uint8_t>& 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<uint8_t>& data, const char separator);
static std::string byte_array_to_hex_string(const uint8_t* data, const size_t length, const char separator);
};
}
}

View File

@ -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;
}