feat: initial prototype

Signed-off-by: bullshit <coding@ow-software.pl>
This commit is contained in:
bullshit committed 2025-08-06 20:20:37 +02:00
1 parent 41242b0ef1
commit 6a9303a4d7
16 files changed
+1843 -8

No files matched your search

+115
View File
@@ -0,0 +1,115 @@
#include "nut_ups.h"
#include "esphome/core/log.h"
namespace esphome {
namespace nut_ups {
static const char *const GEN_TAG = "nut_ups.generic";
bool GenericHidProtocol::detect() {
ESP_LOGD(GEN_TAG, "Detecting Generic HID Protocol...");
// Generic HID detection - try basic USB HID communication
std::vector<uint8_t> test_command = {0x01, 0x00}; // Basic HID report request
std::vector<uint8_t> response;
if (send_command(test_command, response, 1000)) {
ESP_LOGD(GEN_TAG, "Generic HID response received, %d bytes", response.size());
return true;
}
ESP_LOGD(GEN_TAG, "No Generic HID response");
return false;
}
bool GenericHidProtocol::initialize() {
ESP_LOGD(GEN_TAG, "Initializing Generic HID Protocol...");
// Generic initialization - just verify we can communicate
std::vector<uint8_t> init_command = {0x00}; // Null command
std::vector<uint8_t> response;
if (send_command(init_command, response, 2000)) {
ESP_LOGI(GEN_TAG, "Generic HID Protocol initialized successfully");
return true;
}
ESP_LOGW(GEN_TAG, "Generic HID Protocol initialization failed");
return false;
}
bool GenericHidProtocol::read_data(UpsData &data) {
ESP_LOGV(GEN_TAG, "Reading Generic HID UPS data...");
// Generic HID implementation - this provides basic functionality
// for UPS devices that don't match specific protocols
bool success = false;
std::vector<uint8_t> response;
// Try to read status information using common HID report IDs
for (uint8_t report_id = 1; report_id <= 4; report_id++) {
std::vector<uint8_t> request = {report_id, 0x00};
if (send_command(request, response, 1000) && !response.empty()) {
success = true;
// Parse generic HID response
if (response.size() >= 4) {
// Try to extract meaningful data from generic response
// This is a best-effort approach for unknown devices
uint8_t status_byte = response[0];
// Basic status interpretation
data.status_flags = 0;
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;
// Try to extract numeric values (very generic interpretation)
if (response.size() >= 6) {
uint16_t value1 = response[2] | (response[3] << 8);
uint16_t value2 = response[4] | (response[5] << 8);
// Guess what these values might represent
if (value1 <= 100) {
data.battery_level = static_cast<float>(value1);
}
if (value2 >= 80 && value2 <= 300) {
data.input_voltage = static_cast<float>(value2);
}
}
ESP_LOGV(GEN_TAG, "Generic data parsed: Battery=%.1f%%, Status=0x%08X",
data.battery_level, data.status_flags);
}
break; // Found some data, stop trying other report IDs
}
}
// Set generic device information
if (data.manufacturer.empty()) {
data.manufacturer = "Generic";
}
if (data.model.empty()) {
data.model = "HID UPS";
}
if (!success) {
ESP_LOGW(GEN_TAG, "Failed to read any generic HID data");
// Provide minimal status to indicate device presence
data.status_flags = UPS_STATUS_ONLINE;
data.manufacturer = "Generic";
data.model = "Unknown UPS";
success = true; // Still consider it successful to maintain connection
}
return success;
}
} // namespace nut_ups
} // namespace esphome