main.cpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include <httplib.h>
  2. #include <iostream>
  3. #include <noolite.h>
  4. namespace App {
  5. constexpr auto DefaultPort = 8888;
  6. constexpr auto DefaultHost = "0.0.0.0";
  7. constexpr auto ContentType = "application/json";
  8. static httplib::Server *srv = nullptr;
  9. } // namespace App
  10. void signalHandler(int signum)
  11. {
  12. std::cout << "\n\nInterrupt signal (" << signum << ") received.\n";
  13. if (App::srv) {
  14. App::srv->stop();
  15. delete App::srv;
  16. }
  17. exit(signum);
  18. }
  19. int main(int argc, char *argv[])
  20. {
  21. std::signal(SIGINT, signalHandler);
  22. App::srv = new httplib::Server();
  23. const std::map<std::string, noolitelib::Command> commands
  24. = {{"off", noolitelib::Off},
  25. {"decraseBrightnes", noolitelib::DecraseBrightnes},
  26. {"on", noolitelib::On},
  27. {"incraseBrightnes", noolitelib::IncreaseBrightnes},
  28. {"switch", noolitelib::Switch},
  29. {"invertBrightnes", noolitelib::InvertBrightnes},
  30. {"set", noolitelib::Set},
  31. {"callScenario", noolitelib::CallScenario},
  32. {"saveScenario", noolitelib::SaveScenario},
  33. {"unbind", noolitelib::Unbind},
  34. {"stopColorSelection", noolitelib::StopColorSelection},
  35. {"bind", noolitelib::Bind},
  36. {"colorSelection", noolitelib::ColorSelection},
  37. {"colorSwitch", noolitelib::ColorSwitch},
  38. {"modeSwitch", noolitelib::ModeSwitch},
  39. {"effectSpeed", noolitelib::EffectSpeed}};
  40. App::srv->Get("/noolite/:command/:channel",
  41. [&commands](const httplib::Request &req, httplib::Response &res) {
  42. auto formatResult = [](bool successful,
  43. const std::string &text) -> std::string {
  44. std::stringstream res;
  45. res << "{\"" << (successful ? "result" : "error") << "\":\""
  46. << (successful ? "ok" : text) << "\"";
  47. if (successful) {
  48. res << (",\"command\":\"" + text + "\"");
  49. }
  50. res << "}";
  51. return res.str();
  52. };
  53. auto command = req.path_params.at("command");
  54. if (!commands.count(command)) {
  55. res.set_content(formatResult(false, "Command not found"),
  56. App::ContentType);
  57. return;
  58. }
  59. auto channel = req.path_params.at("channel");
  60. noolitelib::Noolite adapter;
  61. if (adapter.sendCommand(std::stoi(channel), commands.at(command))) {
  62. res.set_content(formatResult(true, command + " " + channel),
  63. App::ContentType);
  64. } else {
  65. res.set_content(formatResult(false, "Device error"), App::ContentType);
  66. }
  67. });
  68. App::srv->set_mount_point("/static", "/var/www/static");
  69. auto host = argc > 1 ? argv[1] : App::DefaultHost;
  70. auto port = argc > 2 ? atoi(argv[2]) : App::DefaultPort;
  71. std::cout << "Listen on " << host << ":" << port << std::endl;
  72. App::srv->listen(host, port);
  73. return 0;
  74. }