WebAPI.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. std::string WebAPI::ProcessQuery(DataStorage::DataStorageClient* pDataStorageClient, DataInterface::GraphClient* pGraphClient, const std::string& uri, const std::vector<Http::HttpPostData>& /*postData*/)
  36. {
  37. std::string output;
  38. std::stringstream ss;
  39. ss << "API Query: " << uri << std::endl;
  40. Logging::Log(Logging::Severity::Debug, ss.str());
  41. if (StringAlgorithm::istarts_with(uri, "/log/"))
  42. output = ProcessLogQuery(pDataStorageClient, uri);
  43. else if (StringAlgorithm::istarts_with(uri, "/graphheader/"))
  44. output = ProcessGraphHeaderQuery(pGraphClient, uri);
  45. else if (StringAlgorithm::istarts_with(uri, "/graph/"))
  46. output = ProcessGraphQuery(pGraphClient, uri);
  47. else if (StringAlgorithm::istarts_with(uri, "/settings/"))
  48. output = ProcessSettingsQuery(uri);
  49. else
  50. output = "{\"result\": \"error\"}\r\n";
  51. return output;
  52. }
  53. std::string WebAPI::ProcessLogQuery(DataStorage::DataStorageClient* pDataStorageClient, const std::string& uri)
  54. {
  55. // /log/<timestamp>/<device_id>/<data_id>/<value>
  56. std::string output;
  57. std::string error = "{\"result\": \"error\"}\r\n";
  58. std::vector<std::string> uriTokens = StringAlgorithm::split(uri, '/');
  59. if (uriTokens.size() < 6)
  60. return error;
  61. if (uriTokens[2].empty() ||
  62. uriTokens[3].empty() ||
  63. uriTokens[4].empty())
  64. return error;
  65. int timestamp = std::stoi(uriTokens[2]);
  66. int deviceId = std::stoi(uriTokens[3]);
  67. int dataId = DataStorage::DataId(uriTokens[4]).Id();
  68. std::string value = uriTokens[5];
  69. std::stringstream ss;
  70. ss << "Log Query: " << std::endl;
  71. ss << " " << timestamp << std::endl;
  72. ss << " " << deviceId << std::endl;
  73. ss << " " << dataId << std::endl;
  74. ss << " " << value << std::endl;
  75. Logging::Log(Logging::Severity::Debug, ss.str());
  76. // TODO: Actually Log...
  77. output = "{\"result\": \"success\"}\r\n";
  78. return output;
  79. }
  80. std::string WebAPI::ProcessGraphHeaderQuery(DataInterface::GraphClient* pGraphClient, const std::string& uri)
  81. {
  82. // /graphheader/<device_id>/<data_ids>/<timespan>
  83. std::string output;
  84. std::string error = "{\"result\": \"error\"}\r\n";
  85. std::vector<std::string> uriTokens = StringAlgorithm::split(uri, '/');
  86. if (uriTokens.size() != 5)
  87. return error;
  88. if (uriTokens[2].empty() ||
  89. uriTokens[3].empty())
  90. return error;
  91. try
  92. {
  93. int deviceId = stoi(uriTokens[2]);
  94. std::vector<int> dataIds;
  95. for (auto& id : StringAlgorithm::split(uriTokens[3], '-'))
  96. dataIds.push_back(DataStorage::DataId(id).Id());
  97. std::string period = uriTokens[4];
  98. if (uriTokens[4].empty())
  99. period = "day";
  100. nlohmann::json json = GetGraphHeader(pGraphClient, dataIds, period);
  101. output = json.dump();
  102. }
  103. catch (const std::exception& e)
  104. {
  105. std::cout << "ProcessGraphHeaderQuery() - Error: " << e.what() << std::endl;
  106. return error;
  107. }
  108. return output;
  109. }
  110. std::string WebAPI::ProcessGraphQuery(DataInterface::GraphClient* pGraphClient, const std::string& uri)
  111. {
  112. // /graph/<device_id>/<data_ids>/<timespan>
  113. std::string output;
  114. std::string error = "{\"result\": \"error\"}\r\n";
  115. std::vector<std::string> uriTokens = StringAlgorithm::split(uri, '/');
  116. if (uriTokens.size() != 5)
  117. return error;
  118. if (uriTokens[2].empty() ||
  119. uriTokens[3].empty())
  120. return error;
  121. try
  122. {
  123. int deviceId = stoi(uriTokens[2]);
  124. std::vector<int> dataIds;
  125. for (auto& id : StringAlgorithm::split(uriTokens[3], '-'))
  126. dataIds.push_back(DataStorage::DataId(id).Id());
  127. std::string period = uriTokens[4];
  128. if (uriTokens[4].empty())
  129. period = "day";
  130. nlohmann::json json = GetGraphData(pGraphClient, deviceId, dataIds, period);
  131. output = json.dump();
  132. }
  133. catch (const std::exception& e)
  134. {
  135. std::cout << "ProcessGraphQuery() - Error: " << e.what() << std::endl;
  136. return error;
  137. }
  138. return output;
  139. }
  140. std::string WebAPI::ProcessSettingsQuery(const std::string& uri)
  141. {
  142. // /settings/<setting>/<value>
  143. std::string success = "{\"result\": \"success\"}\r\n";
  144. std::string error = "{\"result\": \"error\"}\r\n";
  145. std::vector<std::string> uriTokens = StringAlgorithm::split(uri, '/');
  146. if (uriTokens.size() < 4)
  147. return std::string();
  148. std::string setting = uriTokens[2];
  149. std::string value = uriTokens[3];
  150. // Generalize in Settings struct?
  151. if (StringAlgorithm::iequals(setting, "debug"))
  152. {
  153. if (StringAlgorithm::iequals(value, "on"))
  154. Logging::SetLogMask(Logging::Severity::Debug);
  155. else if (StringAlgorithm::iequals(value, "off"))
  156. Logging::SetLogMask(Logging::Severity::Info);
  157. else
  158. return error;
  159. }
  160. else
  161. {
  162. return error;
  163. }
  164. return success;
  165. }
  166. nlohmann::json WebAPI::GetGraphHeader(DataInterface::GraphClient* pGraphClient, const std::vector<int>& dataIds, const std::string& timespan)
  167. {
  168. return pGraphClient->GetGraphHeader(dataIds, DataStorage::Conversions::Timespan(timespan));
  169. }
  170. nlohmann::json WebAPI::GetGraphData(DataInterface::GraphClient* pGraphClient, int deviceId, const std::vector<int>& dataIds, const std::string& timespan)
  171. {
  172. return pGraphClient->GetGraphData(deviceId, dataIds, DataStorage::Conversions::Timespan(timespan));
  173. }
  174. } // namespace API
  175. } // namespace DataStorageInterface