From 3a12256d497ce3a6d8f24b6cdbeb793cf352a58d Mon Sep 17 00:00:00 2001 From: JDierkse Date: Fri, 1 May 2020 10:56:12 +0200 Subject: [PATCH] Initial commit --- .gitignore | 6 ++++ .gitmodules | 3 ++ DataStorage/DataId.cpp | 43 +++++++++++++++++++++++++++++ DataStorage/DataStorageClient.cpp | 46 +++++++++++++++++++++++++++++++ DataStorage/Makefile | 1 + DataStorage/Timespan.cpp | 43 +++++++++++++++++++++++++++++ Libraries/Logging | 1 + Libraries/MySQL | 1 + Libraries/Utilities | 1 + Makefile | 1 + Makefile.conf | 21 ++++++++++++++ Makefile.target | 17 ++++++++++++ Makefiles | 1 + include/DataId.h | 26 +++++++++++++++++ include/DataStorageClient.h | 37 +++++++++++++++++++++++++ include/Timespan.h | 30 ++++++++++++++++++++ 16 files changed, 278 insertions(+) create mode 100644 .gitignore create mode 100644 .gitmodules create mode 100644 DataStorage/DataId.cpp create mode 100644 DataStorage/DataStorageClient.cpp create mode 120000 DataStorage/Makefile create mode 100644 DataStorage/Timespan.cpp create mode 120000 Libraries/Logging create mode 120000 Libraries/MySQL create mode 120000 Libraries/Utilities create mode 120000 Makefile create mode 100644 Makefile.conf create mode 100644 Makefile.target create mode 160000 Makefiles create mode 100644 include/DataId.h create mode 100644 include/DataStorageClient.h create mode 100644 include/Timespan.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..851ef09 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +*.o.* +*.d.* +*.a.* +.*.swp +.AppleDouble +lib diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..876e403 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "Makefiles"] + path = Makefiles + url = https://gogs.dierkse.nl/JDierkse/Makefiles diff --git a/DataStorage/DataId.cpp b/DataStorage/DataId.cpp new file mode 100644 index 0000000..243ebd8 --- /dev/null +++ b/DataStorage/DataId.cpp @@ -0,0 +1,43 @@ +#include "DataId.h" +#include + + +namespace DataStorage { + +DataId::DataId(const std::string& table) : + m_table(table) +{ +} + +int DataId::Id() const +{ + return GetId(m_table); +} + +std::string DataId::Table() const +{ + return m_table; +} + +int DataId::GetId(const std::string& dataName) const +{ + if (StringAlgorithm::iequals(dataName, "voltage")) + return 0; + + if (StringAlgorithm::iequals(dataName, "gas") || + StringAlgorithm::iequals(dataName, "intensity") || + StringAlgorithm::iequals(dataName, "power") || + StringAlgorithm::iequals(dataName, "pressure") || + StringAlgorithm::iequals(dataName, "state") || + StringAlgorithm::iequals(dataName, "temperature")) + return 1; + + if (StringAlgorithm::iequals(dataName, "humidity") || + StringAlgorithm::iequals(dataName, "setpoint") || + StringAlgorithm::iequals(dataName, "forecast")) + return 2; + + return -1; +} + +} // namespace DataStorage diff --git a/DataStorage/DataStorageClient.cpp b/DataStorage/DataStorageClient.cpp new file mode 100644 index 0000000..4b4c613 --- /dev/null +++ b/DataStorage/DataStorageClient.cpp @@ -0,0 +1,46 @@ +#include "DataStorageClient.h" +#include "DataId.h" +#include +#include + + +namespace DataStorage { + +DataStorageClient::DataStorageClient(const std::shared_ptr& pMySQLClient, const std::string& table) : + m_pMySQLClient(pMySQLClient), + m_table(table) +{ +} + +DataStorageClient::~DataStorageClient() +{ +} + +void DataStorageClient::LogValue(int deviceId, const std::string& dataName, int timestamp, int value) +{ + int dataId = DataId(dataName).Id(); + + std::stringstream query; + query << "INSERT INTO `" << m_table << "` (device_id, data_id, timestamp, value) VALUES (" << deviceId << "," << dataId << ",FROM_UNIXTIME(" << timestamp << ")," << value << ");"; + m_pMySQLClient->Execute(query.str()); +} + +void DataStorageClient::LogValue(int deviceId, const std::string& dataName, int timestamp, double value) +{ + int dataId = DataId(dataName).Id(); + + std::stringstream query; + query << "INSERT INTO `" << m_table << "` (device_id, data_id, timestamp, value) VALUES (" << deviceId << "," << dataId << ",FROM_UNIXTIME(" << timestamp << ")," << value << ");"; + m_pMySQLClient->Execute(query.str()); +} + +void DataStorageClient::LogValue(int deviceId, const std::string& dataName, int timestamp, const std::string& value) +{ + int dataId = DataId(dataName).Id(); + + std::stringstream query; + query << "INSERT INTO `" << m_table << "` (device_id, data_id, timestamp, value) VALUES (" << deviceId << "," << dataId << ",FROM_UNIXTIME(" << timestamp << "),\"" << value << "\");"; + m_pMySQLClient->Execute(query.str()); +} + +} // namespace DataStorageClientImpl diff --git a/DataStorage/Makefile b/DataStorage/Makefile new file mode 120000 index 0000000..d0b0e8e --- /dev/null +++ b/DataStorage/Makefile @@ -0,0 +1 @@ +../Makefile \ No newline at end of file diff --git a/DataStorage/Timespan.cpp b/DataStorage/Timespan.cpp new file mode 100644 index 0000000..02cae9b --- /dev/null +++ b/DataStorage/Timespan.cpp @@ -0,0 +1,43 @@ +#include "Timespan.h" +#include + + +namespace DataStorage { + +namespace Conversions { + +Timespan::type Timespan(const std::string& timespan) +{ + if (StringAlgorithm::iequals(timespan, "Day")) + return Timespan::Day; + else if (StringAlgorithm::iequals(timespan, "Week")) + return Timespan::Week; + else if (StringAlgorithm::iequals(timespan, "Month")) + return Timespan::Month; + else if (StringAlgorithm::iequals(timespan, "Year")) + return Timespan::Year; + else + return Timespan::Unknown; +} + +std::string Timespan(Timespan::type timespan) +{ + switch (timespan) + { + case Timespan::Day: + return "Day"; + case Timespan::Week: + return "Week"; + case Timespan::Month: + return "Month"; + case Timespan::Year: + return "Year"; + default: + case Timespan::Unknown: + return "Unknown"; + } +} + +} // namespace Conversions + +} // namespace DataStorage diff --git a/Libraries/Logging b/Libraries/Logging new file mode 120000 index 0000000..e18bda1 --- /dev/null +++ b/Libraries/Logging @@ -0,0 +1 @@ +../../Logging \ No newline at end of file diff --git a/Libraries/MySQL b/Libraries/MySQL new file mode 120000 index 0000000..54c644a --- /dev/null +++ b/Libraries/MySQL @@ -0,0 +1 @@ +../../MySQL \ No newline at end of file diff --git a/Libraries/Utilities b/Libraries/Utilities new file mode 120000 index 0000000..4a6b454 --- /dev/null +++ b/Libraries/Utilities @@ -0,0 +1 @@ +../../Utilities \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 120000 index 0000000..6aa5187 --- /dev/null +++ b/Makefile @@ -0,0 +1 @@ +Makefiles/Makefile \ No newline at end of file diff --git a/Makefile.conf b/Makefile.conf new file mode 100644 index 0000000..ecfd4d1 --- /dev/null +++ b/Makefile.conf @@ -0,0 +1,21 @@ +# +# Makefile.conf +# + +LFLAGS += -lUtilities +LFLAGS += -L$(ROOTPATH)/Libraries/Utilities/lib/$(ARCH) +CFLAGS += -I$(ROOTPATH)/Libraries/Utilities/include + +LFLAGS += -lLogging +LFLAGS += -L$(ROOTPATH)/Libraries/Logging/lib/$(ARCH) +CFLAGS += -I$(ROOTPATH)/Libraries/Logging/include + +LFLAGS += -lMySQL -lmysqlclient -lmysqlcppconn +LFLAGS += -L$(ROOTPATH)/Libraries/MySQL/lib/$(ARCH) +CFLAGS += -I$(ROOTPATH)/Libraries/MySQL/include + +LFLAGS += -lDataStorage +LFLAGS += -L$(ROOTPATH)/lib/$(ARCH) +CFLAGS += -I$(ROOTPATH) -I$(ROOTPATH)/include + +DEBUGDIR := .debug diff --git a/Makefile.target b/Makefile.target new file mode 100644 index 0000000..2f5a8dd --- /dev/null +++ b/Makefile.target @@ -0,0 +1,17 @@ +# +# Makefile.target +# + +DataStorage.a.$(ARCH) : $(OBJECTS) + $(call build_target_library_arch,$@,$^) +DataStorage.a: + $(call build_target,$@) + +test.$(ARCH): $(OBJECTS) Test/Test.o.$(ARCH) | DataStorage.a.$(ARCH) + $(call build_target_arch,$@,$^) +test: + $(call build_target,$@) + +.DEFAULT_GOAL := DataStorage.a + +TARGETS += DataStorage.a test diff --git a/Makefiles b/Makefiles new file mode 160000 index 0000000..cd9ef1a --- /dev/null +++ b/Makefiles @@ -0,0 +1 @@ +Subproject commit cd9ef1a1b0f7b88f36b0c51b4209d1859aca83aa diff --git a/include/DataId.h b/include/DataId.h new file mode 100644 index 0000000..3a8f8fb --- /dev/null +++ b/include/DataId.h @@ -0,0 +1,26 @@ +#ifndef DATAID_H +#define DATAID_H + +#include + + +namespace DataStorage { + +class DataId +{ +public: + DataId(const std::string& dataName); + + int Id() const; + std::string Table() const; + +private: + int GetId(const std::string& dataName) const; + +private: + std::string m_table; +}; + +} // namespace DataStorage + +#endif // DATAID_H diff --git a/include/DataStorageClient.h b/include/DataStorageClient.h new file mode 100644 index 0000000..dbd61d0 --- /dev/null +++ b/include/DataStorageClient.h @@ -0,0 +1,37 @@ +#ifndef DATASTORAGECLIENT_H +#define DATASTORAGECLIENT_H + +#include "Timespan.h" +#include +#include + + +namespace MySQL { + +class MySQLClient; + +} // namespace MySQL + +namespace DataStorage { + +class DataStorageClientImpl; + +class DataStorageClient +{ +public: + DataStorageClient(const std::shared_ptr& pMySQLClient, const std::string& table); + ~DataStorageClient(); + +public: + void LogValue(int deviceId, const std::string& dataName, int timestamp, int value); + void LogValue(int deviceId, const std::string& dataName, int timestamp, double value); + void LogValue(int deviceId, const std::string& dataName, int timestamp, const std::string& value); + +private: + std::shared_ptr m_pMySQLClient; + std::string m_table; +}; + +} // namespace DataStorage + +#endif // DATASTORAGECLIENT_H diff --git a/include/Timespan.h b/include/Timespan.h new file mode 100644 index 0000000..c99ef7e --- /dev/null +++ b/include/Timespan.h @@ -0,0 +1,30 @@ +#ifndef TIMESPAN_H +#define TIMESPAN_H + +#include + + +namespace DataStorage { + +struct Timespan +{ + enum type + { + Day = 24 * 60 * 60, + Week = 7 * 24 * 60 * 60, + Month = 31 * 24 * 60 * 60, + Year = 365 * 24 * 60 * 60, + Unknown + }; +}; + +namespace Conversions { + +Timespan::type Timespan(const std::string& timespan); +std::string Timespan(Timespan::type timespan); + +} // namespace Conversions + +} // namespace DataStorage + +#endif // TIMESPAN_H