SD Card Configuration File
Demonstrates storing a JSON configuration file with setpoints etc to a sd card.
let sp = {
pumpMode:0,
resFull:99,
resCall:80,
resLow:50,
minFlow:150,
maxFlow:320,
pumpHours:244
};
let cfg = {
changed:false,
lastSaved:now(),
load: function(){
let res = sd.read("setpoints.json");
if(!res.error){
sp = JSON.parse(res.data);
} else {
print("Failed to load configuration: ", string(res));
}
},
save:function(){
if(this.changed || this.lastSaved.sAgo() > 86400){ // save the setpoints immediately if changed otherwise daily to write the pump hours
let res = sd.write("setpoints.json",string(sp));
// Save a backup at least once a month in a seperate year month labeled file
let date = datetime();
let dateString = '/LOG/sp-' + string(date.year) + '-' + string(date.month) + '.json';
sd.write(dateString,string(sp));
if(res){
log("Failed to save setpoints to SD Card",alm.warn);
return false;
}
return true;
}
}
};
cfg.load(); // Load the configuration initially
while(run()){
waitMS(1000);
cfg.save();
}