-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Yue Dayu
committed
Apr 13, 2022
1 parent
80208f6
commit 5c1d1b7
Showing
5 changed files
with
259 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,35 @@ | ||
#ifndef _WEBSERVER_HPP | ||
#define _WEBSERVER_HPP | ||
|
||
#include <WebServer.h> | ||
#include <SPIFFS.h> | ||
#include <functional> | ||
|
||
#include "speed.hpp" | ||
|
||
static const char WEB_ROOT[] = "/web"; | ||
|
||
class NanoServer { | ||
public: | ||
NanoServer(int port) : server_(port), manager_(nullptr) {} | ||
|
||
bool init(fs::FS& fs, SpeedManager* manager); | ||
|
||
void begin(); | ||
void handle_client(); | ||
|
||
private: | ||
WebServer server_; | ||
SpeedManager* manager_; | ||
|
||
std::function<bool()> save_func_; | ||
|
||
bool server_dir(fs::FS &fs); | ||
|
||
void handle_status_query(); | ||
void handle_speed_set(); | ||
void handle_config_query(); | ||
void handle_config_set(); | ||
}; | ||
|
||
#endif // _WEBSERVER_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,89 @@ | ||
#include "web_server.hpp" | ||
|
||
#include "configure.hpp" | ||
|
||
bool NanoServer::init(fs::FS& fs, SpeedManager* manager) { | ||
manager_ = manager; | ||
save_func_ = [&fs]() { | ||
return Config::inst().save(fs); | ||
}; | ||
if (!server_dir(fs)) { | ||
Serial.println("failed to server static"); | ||
return false; | ||
} | ||
server_.on("/status", HTTP_POST, [this]() { this->handle_status_query(); }); | ||
server_.on("/set_status", HTTP_POST, | ||
[this]() { this->handle_speed_set(); }); | ||
server_.on("/config", HTTP_POST, [this]() { this->handle_config_query(); }); | ||
server_.on("/set_config", HTTP_POST, | ||
[this]() { this->handle_config_set(); }); | ||
return true; | ||
} | ||
|
||
void NanoServer::begin() { server_.begin(); } | ||
|
||
void NanoServer::handle_client() { server_.handleClient(); } | ||
|
||
bool NanoServer::server_dir(fs::FS& fs) { | ||
File root = fs.open(WEB_ROOT); | ||
if (!root) { | ||
Serial.println("failed to open www"); | ||
return false; | ||
} | ||
if (!root.isDirectory()) { | ||
Serial.println(" - not a directory"); | ||
return false; | ||
} | ||
File file = root.openNextFile(); | ||
auto folder_len = strlen(WEB_ROOT); | ||
server_.serveStatic("/", fs, "/web/index.html"); | ||
while (file) { | ||
if (!file.isDirectory()) { | ||
auto filename = file.name(); | ||
server_.serveStatic(filename + folder_len, fs, filename); | ||
} | ||
file = root.openNextFile(); | ||
} | ||
return true; | ||
} | ||
|
||
void NanoServer::handle_status_query() { | ||
char buf[STATUS_JSON_STR_LEN]; | ||
int len; | ||
if (!manager_->get_json_status(buf, STATUS_JSON_STR_LEN, &len)) { | ||
server_.send(500, "text/json", R"({"ret": false})"); | ||
} | ||
server_.send(200, "text/json", buf); | ||
} | ||
|
||
void NanoServer::handle_speed_set() { | ||
String arg = server_.arg(0); | ||
if (!manager_->set_web_json_speed(arg.c_str(), arg.length())) { | ||
server_.send(500, "text/json", R"({"ret": false})"); | ||
} | ||
manager_->need_update(); | ||
handle_status_query(); | ||
} | ||
|
||
void NanoServer::handle_config_query() { | ||
char buf[CONFIG_STR_LEN]; | ||
int len; | ||
if (!Config::inst().data.dump_json(buf, CONFIG_STR_LEN, &len)) { | ||
server_.send(500, "text/json", R"({"ret": false})"); | ||
} | ||
server_.send(200, "text/json", buf); | ||
} | ||
|
||
void NanoServer::handle_config_set() { | ||
String arg = server_.arg(0); | ||
if (!Config::inst().data.load_json(arg.c_str(), arg.length())) { | ||
server_.send(500, "text/json", R"({"ret": false})"); | ||
} | ||
if (save_func_) { | ||
if (!save_func_()) { | ||
server_.send(500, "text/json", R"({"ret": false})"); | ||
} | ||
} | ||
manager_->need_update(); | ||
handle_config_query(); | ||
} |
Oops, something went wrong.