Saving progress
This commit is contained in:
parent
c7beacf3b9
commit
71473fd5c4
@ -89,7 +89,7 @@ void handleIR();
|
|||||||
void deserializeSegment(JsonObject elem, byte it);
|
void deserializeSegment(JsonObject elem, byte it);
|
||||||
bool deserializeState(JsonObject root);
|
bool deserializeState(JsonObject root);
|
||||||
void serializeSegment(JsonObject& root, WS2812FX::Segment& seg, byte id);
|
void serializeSegment(JsonObject& root, WS2812FX::Segment& seg, byte id);
|
||||||
void serializeState(JsonObject root);
|
void serializeState(JsonObject root, bool forPreset = false);
|
||||||
void serializeInfo(JsonObject root);
|
void serializeInfo(JsonObject root);
|
||||||
void serveJson(AsyncWebServerRequest* request);
|
void serveJson(AsyncWebServerRequest* request);
|
||||||
bool serveLiveLeds(AsyncWebServerRequest* request, uint32_t wsClient = 0);
|
bool serveLiveLeds(AsyncWebServerRequest* request, uint32_t wsClient = 0);
|
||||||
|
@ -18,7 +18,9 @@
|
|||||||
//find() that reads and buffers data from file stream in 256-byte blocks.
|
//find() that reads and buffers data from file stream in 256-byte blocks.
|
||||||
//Significantly faster, f.find(key) can take SECONDS for multi-kB files
|
//Significantly faster, f.find(key) can take SECONDS for multi-kB files
|
||||||
bool bufferedFind(const char *target, File f) {
|
bool bufferedFind(const char *target, File f) {
|
||||||
|
if (!f || !f.size()) return false;
|
||||||
size_t targetLen = strlen(target);
|
size_t targetLen = strlen(target);
|
||||||
|
Serial.print("bfind ");
|
||||||
Serial.println(target);
|
Serial.println(target);
|
||||||
//Serial.println(f.position());
|
//Serial.println(f.position());
|
||||||
size_t index = 0;
|
size_t index = 0;
|
||||||
@ -28,7 +30,7 @@ bool bufferedFind(const char *target, File f) {
|
|||||||
|
|
||||||
while (f.position() < f.size() -1) {
|
while (f.position() < f.size() -1) {
|
||||||
//c = f.read();
|
//c = f.read();
|
||||||
//Serial.println(f.position());
|
Serial.println(f.position());
|
||||||
bufsize = f.read(buf, 256);
|
bufsize = f.read(buf, 256);
|
||||||
count = 0;
|
count = 0;
|
||||||
while (count < bufsize) {
|
while (count < bufsize) {
|
||||||
@ -44,13 +46,15 @@ bool bufferedFind(const char *target, File f) {
|
|||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Serial.println("No match");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
//find empty spots in file stream in 256-byte blocks.
|
//find empty spots in file stream in 256-byte blocks.
|
||||||
bool bufferedFindSpace(uint16_t targetLen, File f) {
|
bool bufferedFindSpace(uint16_t targetLen, File f) {
|
||||||
size_t index = 0;
|
Serial.print("bfs ");
|
||||||
byte c;
|
if (!f || !f.size()) return false;
|
||||||
|
uint16_t index = 0;
|
||||||
uint16_t bufsize = 0, count = 0;
|
uint16_t bufsize = 0, count = 0;
|
||||||
byte buf[256];
|
byte buf[256];
|
||||||
|
|
||||||
@ -64,6 +68,7 @@ bool bufferedFindSpace(uint16_t targetLen, File f) {
|
|||||||
if(buf[count] == ' ') {
|
if(buf[count] == ' ') {
|
||||||
if(++index >= targetLen) { // return true if space long enough
|
if(++index >= targetLen) { // return true if space long enough
|
||||||
f.seek(f.position() - targetLen);
|
f.seek(f.position() - targetLen);
|
||||||
|
Serial.print("SPAAAACE!");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -89,30 +94,49 @@ bool writeObjectToFile(const char* file, const char* key, JsonDocument* content)
|
|||||||
//f.setTimeout(1);
|
//f.setTimeout(1);
|
||||||
f.seek(0, SeekSet);
|
f.seek(0, SeekSet);
|
||||||
|
|
||||||
|
Serial.print("Writing to ");
|
||||||
|
Serial.print(file);
|
||||||
|
Serial.print(" with key ");
|
||||||
|
Serial.print(key);
|
||||||
|
Serial.print(" > ");
|
||||||
|
serializeJson(*content, Serial);
|
||||||
|
Serial.println();
|
||||||
|
|
||||||
if (!bufferedFind(key, f)) //key does not exist in file
|
if (!bufferedFind(key, f)) //key does not exist in file
|
||||||
{
|
{
|
||||||
|
Serial.println("Key not found");
|
||||||
return appendObjectToFile(file, key, content, f);
|
return appendObjectToFile(file, key, content, f);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Serial.println("Key found!");
|
||||||
//exists
|
//exists
|
||||||
pos = f.position();
|
pos = f.position();
|
||||||
|
Serial.println(pos);
|
||||||
//measure out end of old object
|
//measure out end of old object
|
||||||
StaticJsonDocument<512> doc;
|
StaticJsonDocument<1024> doc;
|
||||||
deserializeJson(doc, f);
|
deserializeJson(doc, f);
|
||||||
uint32_t pos2 = f.position();
|
uint32_t pos2 = f.position();
|
||||||
uint32_t oldLen = pos2 - pos;
|
uint32_t oldLen = pos2 - pos;
|
||||||
|
Serial.print("Old obj len: ");
|
||||||
|
Serial.print(oldLen);
|
||||||
|
Serial.print(" > ");
|
||||||
|
serializeJson(doc, Serial);
|
||||||
|
Serial.println();
|
||||||
|
|
||||||
if (!content->isNull() && measureJson(*content) <= oldLen) //replace
|
if (!content->isNull() && measureJson(*content) <= oldLen) //replace
|
||||||
{
|
{
|
||||||
|
Serial.println("replace");
|
||||||
|
f.seek(pos);
|
||||||
serializeJson(*content, f);
|
serializeJson(*content, f);
|
||||||
//pad rest
|
//pad rest
|
||||||
for (uint32_t i = f.position(); i < pos2; i++) {
|
for (uint32_t i = f.position(); i < pos2; i++) {
|
||||||
f.write(' ');
|
f.write(' ');
|
||||||
}
|
}
|
||||||
} else { //delete
|
} else { //delete
|
||||||
pos -+ strlen(key);
|
Serial.println("delete");
|
||||||
|
pos -= strlen(key);
|
||||||
oldLen = pos2 - pos;
|
oldLen = pos2 - pos;
|
||||||
f.seek(pos, SeekSet);
|
f.seek(pos);
|
||||||
for (uint32_t i = pos; i < pos2; i++) {
|
for (uint32_t i = pos; i < pos2; i++) {
|
||||||
f.write(' ');
|
f.write(' ');
|
||||||
}
|
}
|
||||||
@ -133,8 +157,10 @@ bool appendObjectToFile(const char* file, const char* key, JsonDocument* content
|
|||||||
//if there is enough empty space in file, insert there instead of appending
|
//if there is enough empty space in file, insert there instead of appending
|
||||||
uint32_t contentLen = measureJson(*content);
|
uint32_t contentLen = measureJson(*content);
|
||||||
Serial.print("clen"); Serial.println(contentLen);
|
Serial.print("clen"); Serial.println(contentLen);
|
||||||
if (bufferedFindSpace(contentLen, f)) {
|
if (bufferedFindSpace(contentLen + strlen(key) + 1, f)) {
|
||||||
Serial.println("space");
|
Serial.println("space");
|
||||||
|
f.write(",");
|
||||||
|
f.print(key);
|
||||||
serializeJson(*content, f);
|
serializeJson(*content, f);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -152,9 +178,9 @@ bool appendObjectToFile(const char* file, const char* key, JsonDocument* content
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Serial.print("pos"); Serial.println(pos);
|
Serial.print("pos"); Serial.println(pos);
|
||||||
if (pos < 3)
|
if (pos > 2)
|
||||||
{
|
{
|
||||||
f.seek(pos -1, SeekSet);
|
f.seek(pos, SeekSet);
|
||||||
f.write(',');
|
f.write(',');
|
||||||
} else { //file content is not valid JSON object
|
} else { //file content is not valid JSON object
|
||||||
f.seek(0, SeekSet);
|
f.seek(0, SeekSet);
|
||||||
|
@ -273,38 +273,40 @@ void serializeSegment(JsonObject& root, WS2812FX::Segment& seg, byte id)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void serializeState(JsonObject root)
|
void serializeState(JsonObject root, bool forPreset)
|
||||||
{
|
{
|
||||||
if (errorFlag) root["error"] = errorFlag;
|
|
||||||
|
|
||||||
root["on"] = (bri > 0);
|
root["on"] = (bri > 0);
|
||||||
root["bri"] = briLast;
|
root["bri"] = briLast;
|
||||||
root["transition"] = transitionDelay/100; //in 100ms
|
root["transition"] = transitionDelay/100; //in 100ms
|
||||||
|
|
||||||
root["ps"] = currentPreset;
|
if (!forPreset) {
|
||||||
root["pss"] = savedPresets;
|
if (errorFlag) root["error"] = errorFlag;
|
||||||
root["pl"] = (presetCyclingEnabled) ? 0: -1;
|
|
||||||
|
|
||||||
usermods.addToJsonState(root);
|
root["ps"] = currentPreset;
|
||||||
|
root["pss"] = savedPresets;
|
||||||
|
root["pl"] = (presetCyclingEnabled) ? 0: -1;
|
||||||
|
|
||||||
//temporary for preset cycle
|
usermods.addToJsonState(root);
|
||||||
JsonObject ccnf = root.createNestedObject("ccnf");
|
|
||||||
ccnf["min"] = presetCycleMin;
|
|
||||||
ccnf["max"] = presetCycleMax;
|
|
||||||
ccnf["time"] = presetCycleTime;
|
|
||||||
|
|
||||||
JsonObject nl = root.createNestedObject("nl");
|
//temporary for preset cycle
|
||||||
nl["on"] = nightlightActive;
|
JsonObject ccnf = root.createNestedObject("ccnf");
|
||||||
nl["dur"] = nightlightDelayMins;
|
ccnf["min"] = presetCycleMin;
|
||||||
nl["fade"] = (nightlightMode > NL_MODE_SET); //deprecated
|
ccnf["max"] = presetCycleMax;
|
||||||
nl["mode"] = nightlightMode;
|
ccnf["time"] = presetCycleTime;
|
||||||
nl["tbri"] = nightlightTargetBri;
|
|
||||||
|
|
||||||
JsonObject udpn = root.createNestedObject("udpn");
|
JsonObject nl = root.createNestedObject("nl");
|
||||||
udpn["send"] = notifyDirect;
|
nl["on"] = nightlightActive;
|
||||||
udpn["recv"] = receiveNotifications;
|
nl["dur"] = nightlightDelayMins;
|
||||||
|
nl["fade"] = (nightlightMode > NL_MODE_SET); //deprecated
|
||||||
|
nl["mode"] = nightlightMode;
|
||||||
|
nl["tbri"] = nightlightTargetBri;
|
||||||
|
|
||||||
root["lor"] = realtimeOverride;
|
JsonObject udpn = root.createNestedObject("udpn");
|
||||||
|
udpn["send"] = notifyDirect;
|
||||||
|
udpn["recv"] = receiveNotifications;
|
||||||
|
|
||||||
|
root["lor"] = realtimeOverride;
|
||||||
|
}
|
||||||
|
|
||||||
root["mainseg"] = strip.getMainSegmentId();
|
root["mainseg"] = strip.getMainSegmentId();
|
||||||
|
|
||||||
|
@ -675,9 +675,9 @@ bool applyPreset(byte index, bool loadBri)
|
|||||||
void savePreset(byte index, bool persist)
|
void savePreset(byte index, bool persist)
|
||||||
{
|
{
|
||||||
StaticJsonDocument<1024> doc;
|
StaticJsonDocument<1024> doc;
|
||||||
serializeState(doc.to<JsonObject>());
|
serializeState(doc.to<JsonObject>(), true);
|
||||||
doc["p"]=50;
|
doc["p"]=50;
|
||||||
serializeJson(doc, Serial);
|
//serializeJson(doc, Serial);
|
||||||
writeObjectToFileUsingId("/presets.json", index, &doc);
|
writeObjectToFileUsingId("/presets.json", index, &doc);
|
||||||
return;
|
return;
|
||||||
if (index > 16) return;
|
if (index > 16) return;
|
||||||
|
Loading…
Reference in New Issue
Block a user