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

@@ -1,6 +1,6 @@
#pragma once
#ifndef _CAMERAMANAGER_HPP_
#define _CAMERAMANAGER_HPP_
#ifndef CAMERAMANAGER_HPP
#define CAMERAMANAGER_HPP
#include "esp_log.h"
#include "esp_camera.h"
@@ -31,12 +31,11 @@ private:
public:
CameraManager(std::shared_ptr<ProjectConfig> projectConfig, QueueHandle_t eventQueue);
int setCameraResolution(framesize_t frameSize);
bool setupCamera(); // todo, once we have observers, make it private
bool setupCamera();
int setVFlip(int direction);
int setHFlip(int direction);
int setVieWindow(int offsetX, int offsetY, int outputX, int outputY);
void resetCamera(bool type = 0);
void resetCamera(bool type);
private:
void loadConfigData();
@@ -45,4 +44,4 @@ private:
void setupBasicResolution();
};
#endif // _CAMERAMANAGER_HPP_
#endif // CAMERAMANAGER_HPP

View File

@@ -5,10 +5,9 @@ std::unordered_map<std::string, CommandType> commandTypeMap = {
{"set_wifi", CommandType::SET_WIFI},
{"update_wifi", CommandType::UPDATE_WIFI},
{"set_streaming_mode", CommandType::SET_STREAMING_MODE},
{"update_device", CommandType::UPDATE_DEVICE},
{"update_ota_credentials", CommandType::UPDATE_OTA_CREDENTIALS},
{"delete_network", CommandType::DELETE_NETWORK},
{"update_ap_wifi", CommandType::UPDATE_AP_WIFI},
{"update_mdns", CommandType::UPDATE_MDNS},
{"set_mdns", CommandType::SET_MDNS},
{"update_camera", CommandType::UPDATE_CAMERA},
{"restart_camera", CommandType::RESTART_CAMERA},
@@ -18,13 +17,15 @@ std::unordered_map<std::string, CommandType> commandTypeMap = {
{"restart_device", CommandType::RESTART_DEVICE},
};
std::function<CommandResult()> CommandManager::createCommand(CommandType type, std::string_view json) const {
std::function<CommandResult()> CommandManager::createCommand(const CommandType type, std::string_view json) const {
switch (type)
{
case CommandType::PING:
return { PingCommand };
case CommandType::SET_STREAMING_MODE:
case CommandType::SET_STREAMING_MODE:
return [this, json] {return setDeviceModeCommand(this->registry, json); };
case CommandType::UPDATE_OTA_CREDENTIALS:
return [this, json] { return updateOTACredentialsCommand(this->registry, json); };
case CommandType::SET_WIFI:
return [this, json] { return setWiFiCommand(this->registry, json); };
case CommandType::UPDATE_WIFI:
@@ -35,9 +36,6 @@ std::function<CommandResult()> CommandManager::createCommand(CommandType type, s
return [this, json] { return deleteWiFiCommand(this->registry, json); };
case CommandType::SET_MDNS:
return [this, json] { return setMDNSCommand(this->registry, json); };
// updating the mnds name is essentially the same operation
case CommandType::UPDATE_MDNS:
return [this, json] { return setMDNSCommand(this->registry, json); };
case CommandType::UPDATE_CAMERA:
return [this, json] { return updateCameraCommand(this->registry, json); };
case CommandType::RESTART_CAMERA:

View File

@@ -24,12 +24,11 @@ enum class CommandType
None,
PING,
SET_WIFI,
UPDATE_DEVICE,
UPDATE_OTA_CREDENTIALS,
SET_STREAMING_MODE,
UPDATE_WIFI,
DELETE_NETWORK,
UPDATE_AP_WIFI,
UPDATE_MDNS,
SET_MDNS,
UPDATE_CAMERA,
RESTART_CAMERA,

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();

View File

@@ -103,7 +103,7 @@ void RestAPI::handle_update_device(RequestContext *context) {
return;
}
auto const result = command_manager->executeFromType(CommandType::UPDATE_DEVICE, context->body);
auto const result = command_manager->executeFromType(CommandType::UPDATE_OTA_CREDENTIALS, context->body);
auto const code = result.isSuccess() ? 200 : 500;
mg_http_reply(context->connection, code, JSON_RESPONSE, result.getResult().c_str());
}

View File

@@ -31,15 +31,13 @@ namespace UVCStreamHelpers
static esp_err_t camera_start_cb(uvc_format_t format, int width, int height, int rate, void *cb_ctx);
static void camera_stop_cb(void *cb_ctx);
static esp_err_t camera_start_cb(uvc_format_t format, int width, int height, int rate, void *cb_ctx);
static uvc_fb_t *camera_fb_get_cb(void *cb_ctx);
static void camera_fb_return_cb(uvc_fb_t *fb, void *cb_ctx);
}
class UVCStreamManager
{
private:
uint8_t *uvc_buffer;
uint8_t *uvc_buffer = nullptr;
public:
esp_err_t setup();