#include #include #include #include #include #include #include #include #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); } } } }