GroupStatePersistence.cpp 949 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #include <GroupStatePersistence.h>
  2. #include <FS.h>
  3. static const char FILE_PREFIX[] = "group_states/";
  4. void GroupStatePersistence::get(const BulbId &id, GroupState& state) {
  5. char path[30];
  6. memset(path, 0, 30);
  7. buildFilename(id, path);
  8. if (SPIFFS.exists(path)) {
  9. File f = SPIFFS.open(path, "r");
  10. state.load(f);
  11. f.close();
  12. }
  13. }
  14. void GroupStatePersistence::set(const BulbId &id, const GroupState& state) {
  15. char path[30];
  16. memset(path, 0, 30);
  17. buildFilename(id, path);
  18. File f = SPIFFS.open(path, "w");
  19. state.dump(f);
  20. f.close();
  21. }
  22. void GroupStatePersistence::clear(const BulbId &id) {
  23. char path[30];
  24. buildFilename(id, path);
  25. if (SPIFFS.exists(path)) {
  26. SPIFFS.remove(path);
  27. }
  28. }
  29. char* GroupStatePersistence::buildFilename(const BulbId &id, char *buffer) {
  30. uint32_t compactId = (id.deviceId << 24) | (id.deviceType << 8) | id.groupId;
  31. return buffer + sprintf(buffer, "%s%x", FILE_PREFIX, compactId);
  32. }