I just submitted a pull request with some extensions to the gcp library.
Most additions have been copied from existing code and from the mqtt library.
New function to send state changes
C handlers to adapt config and command messages so mJS can easily register a function to receive them
Expanded mJS API to include these.
Here
you can test it by including it:
libs:
- origin: https://github.com/CikaElectronica/gcp2 # modified version of GCP libraries
name: gcp
in place of the original lib
Usage:
load('api_gcp.js');
// send telemetry (events) has no changes
Timer.set(5000 /* milliseconds */, Timer.REPEAT, function() {
if(GCP.isConnected()){
let msg=JSON.stringify({t: Timer.fmt("%F %T", Timer.now())});
Log.print(Log.INFO, 'Send event:' + ((GCP.sendEvent(msg)) ? 'OK' : 'FAIL') + ' msg:' + msg);
}
}, null);
// send state change
GPIO.set_button_handler(button_pin, GPIO.PULL_NONE, GPIO.INT_EDGE_POS, 100, function() {
if(GCP.isConnected()){
let msg = JSON.stringify({free: Sys.free_ram(), total: Sys.total_ram()});
Log.print(Log.INFO, 'Send state:' + ((GCP.sendState(msg)) ? 'OK' : 'FAIL') + ' msg:' + msg);
}
}, null);
// receive config changes
GCP.config(function (confdata, ud) {
if(!confdata)
return;
let obj = JSON.parse(confdata) || {out1: 0};
GPIO.write(led_pin, obj.out1);
Log.print(Log.INFO, 'Received config: ' + confdata + ' output: ' + ((obj.out1)? 'ON':'OFF'));
}, null);
// receive commands
GCP.command(function (cmddata, subfolder, ud) {
if(!cmddata)
return;
let cmd = JSON.parse(cmddata) || {};
Log.print(Log.INFO, 'Received command: ' + cmddata);
if(subfolder)
Log.print(Log.INFO, 'for subfolder:: ' + subfolder);
}, null);
// handle connect/disconnect
Event.addHandler(Event.GCP_CONNECT, function (ev, evdata, ud) {
Log.print(Log.INFO, 'Connected to GCP');
}, null);
Event.addHandler(Event.GCP_CLOSE, function (ev, evdata, ud) {
Log.print(Log.INFO, 'Disconnected from GCP');
}, null);