feat(ups-hid): timer delay commands

Signed-off-by: bullshit <coding@ow-software.pl>
This commit is contained in:
bullshit committed 2025-08-26 09:27:32 +02:00
1 parent 58e5cc71f7
commit ae6cf92f27
19 files changed
+1008 -40

No files matched your search

+102
View File
@@ -858,6 +858,108 @@ float GenericHidProtocol::parse_frequency_from_report(uint8_t* data, size_t len)
return NAN;
}
// Delay configuration methods
bool GenericHidProtocol::set_shutdown_delay(int seconds) {
ESP_LOGI(TAG, "Setting shutdown delay to %d seconds (Generic HID)", seconds);
// Validate range (0-7200 seconds = 0-2 hours)
if (seconds < 0 || seconds > 7200) {
ESP_LOGW(TAG, "Shutdown delay %d seconds out of range (0-7200)", seconds);
return false;
}
// Try common report IDs for delay configuration
// Different vendors use different report IDs, so we'll try the most common ones
const std::vector<uint8_t> delay_report_ids = {
0x15, // CyberPower uses this
0x41, // APC uses this
0x57, // Some other vendors
HID_USAGE_POW_DELAY_BEFORE_SHUTDOWN, // Standard HID usage
};
bool success = false;
for (uint8_t report_id : delay_report_ids) {
uint8_t delay_data[2];
delay_data[0] = seconds & 0xFF; // Low byte
delay_data[1] = (seconds >> 8) & 0xFF; // High byte
ESP_LOGD(TAG, "Trying shutdown delay on report 0x%02X: %d seconds", report_id, seconds);
esp_err_t ret = parent_->hid_set_report(HID_REPORT_TYPE_FEATURE, report_id,
delay_data, 2, parent_->get_protocol_timeout());
if (ret == ESP_OK) {
ESP_LOGI(TAG, "Shutdown delay set successfully via report 0x%02X", report_id);
success = true;
break;
}
}
if (!success) {
ESP_LOGW(TAG, "Failed to set shutdown delay - no compatible report found");
}
return success;
}
bool GenericHidProtocol::set_start_delay(int seconds) {
ESP_LOGI(TAG, "Setting start delay to %d seconds (Generic HID)", seconds);
// Validate range (0-7200 seconds = 0-2 hours)
if (seconds < 0 || seconds > 7200) {
ESP_LOGW(TAG, "Start delay %d seconds out of range (0-7200)", seconds);
return false;
}
// Try common report IDs for start delay configuration
const std::vector<uint8_t> delay_report_ids = {
0x16, // CyberPower uses this
0x40, // APC might use this
0x58, // Some other vendors
HID_USAGE_POW_DELAY_BEFORE_STARTUP, // Standard HID usage
};
bool success = false;
for (uint8_t report_id : delay_report_ids) {
uint8_t delay_data[2];
delay_data[0] = seconds & 0xFF; // Low byte
delay_data[1] = (seconds >> 8) & 0xFF; // High byte
ESP_LOGD(TAG, "Trying start delay on report 0x%02X: %d seconds", report_id, seconds);
esp_err_t ret = parent_->hid_set_report(HID_REPORT_TYPE_FEATURE, report_id,
delay_data, 2, parent_->get_protocol_timeout());
if (ret == ESP_OK) {
ESP_LOGI(TAG, "Start delay set successfully via report 0x%02X", report_id);
success = true;
break;
}
}
if (!success) {
ESP_LOGW(TAG, "Failed to set start delay - no compatible report found");
}
return success;
}
bool GenericHidProtocol::set_reboot_delay(int seconds) {
ESP_LOGI(TAG, "Setting reboot delay to %d seconds (Generic HID)", seconds);
// For generic HID, reboot typically involves setting both shutdown and start delays
bool shutdown_ok = set_shutdown_delay(seconds);
bool start_ok = set_start_delay(seconds);
if (shutdown_ok || start_ok) {
ESP_LOGI(TAG, "Reboot delay partially set to %d seconds", seconds);
return true; // Return true if at least one succeeded
} else {
ESP_LOGW(TAG, "Failed to set any reboot delay");
return false;
}
}
} // namespace ups_hid
} // namespace esphome