Events.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #pragma once
  2. #ifdef __cplusplus
  3. #include "esp_log.h"
  4. #include "tools.h"
  5. #include <cJSON.h>
  6. #include <ctime>
  7. #include <list>
  8. #include <map>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <string>
  12. namespace Metrics {
  13. struct StrCompare {
  14. bool operator()(const char* a, const char* b) const { return strcmp(a, b) < 0; }
  15. };
  16. class Event {
  17. public:
  18. std::map<char*, char*, StrCompare> properties;
  19. Event& add_property(const char* name, const char* value);
  20. bool has_property_value(const char* name, const char* value) const;
  21. void remove_property(const char* name, const char* value);
  22. cJSON* properties_to_json();
  23. cJSON* to_json(const char* distinct_id);
  24. void free_json();
  25. void update_time();
  26. explicit Event(const char* name) {
  27. _name = strdup_psram(name);
  28. memset(&_time, 0x00, sizeof(_time));
  29. }
  30. const char* get_name() const { return _name; }
  31. ~Event() {
  32. FREE_AND_NULL(_name);
  33. // Iterate through the map and free the elements
  34. for (auto& kv : properties) {
  35. free((void*)kv.first);
  36. free(kv.second);
  37. }
  38. properties.clear(); // Clear the map after freeing memory
  39. FREE_AND_NULL(_json);
  40. }
  41. private:
  42. char* _name = nullptr;
  43. uint32_t _time;
  44. cJSON* _json = nullptr;
  45. };
  46. } // namespace Metrics
  47. #endif