WebAPI.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #include "WebAPI.h"
  2. #include "DataInterface/GraphClient.h"
  3. #include <DataId.h>
  4. #include <DataStorageClient.h>
  5. #include <Logging.h>
  6. #include <StringAlgorithm.h>
  7. #include <iostream>
  8. #include <sstream>
  9. namespace DataStorageInterface {
  10. namespace API {
  11. // API Description
  12. // Log Data
  13. // Goal: Log data into the Database
  14. // Use:
  15. // URL: http://<ip>/API/log/<timestamp>/<device_id>/<data_id>/<value>
  16. // GraphHeader Query
  17. // Goal:
  18. // Use:
  19. // URL: http://<ip>/API/graphheader/<device_id>/<data_ids>/<timespan>
  20. // Graph Query
  21. // Goal:
  22. // Use:
  23. // URL: http://<ip>/API/graph/
  24. // URL: http://<ip>/API/graph/<device_id>/<data_ids>/<timespan>
  25. // Settings Query
  26. // Goal: Change / query settings
  27. // Use:
  28. // URL: http://<ip>/API/settings/<setting>/<value>
  29. WebAPI::WebAPI()
  30. {
  31. }
  32. WebAPI::~WebAPI()
  33. {
  34. }
  35. Http::HttpServer::HttpReply WebAPI::ProcessQuery(DataStorage::DataStorageClient* pDataStorageClient, DataInterface::GraphClient* pGraphClient, const std::string& uri, const std::vector<Http::HttpPostData>& /*postData*/)
  36. {
  37. if (!StringAlgorithm::iequals(uri, "/api") && !StringAlgorithm::istarts_with(uri, "/api/"))
  38. {
  39. Http::HttpServer::HttpReply reply;
  40. reply.status = Http::HttpServer::HttpReply::Status::Unauthorized;
  41. return reply;
  42. }
  43. std::string apiURI = uri.substr(4);
  44. std::stringstream ss;
  45. ss << "API Query: " << apiURI << std::endl;
  46. Logging::Log(Logging::Severity::Debug, ss.str());
  47. if (StringAlgorithm::istarts_with(apiURI, "/log/"))
  48. return ProcessLogQuery(pDataStorageClient, apiURI);
  49. else if (StringAlgorithm::istarts_with(apiURI, "/graphheader/"))
  50. return ProcessGraphHeaderQuery(pGraphClient, apiURI);
  51. else if (StringAlgorithm::istarts_with(apiURI, "/graph/"))
  52. return ProcessGraphQuery(pGraphClient, apiURI);
  53. else if (StringAlgorithm::istarts_with(apiURI, "/settings/"))
  54. return ProcessSettingsQuery(apiURI);
  55. Http::HttpServer::HttpReply reply;
  56. reply.status = Http::HttpServer::HttpReply::Status::InternalServerError;
  57. reply.content = "{\"result\": \"error\"}\r\n";
  58. return reply;
  59. }
  60. Http::HttpServer::HttpReply WebAPI::ProcessLogQuery(DataStorage::DataStorageClient* pDataStorageClient, const std::string& uri)
  61. {
  62. // /log/<timestamp>/<device_id>/<data_id>/<value>
  63. Http::HttpServer::HttpReply reply;
  64. reply.status = Http::HttpServer::HttpReply::Status::InternalServerError;
  65. reply.content = "{\"result\": \"error\"}\r\n";
  66. std::vector<std::string> uriTokens = StringAlgorithm::split(uri, '/');
  67. if (uriTokens.size() < 6)
  68. return reply;
  69. if (uriTokens[2].empty() ||
  70. uriTokens[3].empty() ||
  71. uriTokens[4].empty())
  72. return reply;
  73. int timestamp = std::stoi(uriTokens[2]);
  74. int deviceId = std::stoi(uriTokens[3]);
  75. int dataId = DataStorage::DataId(uriTokens[4]).Id();
  76. std::string value = uriTokens[5];
  77. std::stringstream ss;
  78. ss << "Log Query: " << std::endl;
  79. ss << " " << timestamp << std::endl;
  80. ss << " " << deviceId << std::endl;
  81. ss << " " << dataId << std::endl;
  82. ss << " " << value << std::endl;
  83. Logging::Log(Logging::Severity::Debug, ss.str());
  84. // TODO: Actually Log...
  85. reply.status = Http::HttpServer::HttpReply::Status::Ok;
  86. reply.content = "{\"result\": \"success\"}\r\n";
  87. return reply;
  88. }
  89. Http::HttpServer::HttpReply WebAPI::ProcessGraphHeaderQuery(DataInterface::GraphClient* pGraphClient, const std::string& uri)
  90. {
  91. // /graphheader/<device_id>/<data_ids>/<timespan>
  92. Http::HttpServer::HttpReply reply;
  93. reply.status = Http::HttpServer::HttpReply::Status::InternalServerError;
  94. reply.content = "{\"result\": \"error\"}\r\n";
  95. std::vector<std::string> uriTokens = StringAlgorithm::split(uri, '/');
  96. if (uriTokens.size() != 5)
  97. return reply;
  98. if (uriTokens[2].empty() ||
  99. uriTokens[3].empty())
  100. return reply;
  101. try
  102. {
  103. int deviceId = stoi(uriTokens[2]);
  104. std::vector<int> dataIds;
  105. for (auto& id : StringAlgorithm::split(uriTokens[3], '-'))
  106. dataIds.push_back(DataStorage::DataId(id).Id());
  107. std::string period = uriTokens[4];
  108. if (uriTokens[4].empty())
  109. period = "day";
  110. nlohmann::json json = GetGraphHeader(pGraphClient, dataIds, period);
  111. reply.content = json.dump();
  112. reply.status = Http::HttpServer::HttpReply::Status::Ok;
  113. }
  114. catch (const std::exception& e)
  115. {
  116. std::cout << "ProcessGraphHeaderQuery() - Error: " << e.what() << std::endl;
  117. return reply;
  118. }
  119. return reply;
  120. }
  121. Http::HttpServer::HttpReply WebAPI::ProcessGraphQuery(DataInterface::GraphClient* pGraphClient, const std::string& uri)
  122. {
  123. // /graph/<device_id>/<data_ids>/<timespan>
  124. Http::HttpServer::HttpReply reply;
  125. reply.status = Http::HttpServer::HttpReply::Status::InternalServerError;
  126. reply.content = "{\"result\": \"error\"}\r\n";
  127. std::vector<std::string> uriTokens = StringAlgorithm::split(uri, '/');
  128. if (uriTokens.size() != 5)
  129. return reply;
  130. if (uriTokens[2].empty() ||
  131. uriTokens[3].empty())
  132. return reply;
  133. try
  134. {
  135. int deviceId = stoi(uriTokens[2]);
  136. std::vector<int> dataIds;
  137. for (auto& id : StringAlgorithm::split(uriTokens[3], '-'))
  138. dataIds.push_back(DataStorage::DataId(id).Id());
  139. std::string period = uriTokens[4];
  140. if (uriTokens[4].empty())
  141. period = "day";
  142. nlohmann::json json = GetGraphData(pGraphClient, deviceId, dataIds, period);
  143. reply.content = json.dump();
  144. reply.status = Http::HttpServer::HttpReply::Status::Ok;
  145. }
  146. catch (const std::exception& e)
  147. {
  148. std::cout << "ProcessGraphQuery() - Error: " << e.what() << std::endl;
  149. return reply;
  150. }
  151. return reply;
  152. }
  153. Http::HttpServer::HttpReply WebAPI::ProcessSettingsQuery(const std::string& uri)
  154. {
  155. // /settings/<setting>/<value>
  156. Http::HttpServer::HttpReply reply;
  157. reply.status = Http::HttpServer::HttpReply::Status::InternalServerError;
  158. reply.content = "{\"result\": \"error\"}\r\n";
  159. std::vector<std::string> uriTokens = StringAlgorithm::split(uri, '/');
  160. if (uriTokens.size() < 4)
  161. return reply;
  162. std::string setting = uriTokens[2];
  163. std::string value = uriTokens[3];
  164. // Generalize in Settings struct?
  165. if (StringAlgorithm::iequals(setting, "debug"))
  166. {
  167. if (StringAlgorithm::iequals(value, "on"))
  168. Logging::SetLogMask(Logging::Severity::Debug);
  169. else if (StringAlgorithm::iequals(value, "off"))
  170. Logging::SetLogMask(Logging::Severity::Info);
  171. else
  172. return reply;
  173. }
  174. else
  175. {
  176. return reply;
  177. }
  178. reply.status = Http::HttpServer::HttpReply::Status::Ok;
  179. reply.content = "{\"result\": \"success\"}\r\n";
  180. return reply;
  181. }
  182. nlohmann::json WebAPI::GetGraphHeader(DataInterface::GraphClient* pGraphClient, const std::vector<int>& dataIds, const std::string& timespan)
  183. {
  184. return pGraphClient->GetGraphHeader(dataIds, DataStorage::Conversions::Timespan(timespan));
  185. }
  186. nlohmann::json WebAPI::GetGraphData(DataInterface::GraphClient* pGraphClient, int deviceId, const std::vector<int>& dataIds, const std::string& timespan)
  187. {
  188. return pGraphClient->GetGraphData(deviceId, dataIds, DataStorage::Conversions::Timespan(timespan));
  189. }
  190. } // namespace API
  191. } // namespace DataStorageInterface