added mutex #24
@@ -3,12 +3,14 @@
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <json.hpp>
|
||||
#include <mutex>
|
||||
|
||||
class ConfigManager
|
||||
{
|
||||
private:
|
||||
nlohmann::json config;
|
||||
std::fstream configfile;
|
||||
std::mutex configfilemutex;
|
||||
std::string configFileName;
|
||||
public:
|
||||
ConfigManager()
|
||||
@@ -21,6 +23,7 @@ public:
|
||||
}
|
||||
ConfigManager(std::string filename)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(this->configfilemutex);
|
||||
this->configfile.open(filename);
|
||||
this->config = nlohmann::json::parse(this->configfile);
|
||||
this->configfile.close();
|
||||
@@ -28,6 +31,7 @@ public:
|
||||
}
|
||||
ConfigManager(nlohmann::json object, std::string filename)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(this->configfilemutex);
|
||||
this->config = object;
|
||||
this->configFileName = filename;
|
||||
}
|
||||
@@ -50,6 +54,7 @@ public:
|
||||
}
|
||||
int setConfig(std::string filename)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(this->configfilemutex);
|
||||
this->configfile.open(filename);
|
||||
this->config = nlohmann::json::parse(this->configfile);
|
||||
this->configFileName = filename;
|
||||
@@ -113,6 +118,7 @@ public:
|
||||
}
|
||||
int saveConfig()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(this->configfilemutex);
|
||||
this->configfile.open(this->configFileName, std::ios::trunc | std::ios::out | std::ios::in);
|
||||
this->configfile << this->config.dump(4);
|
||||
this->configfile.close();
|
||||
@@ -120,6 +126,7 @@ public:
|
||||
}
|
||||
int saveConfig(std::string filename)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(this->configfilemutex);
|
||||
this->configfile.open(filename, std::ios::trunc | std::ios::out | std::ios::in);
|
||||
this->configfile << this->config.dump(4);
|
||||
this->configfile.close();
|
||||
|
||||
@@ -46,7 +46,6 @@ namespace api
|
||||
if (std::filesystem::exists(config::config.getConfigFileName())) {
|
||||
std::filesystem::remove(config::config.getConfigFileName());
|
||||
std::filesystem::copy("../config_default.json", config::config.getConfigFileName());
|
||||
usleep(300);
|
||||
config::config.setConfig(config::config.getConfigFileName());
|
||||
set_all_settings(config::config.getConfig());
|
||||
response.set_content(R"({"status":true})", JSON_TYPE);
|
||||
|
||||
@@ -80,7 +80,6 @@ namespace api
|
||||
response.set_content(R"({"access":"reject"})", JSON_TYPE);
|
||||
return;
|
||||
}
|
||||
usleep(300);
|
||||
config::config.setConfig(config::config.getConfigFileName());
|
||||
nlohmann::json new_config = config::config.getConfig();
|
||||
new_config["settings"] = json_body["settings"];
|
||||
@@ -89,27 +88,6 @@ namespace api
|
||||
|
||||
set_all_settings(json_body);
|
||||
|
||||
// api::settings::wifi::set_wifi_status(json_body["settings"]["wifi"]["enabled"]);
|
||||
// std::string wifichannel = json_body["settings"]["wifi"]["channel"];
|
||||
// api::settings::wifi::set_wifi_channel(std::stoi(wifichannel));
|
||||
// api::settings::wifi::set_wifi_name(json_body["settings"]["wifi"]["name"]);
|
||||
|
||||
// api::settings::telnet::set_telnet(json_body["settings"]["telnet"]["enabled"]);
|
||||
// std::string telnetport = json_body["settings"]["telnet"]["port"];
|
||||
// api::settings::telnet::set_telnet_port(std::stoi(telnetport));
|
||||
|
||||
// api::settings::ssh::set_ssh_status(json_body["settings"]["ssh"]["enabled"]);
|
||||
// api::settings::ssh::set_ssh_server(json_body["settings"]["ssh"]["server"]);
|
||||
// std::string sshport = json_body["settings"]["ssh"]["port"];
|
||||
// api::settings::ssh::set_ssh_port(std::stoi(sshport));
|
||||
// api::settings::ssh::set_ssh_keys(json_body["settings"]["ssh"]["keys"]);
|
||||
|
||||
// api::settings::ftp::set_ftp(json_body["settings"]["ftp"]["enabled"]);
|
||||
|
||||
// api::settings::dns::set_dns(json_body["settings"]["dns"]["enabled"]);
|
||||
// api::settings::dns::set_dns_servers(json_body["settings"]["dns"]["servers"]);
|
||||
// api::settings::dns::set_local_domains(json_body["settings"]["dns"]["localdomains"]);
|
||||
|
||||
std::string responsedata = R"({"settings":)" + config::config.getConfig()["settings"].dump() + R"(})";
|
||||
response.set_content(responsedata, JSON_TYPE);
|
||||
}
|
||||
|
||||
@@ -65,7 +65,6 @@ namespace api
|
||||
system("rc restart hostapd");
|
||||
std::string syslogstring = "WIFI password changed";
|
||||
syslog(LOG_INFO, "%s", syslogstring.c_str());
|
||||
usleep(500);
|
||||
config::config.setConfig(config::config.getConfigFileName());
|
||||
nlohmann::json new_config = config::config.getConfig();
|
||||
new_config["settings"]["wifi"]["password"] = new_wifi_password;
|
||||
|
||||
Reference in New Issue
Block a user