Chris Mullins лет назад: 8
Родитель
Сommit
396a66bf0d

+ 2 - 4
lib/MiLightState/GroupState.cpp

@@ -179,16 +179,14 @@ bool GroupState::clearDirty() { state.fields._dirty = 0; }
 
 void GroupState::load(Stream& stream) {
   for (size_t i = 0; i < DATA_BYTES; i++) {
-    state.data[i] = stream.read();
+    stream.readBytes(reinterpret_cast<uint8_t*>(&state.data[i]), 4);
   }
   clearDirty();
 }
 
 void GroupState::dump(Stream& stream) const {
   for (size_t i = 0; i < DATA_BYTES; i++) {
-    uint32_t val = state.data[i];
-    uint8_t* bytePtr = reinterpret_cast<uint8_t*>(&val);
-    stream.write(bytePtr, 4);
+    stream.write(reinterpret_cast<const uint8_t*>(&state.data[i]), 4);
   }
 }
 

+ 2 - 0
lib/MiLightState/GroupStatePersistence.cpp

@@ -5,6 +5,7 @@ static const char FILE_PREFIX[] = "group_states/";
 
 void GroupStatePersistence::get(const GroupId &id, GroupState& state) {
   char path[30];
+  memset(path, 0, 30);
   buildFilename(id, path);
 
   if (SPIFFS.exists(path)) {
@@ -16,6 +17,7 @@ void GroupStatePersistence::get(const GroupId &id, GroupState& state) {
 
 void GroupStatePersistence::set(const GroupId &id, const GroupState& state) {
   char path[30];
+  memset(path, 0, 30);
   buildFilename(id, path);
 
   File f = SPIFFS.open(path, "w");

+ 5 - 2
lib/MiLightState/GroupStateStore.cpp

@@ -9,7 +9,10 @@ GroupState* GroupStateStore::get(const GroupId& id) {
 
   if (state == NULL) {
     trackEviction();
-    state = cache.set(id, GroupState::defaultState(id.deviceType));
+    GroupState loadedState = GroupState::defaultState(id.deviceType);
+    persistence.get(id, loadedState);
+
+    state = cache.set(id, loadedState);
   }
 
   return state;
@@ -27,7 +30,7 @@ void GroupStateStore::trackEviction() {
 
 void GroupStateStore::flush() {
   ListNode<GroupCacheNode*>* curr = cache.getHead();
-
+  
   while (curr != NULL && curr->data->state.isDirty()) {
     persistence.set(curr->data->id, curr->data->state);
     curr->data->state.clearDirty();