WebAPI.cpp 4.1 KB

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