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 9762afa commit ad00e39
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
7 changes: 7 additions & 0 deletions mjs_fs/api_azure.js
Expand Up @@ -4,7 +4,14 @@ let Azure = {
EVENT_GRP: Event.baseNumber('AZR'),
_c2dd: ffi('void *mgos_azure_get_c2dd(void)')(),

// ## **`Azure.isConnected()`**
// Return value: true if Azure connection is up, false otherwise.
isConnected: ffi('bool mgos_azure_is_connected()'),

getC2DArg: function(evdata) { return s2o(evdata, Azure._c2dd) },

// ## **`Azure.sendD2CMsg(props, body)`**
// Send a Device to Cloud message. `props`, if specified, must be URL-encoded.
sendD2CMsg: ffi('bool mgos_azure_send_d2c_msgp(struct mg_str *, struct mg_str *)'),
};

Expand Down
4 changes: 4 additions & 0 deletions src/mgos_azure.c
Expand Up @@ -65,6 +65,7 @@ static void ev_cb(void *arg) {
static void azure_mqtt_ev(struct mg_connection *nc, int ev, void *ev_data,
void *user_data) {
struct mgos_azure_ctx *ctx = (struct mgos_azure_ctx *) user_data;
struct mgos_cloud_arg arg = {.type = MGOS_CLOUD_AZURE};
switch (ev) {
case MG_EV_MQTT_CONNACK: {
struct mg_mqtt_message *msg = (struct mg_mqtt_message *) ev_data;
Expand All @@ -73,6 +74,7 @@ static void azure_mqtt_ev(struct mg_connection *nc, int ev, void *ev_data,
/* TODO(rojer): Should wait for CM and DM SUBACK, if enabled. */
ctx->is_connected = true;
mgos_invoke_cb(ev_cb, (void *) MGOS_AZURE_EV_CONNECT, false);
mgos_event_trigger(MGOS_EVENT_CLOUD_CONNECTED, &arg);
break;
default:
LOG(LL_ERROR, ("Azure MQTT connection failed (%d). "
Expand All @@ -85,6 +87,8 @@ static void azure_mqtt_ev(struct mg_connection *nc, int ev, void *ev_data,
if (ctx->is_connected) {
ctx->is_connected = false;
mgos_invoke_cb(ev_cb, (void *) MGOS_AZURE_EV_CLOSE, false);
struct mgos_cloud_arg arg = {.type = MGOS_CLOUD_AZURE};
mgos_event_trigger(MGOS_EVENT_CLOUD_DISCONNECTED, &arg);
}
break;
}
Expand Down

0 comments on commit ad00e39

Please sign in to comment.