WebAPI.cpp 5.1 KB

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