From 58e5cc71f71be54709a5a85b1d5d8d55585da4b4 Mon Sep 17 00:00:00 2001 From: bullshit Date: Mon, 25 Aug 2025 21:59:17 +0200 Subject: [PATCH] refactor(ups-hid): test result constants Signed-off-by: bullshit --- components/ups_hid/apc_hid_protocol.cpp | 18 +++++++++--------- components/ups_hid/cyberpower_protocol.cpp | 18 +++++++++--------- components/ups_hid/generic_hid_protocol.cpp | 2 +- components/ups_hid/ups_constants.h | 10 ++++++++++ 4 files changed, 29 insertions(+), 19 deletions(-) diff --git a/components/ups_hid/apc_hid_protocol.cpp b/components/ups_hid/apc_hid_protocol.cpp index e3f9ea2..64b84c6 100644 --- a/components/ups_hid/apc_hid_protocol.cpp +++ b/components/ups_hid/apc_hid_protocol.cpp @@ -288,7 +288,7 @@ bool ApcHidProtocol::read_data(UpsData &data) { // Set default test result if (data.test.ups_test_result.empty()) { - data.test.ups_test_result = "No test initiated"; + data.test.ups_test_result = test::RESULT_NO_TEST; } return success; @@ -1718,7 +1718,7 @@ bool ApcHidProtocol::stop_ups_test() { void ApcHidProtocol::parse_test_result_report(const HidReport &report, UpsData &data) { if (report.data.size() < 2) { ESP_LOGW(APC_HID_TAG, "Test result report too short: %zu bytes", report.data.size()); - data.test.ups_test_result = "Error reading test result"; + data.test.ups_test_result = test::RESULT_ERROR_READING; return; } @@ -1731,25 +1731,25 @@ void ApcHidProtocol::parse_test_result_report(const HidReport &report, UpsData & switch (test_result_value) { case 1: - data.test.ups_test_result = "Done and passed"; + data.test.ups_test_result = test::RESULT_DONE_PASSED; break; case 2: - data.test.ups_test_result = "Done and warning"; + data.test.ups_test_result = test::RESULT_DONE_WARNING; break; case 3: - data.test.ups_test_result = "Done and error"; + data.test.ups_test_result = test::RESULT_DONE_ERROR; break; case 4: - data.test.ups_test_result = "Aborted"; + data.test.ups_test_result = test::RESULT_ABORTED; break; case 5: - data.test.ups_test_result = "In progress"; + data.test.ups_test_result = test::RESULT_IN_PROGRESS; break; case 6: - data.test.ups_test_result = "No test initiated"; + data.test.ups_test_result = test::RESULT_NO_TEST; break; case 7: - data.test.ups_test_result = "Test scheduled"; + data.test.ups_test_result = test::RESULT_SCHEDULED; break; default: data.test.ups_test_result = "Unknown test result (" + std::to_string(test_result_value) + ")"; diff --git a/components/ups_hid/cyberpower_protocol.cpp b/components/ups_hid/cyberpower_protocol.cpp index ec92ad8..06a2042 100644 --- a/components/ups_hid/cyberpower_protocol.cpp +++ b/components/ups_hid/cyberpower_protocol.cpp @@ -844,7 +844,7 @@ void CyberPowerProtocol::read_missing_dynamic_values(UpsData &data) { } // 5. Set static/derived values based on NUT behavior - data.test.ups_test_result = "No test initiated"; // Default test result + data.test.ups_test_result = test::RESULT_NO_TEST; // Default test result // NOTE: battery_status is now properly set based on charging state in parse_present_status_report @@ -1127,7 +1127,7 @@ void CyberPowerProtocol::parse_test_result_report(const HidReport &report, UpsDa // Parse test result from Report 0x14 (UPS.Output.Test) // Based on NUT test_read_info lookup table if (report.data.size() < 2) { - data.test.ups_test_result = "Error reading test result"; + data.test.ups_test_result = test::RESULT_ERROR_READING; return; } @@ -1140,25 +1140,25 @@ void CyberPowerProtocol::parse_test_result_report(const HidReport &report, UpsDa switch (test_result_value) { case 1: - data.test.ups_test_result = "Done and passed"; + data.test.ups_test_result = test::RESULT_DONE_PASSED; break; case 2: - data.test.ups_test_result = "Done and warning"; + data.test.ups_test_result = test::RESULT_DONE_WARNING; break; case 3: - data.test.ups_test_result = "Done and error"; + data.test.ups_test_result = test::RESULT_DONE_ERROR; break; case 4: - data.test.ups_test_result = "Aborted"; + data.test.ups_test_result = test::RESULT_ABORTED; break; case 5: - data.test.ups_test_result = "In progress"; + data.test.ups_test_result = test::RESULT_IN_PROGRESS; break; case 6: - data.test.ups_test_result = "No test initiated"; + data.test.ups_test_result = test::RESULT_NO_TEST; break; case 7: - data.test.ups_test_result = "Test scheduled"; + data.test.ups_test_result = test::RESULT_SCHEDULED; break; default: data.test.ups_test_result = "Unknown test result (" + std::to_string(test_result_value) + ")"; diff --git a/components/ups_hid/generic_hid_protocol.cpp b/components/ups_hid/generic_hid_protocol.cpp index 56b59d8..cd2ecb1 100644 --- a/components/ups_hid/generic_hid_protocol.cpp +++ b/components/ups_hid/generic_hid_protocol.cpp @@ -230,7 +230,7 @@ bool GenericHidProtocol::read_data(UpsData &data) { } // Set default test result - data.test.ups_test_result = "No test initiated"; + data.test.ups_test_result = test::RESULT_NO_TEST; return success; } diff --git a/components/ups_hid/ups_constants.h b/components/ups_hid/ups_constants.h index 8503448..8e39c73 100644 --- a/components/ups_hid/ups_constants.h +++ b/components/ups_hid/ups_constants.h @@ -160,6 +160,16 @@ namespace test { static constexpr uint8_t COMMAND_QUICK = 1; static constexpr uint8_t COMMAND_DEEP = 2; static constexpr uint8_t COMMAND_ABORT = 3; + + // Test result status strings (shared by APC and CyberPower) + static constexpr const char* RESULT_DONE_PASSED = "Done and passed"; + static constexpr const char* RESULT_DONE_WARNING = "Done and warning"; + static constexpr const char* RESULT_DONE_ERROR = "Done and error"; + static constexpr const char* RESULT_ABORTED = "Aborted"; + static constexpr const char* RESULT_IN_PROGRESS = "In progress"; + static constexpr const char* RESULT_NO_TEST = "No test initiated"; + static constexpr const char* RESULT_SCHEDULED = "Test scheduled"; + static constexpr const char* RESULT_ERROR_READING = "Error reading test result"; } // ==================== Sensor Type Identifiers ====================