interface_influxdb.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #ifdef ENABLE_INFLUXDB
  2. #pragma once
  3. #ifndef INTERFACE_INFLUXDB_H
  4. #define INTERFACE_INFLUXDB_H
  5. #include <string>
  6. #include <map>
  7. #include <functional>
  8. #include <string>
  9. #include "esp_http_client.h"
  10. #include "esp_log.h"
  11. enum InfluxDBVersion {
  12. INFLUXDB_V1,
  13. INFLUXDB_V2
  14. };
  15. /**
  16. * @class InfluxDB
  17. * @brief A class to handle connections and data publishing to InfluxDB servers.
  18. *
  19. * This class supports both InfluxDB v1.x and v2.x versions. It provides methods to initialize
  20. * the connection parameters, publish data, and destroy the connection.
  21. *
  22. * @private
  23. * @var std::string influxDBURI
  24. * URI for the InfluxDB server.
  25. *
  26. * @var std::string database
  27. * Database name for InfluxDB v1.x.
  28. *
  29. * @var std::string user
  30. * Username for InfluxDB v1.x.
  31. *
  32. * @var std::string password
  33. * Password for InfluxDB v1.x.
  34. *
  35. * @var std::string bucket
  36. * Bucket name for InfluxDB v2.x.
  37. *
  38. * @var std::string org
  39. * Organization name for InfluxDB v2.x.
  40. *
  41. * @var std::string token
  42. * Token for InfluxDB v2.x.
  43. *
  44. * @var InfluxDBVersion version
  45. * Version of the InfluxDB server (v1.x or v2.x).
  46. *
  47. * @var esp_http_client_handle_t httpClient
  48. * HTTP client handle for making requests to the InfluxDB server.
  49. *
  50. * @var void connectHTTP()
  51. * Establishes an HTTP connection to the InfluxDB server.
  52. *
  53. * @public
  54. * @fn void InfluxDBInitV1(std::string _influxDBURI, std::string _database, std::string _user, std::string _password)
  55. * Initializes the connection parameters for InfluxDB v1.x.
  56. *
  57. * @fn void InfluxDBInitV2(std::string _influxDBURI, std::string _bucket, std::string _org, std::string _token)
  58. * Initializes the connection parameters for InfluxDB v2.x.
  59. *
  60. * @fn void InfluxDBdestroy()
  61. * Destroys the InfluxDB connection.
  62. *
  63. * @fn void InfluxDBPublish(std::string _measurement, std::string _key, std::string _content, long int _timeUTC)
  64. * Publishes data to the InfluxDB server.
  65. *
  66. * @param _measurement The measurement name.
  67. * @param _key The key for the data point.
  68. * @param _content The content or value of the data point.
  69. * @param _timeUTC The timestamp in UTC for the data point.
  70. */
  71. class InfluxDB {
  72. private:
  73. // Information for InfluxDB v1.x
  74. std::string influxDBURI = "";
  75. // Information for InfluxDB v1.x
  76. std::string database = "";
  77. std::string user = "";
  78. std::string password = "";
  79. // Information for InfluxDB v2.x
  80. std::string bucket = "";
  81. std::string org = "";
  82. std::string token = "";
  83. InfluxDBVersion version;
  84. esp_http_client_handle_t httpClient = NULL;
  85. void connectHTTP();
  86. public:
  87. // Initialize the InfluxDB connection parameters
  88. void InfluxDBInitV1(std::string _influxDBURI, std::string _database, std::string _user, std::string _password);
  89. void InfluxDBInitV2(std::string _influxDBURI, std::string _bucket, std::string _org, std::string _token);
  90. // Destroy the InfluxDB connection
  91. void InfluxDBdestroy();
  92. // Publish data to the InfluxDB server
  93. void InfluxDBPublish(std::string _measurement, std::string _key, std::string _content, long int _timeUTC);
  94. };
  95. #endif //INTERFACE_INFLUXDB_H
  96. #endif //ENABLE_INFLUXDB