GroupStateStore.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include <GroupStateStore.h>
  2. #include <MiLightRemoteConfig.h>
  3. GroupStateStore::GroupStateStore(const size_t maxSize)
  4. : cache(GroupStateCache(maxSize))
  5. { }
  6. GroupState& GroupStateStore::get(const BulbId& id) {
  7. GroupState* state = cache.get(id);
  8. if (state == NULL) {
  9. trackEviction();
  10. GroupState loadedState = GroupState::defaultState(id.deviceType);
  11. persistence.get(id, loadedState);
  12. state = cache.set(id, loadedState);
  13. }
  14. return *state;
  15. }
  16. GroupState& GroupStateStore::set(const BulbId &id, const GroupState& state) {
  17. GroupState& storedState = get(id);
  18. storedState = state;
  19. if (id.groupId == 0) {
  20. const MiLightRemoteConfig* remote = MiLightRemoteConfig::fromType(id.deviceType);
  21. BulbId individualBulb(id);
  22. for (size_t i = 1; i < remote->numGroups; i++) {
  23. individualBulb.groupId = i;
  24. set(individualBulb, state);
  25. }
  26. }
  27. return storedState;
  28. }
  29. void GroupStateStore::trackEviction() {
  30. if (cache.isFull()) {
  31. evictedIds.add(cache.getLru());
  32. }
  33. }
  34. void GroupStateStore::flush() {
  35. ListNode<GroupCacheNode*>* curr = cache.getHead();
  36. while (curr != NULL && curr->data->state.isDirty()) {
  37. persistence.set(curr->data->id, curr->data->state);
  38. curr->data->state.clearDirty();
  39. curr = curr->next;
  40. }
  41. while (evictedIds.size() > 0) {
  42. persistence.clear(evictedIds.shift());
  43. }
  44. }