main.cpp 2.8 KB

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