#include "WebAPI.h" #include "DataInterface/GraphClient.h" #include #include #include #include #include #include namespace DataStorageInterface { namespace API { // API Description // Log Data // Goal: Log data into the Database // Use: // URL: http:///API/log//// // GraphHeader Query // Goal: // Use: // URL: http:///API/graphheader/// // Graph Query // Goal: // Use: // URL: http:///API/graph/ // URL: http:///API/graph/// // Settings Query // Goal: Change / query settings // Use: // URL: http:///API/settings// WebAPI::WebAPI() { } WebAPI::~WebAPI() { } Http::HttpServer::HttpReply WebAPI::ProcessQuery(DataStorage::DataStorageClient* pDataStorageClient, DataInterface::GraphClient* pGraphClient, const std::string& uri, const std::vector& /*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//// Http::HttpServer::HttpReply reply; reply.status = Http::HttpServer::HttpReply::Status::InternalServerError; reply.content = "{\"result\": \"error\"}\r\n"; std::vector 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/// Http::HttpServer::HttpReply reply; reply.status = Http::HttpServer::HttpReply::Status::InternalServerError; reply.content = "{\"result\": \"error\"}\r\n"; std::vector 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 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/// Http::HttpServer::HttpReply reply; reply.status = Http::HttpServer::HttpReply::Status::InternalServerError; reply.content = "{\"result\": \"error\"}\r\n"; std::vector 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 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// Http::HttpServer::HttpReply reply; reply.status = Http::HttpServer::HttpReply::Status::InternalServerError; reply.content = "{\"result\": \"error\"}\r\n"; std::vector 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& 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& dataIds, const std::string& timespan) { return pGraphClient->GetGraphData(deviceId, dataIds, DataStorage::Conversions::Timespan(timespan)); } } // namespace API } // namespace DataStorageInterface