Hi,
I use some shared classes to send/receive data between my agent and my device codes. Basically, those classes are defined in 1 single file “models_common.nut” imported in “agent.nut” and “device.nut” so I am 100% sure both have the same version of the models.
I have also written 2 utility functions to serialize / unserialize this kink of data. Everything is based on the assumption that a foreach on the class entries will get the entries in the exact same order, each time and on both side of the code.
This currently works, but I cannot see any specification that says that this will always work in the future. Any opinion?
Below a sample of my code.
Thanks!
// file "models_common.nut"
class Measure {
field1 = null;
field2 = null;
field3 = null;
...
}
//
// Generic serialize / unserialize
// Here, I had a look at the Electric Imp Serializer module but I want to make it simpler
//
function serialize(obj) {
local data = array(64); // make a guess on the size
local i = 0;
foreach (k, v in obj.getclass()) {
if (typeof v != "function") {
data[i++] = obj[k];
}
}
data.resize(i);
return data;
}
function unserialize(obj, data) {
local i = 0;
foreach (k, v in obj.getclass()) {
if (typeof v != "function") {
obj[k] = data[i++];
}
}
return obj;
}
// file "device.nut"
local measure = Measure()
measure.field1 = 123
...
agent.send("measure", serialize(measure))
// file "agent.nut"
device.on("measure", function(data) {
local measure = unserialize(Measure(), data);
...
}