I need to be able to “cache” about 4 minutes worth of samples before sending it off to my Force.com API. Is there a way to do that reliably in the Agent without fear of losing data? Can I use the “nv” variable or do I need to use the setpermanent?
@ozzieg see below I’ve been using agent storage for a few weeks. Example below of one way please note I’m not programmer
`// Send to cloud store
function putValues(){
local body = http.jsonencode(valueStore);
local req = http.post(“your site URL”, {“Content-Type”: “application/json”}, body);
local res = req.sendsync();
server.log(“put: “+res.statuscode+”:”+res.body);
return res.statuscode;
}
// Storage obj
valueStore <-{
“timeStamp”: 0,
“pointer”: 0,
“values”: [null,null,null,null,null,null,null,null,null,null,null,null]
}
// Deal with Agent re-start
local valueStorePersisted = server.load();
// if value store exists copy it
if(“pointer” in valueStorePersisted ){
valueStore = clone valueStorePersisted;
} else {
server.log(“Load New valueStore”);
// do something on first load
}
// Handel imp values & storage
device.on(“storeEvent”,function(impVal){
valueStore.timeStamp = time();
valueStore.values[valueStore.pointer]=impVal.value;
valueStore.pointer++;
server.save(valueStore); // Persist
// Send when full or time based etc
if(valueStore.pointer == 9){
putValues(); // probably want to test to ensure cloud store go values: e.g 200 OK
valueStore.pointer=0;
server.save(valueStore); // Persist
}
});`