|
@@ -0,0 +1,89 @@
|
|
|
|
|
+#include "DataStorageClientImpl.h"
|
|
|
|
|
+#include "DataId.h"
|
|
|
|
|
+#include <DateTime.h>
|
|
|
|
|
+#include <MySQLClient.h>
|
|
|
|
|
+#include <sstream>
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+namespace DataStorage {
|
|
|
|
|
+
|
|
|
|
|
+DataStorageClientImpl::DataStorageClientImpl(const std::shared_ptr<MySQL::MySQLClient>& pMySQLClient, const std::string& table) :
|
|
|
|
|
+ m_pMySQLClient(pMySQLClient),
|
|
|
|
|
+ m_table(table),
|
|
|
|
|
+ m_metronome(Timer::Metronome::Quantity::Minutes, 5)
|
|
|
|
|
+{
|
|
|
|
|
+ m_metronome.Connect(std::bind(&DataStorageClientImpl::MetronomeCallback, this));
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+DataStorageClientImpl::~DataStorageClientImpl()
|
|
|
|
|
+{
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void DataStorageClientImpl::LogValue(int deviceId, const std::string& dataName, int timestamp, int value)
|
|
|
|
|
+{
|
|
|
|
|
+ int dataId = DataId(dataName).Id();
|
|
|
|
|
+ WriteValue(Device_Data(deviceId, dataId), timestamp, value);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void DataStorageClientImpl::LogValue(int deviceId, const std::string& dataName, int timestamp, double value)
|
|
|
|
|
+{
|
|
|
|
|
+ int dataId = DataId(dataName).Id();
|
|
|
|
|
+ WriteValue(Device_Data(deviceId, dataId), timestamp, value);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void DataStorageClientImpl::LogValue(int deviceId, const std::string& dataName, int timestamp, const std::string& value)
|
|
|
|
|
+{
|
|
|
|
|
+ int dataId = DataId(dataName).Id();
|
|
|
|
|
+ WriteValue(Device_Data(deviceId, dataId), timestamp, value);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void DataStorageClientImpl::LogValueTimed(int deviceId, const std::string& dataName, int value)
|
|
|
|
|
+{
|
|
|
|
|
+ std::unique_lock<std::mutex> lock(m_mutex);
|
|
|
|
|
+ m_integerValues[Device_Data(deviceId, DataId(dataName).Id())] = value;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void DataStorageClientImpl::LogValueTimed(int deviceId, const std::string& dataName, double value)
|
|
|
|
|
+{
|
|
|
|
|
+ std::unique_lock<std::mutex> lock(m_mutex);
|
|
|
|
|
+ m_doubleValues[Device_Data(deviceId, DataId(dataName).Id())] = value;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void DataStorageClientImpl::LogValueTimed(int deviceId, const std::string& dataName, const std::string& value)
|
|
|
|
|
+{
|
|
|
|
|
+ std::unique_lock<std::mutex> lock(m_mutex);
|
|
|
|
|
+ m_stringValues[Device_Data(deviceId, DataId(dataName).Id())] = value;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void DataStorageClientImpl::MetronomeCallback()
|
|
|
|
|
+{
|
|
|
|
|
+ int timestamp = DateTime::GetTimestamp();
|
|
|
|
|
+
|
|
|
|
|
+ std::unique_lock<std::mutex> lock(m_mutex);
|
|
|
|
|
+ ProcessValueMap(m_integerValues, timestamp);
|
|
|
|
|
+ ProcessValueMap(m_doubleValues, timestamp);
|
|
|
|
|
+ ProcessValueMap(m_stringValues, timestamp);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void DataStorageClientImpl::WriteValue(const Device_Data& device_data, int timestamp, int value)
|
|
|
|
|
+{
|
|
|
|
|
+ std::stringstream query;
|
|
|
|
|
+ query << "INSERT INTO `" << m_table << "` (device_id, data_id, timestamp, value) VALUES (" << device_data.DeviceId() << "," << device_data.DataId() << ",FROM_UNIXTIME(" << timestamp << ")," << value << ");";
|
|
|
|
|
+ m_pMySQLClient->Execute(query.str());
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void DataStorageClientImpl::WriteValue(const Device_Data& device_data, int timestamp, double value)
|
|
|
|
|
+{
|
|
|
|
|
+ std::stringstream query;
|
|
|
|
|
+ query << "INSERT INTO `" << m_table << "` (device_id, data_id, timestamp, value) VALUES (" << device_data.DeviceId() << "," << device_data.DataId() << ",FROM_UNIXTIME(" << timestamp << ")," << value << ");";
|
|
|
|
|
+ m_pMySQLClient->Execute(query.str());
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void DataStorageClientImpl::WriteValue(const Device_Data& device_data, int timestamp, const std::string& value)
|
|
|
|
|
+{
|
|
|
|
|
+ std::stringstream query;
|
|
|
|
|
+ query << "INSERT INTO `" << m_table << "` (device_id, data_id, timestamp, value) VALUES (" << device_data.DeviceId() << "," << device_data.DataId() << ",FROM_UNIXTIME(" << timestamp << "),\"" << value << "\");";
|
|
|
|
|
+ m_pMySQLClient->Execute(query.str());
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+} // namespace DataStorage
|