Skip to content

Commit

Permalink
Cloud connection status logic cleanup
Browse files Browse the repository at this point in the history
 * Add mgos_xxx_is_connected to all cloud libs

 * Raise MGOS_EVENT_CLOUD_{CONNECTED,DISCONNECTED} with appropriate cloud type.

 * Add a button handler to demo-js that sends an event to the cloud, using appropriate method depending on cloud type.

CL: Cloud connection status logic cleanup

PUBLISHED_FROM=c466921b09341c0456229f45dada0914c5c1df8f
  • Loading branch information
Deomid Ryabkov authored and cesantabot committed Nov 6, 2018
1 parent 3165f1b commit 69f63f7
Showing 1 changed file with 56 additions and 4 deletions.
60 changes: 56 additions & 4 deletions fs/init.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
load('api_aws.js');
load('api_azure.js');
load('api_config.js');
load('api_dash.js');
load('api_events.js');
load('api_gcp.js');
load('api_gpio.js');
load('api_mqtt.js');
load('api_shadow.js');
load('api_timer.js');
load('api_sys.js');
load('api_watson.js');

let led = Cfg.get('board.led1.pin'); // Built-in LED GPIO number
let onhi = Cfg.get('board.led1.active_high'); // LED on when high?
let state = {on: false, uptime: 0}; // Device state
let online = false; // Connected to the cloud?
let btn = Cfg.get('board.btn1.pin'); // Built-in button GPIO
let led = Cfg.get('board.led1.pin'); // Built-in LED GPIO number
let onhi = Cfg.get('board.led1.active_high'); // LED on when high?
let state = {on: false, btnCount: 0, uptime: 0}; // Device state
let online = false; // Connected to the cloud?

let setLED = function(on) {
let level = onhi ? on : !on;
Expand Down Expand Up @@ -49,6 +56,51 @@ Shadow.addHandler(function(event, obj) {
}
});

if (btn >= 0) {
let btnCount = 0;
let btnPull, btnEdge;
if (Cfg.get('board.btn1.pull_up') ? GPIO.PULL_UP : GPIO.PULL_DOWN) {
btnPull = GPIO.PULL_UP;
btnEdge = GPIO.INT_EDGE_NEG;
} else {
btnPull = GPIO.PULL_DOWN;
btnEdge = GPIO.INT_EDGE_POS;
}
GPIO.set_button_handler(btn, btnPull, btnEdge, 20, function() {
state.btnCount++;
let message = JSON.stringify(state);
let sendMQTT = true;
if (Azure.isConnected()) {
print('== Sending Azure D2C message:', message);
Azure.sendD2CMsg("", message);
sendMQTT = false;
}
if (GCP.isConnected()) {
print('== Sending GCP event:', message);
GCP.sendEvent(message);
sendMQTT = false;
}
if (Watson.isConnected()) {
print('== Sending Watson event:', message);
Watson.sendEventJSON("ev", {d: state});
sendMQTT = false;
}
if (Dash.isConnected()) {
print('== Click!');
// TODO: Maybe do something else?
sendMQTT = false;
}
// AWS is handled as plain MQTT since it allows arbitrary topics.
if (AWS.isConnected() || (MQTT.isConnected() && sendMQTT)) {
let topic = 'devices/' + Cfg.get('device.id') + '/events';
print('== Publishing to ' + topic + ':', message);
MQTT.pub(topic, message, 0 /* QoS */);
} else if (sendMQTT) {
print('== Not connected!');
}
}, null);
}

Event.on(Event.CLOUD_CONNECTED, function() {
online = true;
}, null);
Expand Down

0 comments on commit 69f63f7

Please sign in to comment.