| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- #include "WebAPI.h"
- #include "DataInterface/GraphClient.h"
- #include <DataId.h>
- #include <DataStorageClient.h>
- #include <Logging.h>
- #include <StringAlgorithm.h>
- #include <iostream>
- #include <sstream>
- namespace DataStorageInterface {
- namespace API {
- // API Description
- // Log Data
- // Goal: Log data into the Database
- // Use:
- // URL: http://<ip>/API/log/<timestamp>/<device_id>/<data_id>/<value>
- // GraphHeader Query
- // Goal:
- // Use:
- // URL: http://<ip>/API/graphheader/<device_id>/<data_ids>/<timespan>
- // Graph Query
- // Goal:
- // Use:
- // URL: http://<ip>/API/graph/
- // URL: http://<ip>/API/graph/<device_id>/<data_ids>/<timespan>
- // Settings Query
- // Goal: Change / query settings
- // Use:
- // URL: http://<ip>/API/settings/<setting>/<value>
- WebAPI::WebAPI()
- {
- }
- WebAPI::~WebAPI()
- {
- }
- Http::HttpServer::HttpReply WebAPI::ProcessQuery(DataStorage::DataStorageClient* pDataStorageClient, DataInterface::GraphClient* pGraphClient, const std::string& uri, const std::vector<Http::HttpPostData>& /*postData*/)
- {
- if (!StringAlgorithm::iequals(uri, "/api") && !StringAlgorithm::istarts_with(uri, "/api/"))
- {
- Http::HttpServer::HttpReply reply;
- reply.status = Http::HttpServer::HttpReply::Status::Unauthorized;
- return reply;
- }
- std::string apiURI = uri.substr(4);
- std::stringstream ss;
- ss << "API Query: " << apiURI << std::endl;
- Logging::Log(Logging::Severity::Debug, ss.str());
- if (StringAlgorithm::istarts_with(apiURI, "/log/"))
- return ProcessLogQuery(pDataStorageClient, apiURI);
- else if (StringAlgorithm::istarts_with(apiURI, "/graphheader/"))
- return ProcessGraphHeaderQuery(pGraphClient, apiURI);
- else if (StringAlgorithm::istarts_with(apiURI, "/graph/"))
- return ProcessGraphQuery(pGraphClient, apiURI);
- else if (StringAlgorithm::istarts_with(apiURI, "/settings/"))
- return ProcessSettingsQuery(apiURI);
- Http::HttpServer::HttpReply reply;
- reply.status = Http::HttpServer::HttpReply::Status::InternalServerError;
- reply.content = "{\"result\": \"error\"}\r\n";
- return reply;
- }
- Http::HttpServer::HttpReply WebAPI::ProcessLogQuery(DataStorage::DataStorageClient* pDataStorageClient, const std::string& uri)
- {
- // /log/<timestamp>/<device_id>/<data_id>/<value>
- Http::HttpServer::HttpReply reply;
- reply.status = Http::HttpServer::HttpReply::Status::InternalServerError;
- reply.content = "{\"result\": \"error\"}\r\n";
- std::vector<std::string> uriTokens = StringAlgorithm::split(uri, '/');
- if (uriTokens.size() < 6)
- return reply;
- if (uriTokens[2].empty() ||
- uriTokens[3].empty() ||
- uriTokens[4].empty())
- return reply;
- int timestamp = std::stoi(uriTokens[2]);
- int deviceId = std::stoi(uriTokens[3]);
- int dataId = DataStorage::DataId(uriTokens[4]).Id();
- std::string value = uriTokens[5];
- std::stringstream ss;
- ss << "Log Query: " << std::endl;
- ss << " " << timestamp << std::endl;
- ss << " " << deviceId << std::endl;
- ss << " " << dataId << std::endl;
- ss << " " << value << std::endl;
- Logging::Log(Logging::Severity::Debug, ss.str());
- // TODO: Actually Log...
- reply.status = Http::HttpServer::HttpReply::Status::Ok;
- reply.content = "{\"result\": \"success\"}\r\n";
- return reply;
- }
- Http::HttpServer::HttpReply WebAPI::ProcessGraphHeaderQuery(DataInterface::GraphClient* pGraphClient, const std::string& uri)
- {
- // /graphheader/<device_id>/<data_ids>/<timespan>
- Http::HttpServer::HttpReply reply;
- reply.status = Http::HttpServer::HttpReply::Status::InternalServerError;
- reply.content = "{\"result\": \"error\"}\r\n";
- std::vector<std::string> uriTokens = StringAlgorithm::split(uri, '/');
- if (uriTokens.size() != 5)
- return reply;
- if (uriTokens[2].empty() ||
- uriTokens[3].empty())
- return reply;
- try
- {
- int deviceId = stoi(uriTokens[2]);
- std::vector<int> dataIds;
- for (auto& id : StringAlgorithm::split(uriTokens[3], '-'))
- dataIds.push_back(DataStorage::DataId(id).Id());
- std::string period = uriTokens[4];
- if (uriTokens[4].empty())
- period = "day";
- nlohmann::json json = GetGraphHeader(pGraphClient, dataIds, period);
- reply.content = json.dump();
- reply.status = Http::HttpServer::HttpReply::Status::Ok;
- }
- catch (const std::exception& e)
- {
- std::cout << "ProcessGraphHeaderQuery() - Error: " << e.what() << std::endl;
- return reply;
- }
- return reply;
- }
- Http::HttpServer::HttpReply WebAPI::ProcessGraphQuery(DataInterface::GraphClient* pGraphClient, const std::string& uri)
- {
- // /graph/<device_id>/<data_ids>/<timespan>
- Http::HttpServer::HttpReply reply;
- reply.status = Http::HttpServer::HttpReply::Status::InternalServerError;
- reply.content = "{\"result\": \"error\"}\r\n";
- std::vector<std::string> uriTokens = StringAlgorithm::split(uri, '/');
- if (uriTokens.size() != 5)
- return reply;
- if (uriTokens[2].empty() ||
- uriTokens[3].empty())
- return reply;
- try
- {
- int deviceId = stoi(uriTokens[2]);
- std::vector<int> dataIds;
- for (auto& id : StringAlgorithm::split(uriTokens[3], '-'))
- dataIds.push_back(DataStorage::DataId(id).Id());
- std::string period = uriTokens[4];
- if (uriTokens[4].empty())
- period = "day";
- nlohmann::json json = GetGraphData(pGraphClient, deviceId, dataIds, period);
- reply.content = json.dump();
- reply.status = Http::HttpServer::HttpReply::Status::Ok;
- }
- catch (const std::exception& e)
- {
- std::cout << "ProcessGraphQuery() - Error: " << e.what() << std::endl;
- return reply;
- }
- return reply;
- }
- Http::HttpServer::HttpReply WebAPI::ProcessSettingsQuery(const std::string& uri)
- {
- // /settings/<setting>/<value>
- Http::HttpServer::HttpReply reply;
- reply.status = Http::HttpServer::HttpReply::Status::InternalServerError;
- reply.content = "{\"result\": \"error\"}\r\n";
- std::vector<std::string> uriTokens = StringAlgorithm::split(uri, '/');
- if (uriTokens.size() < 4)
- return reply;
- std::string setting = uriTokens[2];
- std::string value = uriTokens[3];
- // Generalize in Settings struct?
- if (StringAlgorithm::iequals(setting, "debug"))
- {
- if (StringAlgorithm::iequals(value, "on"))
- Logging::SetLogMask(Logging::Severity::Debug);
- else if (StringAlgorithm::iequals(value, "off"))
- Logging::SetLogMask(Logging::Severity::Info);
- else
- return reply;
- }
- else
- {
- return reply;
- }
- reply.status = Http::HttpServer::HttpReply::Status::Ok;
- reply.content = "{\"result\": \"success\"}\r\n";
- return reply;
- }
- nlohmann::json WebAPI::GetGraphHeader(DataInterface::GraphClient* pGraphClient, const std::vector<int>& dataIds, const std::string& timespan)
- {
- return pGraphClient->GetGraphHeader(dataIds, DataStorage::Conversions::Timespan(timespan));
- }
- nlohmann::json WebAPI::GetGraphData(DataInterface::GraphClient* pGraphClient, int deviceId, const std::vector<int>& dataIds, const std::string& timespan)
- {
- return pGraphClient->GetGraphData(deviceId, dataIds, DataStorage::Conversions::Timespan(timespan));
- }
- } // namespace API
- } // namespace DataStorageInterface
|