mirror of
https://github.com/EyeTrackVR/OpenIris.git
synced 2025-11-04 15:39:42 +08:00
Make project config use update methods
This commit is contained in:
parent
a085b6d885
commit
18cf39f291
@ -213,59 +213,44 @@ void ProjectConfig::load() {
|
|||||||
//! DeviceConfig
|
//! DeviceConfig
|
||||||
//*
|
//*
|
||||||
//**********************************************************************************************************************
|
//**********************************************************************************************************************
|
||||||
void ProjectConfig::setDeviceConfig(const std::string& OTALogin,
|
void ProjectConfig::setDeviceConfig(JsonVariant& data, bool shouldNotify) {
|
||||||
const std::string& OTAPassword,
|
|
||||||
int OTAPort,
|
|
||||||
bool shouldNotify) {
|
|
||||||
log_d("Updating device config");
|
log_d("Updating device config");
|
||||||
this->config.device.OTALogin.assign(OTALogin);
|
for (JsonPair kv : data.as<JsonObject>()) {
|
||||||
this->config.device.OTAPassword.assign(OTAPassword);
|
config.device.update(kv.key().c_str(), kv.value());
|
||||||
this->config.device.OTAPort = OTAPort;
|
}
|
||||||
|
|
||||||
if (shouldNotify)
|
if (shouldNotify)
|
||||||
this->notifyAll(ConfigState_e::deviceConfigUpdated);
|
this->notifyAll(ConfigState_e::deviceConfigUpdated);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ProjectConfig::setMDNSConfig(const std::string& hostname,
|
void ProjectConfig::setMDNSConfig(JsonVariant& data, bool shouldNotify) {
|
||||||
const std::string& service,
|
|
||||||
bool shouldNotify) {
|
|
||||||
log_d("Updating MDNS config");
|
log_d("Updating MDNS config");
|
||||||
this->config.mdns.hostname.assign(hostname);
|
|
||||||
this->config.mdns.service.assign(service);
|
for (JsonPair kv : data.as<JsonObject>()) {
|
||||||
|
this->config.mdns.update(kv.key().c_str(), kv.value());
|
||||||
|
}
|
||||||
|
|
||||||
if (shouldNotify)
|
if (shouldNotify)
|
||||||
this->notifyAll(ConfigState_e::mdnsConfigUpdated);
|
this->notifyAll(ConfigState_e::mdnsConfigUpdated);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ProjectConfig::setCameraConfig(uint8_t vflip,
|
void ProjectConfig::setCameraConfig(JsonVariant& data, bool shouldNotify) {
|
||||||
uint8_t framesize,
|
|
||||||
uint8_t href,
|
|
||||||
uint8_t quality,
|
|
||||||
uint8_t brightness,
|
|
||||||
bool shouldNotify) {
|
|
||||||
log_d("Updating camera config");
|
log_d("Updating camera config");
|
||||||
this->config.camera.vflip = vflip;
|
|
||||||
this->config.camera.href = href;
|
|
||||||
this->config.camera.framesize = framesize;
|
|
||||||
this->config.camera.quality = quality;
|
|
||||||
this->config.camera.brightness = brightness;
|
|
||||||
|
|
||||||
log_d("Updating Camera config");
|
for (JsonPair kv : data.as<JsonObject>()) {
|
||||||
|
this->config.camera.update(kv.key().c_str(), kv.value());
|
||||||
|
}
|
||||||
|
|
||||||
if (shouldNotify)
|
if (shouldNotify)
|
||||||
this->notifyAll(ConfigState_e::cameraConfigUpdated);
|
this->notifyAll(ConfigState_e::cameraConfigUpdated);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ProjectConfig::setWifiConfig(const std::string& networkName,
|
void ProjectConfig::setWifiConfig(JsonVariant& data, bool shouldNotify) {
|
||||||
const std::string& ssid,
|
|
||||||
const std::string& password,
|
|
||||||
uint8_t channel,
|
|
||||||
uint8_t power,
|
|
||||||
bool adhoc,
|
|
||||||
bool shouldNotify) {
|
|
||||||
// we store the ADHOC flag as false because the networks we store in the
|
// we store the ADHOC flag as false because the networks we store in the
|
||||||
// config are the ones we want the esp to connect to, rather than host as AP,
|
// config are the ones we want the esp to connect to, rather than host as AP,
|
||||||
// and here we're just updating them
|
// and here we're just updating them
|
||||||
size_t size = this->config.networks.size();
|
size_t size = this->config.networks.size();
|
||||||
|
auto networkName = data["name"].as<std::string>();
|
||||||
|
|
||||||
for (auto it = this->config.networks.begin();
|
for (auto it = this->config.networks.begin();
|
||||||
it != this->config.networks.end();) {
|
it != this->config.networks.end();) {
|
||||||
@ -273,12 +258,10 @@ void ProjectConfig::setWifiConfig(const std::string& networkName,
|
|||||||
log_i("[Project Config]: Found network %s, updating it ...",
|
log_i("[Project Config]: Found network %s, updating it ...",
|
||||||
it->name.c_str());
|
it->name.c_str());
|
||||||
|
|
||||||
it->name = networkName;
|
// we found the network, so we can just update it with the new values
|
||||||
it->ssid = ssid;
|
for (JsonPair kv : data.as<JsonObject>()) {
|
||||||
it->password = password;
|
it->update(kv.key().c_str(), kv.value());
|
||||||
it->channel = channel;
|
}
|
||||||
it->power = power;
|
|
||||||
it->adhoc = false;
|
|
||||||
|
|
||||||
if (shouldNotify) {
|
if (shouldNotify) {
|
||||||
wifiStateManager.setState(WiFiState_e::WiFiState_Disconnected);
|
wifiStateManager.setState(WiFiState_e::WiFiState_Disconnected);
|
||||||
@ -293,20 +276,19 @@ void ProjectConfig::setWifiConfig(const std::string& networkName,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (size < 3 && size > 0) {
|
if (size < 3) {
|
||||||
|
if (size == 0)
|
||||||
|
Serial.println("No networks, We're adding a new network");
|
||||||
|
else
|
||||||
Serial.println("We're adding a new network");
|
Serial.println("We're adding a new network");
|
||||||
// we don't have that network yet, we can add it as we still have some
|
// we don't have that network yet, we can add it as we still have some
|
||||||
// space we're using emplace_back as push_back will create a copy of it,
|
// space we're using emplace_back as push_back will create a copy of it,
|
||||||
// we want to avoid that
|
// we want to avoid that
|
||||||
this->config.networks.emplace_back(networkName, ssid, password, channel,
|
this->config.networks.emplace_back(
|
||||||
power, false);
|
networkName, data["ssid"].as<std::string>(),
|
||||||
}
|
// todo add validation for this, maybe deconstruct it before emplacing
|
||||||
|
data["password"].as<std::string>(), data["channel"].as<std::string>(),
|
||||||
// we're allowing to store up to three additional networks
|
data["power"].as<std::string>(), false);
|
||||||
if (size == 0) {
|
|
||||||
Serial.println("No networks, We're adding a new network");
|
|
||||||
this->config.networks.emplace_back(networkName, ssid, password, channel,
|
|
||||||
power, false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (shouldNotify) {
|
if (shouldNotify) {
|
||||||
@ -324,17 +306,19 @@ void ProjectConfig::deleteWifiConfig(const std::string& networkName,
|
|||||||
Serial.println("No networks, nothing to delete");
|
Serial.println("No networks, nothing to delete");
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto it = this->config.networks.begin();
|
auto networkPredicate = [networkName](WiFiConfig_t network) {
|
||||||
it != this->config.networks.end();) {
|
return network.name == networkName;
|
||||||
if (it->name == networkName) {
|
};
|
||||||
log_i("[Project Config]: Found network %s", it->name.c_str());
|
|
||||||
it = this->config.networks.erase(it);
|
|
||||||
log_i("[Project Config]: Deleted network %s", networkName.c_str());
|
|
||||||
|
|
||||||
} else {
|
if (auto networkToBeDeleted =
|
||||||
++it;
|
std::find_if(this->config.networks.begin(),
|
||||||
}
|
this->config.networks.end(), networkPredicate);
|
||||||
}
|
networkToBeDeleted != this->config.networks.end()) {
|
||||||
|
log_i("[Project Config]: Found network %s",
|
||||||
|
networkToBeDeleted->name.c_str());
|
||||||
|
this->config.networks.erase(networkToBeDeleted);
|
||||||
|
log_i("[Project Config]: Deleted network %s", networkName.c_str());
|
||||||
|
};
|
||||||
|
|
||||||
if (shouldNotify) {
|
if (shouldNotify) {
|
||||||
this->wifiConfigSave();
|
this->wifiConfigSave();
|
||||||
@ -343,24 +327,20 @@ void ProjectConfig::deleteWifiConfig(const std::string& networkName,
|
|||||||
}
|
}
|
||||||
|
|
||||||
void ProjectConfig::setWiFiTxPower(uint8_t power, bool shouldNotify) {
|
void ProjectConfig::setWiFiTxPower(uint8_t power, bool shouldNotify) {
|
||||||
this->config.txpower.power = power;
|
|
||||||
log_d("Updating wifi tx power");
|
log_d("Updating wifi tx power");
|
||||||
|
|
||||||
|
this->config.txpower.power = power;
|
||||||
if (shouldNotify)
|
if (shouldNotify)
|
||||||
this->notifyAll(ConfigState_e::wifiTxPowerUpdated);
|
this->notifyAll(ConfigState_e::wifiTxPowerUpdated);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ProjectConfig::setAPWifiConfig(const std::string& ssid,
|
void ProjectConfig::setAPWifiConfig(JsonVariant& data, bool shouldNotify) {
|
||||||
const std::string& password,
|
|
||||||
uint8_t channel,
|
|
||||||
bool adhoc,
|
|
||||||
bool shouldNotify) {
|
|
||||||
this->config.ap_network.ssid.assign(ssid);
|
|
||||||
this->config.ap_network.password.assign(password);
|
|
||||||
this->config.ap_network.channel = channel;
|
|
||||||
this->config.ap_network.adhoc = adhoc;
|
|
||||||
|
|
||||||
log_d("Updating access point config");
|
log_d("Updating access point config");
|
||||||
|
|
||||||
|
for (JsonPair kv : data.as<JsonObject>()) {
|
||||||
|
config.device.update(kv.key().c_str(), kv.value());
|
||||||
|
}
|
||||||
|
|
||||||
if (shouldNotify) {
|
if (shouldNotify) {
|
||||||
wifiStateManager.setState(WiFiState_e::WiFiState_None);
|
wifiStateManager.setState(WiFiState_e::WiFiState_None);
|
||||||
WiFi.disconnect();
|
WiFi.disconnect();
|
||||||
|
|||||||
@ -2,9 +2,12 @@
|
|||||||
#ifndef PROJECT_CONFIG_HPP
|
#ifndef PROJECT_CONFIG_HPP
|
||||||
#define PROJECT_CONFIG_HPP
|
#define PROJECT_CONFIG_HPP
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
#include <ArduinoJson.h>
|
||||||
#include <Preferences.h>
|
#include <Preferences.h>
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <variant>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "data/StateManager/StateManager.hpp"
|
#include "data/StateManager/StateManager.hpp"
|
||||||
@ -33,22 +36,62 @@ class ProjectConfig : public Preferences, public ISubject<ConfigState_e> {
|
|||||||
std::string OTAPassword;
|
std::string OTAPassword;
|
||||||
int OTAPort;
|
int OTAPort;
|
||||||
std::string toRepresentation();
|
std::string toRepresentation();
|
||||||
|
|
||||||
|
void update(std::string field, JsonVariant value) {
|
||||||
|
// this technically could be done with a hashmap that takes
|
||||||
|
// and std::function<void(JsonVariant)> and some lambda captures
|
||||||
|
// but it seemed too unvieldy for the time being
|
||||||
|
|
||||||
|
if (field == "OTALogin") {
|
||||||
|
this->OTALogin = value.as<std::string>();
|
||||||
|
} else if (field == "OTAPassword") {
|
||||||
|
this->OTAPassword = value.as<std::string>();
|
||||||
|
} else if (field == "OTAPort") {
|
||||||
|
this->OTAPort = value.as<int>();
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct MDNSConfig_t {
|
struct MDNSConfig_t {
|
||||||
std::string hostname;
|
std::string hostname;
|
||||||
std::string service;
|
std::string service;
|
||||||
std::string toRepresentation();
|
std::string toRepresentation();
|
||||||
|
|
||||||
|
void update(std::string field, JsonVariant value) {
|
||||||
|
if (field == "hostname") {
|
||||||
|
this->hostname = std::move(value.as<std::string>());
|
||||||
|
} else if (field == "service") {
|
||||||
|
this->service = std::move(value.as<std::string>());
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct CameraConfig_t {
|
struct CameraConfig_t {
|
||||||
|
public:
|
||||||
uint8_t vflip;
|
uint8_t vflip;
|
||||||
uint8_t href;
|
uint8_t href;
|
||||||
uint8_t framesize;
|
uint8_t framesize;
|
||||||
uint8_t quality;
|
uint8_t quality;
|
||||||
uint8_t brightness;
|
uint8_t brightness;
|
||||||
|
uint8_t fps;
|
||||||
|
|
||||||
std::string toRepresentation();
|
std::string toRepresentation();
|
||||||
|
|
||||||
|
void update(std::string field, JsonVariant value) {
|
||||||
|
if (field == "vflip") {
|
||||||
|
this->vflip = value.as<uint8_t>();
|
||||||
|
} else if (field == "href") {
|
||||||
|
this->href = value.as<uint8_t>();
|
||||||
|
} else if (field == "framesize") {
|
||||||
|
this->framesize = value.as<uint8_t>();
|
||||||
|
} else if (field == "quality") {
|
||||||
|
this->quality = value.as<uint8_t>();
|
||||||
|
} else if (field == "brightness") {
|
||||||
|
this->brightness = value.as<uint8_t>();
|
||||||
|
} else if (field == "fps") {
|
||||||
|
this->fps = value.as<uint8_t>();
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct WiFiConfig_t {
|
struct WiFiConfig_t {
|
||||||
@ -73,6 +116,22 @@ class ProjectConfig : public Preferences, public ISubject<ConfigState_e> {
|
|||||||
bool adhoc;
|
bool adhoc;
|
||||||
|
|
||||||
std::string toRepresentation();
|
std::string toRepresentation();
|
||||||
|
|
||||||
|
void update(std::string field, JsonVariant value) {
|
||||||
|
if (field == "name") {
|
||||||
|
this->name = std::move(value.as<std::string>());
|
||||||
|
} else if (field == "ssid") {
|
||||||
|
this->ssid = std::move(value.as<std::string>());
|
||||||
|
} else if (field == "password") {
|
||||||
|
this->password = std::move(value.as<std::string>());
|
||||||
|
} else if (field == "channel") {
|
||||||
|
this->channel = value.as<uint8_t>();
|
||||||
|
} else if (field == "power") {
|
||||||
|
this->power = value.as<uint8_t>();
|
||||||
|
} else if (field == "adhoc") {
|
||||||
|
this->adhoc = value.as<bool>();
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct AP_WiFiConfig_t {
|
struct AP_WiFiConfig_t {
|
||||||
@ -81,11 +140,23 @@ class ProjectConfig : public Preferences, public ISubject<ConfigState_e> {
|
|||||||
uint8_t channel;
|
uint8_t channel;
|
||||||
bool adhoc;
|
bool adhoc;
|
||||||
std::string toRepresentation();
|
std::string toRepresentation();
|
||||||
|
void update(std::string field, JsonVariant value) {
|
||||||
|
if (field == "ssid") {
|
||||||
|
this->ssid = std::move(value.as<std::string>());
|
||||||
|
} else if (field == "password") {
|
||||||
|
this->password = std::move(value.as<std::string>());
|
||||||
|
} else if (field == "channel") {
|
||||||
|
this->channel = value.as<uint8_t>();
|
||||||
|
} else if (field == "adhoc") {
|
||||||
|
this->adhoc = value.as<bool>();
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct WiFiTxPower_t {
|
struct WiFiTxPower_t {
|
||||||
uint8_t power;
|
uint8_t power;
|
||||||
std::string toRepresentation();
|
std::string toRepresentation();
|
||||||
|
void update(uint8_t power) { this->power = power; }
|
||||||
};
|
};
|
||||||
|
|
||||||
struct TrackerConfig_t {
|
struct TrackerConfig_t {
|
||||||
@ -104,31 +175,13 @@ class ProjectConfig : public Preferences, public ISubject<ConfigState_e> {
|
|||||||
MDNSConfig_t& getMDNSConfig();
|
MDNSConfig_t& getMDNSConfig();
|
||||||
WiFiTxPower_t& getWiFiTxPowerConfig();
|
WiFiTxPower_t& getWiFiTxPowerConfig();
|
||||||
|
|
||||||
void setDeviceConfig(const std::string& OTALogin,
|
void setDeviceConfig(JsonVariant& data, bool shouldNotify);
|
||||||
const std::string& OTAPassword,
|
|
||||||
int OTAPort,
|
void setMDNSConfig(JsonVariant& data, bool shouldNotify);
|
||||||
bool shouldNotify);
|
void setCameraConfig(JsonVariant& data, bool shouldNotify);
|
||||||
void setMDNSConfig(const std::string& hostname,
|
void setWifiConfig(JsonVariant& data, bool shouldNotify);
|
||||||
const std::string& service,
|
void setAPWifiConfig(JsonVariant& data, bool shouldNotify);
|
||||||
bool shouldNotify);
|
|
||||||
void setCameraConfig(uint8_t vflip,
|
|
||||||
uint8_t framesize,
|
|
||||||
uint8_t href,
|
|
||||||
uint8_t quality,
|
|
||||||
uint8_t brightness,
|
|
||||||
bool shouldNotify);
|
|
||||||
void setWifiConfig(const std::string& networkName,
|
|
||||||
const std::string& ssid,
|
|
||||||
const std::string& password,
|
|
||||||
uint8_t channel,
|
|
||||||
uint8_t power,
|
|
||||||
bool adhoc,
|
|
||||||
bool shouldNotify);
|
|
||||||
void setAPWifiConfig(const std::string& ssid,
|
|
||||||
const std::string& password,
|
|
||||||
uint8_t channel,
|
|
||||||
bool adhoc,
|
|
||||||
bool shouldNotify);
|
|
||||||
void setWiFiTxPower(uint8_t power, bool shouldNotify);
|
void setWiFiTxPower(uint8_t power, bool shouldNotify);
|
||||||
|
|
||||||
void deleteWifiConfig(const std::string& networkName, bool shouldNotify);
|
void deleteWifiConfig(const std::string& networkName, bool shouldNotify);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user