Public Access
153 lines
4.4 KiB
C++
153 lines
4.4 KiB
C++
#include "protocol_goldenmate.h"
|
|
#include "protocol_factory.h"
|
|
#include "esphome/core/log.h"
|
|
#include <cstring>
|
|
#include <cstdlib>
|
|
|
|
namespace esphome {
|
|
namespace ups_hid {
|
|
|
|
static const char *const GM_TAG = "ups_hid.goldenmate";
|
|
|
|
// Register for vendor 0x075D with a UNIQUE macro name identifier
|
|
REGISTER_UPS_PROTOCOL_FOR_VENDOR(
|
|
0x075D, GoldenMateProtocol_075D,
|
|
[](UpsHidComponent *p) { return std::make_unique<GoldenMateProtocol>(p); },
|
|
"GoldenMate", "UPS Pro", 200);
|
|
|
|
bool GoldenMateProtocol::read_feature_report(uint8_t report_id, HidReport &report) {
|
|
if (!parent_->is_device_connected()) {
|
|
return false;
|
|
}
|
|
|
|
uint8_t buffer[64];
|
|
size_t buffer_len = sizeof(buffer);
|
|
|
|
esp_err_t ret = parent_->hid_get_report(HID_REPORT_TYPE_FEATURE, report_id,
|
|
buffer, &buffer_len,
|
|
parent_->get_protocol_timeout());
|
|
if (ret == ESP_OK && buffer_len > 0) {
|
|
report.report_id = report_id;
|
|
report.data.assign(buffer, buffer + buffer_len);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool GoldenMateProtocol::detect() {
|
|
ESP_LOGD(GM_TAG, "Detecting GoldenMate/iDowell UPS for 0x075D...");
|
|
|
|
HidReport megatec;
|
|
if (!read_feature_report(REPORT_ID_MEGATEC, megatec)) {
|
|
return false;
|
|
}
|
|
|
|
if (megatec.data.size() < 62) {
|
|
return false;
|
|
}
|
|
|
|
ESP_LOGI(GM_TAG, "GoldenMate/iDowell UPS protocol successfully detected!");
|
|
return true;
|
|
}
|
|
|
|
bool GoldenMateProtocol::initialize() {
|
|
ESP_LOGD(GM_TAG, "Initializing GoldenMate/iDowell UPS protocol");
|
|
|
|
UpsData data;
|
|
data.device.usb_vendor_id = parent_->get_vendor_id();
|
|
data.device.usb_product_id = parent_->get_product_id();
|
|
|
|
data.device.manufacturer = "GoldenMate";
|
|
data.device.model = "UPS Pro";
|
|
|
|
// Get Serial Number from descriptor 3
|
|
std::string str;
|
|
if (parent_->get_string_descriptor(3, str) == ESP_OK && !str.empty()) {
|
|
data.device.serial_number = str;
|
|
}
|
|
|
|
data.device.capabilities.supports_hid_get_report = true;
|
|
data.device.capabilities.supports_runtime_estimation = true;
|
|
|
|
ESP_LOGI(GM_TAG, "UPS initialized: %s %s (S/N: %s)",
|
|
data.device.manufacturer.c_str(),
|
|
data.device.model.c_str(),
|
|
data.device.serial_number.c_str());
|
|
|
|
return true;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
bool GoldenMateProtocol::parse_megatec_string(const HidReport &report, UpsData &data) {
|
|
if (report.data.size() < 62) return false;
|
|
|
|
const uint8_t *d = report.data.data();
|
|
std::string packed;
|
|
for (int i = 30; i < 62; i++) {
|
|
if (d[i] >= '0' && d[i] <= '9') {
|
|
packed += static_cast<char>(d[i]);
|
|
}
|
|
}
|
|
|
|
if (packed.size() < 30) return false;
|
|
|
|
// Extract the live values
|
|
float runtime_mins = std::atof(packed.substr(0, 4).c_str());
|
|
float battery_pct = std::atof(packed.substr(12, 3).c_str());
|
|
|
|
data.battery.runtime_minutes = runtime_mins;
|
|
data.battery.level = battery_pct;
|
|
|
|
// Parse Status to perfectly mimic NUT's "ups.status"
|
|
std::string status_str = packed.substr(24, 8);
|
|
bool on_battery = (status_str[0] == '1');
|
|
|
|
if (on_battery) {
|
|
data.power.status = "OB DISCHRG"; // On Battery, Discharging
|
|
data.battery.status = "Discharging";
|
|
} else {
|
|
if (battery_pct >= 100.0f) {
|
|
data.power.status = "OL"; // Online
|
|
data.battery.status = "Full";
|
|
} else {
|
|
data.power.status = "OL CHRG"; // Online, Charging
|
|
data.battery.status = "Charging";
|
|
}
|
|
}
|
|
|
|
ESP_LOGI(GM_TAG, "True UPS Data: Batt=%.0f%% | Runtime=%.0f min | Status=%s",
|
|
battery_pct, runtime_mins, data.power.status.c_str());
|
|
|
|
return true;
|
|
}
|
|
|
|
bool GoldenMateProtocol::read_data(UpsData &data) {
|
|
bool success = false;
|
|
|
|
// Read ASCII data from Report 0x0C
|
|
HidReport megatec_report;
|
|
if (read_feature_report(REPORT_ID_MEGATEC, megatec_report)) {
|
|
if (parse_megatec_string(megatec_report, data)) {
|
|
success = true;
|
|
}
|
|
}
|
|
|
|
// Enforce NUT mimicry on every read cycle
|
|
data.device.manufacturer = "-BMS-";
|
|
data.device.model = "Smart-Battery";
|
|
|
|
std::string str;
|
|
if (parent_->get_string_descriptor(3, str) == ESP_OK && !str.empty()) {
|
|
data.device.serial_number = str;
|
|
}
|
|
|
|
return success;
|
|
}
|
|
|
|
} // namespace ups_hid
|
|
} // namespace esphome
|