|
@@ -1,5 +1,5 @@
|
|
|
-#include <iostream>
|
|
|
|
|
#include <httplib.h>
|
|
#include <httplib.h>
|
|
|
|
|
+#include <iostream>
|
|
|
#include <noolite.h>
|
|
#include <noolite.h>
|
|
|
|
|
|
|
|
namespace App {
|
|
namespace App {
|
|
@@ -10,9 +10,10 @@ constexpr auto ContentType = "application/json";
|
|
|
|
|
|
|
|
static httplib::Server *srv = nullptr;
|
|
static httplib::Server *srv = nullptr;
|
|
|
|
|
|
|
|
-}
|
|
|
|
|
|
|
+} // namespace App
|
|
|
|
|
|
|
|
-void signalHandler(int signum) {
|
|
|
|
|
|
|
+void signalHandler(int signum)
|
|
|
|
|
+{
|
|
|
std::cout << "\n\nInterrupt signal (" << signum << ") received.\n";
|
|
std::cout << "\n\nInterrupt signal (" << signum << ") received.\n";
|
|
|
|
|
|
|
|
if (App::srv) {
|
|
if (App::srv) {
|
|
@@ -29,54 +30,56 @@ int main(int argc, char *argv[])
|
|
|
|
|
|
|
|
App::srv = new httplib::Server();
|
|
App::srv = new httplib::Server();
|
|
|
|
|
|
|
|
- const std::map< std::string, noolitelib::Command > commands = {
|
|
|
|
|
- { "off", noolitelib::Off },
|
|
|
|
|
- { "decraseBrightnes", noolitelib::DecraseBrightnes },
|
|
|
|
|
- { "on", noolitelib::On },
|
|
|
|
|
- { "incraseBrightnes", noolitelib::IncreaseBrightnes },
|
|
|
|
|
- { "switch", noolitelib::Switch },
|
|
|
|
|
- { "invertBrightnes", noolitelib::InvertBrightnes },
|
|
|
|
|
- { "set", noolitelib::Set },
|
|
|
|
|
- { "callScenario", noolitelib::CallScenario },
|
|
|
|
|
- { "saveScenario", noolitelib::SaveScenario },
|
|
|
|
|
- { "unbind", noolitelib::Unbind },
|
|
|
|
|
- { "stopColorSelection", noolitelib::StopColorSelection },
|
|
|
|
|
- { "bind", noolitelib::Bind },
|
|
|
|
|
- { "colorSelection", noolitelib::ColorSelection },
|
|
|
|
|
- { "colorSwitch", noolitelib::ColorSwitch },
|
|
|
|
|
- { "modeSwitch", noolitelib::ModeSwitch },
|
|
|
|
|
- { "effectSpeed", noolitelib::EffectSpeed }
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
- App::srv->Get("/noolite/:command/:channel", [&commands](const httplib::Request &req, httplib::Response &res) {
|
|
|
|
|
-
|
|
|
|
|
- auto formatResult = [](bool successful, const std::string &text) -> std::string {
|
|
|
|
|
- std::stringstream res;
|
|
|
|
|
- res << "{\"" << (successful ? "result" : "error")
|
|
|
|
|
- << "\":\"" << (successful ? "ok" : text) << "\"";
|
|
|
|
|
- if (successful) {
|
|
|
|
|
- res << (",\"command\":\"" + text + "\"");
|
|
|
|
|
- }
|
|
|
|
|
- res << "}";
|
|
|
|
|
-
|
|
|
|
|
- return res.str();
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
- auto command = req.path_params.at("command");
|
|
|
|
|
- if (!commands.count(command)) {
|
|
|
|
|
- res.set_content(formatResult(false, "Command not found"), App::ContentType);
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- auto channel = req.path_params.at("channel");
|
|
|
|
|
-
|
|
|
|
|
- noolitelib::Noolite adapter;
|
|
|
|
|
- if (adapter.sendCommand(std::stoi(channel), commands.at(command))) {
|
|
|
|
|
- res.set_content(formatResult(true, command + " " + channel), App::ContentType);
|
|
|
|
|
- } else {
|
|
|
|
|
- res.set_content(formatResult(false, "Device error"), App::ContentType);
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ const std::map<std::string, noolitelib::Command> commands
|
|
|
|
|
+ = {{"off", noolitelib::Off},
|
|
|
|
|
+ {"decraseBrightnes", noolitelib::DecraseBrightnes},
|
|
|
|
|
+ {"on", noolitelib::On},
|
|
|
|
|
+ {"incraseBrightnes", noolitelib::IncreaseBrightnes},
|
|
|
|
|
+ {"switch", noolitelib::Switch},
|
|
|
|
|
+ {"invertBrightnes", noolitelib::InvertBrightnes},
|
|
|
|
|
+ {"set", noolitelib::Set},
|
|
|
|
|
+ {"callScenario", noolitelib::CallScenario},
|
|
|
|
|
+ {"saveScenario", noolitelib::SaveScenario},
|
|
|
|
|
+ {"unbind", noolitelib::Unbind},
|
|
|
|
|
+ {"stopColorSelection", noolitelib::StopColorSelection},
|
|
|
|
|
+ {"bind", noolitelib::Bind},
|
|
|
|
|
+ {"colorSelection", noolitelib::ColorSelection},
|
|
|
|
|
+ {"colorSwitch", noolitelib::ColorSwitch},
|
|
|
|
|
+ {"modeSwitch", noolitelib::ModeSwitch},
|
|
|
|
|
+ {"effectSpeed", noolitelib::EffectSpeed}};
|
|
|
|
|
+
|
|
|
|
|
+ App::srv->Get("/noolite/:command/:channel",
|
|
|
|
|
+ [&commands](const httplib::Request &req, httplib::Response &res) {
|
|
|
|
|
+ auto formatResult = [](bool successful,
|
|
|
|
|
+ const std::string &text) -> std::string {
|
|
|
|
|
+ std::stringstream res;
|
|
|
|
|
+ res << "{\"" << (successful ? "result" : "error") << "\":\""
|
|
|
|
|
+ << (successful ? "ok" : text) << "\"";
|
|
|
|
|
+ if (successful) {
|
|
|
|
|
+ res << (",\"command\":\"" + text + "\"");
|
|
|
|
|
+ }
|
|
|
|
|
+ res << "}";
|
|
|
|
|
+
|
|
|
|
|
+ return res.str();
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ auto command = req.path_params.at("command");
|
|
|
|
|
+ if (!commands.count(command)) {
|
|
|
|
|
+ res.set_content(formatResult(false, "Command not found"),
|
|
|
|
|
+ App::ContentType);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ auto channel = req.path_params.at("channel");
|
|
|
|
|
+
|
|
|
|
|
+ noolitelib::Noolite adapter;
|
|
|
|
|
+ if (adapter.sendCommand(std::stoi(channel), commands.at(command))) {
|
|
|
|
|
+ res.set_content(formatResult(true, command + " " + channel),
|
|
|
|
|
+ App::ContentType);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ res.set_content(formatResult(false, "Device error"), App::ContentType);
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
|
|
|
App::srv->set_mount_point("/static", "/var/www/static");
|
|
App::srv->set_mount_point("/static", "/var/www/static");
|
|
|
|
|
|