DataStorageClientImpl.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #ifndef DATASTORAGE_DATASTORAGECLIENTIMPL_H
  2. #define DATASTORAGE_DATASTORAGECLIENTIMPL_H
  3. #include <Metronome.h>
  4. #include <map>
  5. #include <memory>
  6. #include <mutex>
  7. #include <string>
  8. namespace MySQL {
  9. class MySQLClient;
  10. } // namespace MySQL
  11. namespace DataStorage {
  12. class DataStorageClientImpl
  13. {
  14. public:
  15. DataStorageClientImpl(const std::shared_ptr<MySQL::MySQLClient>& pMySQLClient, const std::string& table);
  16. ~DataStorageClientImpl();
  17. public:
  18. void LogValue(int deviceId, const std::string& dataName, int timestamp, int value);
  19. void LogValue(int deviceId, const std::string& dataName, int timestamp, double value);
  20. void LogValue(int deviceId, const std::string& dataName, int timestamp, const std::string& value);
  21. void LogValueTimed(int deviceId, const std::string& dataName, int value);
  22. void LogValueTimed(int deviceId, const std::string& dataName, double value);
  23. void LogValueTimed(int deviceId, const std::string& dataName, const std::string& value);
  24. public:
  25. void MetronomeCallback();
  26. private:
  27. class Device_Data {
  28. public:
  29. Device_Data(int deviceId, int dataId) :
  30. m_deviceId(deviceId),
  31. m_dataId(dataId)
  32. {
  33. }
  34. public:
  35. int DeviceId() const
  36. {
  37. return m_deviceId;
  38. }
  39. int DataId() const
  40. {
  41. return m_dataId;
  42. }
  43. public:
  44. bool operator=(const Device_Data& other) const
  45. {
  46. return m_deviceId == other.DeviceId() && m_dataId == other.DataId();
  47. }
  48. bool operator<(const Device_Data& other) const
  49. {
  50. return m_deviceId < other.DeviceId() || (m_deviceId == other.DeviceId() && m_dataId < other.DataId());
  51. }
  52. private:
  53. int m_deviceId;
  54. int m_dataId;
  55. };
  56. private:
  57. void WriteValue(const Device_Data& device_data, int timestamp, int value);
  58. void WriteValue(const Device_Data& device_data, int timestamp, double value);
  59. void WriteValue(const Device_Data& device_data, int timestamp, const std::string& value);
  60. private:
  61. template <typename T>
  62. void ProcessValueMap(std::map<Device_Data, T>& map, int timestamp)
  63. {
  64. for (const auto& keyValue : map)
  65. WriteValue(keyValue.first, timestamp, keyValue.second);
  66. map.clear();
  67. }
  68. private:
  69. std::mutex m_mutex;
  70. std::shared_ptr<MySQL::MySQLClient> m_pMySQLClient;
  71. std::string m_table;
  72. Timer::Metronome m_metronome;
  73. std::map<Device_Data, int> m_integerValues;
  74. std::map<Device_Data, double> m_doubleValues;
  75. std::map<Device_Data, std::string> m_stringValues;
  76. };
  77. } // namespace DataStorage
  78. #endif // DATASTORAGE_DATASTORAGECLIENTIMPL_H