An ESP32-based Redis-watcher and info-displayer https://rpjios.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
2.3 KiB

  1. #ifndef __ZW_REDIS__H__
  2. #define __ZW_REDIS__H__
  3. #include <Redis.h>
  4. #include <WiFiClient.h>
  5. #include <vector>
  6. #include "zw_common.h"
  7. #define ZWREDIS_DEFAULT_EXPIRY 120
  8. struct ZWRedisHostConfig
  9. {
  10. const char *host;
  11. uint16_t port;
  12. const char *password;
  13. };
  14. class ZWRedis;
  15. class ZWRedisResponder {
  16. protected:
  17. ZWRedis& redis;
  18. String key;
  19. int expire = ZWREDIS_DEFAULT_EXPIRY;
  20. public:
  21. ZWRedisResponder(ZWRedis& parent, String currentKey) :
  22. redis(parent), key(currentKey) {}
  23. ~ZWRedisResponder() {}
  24. ZWRedisResponder(const ZWRedisResponder &) = delete;
  25. ZWRedisResponder &operator=(const ZWRedisResponder &) = delete;
  26. void setExpire(int newExpire) { expire = newExpire; }
  27. void setValue(const char* format, ...);
  28. };
  29. typedef bool (*ZWRedisUserKeyHandler)(String& userKeyValue, ZWRedisResponder& responder);
  30. class ZWRedis {
  31. protected:
  32. friend class ZWRedisResponder;
  33. struct RedisClientConn
  34. {
  35. Redis *redis;
  36. WiFiClient *wifi;
  37. };
  38. String &hostname;
  39. ZWRedisHostConfig configuration;
  40. RedisClientConn connection;
  41. void responderHelper(const char* key, const char* msg, int expire = 0);
  42. public:
  43. ZWRedis(String &hostname, ZWRedisHostConfig config) :
  44. hostname(hostname), configuration(config)
  45. {}
  46. ~ZWRedis() {}
  47. ZWRedis(const ZWRedis &) = delete;
  48. ZWRedis &operator=(const ZWRedis &) = delete;
  49. // TODO moves
  50. bool connect();
  51. void checkin(
  52. unsigned long ticks,
  53. const char* localIp,
  54. unsigned long immediateLatency,
  55. unsigned long averageLatency,
  56. int expireMessage = 60);
  57. bool heartbeat(int expire = 0);
  58. int incrementBootcount(bool reset = false);
  59. ZWAppConfig readConfig();
  60. int updateConfig(ZWAppConfig newConfig);
  61. bool handleUserKey(const char *keyPostfix, ZWRedisUserKeyHandler handler);
  62. void publishLog(const char* msg);
  63. bool postCompletedUpdate();
  64. std::vector<String> getRange(const char* key, int start, int stop);
  65. bool clearControlPoint();
  66. bool registerDevice(const char* registryName, const char* hostname, const char* ident);
  67. void logCritical(const char* fmt, ...);
  68. void getTime(uint8_t* hour, uint8_t* minute, uint8_t* second);
  69. private:
  70. ZWAppConfig _lastReadConfig;
  71. };
  72. #endif