| 1234567891011121314151617181920212223242526272829303132333435363738394041 |
- #include <GroupStatePersistence.h>
- #include <FS.h>
- static const char FILE_PREFIX[] = "group_states/";
- void GroupStatePersistence::get(const BulbId &id, GroupState& state) {
- char path[30];
- memset(path, 0, 30);
- buildFilename(id, path);
- if (SPIFFS.exists(path)) {
- File f = SPIFFS.open(path, "r");
- state.load(f);
- f.close();
- }
- }
- void GroupStatePersistence::set(const BulbId &id, const GroupState& state) {
- char path[30];
- memset(path, 0, 30);
- buildFilename(id, path);
- File f = SPIFFS.open(path, "w");
- state.dump(f);
- f.close();
- }
- void GroupStatePersistence::clear(const BulbId &id) {
- char path[30];
- buildFilename(id, path);
- if (SPIFFS.exists(path)) {
- SPIFFS.remove(path);
- }
- }
- char* GroupStatePersistence::buildFilename(const BulbId &id, char *buffer) {
- uint32_t compactId = (id.deviceId << 24) | (id.deviceType << 8) | id.groupId;
- return buffer + sprintf(buffer, "%s%x", FILE_PREFIX, compactId);
- }
|