GroupStatePersistence.cpp 907 B

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