Public Access
feat: initial prototype
Signed-off-by: bullshit <coding@ow-software.pl>
This commit is contained in:
16 files changed
+1843
-8
No files matched your search
@@ -0,0 +1,298 @@
|
||||
#include "nut_ups.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace nut_ups {
|
||||
|
||||
static const char *const CP_TAG = "nut_ups.cyberpower";
|
||||
|
||||
// CyberPower HID Report IDs and data structures
|
||||
static const uint8_t CP_REPORT_ID_UPS_STATUS = 0x01;
|
||||
static const uint8_t CP_REPORT_ID_BATTERY_INFO = 0x02;
|
||||
static const uint8_t CP_REPORT_ID_VOLTAGE_INFO = 0x03;
|
||||
static const uint8_t CP_REPORT_ID_DEVICE_INFO = 0x04;
|
||||
|
||||
// HID Usage Page definitions for UPS (Power Device)
|
||||
static const uint16_t HID_USAGE_PAGE_POWER_DEVICE = 0x84;
|
||||
static const uint16_t HID_USAGE_BATTERY_LEVEL = 0x66;
|
||||
static const uint16_t HID_USAGE_RUNTIME_TO_EMPTY = 0x68;
|
||||
static const uint16_t HID_USAGE_AC_PRESENT = 0x5A;
|
||||
static const uint16_t HID_USAGE_BATTERY_PRESENT = 0x5B;
|
||||
|
||||
bool CyberPowerProtocol::detect() {
|
||||
ESP_LOGD(CP_TAG, "Detecting CyberPower HID Protocol...");
|
||||
|
||||
// Try to get a HID device descriptor or basic report
|
||||
HidReport request;
|
||||
request.report_id = CP_REPORT_ID_DEVICE_INFO;
|
||||
request.data = {0x00}; // Basic device info request
|
||||
|
||||
HidReport response;
|
||||
if (send_hid_report(request, response)) {
|
||||
ESP_LOGD(CP_TAG, "CyberPower HID response received");
|
||||
return true;
|
||||
}
|
||||
|
||||
ESP_LOGD(CP_TAG, "No CyberPower HID response");
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CyberPowerProtocol::initialize() {
|
||||
ESP_LOGD(CP_TAG, "Initializing CyberPower HID Protocol...");
|
||||
|
||||
// Get device information
|
||||
HidReport device_info_request;
|
||||
device_info_request.report_id = CP_REPORT_ID_DEVICE_INFO;
|
||||
device_info_request.data = {0x00};
|
||||
|
||||
HidReport device_info_response;
|
||||
if (send_hid_report(device_info_request, device_info_response)) {
|
||||
ESP_LOGD(CP_TAG, "Device info received, %d bytes", device_info_response.data.size());
|
||||
|
||||
// Parse manufacturer and model from device info
|
||||
// CyberPower HID devices typically include this in the device descriptor
|
||||
if (device_info_response.data.size() >= 8) {
|
||||
ESP_LOGD(CP_TAG, "CyberPower device detected");
|
||||
}
|
||||
}
|
||||
|
||||
ESP_LOGI(CP_TAG, "CyberPower HID Protocol initialized successfully");
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CyberPowerProtocol::read_data(UpsData &data) {
|
||||
ESP_LOGV(CP_TAG, "Reading CyberPower UPS data...");
|
||||
|
||||
bool success = true;
|
||||
|
||||
// Read UPS status
|
||||
HidReport status_request;
|
||||
status_request.report_id = CP_REPORT_ID_UPS_STATUS;
|
||||
status_request.data = {0x00};
|
||||
|
||||
HidReport status_response;
|
||||
if (send_hid_report(status_request, status_response)) {
|
||||
if (parse_hid_data(status_response, data)) {
|
||||
ESP_LOGV(CP_TAG, "Status data parsed successfully");
|
||||
} else {
|
||||
ESP_LOGW(CP_TAG, "Failed to parse status data");
|
||||
success = false;
|
||||
}
|
||||
} else {
|
||||
ESP_LOGW(CP_TAG, "Failed to read UPS status");
|
||||
success = false;
|
||||
}
|
||||
|
||||
// Read battery information
|
||||
HidReport battery_request;
|
||||
battery_request.report_id = CP_REPORT_ID_BATTERY_INFO;
|
||||
battery_request.data = {0x00};
|
||||
|
||||
HidReport battery_response;
|
||||
if (send_hid_report(battery_request, battery_response)) {
|
||||
if (parse_hid_data(battery_response, data)) {
|
||||
ESP_LOGV(CP_TAG, "Battery data parsed successfully");
|
||||
} else {
|
||||
ESP_LOGW(CP_TAG, "Failed to parse battery data");
|
||||
}
|
||||
} else {
|
||||
ESP_LOGW(CP_TAG, "Failed to read battery info");
|
||||
}
|
||||
|
||||
// Read voltage information
|
||||
HidReport voltage_request;
|
||||
voltage_request.report_id = CP_REPORT_ID_VOLTAGE_INFO;
|
||||
voltage_request.data = {0x00};
|
||||
|
||||
HidReport voltage_response;
|
||||
if (send_hid_report(voltage_request, voltage_response)) {
|
||||
if (parse_hid_data(voltage_response, data)) {
|
||||
ESP_LOGV(CP_TAG, "Voltage data parsed successfully");
|
||||
} else {
|
||||
ESP_LOGW(CP_TAG, "Failed to parse voltage data");
|
||||
}
|
||||
} else {
|
||||
ESP_LOGW(CP_TAG, "Failed to read voltage info");
|
||||
}
|
||||
|
||||
// Set manufacturer and model for CyberPower
|
||||
if (data.manufacturer.empty()) {
|
||||
data.manufacturer = "CyberPower";
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
bool CyberPowerProtocol::send_hid_report(const HidReport &report, HidReport &response) {
|
||||
ESP_LOGVV(CP_TAG, "Sending HID report ID: 0x%02X, %d bytes", report.report_id, report.data.size());
|
||||
|
||||
// Construct HID report packet
|
||||
std::vector<uint8_t> hid_packet;
|
||||
hid_packet.push_back(report.report_id);
|
||||
hid_packet.insert(hid_packet.end(), report.data.begin(), report.data.end());
|
||||
|
||||
std::vector<uint8_t> raw_response;
|
||||
if (!send_command(hid_packet, raw_response, 2000)) {
|
||||
ESP_LOGW(CP_TAG, "Failed to send HID report");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (raw_response.empty()) {
|
||||
ESP_LOGW(CP_TAG, "Empty HID response");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Parse response
|
||||
response.report_id = raw_response[0];
|
||||
response.data.clear();
|
||||
if (raw_response.size() > 1) {
|
||||
response.data.assign(raw_response.begin() + 1, raw_response.end());
|
||||
}
|
||||
|
||||
ESP_LOGVV(CP_TAG, "HID response ID: 0x%02X, %d bytes", response.report_id, response.data.size());
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CyberPowerProtocol::parse_hid_data(const HidReport &report, UpsData &data) {
|
||||
if (report.data.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ESP_LOGVV(CP_TAG, "Parsing HID report ID: 0x%02X", report.report_id);
|
||||
|
||||
switch (report.report_id) {
|
||||
case CP_REPORT_ID_UPS_STATUS:
|
||||
return parse_status_report(report, data);
|
||||
|
||||
case CP_REPORT_ID_BATTERY_INFO:
|
||||
return parse_battery_report(report, data);
|
||||
|
||||
case CP_REPORT_ID_VOLTAGE_INFO:
|
||||
return parse_voltage_report(report, data);
|
||||
|
||||
case CP_REPORT_ID_DEVICE_INFO:
|
||||
return parse_device_info_report(report, data);
|
||||
|
||||
default:
|
||||
ESP_LOGW(CP_TAG, "Unknown HID report ID: 0x%02X", report.report_id);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool CyberPowerProtocol::parse_status_report(const HidReport &report, UpsData &data) {
|
||||
if (report.data.size() < 4) {
|
||||
ESP_LOGW(CP_TAG, "Status report too short: %d bytes", report.data.size());
|
||||
return false;
|
||||
}
|
||||
|
||||
// CyberPower status format (varies by model, this is a common interpretation)
|
||||
uint8_t status_byte = report.data[0];
|
||||
uint8_t flags_byte = report.data[1];
|
||||
|
||||
data.status_flags = 0;
|
||||
|
||||
// Parse status bits (CyberPower specific)
|
||||
if (status_byte & 0x01) data.status_flags |= UPS_STATUS_ONLINE;
|
||||
if (status_byte & 0x02) data.status_flags |= UPS_STATUS_ON_BATTERY;
|
||||
if (status_byte & 0x04) data.status_flags |= UPS_STATUS_LOW_BATTERY;
|
||||
if (status_byte & 0x08) data.status_flags |= UPS_STATUS_REPLACE_BATTERY;
|
||||
if (status_byte & 0x10) data.status_flags |= UPS_STATUS_CHARGING;
|
||||
if (status_byte & 0x20) data.status_flags |= UPS_STATUS_FAULT;
|
||||
if (status_byte & 0x40) data.status_flags |= UPS_STATUS_OVERLOAD;
|
||||
|
||||
ESP_LOGV(CP_TAG, "Status: 0x%02X, Flags: 0x%08X", status_byte, data.status_flags);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CyberPowerProtocol::parse_battery_report(const HidReport &report, UpsData &data) {
|
||||
if (report.data.size() < 6) {
|
||||
ESP_LOGW(CP_TAG, "Battery report too short: %d bytes", report.data.size());
|
||||
return false;
|
||||
}
|
||||
|
||||
// CyberPower battery format (little-endian)
|
||||
uint16_t battery_level_raw = report.data[0] | (report.data[1] << 8);
|
||||
uint16_t runtime_raw = report.data[2] | (report.data[3] << 8);
|
||||
|
||||
// Battery level is typically 0-100 or 0-1000 scale
|
||||
if (battery_level_raw <= 100) {
|
||||
data.battery_level = static_cast<float>(battery_level_raw);
|
||||
} else if (battery_level_raw <= 1000) {
|
||||
data.battery_level = static_cast<float>(battery_level_raw) / 10.0f;
|
||||
} else {
|
||||
ESP_LOGW(CP_TAG, "Invalid battery level: %u", battery_level_raw);
|
||||
data.battery_level = NAN;
|
||||
}
|
||||
|
||||
// Runtime is typically in seconds or minutes
|
||||
if (runtime_raw < 3600) { // Assume minutes if < 3600
|
||||
data.runtime_minutes = static_cast<float>(runtime_raw);
|
||||
} else { // Assume seconds if >= 3600
|
||||
data.runtime_minutes = static_cast<float>(runtime_raw) / 60.0f;
|
||||
}
|
||||
|
||||
ESP_LOGV(CP_TAG, "Battery: %.1f%%, Runtime: %.1f min", data.battery_level, data.runtime_minutes);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CyberPowerProtocol::parse_voltage_report(const HidReport &report, UpsData &data) {
|
||||
if (report.data.size() < 8) {
|
||||
ESP_LOGW(CP_TAG, "Voltage report too short: %d bytes", report.data.size());
|
||||
return false;
|
||||
}
|
||||
|
||||
// CyberPower voltage format (little-endian, typically in 0.1V units)
|
||||
uint16_t input_voltage_raw = report.data[0] | (report.data[1] << 8);
|
||||
uint16_t output_voltage_raw = report.data[2] | (report.data[3] << 8);
|
||||
uint16_t load_raw = report.data[4] | (report.data[5] << 8);
|
||||
uint16_t frequency_raw = report.data[6] | (report.data[7] << 8);
|
||||
|
||||
// Convert raw values to actual units
|
||||
data.input_voltage = static_cast<float>(input_voltage_raw) / 10.0f;
|
||||
data.output_voltage = static_cast<float>(output_voltage_raw) / 10.0f;
|
||||
|
||||
// Load can be in percentage or watts - assume percentage if <= 100
|
||||
if (load_raw <= 100) {
|
||||
data.load_percent = static_cast<float>(load_raw);
|
||||
} else {
|
||||
// Convert watts to percentage (assuming 1000W max capacity)
|
||||
data.load_percent = static_cast<float>(load_raw) / 10.0f;
|
||||
}
|
||||
|
||||
// Frequency is typically in 0.1 Hz units
|
||||
data.frequency = static_cast<float>(frequency_raw) / 10.0f;
|
||||
|
||||
ESP_LOGV(CP_TAG, "Input: %.1fV, Output: %.1fV, Load: %.1f%%, Freq: %.1fHz",
|
||||
data.input_voltage, data.output_voltage, data.load_percent, data.frequency);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CyberPowerProtocol::parse_device_info_report(const HidReport &report, UpsData &data) {
|
||||
if (report.data.size() < 16) {
|
||||
ESP_LOGW(CP_TAG, "Device info report too short: %d bytes", report.data.size());
|
||||
return false;
|
||||
}
|
||||
|
||||
// Extract model and serial number from device info
|
||||
// This is a simplified parser - actual format varies by model
|
||||
|
||||
std::string info_string = bytes_to_string(report.data);
|
||||
|
||||
// Look for model information
|
||||
if (info_string.find("CP") != std::string::npos ||
|
||||
info_string.find("CYBER") != std::string::npos) {
|
||||
size_t start = info_string.find_first_not_of(" \t\n\r");
|
||||
size_t end = info_string.find_first_of(" \t\n\r", start);
|
||||
if (start != std::string::npos) {
|
||||
data.model = info_string.substr(start, end - start);
|
||||
}
|
||||
}
|
||||
|
||||
data.manufacturer = "CyberPower";
|
||||
|
||||
ESP_LOGV(CP_TAG, "Device: %s %s", data.manufacturer.c_str(), data.model.c_str());
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace nut_ups
|
||||
} // namespace esphome
|
||||
Reference in new issue
Block a user