Public Access
Bugfix 2026.7.0.
This commit is contained in:
1 parent
c69b2e0754
commit
74cdb3d56f
4 files changed
+27
-93
No files matched your search
@@ -3,19 +3,13 @@
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/components/logger/logger.h"
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
|
||||
namespace esphome {
|
||||
namespace ups_hid {
|
||||
|
||||
static const char *const FACTORY_TAG = "ups_hid.factory";
|
||||
|
||||
// Forward declarations for protocol creator functions
|
||||
// These are defined in their respective protocol implementation files
|
||||
extern std::unique_ptr<UpsProtocolBase> create_apc_protocol(UpsHidComponent* parent);
|
||||
extern std::unique_ptr<UpsProtocolBase> create_cyberpower_protocol(UpsHidComponent* parent);
|
||||
extern std::unique_ptr<UpsProtocolBase> create_goldenmate_protocol(UpsHidComponent* parent);
|
||||
extern std::unique_ptr<UpsProtocolBase> create_generic_protocol(UpsHidComponent* parent);
|
||||
|
||||
// Static registry implementations
|
||||
std::unordered_map<uint16_t, std::vector<ProtocolFactory::ProtocolInfo>>&
|
||||
ProtocolFactory::get_vendor_registry() {
|
||||
@@ -30,65 +24,13 @@ ProtocolFactory::get_fallback_registry() {
|
||||
}
|
||||
|
||||
void ProtocolFactory::ensure_initialized() {
|
||||
// Initialize built-in protocols on first access
|
||||
// This ensures all protocols are registered regardless of static initializer order
|
||||
// With the Open/Closed refactor, protocol self-registration using macros handles everything.
|
||||
// This function is kept for backwards compatibility or future core initializations.
|
||||
static bool initialized = false;
|
||||
if (initialized) return;
|
||||
initialized = true;
|
||||
|
||||
ESP_LOGI(FACTORY_TAG, "Initializing built-in UPS protocol support");
|
||||
|
||||
// Register APC protocol for vendor 0x051D
|
||||
{
|
||||
ProtocolInfo info;
|
||||
info.creator = create_apc_protocol;
|
||||
info.name = "APC";
|
||||
info.description = "APC HID Protocol";
|
||||
info.supported_vendors = {0x051D};
|
||||
info.priority = 200;
|
||||
auto& registry = get_vendor_registry();
|
||||
registry[0x051D].push_back(info);
|
||||
ESP_LOGI(FACTORY_TAG, "Registered APC protocol for vendor 0x051D");
|
||||
}
|
||||
|
||||
// Register CyberPower protocol for vendor 0x0764
|
||||
{
|
||||
ProtocolInfo info;
|
||||
info.creator = create_cyberpower_protocol;
|
||||
info.name = "CyberPower";
|
||||
info.description = "CyberPower HID Protocol";
|
||||
info.supported_vendors = {0x0764};
|
||||
info.priority = 200;
|
||||
auto& registry = get_vendor_registry();
|
||||
registry[0x0764].push_back(info);
|
||||
ESP_LOGI(FACTORY_TAG, "Registered CyberPower protocol for vendor 0x0764");
|
||||
}
|
||||
|
||||
// Register GoldenMate protocol for vendor 0x075D
|
||||
{
|
||||
ProtocolInfo info;
|
||||
info.creator = create_goldenmate_protocol;
|
||||
info.name = "GoldenMate";
|
||||
info.description = "GoldenMate HID Protocol";
|
||||
info.supported_vendors = {0x075D};
|
||||
info.priority = 200;
|
||||
auto& registry = get_vendor_registry();
|
||||
registry[0x075D].push_back(info);
|
||||
ESP_LOGI(FACTORY_TAG, "Registered GoldenMate protocol for vendor 0x075D");
|
||||
}
|
||||
|
||||
// Register Generic protocol as fallback (always last)
|
||||
{
|
||||
ProtocolInfo info;
|
||||
info.creator = create_generic_protocol;
|
||||
info.name = "Generic HID Protocol";
|
||||
info.description = "Universal HID protocol fallback for unknown UPS vendors";
|
||||
info.supported_vendors = {};
|
||||
info.priority = 10;
|
||||
auto& registry = get_fallback_registry();
|
||||
registry.push_back(info);
|
||||
ESP_LOGI(FACTORY_TAG, "Registered Generic HID protocol as fallback");
|
||||
}
|
||||
ESP_LOGI(FACTORY_TAG, "Protocol registries initialized.");
|
||||
}
|
||||
|
||||
void ProtocolFactory::register_protocol_for_vendor(uint16_t vendor_id,
|
||||
@@ -249,8 +191,10 @@ ProtocolFactory::create_by_name(const std::string& protocol_name, UpsHidComponen
|
||||
// Match protocol name (case-insensitive)
|
||||
std::string info_name_lower = info.name;
|
||||
std::string protocol_name_lower = protocol_name;
|
||||
std::transform(info_name_lower.begin(), info_name_lower.end(), info_name_lower.begin(), ::tolower);
|
||||
std::transform(protocol_name_lower.begin(), protocol_name_lower.end(), protocol_name_lower.begin(), ::tolower);
|
||||
std::transform(info_name_lower.begin(), info_name_lower.end(), info_name_lower.begin(),
|
||||
[](unsigned char c) { return std::tolower(c); });
|
||||
std::transform(protocol_name_lower.begin(), protocol_name_lower.end(), protocol_name_lower.begin(),
|
||||
[](unsigned char c) { return std::tolower(c); });
|
||||
|
||||
if (info_name_lower.find(protocol_name_lower) != std::string::npos) {
|
||||
ESP_LOGD(FACTORY_TAG, "Found matching protocol '%s' for name '%s'",
|
||||
@@ -270,8 +214,10 @@ ProtocolFactory::create_by_name(const std::string& protocol_name, UpsHidComponen
|
||||
for (const auto& info : fallback_registry) {
|
||||
std::string info_name_lower = info.name;
|
||||
std::string protocol_name_lower = protocol_name;
|
||||
std::transform(info_name_lower.begin(), info_name_lower.end(), info_name_lower.begin(), ::tolower);
|
||||
std::transform(protocol_name_lower.begin(), protocol_name_lower.end(), protocol_name_lower.begin(), ::tolower);
|
||||
std::transform(info_name_lower.begin(), info_name_lower.end(), info_name_lower.begin(),
|
||||
[](unsigned char c) { return std::tolower(c); });
|
||||
std::transform(protocol_name_lower.begin(), protocol_name_lower.end(), protocol_name_lower.begin(),
|
||||
[](unsigned char c) { return std::tolower(c); });
|
||||
|
||||
if (info_name_lower.find(protocol_name_lower) != std::string::npos) {
|
||||
ESP_LOGD(FACTORY_TAG, "Found matching fallback protocol '%s' for name '%s'",
|
||||
|
||||
@@ -13,12 +13,6 @@ namespace ups_hid {
|
||||
class UpsProtocolBase;
|
||||
class UpsHidComponent;
|
||||
|
||||
// Forward declarations for built-in protocol creator functions
|
||||
std::unique_ptr<UpsProtocolBase> create_apc_protocol(UpsHidComponent* parent);
|
||||
std::unique_ptr<UpsProtocolBase> create_cyberpower_protocol(UpsHidComponent* parent);
|
||||
std::unique_ptr<UpsProtocolBase> create_goldenmate_protocol(UpsHidComponent* parent);
|
||||
std::unique_ptr<UpsProtocolBase> create_generic_protocol(UpsHidComponent* parent);
|
||||
|
||||
/**
|
||||
* Protocol Factory with Self-Registration Support
|
||||
*
|
||||
|
||||
@@ -78,7 +78,7 @@ bool GoldenMateProtocol::initialize() {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool GoldenMateProtocol::parse_binary_status(const HidReport &report, UpsData &data) {
|
||||
bool GoldenMateProtocol::parse_binary_status(const HidReport &/*report*/, UpsData &/*data*/) {
|
||||
// We ignore the binary runtime here to prevent the 16-bit overflow bug
|
||||
// The true runtime will be grabbed from the ASCII string instead.
|
||||
return true;
|
||||
@@ -110,6 +110,7 @@ bool GoldenMateProtocol::parse_megatec_string(const HidReport &report, UpsData &
|
||||
}
|
||||
|
||||
// Extract the live values safely
|
||||
// NOTE: Verify if substr(0,4) is actually runtime or current as per the header comments
|
||||
float runtime_mins = std::atof(packed.substr(0, 4).c_str());
|
||||
float battery_pct = std::atof(packed.substr(12, 3).c_str());
|
||||
|
||||
@@ -128,27 +129,24 @@ bool GoldenMateProtocol::parse_megatec_string(const HidReport &report, UpsData &
|
||||
bool on_battery = (status_str.length() > 0 && status_str[0] == '1');
|
||||
|
||||
// --- TIME-BASED DEBOUNCE LOGIC ---
|
||||
static bool is_full = true;
|
||||
static int below_100_count = 0;
|
||||
|
||||
if (battery_pct >= 100.0f) {
|
||||
is_full = true;
|
||||
below_100_count = 0; // Reset counter immediately on a 100% read
|
||||
this->is_full_ = true;
|
||||
this->below_100_count_ = 0; // Reset counter immediately on a 100% read
|
||||
} else {
|
||||
below_100_count++;
|
||||
this->below_100_count_++;
|
||||
// Require 2 consecutive reads (~20 seconds) below 100% to drop the 'Full' status
|
||||
if (below_100_count >= 2) {
|
||||
is_full = false;
|
||||
if (this->below_100_count_ >= 2) {
|
||||
this->is_full_ = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (on_battery) {
|
||||
data.power.status = "OB DISCHRG";
|
||||
data.battery.status = "Discharging";
|
||||
is_full = false; // Force reset if we lose AC power
|
||||
below_100_count = 2; // Keep counter aligned with state
|
||||
this->is_full_ = false; // Force reset if we lose AC power
|
||||
this->below_100_count_ = 2; // Keep counter aligned with state
|
||||
} else {
|
||||
if (is_full) {
|
||||
if (this->is_full_) {
|
||||
data.power.status = "OL";
|
||||
data.battery.status = "Full";
|
||||
} else {
|
||||
@@ -169,6 +167,8 @@ bool GoldenMateProtocol::read_data(UpsData &data) {
|
||||
// CRITICAL HARDWARE TRIGGER:
|
||||
// We MUST poll Report 0x01 first to force the firmware to refresh Report 0x0C
|
||||
HidReport status_report;
|
||||
// If this read times out, we still attempt the Megatec read below,
|
||||
// but it may return stale data if the hardware trigger failed.
|
||||
read_feature_report(REPORT_ID_STATUS, status_report);
|
||||
|
||||
// Now read the freshly updated ASCII data from Report 0x0C
|
||||
@@ -179,15 +179,6 @@ bool GoldenMateProtocol::read_data(UpsData &data) {
|
||||
}
|
||||
}
|
||||
|
||||
// Set actual names on every read cycle
|
||||
data.device.manufacturer = "GoldenMate";
|
||||
data.device.model = "UPS Pro";
|
||||
|
||||
std::string str;
|
||||
if (parent_->get_string_descriptor(3, str) == ESP_OK && !str.empty()) {
|
||||
data.device.serial_number = str;
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
|
||||
@@ -40,6 +40,9 @@ class GoldenMateProtocol : public UpsProtocolBase {
|
||||
static const uint8_t REPORT_ID_STATUS = 0x01;
|
||||
static const uint8_t REPORT_ID_MEGATEC = 0x0C;
|
||||
|
||||
bool is_full_ = true;
|
||||
int below_100_count_ = 0;
|
||||
|
||||
struct HidReport {
|
||||
uint8_t report_id;
|
||||
std::vector<uint8_t> data;
|
||||
|
||||
Reference in new issue
Block a user