GroupStateStore.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include <GroupState.h>
  2. #include <GroupStateCache.h>
  3. #include <GroupStatePersistence.h>
  4. #ifndef _GROUP_STATE_STORE_H
  5. #define _GROUP_STATE_STORE_H
  6. class GroupStateStore {
  7. public:
  8. GroupStateStore(const size_t maxSize, const size_t flushRate);
  9. /*
  10. * Returns the state for the given BulbId. If accessing state for a valid device
  11. * (i.e., NOT group 0) and no state exists, its state will be initialized with a
  12. * default.
  13. *
  14. * Otherwise, we return NULL.
  15. */
  16. GroupState* get(const BulbId& id);
  17. GroupState* get(const uint16_t deviceId, const uint8_t groupId, const MiLightRemoteType deviceType);
  18. /*
  19. * Sets the state for the given BulbId. State will be marked as dirty and
  20. * flushed to persistent storage.
  21. */
  22. GroupState* set(const BulbId& id, const GroupState& state);
  23. GroupState* set(const uint16_t deviceId, const uint8_t groupId, const MiLightRemoteType deviceType, const GroupState& state);
  24. void clear(const BulbId& id);
  25. /*
  26. * Flushes all states to persistent storage. Returns true iff anything was
  27. * flushed.
  28. */
  29. bool flush();
  30. /*
  31. * Flushes at most one dirty state to persistent storage. Rate limit
  32. * specified by Settings.
  33. */
  34. void limitedFlush();
  35. private:
  36. GroupStateCache cache;
  37. GroupStatePersistence persistence;
  38. LinkedList<BulbId> evictedIds;
  39. const size_t flushRate;
  40. unsigned long lastFlush;
  41. void trackEviction();
  42. };
  43. #endif