main.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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::DecraseBrightnes },
  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), noolitelib::Switch)) {
  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("/var/www/static", "/static");
  65. auto host = App::DefaultHost;
  66. auto port = App::DefaultPort;
  67. if (argc > 1) {
  68. host = argv[1];
  69. }
  70. if (argc > 2) {
  71. port = atoi(argv[2]);
  72. }
  73. std::cout << "Listen on " << host << ":" << port << std::endl;
  74. App::srv->listen(host, port);
  75. return 0;
  76. }