wifi channel get set added
This commit is contained in:
1 parent
f3628c4f9c
commit
fdbdda37e3
9 files changed
+169
No files matched your search
@@ -24,6 +24,7 @@ add_executable(
|
||||
src/settings/wifi/name.cpp
|
||||
src/settings/wifi/password.cpp
|
||||
src/settings/wifi/qr.cpp
|
||||
src/settings/wifi/channel.cpp
|
||||
|
||||
src/settings/ftp/ftp.cpp
|
||||
|
||||
|
||||
@@ -45,6 +45,8 @@ int main() {
|
||||
srv.Post("/api/settings/wifi/password/get", get_wifi_password);
|
||||
srv.Post("/api/settings/wifi/password/set", set_wifi_password);
|
||||
srv.Post("/api/settings/wifi/qr/get", get_wifi_qr);
|
||||
srv.Post("/api/settings/wifi/channel/get", get_wifi_channel);
|
||||
srv.Post("/api/settings/wifi/channel/set", set_wifi_channel);
|
||||
|
||||
srv.Post("/api/reboot", reboot);
|
||||
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
#include "wifi.hpp"
|
||||
#include <colors.h>
|
||||
#include "../../types.hpp"
|
||||
#include "../../routes.hpp"
|
||||
#include <json.hpp>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <syslog.h>
|
||||
|
||||
#define WIFI_CHANNEL_REQUIRED_STRING R"({"required":"[token,channel]"})"
|
||||
|
||||
void get_wifi_channel(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 (request.body.empty()) {
|
||||
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
|
||||
return;
|
||||
}
|
||||
|
||||
nlohmann::json json_body = nlohmann::json::parse(request.body);
|
||||
if (json_body["token"] == nullptr) {
|
||||
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
|
||||
return;
|
||||
}
|
||||
|
||||
httplib::Client cli(AUTH_SERVICE);
|
||||
|
||||
std::stringstream body;
|
||||
body << R"({"token":)" << json_body["token"] << R"(})";
|
||||
|
||||
httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE);
|
||||
if (res->status == httplib::OK_200) {
|
||||
nlohmann::json resjson = nlohmann::json::parse(res->body);
|
||||
if (resjson["access"] == "reject") {
|
||||
response.set_content(resjson.dump().c_str(), JSON_TYPE);
|
||||
return;
|
||||
}
|
||||
std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH);
|
||||
std::string wifi_channel;
|
||||
std::string wifisettingsbuffer;
|
||||
while (std::getline(wifisettingsfile, wifisettingsbuffer)) {
|
||||
if (wifisettingsbuffer.find("channel=") == 0) {
|
||||
wifi_channel = wifisettingsbuffer;
|
||||
wifi_channel.erase(0, 8);
|
||||
break;
|
||||
}
|
||||
}
|
||||
wifisettingsfile.close();
|
||||
std::stringstream responsedata;
|
||||
responsedata << R"({"channel":")" << wifi_channel << R"("})";
|
||||
response.set_content(responsedata.str(), JSON_TYPE);
|
||||
}
|
||||
}
|
||||
|
||||
void set_wifi_channel(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 (request.body.empty()) {
|
||||
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
|
||||
return;
|
||||
}
|
||||
|
||||
nlohmann::json json_body = nlohmann::json::parse(request.body);
|
||||
if (json_body["token"] == nullptr) {
|
||||
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
|
||||
return;
|
||||
}
|
||||
|
||||
if (json_body["channel"] == nullptr) {
|
||||
response.set_content(WIFI_CHANNEL_REQUIRED_STRING, JSON_TYPE);
|
||||
return;
|
||||
}
|
||||
|
||||
httplib::Client cli(AUTH_SERVICE);
|
||||
|
||||
std::stringstream body;
|
||||
body << R"({"token":)" << json_body["token"] << R"(})";
|
||||
|
||||
httplib::Result res = cli.Post("/api/access", body.str().c_str(), JSON_TYPE);
|
||||
if (res->status == httplib::OK_200) {
|
||||
nlohmann::json resjson = nlohmann::json::parse(res->body);
|
||||
if (resjson["access"] == "reject") {
|
||||
response.set_content(resjson.dump().c_str(), JSON_TYPE);
|
||||
return;
|
||||
}
|
||||
std::ifstream wifisettingsfile(WIFI_SETTINGS_PATH);
|
||||
std::string old_channel;
|
||||
std::string wifisettingsbuffer;
|
||||
while (std::getline(wifisettingsfile, wifisettingsbuffer)) {
|
||||
if (wifisettingsbuffer.find("channel=") == 0) {
|
||||
old_channel = wifisettingsbuffer;
|
||||
old_channel.erase(0, 8);
|
||||
break;
|
||||
}
|
||||
}
|
||||
wifisettingsfile.close();
|
||||
std::string new_channel = json_body["channel"];
|
||||
std::stringstream systemrequest;
|
||||
systemrequest << "sed -i \"/channel=" << old_channel << "/s//channel=" << new_channel << "/\" /etc/hostapd/hostapd.conf";
|
||||
std::cout << systemrequest.str() << std::endl;
|
||||
system(systemrequest.str().c_str());
|
||||
system("rc restart hostapd");
|
||||
std::string syslogstring = "WIFI channel changed to " + new_channel;
|
||||
syslog(LOG_INFO, syslogstring.c_str());
|
||||
|
||||
std::ifstream changedwifisettingsfile(WIFI_SETTINGS_PATH);
|
||||
std::string wifi_channel;
|
||||
std::string changedwifisettingsbuffer;
|
||||
while (std::getline(changedwifisettingsfile, changedwifisettingsbuffer)) {
|
||||
if (changedwifisettingsbuffer.find("channel=") == 0) {
|
||||
wifi_channel = changedwifisettingsbuffer;
|
||||
wifi_channel.erase(0, 8);
|
||||
break;
|
||||
}
|
||||
}
|
||||
changedwifisettingsfile.close();
|
||||
std::stringstream responsedata;
|
||||
responsedata << R"({"channel":")" << wifi_channel << R"("})";
|
||||
response.set_content(responsedata.str(), JSON_TYPE);
|
||||
}
|
||||
}
|
||||
@@ -16,4 +16,7 @@ void set_wifi_password(const httplib::Request &request, httplib::Response &respo
|
||||
|
||||
void get_wifi_qr(const httplib::Request &request, httplib::Response &response);
|
||||
|
||||
void get_wifi_channel(const httplib::Request &request, httplib::Response &response);
|
||||
void set_wifi_channel(const httplib::Request &request, httplib::Response &response);
|
||||
|
||||
#endif // WIFI_HPP
|
||||
@@ -77,6 +77,11 @@
|
||||
background-color: inherit;
|
||||
}
|
||||
|
||||
.forminput>option {
|
||||
background-color: inherit;
|
||||
color: black;
|
||||
}
|
||||
|
||||
.forminput:focus {
|
||||
border: 5px solid;
|
||||
}
|
||||
|
||||
@@ -153,6 +153,14 @@
|
||||
<button id="shownewpasswordbutton">Show</button>
|
||||
<input type="password" id="newwifipassword" class="forminput">
|
||||
</div>
|
||||
<div style="display:flex;align-items:center;justify-content:space-between;">
|
||||
<label for="wifichannel" class="forminputlabel">WIFI channel</label>
|
||||
<select id="wifichannel" class="forminput">
|
||||
<option class="channeloption" value="1" style="color: black">1</option>
|
||||
<option class="channeloption" value="6">6</option>
|
||||
<option class="channeloption" value="11">11</option>
|
||||
</select>
|
||||
</div>
|
||||
<div style="display:flex;align-items:center;justify-content:space-between;">
|
||||
<label for="wifiqr" class="forminputlabel">Show qr code</label>
|
||||
<div class="checkbox-wrapper-2">
|
||||
|
||||
@@ -34,6 +34,8 @@ const routes = {
|
||||
wifi_password_get: () => [host, prefix, 'settings', 'wifi', 'password', 'get'].join('/'),
|
||||
wifi_password_set: () => [host, prefix, 'settings', 'wifi', 'password', 'set'].join('/'),
|
||||
wifi_qr_get: () => [host, prefix, 'settings', 'wifi', 'qr', 'get' ].join('/'),
|
||||
wifi_channel_get: () => [host, prefix, 'settings', 'wifi', 'channel', 'get' ].join('/'),
|
||||
wifi_channel_set: () => [host, prefix, 'settings', 'wifi', 'channel', 'set' ].join('/'),
|
||||
|
||||
reboot: () => [host, prefix, 'reboot'].join('/'),
|
||||
syslog: () => [host, prefix, 'syslog'].join('/'),
|
||||
|
||||
@@ -14,5 +14,6 @@ const get_all_settings = () => {
|
||||
|
||||
get_wifi_status()
|
||||
get_wifi_name()
|
||||
get_wifi_channel()
|
||||
// get_wifi_password()
|
||||
}
|
||||
@@ -8,6 +8,7 @@ const shownewpasswordbutton = document.querySelector('#shownewpasswordbutton')
|
||||
const newwifipassword = document.querySelector('#newwifipassword')
|
||||
const wifiqr = document.querySelector('#wifiqr')
|
||||
const wifiqrimg = document.querySelector("#wifiqrimg")
|
||||
const channelselect = document.querySelector('#wifichannel')
|
||||
|
||||
let old_wifi_name = ""
|
||||
|
||||
@@ -106,6 +107,7 @@ const set_wifi_settings = () => {
|
||||
start_loader()
|
||||
set_wifi_status()
|
||||
if (wifistatus.checked) {
|
||||
set_wifi_channel()
|
||||
if (old_wifi_name !== wifissid.value) {
|
||||
set_wifi_name()
|
||||
}
|
||||
@@ -164,3 +166,25 @@ wifiqr.addEventListener('click', () => {
|
||||
wifiqrimg.classList.remove('active')
|
||||
wifiqrimg.innerHTML = ""
|
||||
})
|
||||
|
||||
const get_wifi_channel = () => {
|
||||
fetch(routes.wifi_channel_get(), {
|
||||
method: 'POST',
|
||||
body: `{"token":"${localStorage.getItem('token')}"}`
|
||||
}).then(data => data.json()).then(jsondata => {
|
||||
channelselect.value = jsondata.channel
|
||||
}).catch((error) => {
|
||||
notification(`Ошибка на сервере: ${error}`, "error")
|
||||
})
|
||||
}
|
||||
|
||||
const set_wifi_channel = () => {
|
||||
fetch(routes.wifi_channel_set(), {
|
||||
method: 'POST',
|
||||
body: `{"token":"${localStorage.getItem('token')}","channel":"${channelselect.value}"}`
|
||||
}).then(data => data.json()).then(jsondata => {
|
||||
channelselect.value = jsondata.channel
|
||||
}).catch((error) => {
|
||||
notification(`Ошибка на сервере: ${error}`, "error")
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user