Dynamic types, pattern subscribing. and more.
Apart from improving Message Queuing when the device is not connected with Grandeur this release introduces the following major changes:
1. Dynamic Type Var
We added a dynamic type Var
. You can literally do the following in your arduino sketch now:
Var x = true;
Serial.printf("I am a bool: %d.\n", x);
x = 75;
Serial.printf("I am an int: %d.\n", x);
x = "I am a string";
Serial.println(x);
You can use Var
to store JSON objects as well:
Var json;
json["current"] = 10;
json["voltage"]["vpp"] = 440;
json["voltage"]["vpeak"] = 220;
Serial.println(JSON.stringify(json));
/** Prints:
{
"current": 10,
"voltage": {
"vpp": 440.
"vpeak": 220
}
}
*/
2. Update in callback for device.data().on()
function
device.data().on()
function now accepts callback function of void(*)(Var data, const char* path)
type not void(*)(JSONObject)
. In place of Var
, you can use any type (int
, const char*
, String
, bool
, etc). device.data().get()
and device.data().set()
still accept callback function of void(*)(JSONObject)
type.
void handleUpdate(int voltage, const char* path) {
Serial.printf("%s got updated. New voltage is: %d.\n", path, voltage);
}
device.data().on("voltage", handleUpdate);
3. Pattern Subscribing
You can now pattern subscribe device variables. For example, for following device data:
{ "voltage": { "vpp": 440, "vpeak": 220 }, "current": 10 }
You can subscribe to both vpp
and vpeak
by subscribing to the parent, voltage
, that is, by doing:
device.data().on("voltage", /**UpdateHandler**/)