Save blob to server table

Hello!

I am trying to save a squirrel blob from the imp and store it with server.setpermanentvalues(). However the information is not being stored correctly. I believe this is because I have to save each blob index as its own key with corresponding value within the server table. Is there an easy way to do this? I am trying to save a blob of length 500 to the server.

Thanks in advance!!

outBlob <- blob(500); for (local i = 0; i < 500; i++) { // populate the blob with null data outBlob.writen(0x00, 'b'); } function refresh() { imp.wakeup(1.0, refresh); } agent.on("newhttpvalue", function(value) { outBlob = value; server.setpermanentvalues({savedvalues = outBlob}); refresh(); }) imp.configure("Test",[],[]); if ("savedvalues" in server.permanent) { outBlob = server.permanent.savedvalues; server.log("data retrieved"); } else { server.log("table doesn't exist"); } refresh();

See here: http://devwiki.electricimp.com/doku.php?id=serialisablesquirrel&s[]=serialisable

Blobs cannot be used in server.permanent. Would be nice if that wasn’t a restriction…

Does this mean I have to convert to an array first?

You could convert to a string - blob.tostring() would then result in a string that should be serialisable.

Unless it’s already a UTF-8 string, you need to Base64-encode it for it to be serialisable in server.setpermanentvalues:
server.setpermanentvalues({savedvalues = http.base64encode(outBlob)}); [...] outBlob = http.base64decode(server.permanent.savedvalues);

Peter

Thanks Peter! blob.tostring() worked but I need to convert it back!

Peter,

I get the error: the index ‘http’ does not exist

when I use: server.setpermanentvalues({savedvalues = http.base64encode(outBlob)})

Oh whoops, that’s only available in agents, not on the imp itself, sorry. At this point perhaps you are best advised to use an array, as you said to start with. We’ll have a think about how to support this sort of technique better in future.

Peter

Thanks Peter. I tried to send the blob to the agent like:

device.on("savedvalues", function(value) { outBlob = value; server.setpermanentvalues({savedvalues = http.base64encode(outBlob)}); server.log("values stored"); })

But I guess the agent can’t save to the server table (noted in the wiki docs!!) !! I will convert to array…Thanks for your time!

Agent access to server.setpermanentvalues (or, indeed, to “server.save”, which we later realised was a better name for it) is coming soon. Then your much neater solution above will become the best way of doing it!

Peter

Well,

Until that is implemented, I can do this:

// Agent Code //
device.on("convertthisblob", function(value) { device.send("convertedblob", http.base64encode(value)); server.log("values converted"); }) device.on("unconvertthisblob", function(value) { device.send("unconvertedblob", http.base64decode(value)); server.log("values unconverted"); })

// Device Code //
function refresh() { imp.wakeup(1.0, refresh); } agent.on("newblob", function(value) { outBlob = value; agent.send("convertthisblob", outBlob); refresh(); }) agent.on("convertedblob", function(value) { server.setpermanentvalues({savedvalues = value}) }) agent.on("unconvertedblob", function(value) { outBlob = value; }) imp.configure("Test",[],[]); if ("savedvalues" in server.permanent) { agent.send("unconvertthisblob", server.permanent.savedvalues); server.log("values retrieved"); } else { server.log("table doesn't exist"); } refresh();

Can each different impee save its own values with server.setpermanentvalues ? Is there only one table available (meaning all impees will save to same table), or does each impee get its own table?