#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() { } std::string WebAPI::ProcessQuery(DataStorage::DataStorageClient* pDataStorageClient, DataInterface::GraphClient* pGraphClient, const std::string& uri, const std::vector& /*postData*/) { std::string output; std::stringstream ss; ss << "API Query: " << uri << std::endl; Logging::Log(Logging::Severity::Debug, ss.str()); if (StringAlgorithm::istarts_with(uri, "/log/")) output = ProcessLogQuery(pDataStorageClient, uri); else if (StringAlgorithm::istarts_with(uri, "/graphheader/")) output = ProcessGraphHeaderQuery(pGraphClient, uri); else if (StringAlgorithm::istarts_with(uri, "/graph/")) output = ProcessGraphQuery(pGraphClient, uri); else if (StringAlgorithm::istarts_with(uri, "/settings/")) output = ProcessSettingsQuery(uri); else output = "{\"result\": \"error\"}\r\n"; return output; } std::string WebAPI::ProcessLogQuery(DataStorage::DataStorageClient* pDataStorageClient, const std::string& uri) { // /log//// std::string output; std::string error = "{\"result\": \"error\"}\r\n"; std::vector uriTokens = StringAlgorithm::split(uri, '/'); if (uriTokens.size() < 6) return error; if (uriTokens[2].empty() || uriTokens[3].empty() || uriTokens[4].empty()) return error; 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... output = "{\"result\": \"success\"}\r\n"; return output; } std::string WebAPI::ProcessGraphHeaderQuery(DataInterface::GraphClient* pGraphClient, const std::string& uri) { // /graphheader/// std::string output; std::string error = "{\"result\": \"error\"}\r\n"; std::vector uriTokens = StringAlgorithm::split(uri, '/'); if (uriTokens.size() != 5) return error; if (uriTokens[2].empty() || uriTokens[3].empty()) return error; 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); output = json.dump(); } catch (const std::exception& e) { std::cout << "ProcessGraphHeaderQuery() - Error: " << e.what() << std::endl; return error; } return output; } std::string WebAPI::ProcessGraphQuery(DataInterface::GraphClient* pGraphClient, const std::string& uri) { // /graph/// std::string output; std::string error = "{\"result\": \"error\"}\r\n"; std::vector uriTokens = StringAlgorithm::split(uri, '/'); if (uriTokens.size() != 5) return error; if (uriTokens[2].empty() || uriTokens[3].empty()) return error; 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); output = json.dump(); } catch (const std::exception& e) { std::cout << "ProcessGraphQuery() - Error: " << e.what() << std::endl; return error; } return output; } std::string WebAPI::ProcessSettingsQuery(const std::string& uri) { // /settings// std::string success = "{\"result\": \"success\"}\r\n"; std::string error = "{\"result\": \"error\"}\r\n"; std::vector uriTokens = StringAlgorithm::split(uri, '/'); if (uriTokens.size() < 4) return std::string(); 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 error; } else { return error; } return success; } 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