ソースを参照

Initial commit

JDierkse 5 年 前
コミット
3a12256d49

+ 6 - 0
.gitignore

@@ -0,0 +1,6 @@
+*.o.*
+*.d.*
+*.a.*
+.*.swp
+.AppleDouble
+lib

+ 3 - 0
.gitmodules

@@ -0,0 +1,3 @@
+[submodule "Makefiles"]
+	path = Makefiles
+	url = https://gogs.dierkse.nl/JDierkse/Makefiles

+ 43 - 0
DataStorage/DataId.cpp

@@ -0,0 +1,43 @@
+#include "DataId.h"
+#include <StringAlgorithm.h>
+
+
+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

+ 46 - 0
DataStorage/DataStorageClient.cpp

@@ -0,0 +1,46 @@
+#include "DataStorageClient.h"
+#include "DataId.h"
+#include <MySQLClient.h>
+#include <sstream>
+
+
+namespace DataStorage {
+
+DataStorageClient::DataStorageClient(const std::shared_ptr<MySQL::MySQLClient>& 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

+ 1 - 0
DataStorage/Makefile

@@ -0,0 +1 @@
+../Makefile

+ 43 - 0
DataStorage/Timespan.cpp

@@ -0,0 +1,43 @@
+#include "Timespan.h"
+#include <StringAlgorithm.h>
+
+
+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

+ 1 - 0
Libraries/Logging

@@ -0,0 +1 @@
+../../Logging

+ 1 - 0
Libraries/MySQL

@@ -0,0 +1 @@
+../../MySQL

+ 1 - 0
Libraries/Utilities

@@ -0,0 +1 @@
+../../Utilities

+ 1 - 0
Makefile

@@ -0,0 +1 @@
+Makefiles/Makefile

+ 21 - 0
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

+ 17 - 0
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

+ 1 - 0
Makefiles

@@ -0,0 +1 @@
+Subproject commit cd9ef1a1b0f7b88f36b0c51b4209d1859aca83aa

+ 26 - 0
include/DataId.h

@@ -0,0 +1,26 @@
+#ifndef DATAID_H
+#define DATAID_H
+
+#include <string>
+
+
+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

+ 37 - 0
include/DataStorageClient.h

@@ -0,0 +1,37 @@
+#ifndef DATASTORAGECLIENT_H
+#define DATASTORAGECLIENT_H
+
+#include "Timespan.h"
+#include <memory>
+#include <string>
+
+
+namespace MySQL {
+
+class MySQLClient;
+
+} // namespace MySQL
+
+namespace DataStorage {
+
+class DataStorageClientImpl;
+
+class DataStorageClient
+{
+public:
+	DataStorageClient(const std::shared_ptr<MySQL::MySQLClient>& 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<MySQL::MySQLClient> m_pMySQLClient;
+	std::string m_table;
+};
+
+} // namespace DataStorage
+
+#endif // DATASTORAGECLIENT_H

+ 30 - 0
include/Timespan.h

@@ -0,0 +1,30 @@
+#ifndef TIMESPAN_H
+#define TIMESPAN_H
+
+#include <string>
+
+
+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