Skip to content

Commit

Permalink
Add config merge
Browse files Browse the repository at this point in the history
  • Loading branch information
lbussy committed Apr 1, 2020
1 parent da97ffc commit e42e7ba
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
28 changes: 23 additions & 5 deletions src/jsonconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,28 @@ bool printConfig(const Config &config)
return serializeJsonPretty(doc, Serial) > 0;
}

bool mergeConfig(JsonVariantConst src) {
// Serialize configuration
DynamicJsonDocument doc(capacitySerial);

// Create an object at the root
JsonObject root = doc.to<JsonObject>();

// Fill the object
config.save(root);

// Merge in the configuration
if (merge(root, src)) {
// Move new configuration to Config object and save
config.load(root);
if (saveConfig()) {
return true;
}
}

return false;
}

bool merge(JsonVariant dst, JsonVariantConst src)
{
if (src.is<JsonObject>())
Expand Down Expand Up @@ -348,11 +370,7 @@ void KeyTarget::load(JsonObjectConst obj)
}
}

void Config::load(JsonObjectConst obj) {
load(obj, false);
}

void Config::load(JsonObjectConst obj, bool fromweb)
void Config::load(JsonObjectConst obj)
{
// Load all config objects
//
Expand Down
6 changes: 3 additions & 3 deletions src/jsonconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ struct Config
bool didupdate;

void load(JsonObjectConst);
void load(JsonObjectConst, bool);
void save(JsonObject) const;
};

Expand All @@ -106,9 +105,10 @@ bool saveConfig();
bool loadFile();
bool saveFile();
bool printFile();
bool serializeConfig(Print &dst);
bool deserializeConfig(Stream &src);
bool serializeConfig(Print &);
bool deserializeConfig(Stream &);
bool merge(JsonVariant, JsonVariantConst);
bool mergeConfig(JsonVariantConst);

#ifdef JC_DEBUG
#include <ArduinoLog.h>
Expand Down

0 comments on commit e42e7ba

Please sign in to comment.