Initial commit
This commit is contained in:
+227
@@ -0,0 +1,227 @@
|
||||
#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()
|
||||
{
|
||||
}
|
||||
|
||||
std::string WebAPI::ProcessQuery(DataStorage::DataStorageClient* pDataStorageClient, DataInterface::GraphClient* pGraphClient, const std::string& uri, const std::vector<Http::HttpPostData>& /*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/<timestamp>/<device_id>/<data_id>/<value>
|
||||
|
||||
std::string output;
|
||||
std::string error = "{\"result\": \"error\"}\r\n";
|
||||
|
||||
std::vector<std::string> 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/<device_id>/<data_ids>/<timespan>
|
||||
|
||||
std::string output;
|
||||
std::string error = "{\"result\": \"error\"}\r\n";
|
||||
|
||||
std::vector<std::string> 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<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);
|
||||
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/<device_id>/<data_ids>/<timespan>
|
||||
|
||||
std::string output;
|
||||
std::string error = "{\"result\": \"error\"}\r\n";
|
||||
|
||||
std::vector<std::string> 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<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);
|
||||
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/<setting>/<value>
|
||||
|
||||
std::string success = "{\"result\": \"success\"}\r\n";
|
||||
std::string error = "{\"result\": \"error\"}\r\n";
|
||||
|
||||
std::vector<std::string> 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<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
|
||||
Reference in New Issue
Block a user