Skip to content

Instantly share code, notes, and snippets.

@ElectricImpSampleCode
Last active September 10, 2018 10:28
Show Gist options
  • Save ElectricImpSampleCode/5f9ffa9da08e823d8a35 to your computer and use it in GitHub Desktop.
Save ElectricImpSampleCode/5f9ffa9da08e823d8a35 to your computer and use it in GitHub Desktop.
This example shows a typical flow for the persistence of data across agent restarts.
// Load the data from persistent storage
settings <- server.load();
// It the load is null, it tells us this is the first time the agent is running
if (settings.len() == 0) {
// Set and save defaults
settings = { "colour" : { "r" : 255,
"g" : 0,
"b" : 0 },
"state" : 0 };
local result = server.save(settings);
if (result != 0) server.error("Could not save settings");
}
// Establish HTTP request handler to persist settings changes
http.onrequest(function(request, response) {
try {
// If there's a state query parameter
if ("state" in request.query) {
// Update settings table and save it
settings.state = req.query.state;
local result = server.save(settings);
if (result != 0) server.error("Could not save settings");
}
response.send(200, "OK");
}
catch (err) {
server.error("Server error: " + err);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment