WebAPI.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include "WebAPI.h"
  2. #include <StringAlgorithm.h>
  3. #include <iostream>
  4. #include <memory>
  5. namespace StreamRecoder {
  6. namespace API {
  7. // API Description
  8. // Status Query
  9. // Goal: Provide status information based on POST data
  10. // Use: DomoticaSite status update
  11. // URL: http://<ip>/API/<action>/<brand>/<ipaddress>/<port>/<url(base64)>/<username>/<password>
  12. WebAPI::WebAPI()
  13. {
  14. }
  15. WebAPI::~WebAPI()
  16. {
  17. }
  18. Http::HttpServer::HttpReply WebAPI::ProcessQuery(ctpl::thread_pool* pThreadPool, const std::string& ffmpeg, const std::string& uri, const std::vector<Http::HttpPostData>& postData)
  19. {
  20. if (!StringAlgorithm::iequals(uri, "/api") && !StringAlgorithm::istarts_with(uri, "/api/"))
  21. {
  22. Http::HttpServer::HttpReply reply;
  23. reply.status = Http::HttpServer::HttpReply::Status::Unauthorized;
  24. return reply;
  25. }
  26. Http::HttpServer::HttpReply reply;
  27. reply.status = Http::HttpServer::HttpReply::Status::InternalServerError;
  28. reply.content = "{\"result\": \"error\"}\r\n";
  29. /*
  30. try {
  31. std::string apiURI = uri.substr(4);
  32. std::vector<std::string> uriTokens = StringAlgorithm::split(apiURI, '/');
  33. if (uriTokens.size() < 7)
  34. return reply;
  35. std::string action = uriTokens[1];
  36. std::string brand = uriTokens[2];
  37. Recorder::Settings settings;
  38. settings.Path = path;
  39. settings.IpAddress = uriTokens[3];
  40. settings.Port = uriTokens[4];
  41. settings.URL = cppcodec::base64_url::decode<std::string>(uriTokens[5].c_str(), uriTokens[5].length());
  42. settings.Username = uriTokens[6];
  43. settings.Password = uriTokens[7];
  44. int numberOfImages = 5;
  45. if (uriTokens.size() > 8)
  46. numberOfImages = stoi(uriTokens[8]);
  47. reply.status = Http::HttpServer::HttpReply::Status::Ok;
  48. if (StringAlgorithm::iequals("Digoo", brand))
  49. {
  50. auto recorder = Recorder::DigooRecorder(pHttpClient, settings);
  51. if (StringAlgorithm::iequals("Snapshot", action))
  52. reply.content = recorder.Snapshot(pThreadPool, ffmpeg);
  53. else if (StringAlgorithm::iequals("MultiSnapshot", action))
  54. reply.content = recorder.MultiSnapshot(pThreadPool, ffmpeg, numberOfImages);
  55. else if (StringAlgorithm::iequals("Video", action))
  56. reply.content = recorder.Video(pThreadPool, ffmpeg);
  57. }
  58. else if (StringAlgorithm::iequals("Foscam", brand))
  59. {
  60. auto recorder = Recorder::FoscamRecorder(pHttpClient, settings);
  61. if (StringAlgorithm::iequals("Snapshot", action))
  62. reply.content = recorder.Snapshot(pThreadPool, ffmpeg);
  63. else if (StringAlgorithm::iequals("MultiSnapshot", action))
  64. reply.content = recorder.MultiSnapshot(pThreadPool, ffmpeg, numberOfImages);
  65. else if (StringAlgorithm::iequals("Video", action))
  66. reply.content = recorder.Video(pThreadPool, ffmpeg);
  67. }
  68. else if (StringAlgorithm::iequals("RTSP", brand))
  69. {
  70. auto recorder = Recorder::RTSPRecorder(pHttpClient, settings);
  71. if (StringAlgorithm::iequals("Snapshot", action))
  72. reply.content = recorder.Snapshot(pThreadPool, ffmpeg);
  73. else if (StringAlgorithm::iequals("MultiSnapshot", action))
  74. reply.content = recorder.MultiSnapshot(pThreadPool, ffmpeg, numberOfImages);
  75. else if (StringAlgorithm::iequals("Video", action))
  76. reply.content = recorder.Video(pThreadPool, ffmpeg);
  77. }
  78. else if (StringAlgorithm::iequals("VStarCam", brand))
  79. {
  80. auto recorder = Recorder::VStarCamRecorder(pHttpClient, settings);
  81. if (StringAlgorithm::iequals("Snapshot", action))
  82. reply.content = recorder.Snapshot(pThreadPool, ffmpeg);
  83. else if (StringAlgorithm::iequals("MultiSnapshot", action))
  84. reply.content = recorder.MultiSnapshot(pThreadPool, ffmpeg, numberOfImages);
  85. else if (StringAlgorithm::iequals("Video", action))
  86. reply.content = recorder.Video(pThreadPool, ffmpeg);
  87. }
  88. else if (StringAlgorithm::iequals("WatchBot", brand))
  89. {
  90. auto recorder = Recorder::WatchBotRecorder(pHttpClient, settings);
  91. if (StringAlgorithm::iequals("Snapshot", action))
  92. reply.content = recorder.Snapshot(pThreadPool, ffmpeg);
  93. else if (StringAlgorithm::iequals("MultiSnapshot", action))
  94. reply.content = recorder.MultiSnapshot(pThreadPool, ffmpeg, numberOfImages);
  95. else if (StringAlgorithm::iequals("Video", action))
  96. reply.content = recorder.Video(pThreadPool, ffmpeg);
  97. }
  98. else if (StringAlgorithm::iequals("ZModo", brand))
  99. {
  100. auto recorder = Recorder::ZModoRecorder(pHttpClient, settings);
  101. if (StringAlgorithm::iequals("Snapshot", action))
  102. reply.content = recorder.Snapshot(pThreadPool, ffmpeg);
  103. else if (StringAlgorithm::iequals("MultiSnapshot", action))
  104. reply.content = recorder.MultiSnapshot(pThreadPool, ffmpeg, numberOfImages);
  105. else if (StringAlgorithm::iequals("Video", action))
  106. reply.content = recorder.Video(pThreadPool, ffmpeg);
  107. }
  108. }
  109. catch (const std::exception& e)
  110. {
  111. std::cout << "WebAPI::ProcessQuery() - Error: " << e.what() << std::endl;
  112. return reply;
  113. }
  114. */
  115. return reply;
  116. }
  117. } // namespace API
  118. } // namespace StreamRecoder