Minor cleanup, add missing command for updating OTA credentials

This commit is contained in:
Lorow
2025-05-25 16:31:42 +02:00
parent cd2791ba6f
commit b5c6bc3765
7 changed files with 48 additions and 19 deletions

View File

@@ -27,6 +27,39 @@ CommandResult setDeviceModeCommand(std::shared_ptr<DependencyRegistry> registry,
return CommandResult::getSuccessResult("Device mode set");
}
CommandResult updateOTACredentialsCommand(std::shared_ptr<DependencyRegistry> registry, std::string_view jsonPayload) {
const auto parsedJson = cJSON_Parse(jsonPayload.data());
if (parsedJson == nullptr) {
return CommandResult::getErrorResult("Invalid payload");
}
const auto projectConfig = registry->resolve<ProjectConfig>(DependencyType::project_config);
const auto oldDeviceConfig = projectConfig->getDeviceConfig();
auto OTALogin = oldDeviceConfig.OTALogin;
auto OTAPassword = oldDeviceConfig.OTAPassword;
auto OTAPort = oldDeviceConfig.OTAPort;
if (const auto OTALoginObject = cJSON_GetObjectItem(parsedJson, "login"); OTALoginObject != nullptr) {
if (const auto newLogin = OTALoginObject->valuestring; strcmp(newLogin, "") != 0) {
OTALogin = newLogin;
}
}
if (const auto OTAPasswordObject = cJSON_GetObjectItem(parsedJson, s"password"); OTAPasswordObject != nullptr) {
OTAPassword = OTAPasswordObject->valuestring;
}
if (const auto OTAPortObject = cJSON_GetObjectItem(parsedJson, "port"); OTAPortObject != nullptr) {
if (const auto newPort = OTAPortObject->valueint; newPort >= 82) {
OTAPort = newPort;
}
}
projectConfig->setDeviceConfig(OTALogin, OTAPassword, OTAPort);
return CommandResult::getSuccessResult("OTA Config set");
}
CommandResult restartDeviceCommand() {
OpenIrisTasks::ScheduleRestart(2000);
return CommandResult::getSuccessResult("Device restarted");

View File

@@ -4,4 +4,6 @@
CommandResult setDeviceModeCommand(std::shared_ptr<DependencyRegistry> registry, std::string_view jsonPayload);
CommandResult updateOTACredentialsCommand(std::shared_ptr<DependencyRegistry> registry, std::string_view jsonPayload);
CommandResult restartDeviceCommand();