Files
openrouter/src/settings/wifi/status.cpp
T
wt f4bcead439
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make (push) Has been cancelled
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake (push) Has been cancelled
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson (push) Has been cancelled
formated code
2025-02-13 19:25:56 +07:00

88 lines
3.4 KiB
C++

#include <settings/wifi/wifi.hpp>
#include <colors.h>
#include <json.hpp>
#include <types.hpp>
#include <syslog.h>
#include <auth/auth.hpp>
#include <helpers.hpp>
#include <config/config.hpp>
#define WIFI_REQUIRED_STRING R"({"required":"[token,status]"})"
namespace api
{
namespace settings
{
namespace wifi
{
void get_wifi_status(const httplib::Request& request, httplib::Response& response)
{
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
response.set_header("Access-Control-Allow-Origin", "*");
if (!check_body(request))
{
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
return;
}
nlohmann::json json_body = nlohmann::json::parse(request.body);
if (!authenticate(json_body["token"]))
{
response.set_content(R"({"access":"reject"})", JSON_TYPE);
return;
}
int wifi_status = config::config.getConfig()["settings"]["wifi"]["enabled"];
std::stringstream responsedata;
responsedata << R"({"status":")" << wifi_status << R"("})";
response.set_content(responsedata.str(), JSON_TYPE);
}
void set_wifi_status(const httplib::Request& request, httplib::Response& response)
{
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
response.set_header("Access-Control-Allow-Origin", "*");
if (!check_body(request))
{
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
return;
}
nlohmann::json json_body = nlohmann::json::parse(request.body);
if (json_body["status"] == nullptr)
{
response.set_content(WIFI_REQUIRED_STRING, JSON_TYPE);
return;
}
if (!authenticate(json_body["token"]))
{
response.set_content(R"({"access":"reject"})", JSON_TYPE);
return;
}
bool new_wifi_status = json_body["status"];
if (!new_wifi_status)
{
system("rc stop hostapd");
system("rc del hostapd");
syslog(LOG_INFO, "%s", "WIFI is disabled");
}
else
{
system("rc start hostapd");
system("rc add hostapd");
syslog(LOG_INFO, "%s", "WIFI is enabled");
}
config::config.setConfig(std::string("../config.json"));
nlohmann::json new_config = config::config.getConfig();
new_config["settings"]["wifi"]["enabled"] = new_wifi_status;
config::config.setConfig(new_config);
config::config.saveConfig();
int wifi_status = config::config.getConfig()["settings"]["wifi"]["enabled"];
std::stringstream responsedata;
responsedata << R"({"status":")" << wifi_status << R"("})";
response.set_content(responsedata.str(), JSON_TYPE);
}
}
}
}