| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- #ifndef DATASTORAGE_DATASTORAGECLIENTIMPL_H
- #define DATASTORAGE_DATASTORAGECLIENTIMPL_H
- #include <Metronome.h>
- #include <map>
- #include <memory>
- #include <mutex>
- #include <string>
- namespace MySQL {
- class MySQLClient;
- } // namespace MySQL
- namespace DataStorage {
- class DataStorageClientImpl
- {
- public:
- DataStorageClientImpl(const std::shared_ptr<MySQL::MySQLClient>& pMySQLClient, const std::string& table);
- ~DataStorageClientImpl();
- 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);
- void LogValueTimed(int deviceId, const std::string& dataName, int value);
- void LogValueTimed(int deviceId, const std::string& dataName, double value);
- void LogValueTimed(int deviceId, const std::string& dataName, const std::string& value);
- public:
- void MetronomeCallback();
- private:
- class Device_Data {
- public:
- Device_Data(int deviceId, int dataId) :
- m_deviceId(deviceId),
- m_dataId(dataId)
- {
- }
- public:
- int DeviceId() const
- {
- return m_deviceId;
- }
- int DataId() const
- {
- return m_dataId;
- }
- public:
- bool operator=(const Device_Data& other) const
- {
- return m_deviceId == other.DeviceId() && m_dataId == other.DataId();
- }
- bool operator<(const Device_Data& other) const
- {
- return m_deviceId < other.DeviceId() || (m_deviceId == other.DeviceId() && m_dataId < other.DataId());
- }
- private:
- int m_deviceId;
- int m_dataId;
- };
- private:
- void WriteValue(const Device_Data& device_data, int timestamp, int value);
- void WriteValue(const Device_Data& device_data, int timestamp, double value);
- void WriteValue(const Device_Data& device_data, int timestamp, const std::string& value);
- private:
- template <typename T>
- void ProcessValueMap(std::map<Device_Data, T>& map, int timestamp)
- {
- for (const auto& keyValue : map)
- WriteValue(keyValue.first, timestamp, keyValue.second);
- map.clear();
- }
- private:
- std::mutex m_mutex;
- std::shared_ptr<MySQL::MySQLClient> m_pMySQLClient;
- std::string m_table;
- Timer::Metronome m_metronome;
- std::map<Device_Data, int> m_integerValues;
- std::map<Device_Data, double> m_doubleValues;
- std::map<Device_Data, std::string> m_stringValues;
- };
- } // namespace DataStorage
- #endif // DATASTORAGE_DATASTORAGECLIENTIMPL_H
|