@@ -72,7 +72,11 @@ ArduinoIoTCloudClass::ArduinoIoTCloudClass() :
72
72
_dataTopicOut(" " ),
73
73
_dataTopicIn(" " ),
74
74
_otaTopic(" " ),
75
- _lastSyncRequestTickTime(0 ) {
75
+ _lastSyncRequestTickTime(0 ),
76
+ _on_sync_event_callback(NULL ),
77
+ _on_connect_event_callback(NULL ),
78
+ _on_disconnect_event_callback(NULL ) {
79
+
76
80
}
77
81
78
82
ArduinoIoTCloudClass::~ArduinoIoTCloudClass () {
@@ -237,21 +241,25 @@ void ArduinoIoTCloudClass::update(int const reconnectionMaxRetries, int const re
237
241
_mqttClient->poll ();
238
242
239
243
switch (_syncStatus) {
240
- case ArduinoIoTSynchronizationStatus::SYNC_STATUS_SYNCHRONIZED:
244
+ case ArduinoIoTSynchronizationStatus::SYNC_STATUS_SYNCHRONIZED: {
241
245
sendPropertiesToCloud ();
242
- break ;
243
- case ArduinoIoTSynchronizationStatus::SYNC_STATUS_WAIT_FOR_CLOUD_VALUES:
246
+ }
247
+ break ;
248
+ case ArduinoIoTSynchronizationStatus::SYNC_STATUS_WAIT_FOR_CLOUD_VALUES: {
244
249
if (millis () - _lastSyncRequestTickTime > TIMEOUT_FOR_LASTVALUES_SYNC) {
245
250
requestLastValue ();
246
251
_lastSyncRequestTickTime = millis ();
247
252
}
248
- break ;
249
- case ArduinoIoTSynchronizationStatus::SYNC_STATUS_VALUES_PROCESSED:
253
+ }
254
+ break ;
255
+ case ArduinoIoTSynchronizationStatus::SYNC_STATUS_VALUES_PROCESSED: {
250
256
if (onSyncCompleteCallback != NULL ) {
251
257
(*onSyncCompleteCallback)();
252
258
}
259
+ execCloudConnectionEventCallback (_on_sync_event_callback, 0 /* callback_arg */ );
253
260
_syncStatus = ArduinoIoTSynchronizationStatus::SYNC_STATUS_SYNCHRONIZED;
254
- break ;
261
+ }
262
+ break ;
255
263
}
256
264
}
257
265
@@ -385,6 +393,7 @@ void ArduinoIoTCloudClass::connectionCheck() {
385
393
debugMessageNoTimestamp (DebugLevel::Verbose, " ." );
386
394
if (!_mqttClient->connected ()) {
387
395
setIoTConnectionState (ArduinoIoTConnectionStatus::DISCONNECTED);
396
+ execCloudConnectionEventCallback (_on_disconnect_event_callback, 0 /* callback_arg - e.g. could be error code casted to void * */ );
388
397
}
389
398
}
390
399
break ;
@@ -397,6 +406,7 @@ void ArduinoIoTCloudClass::connectionCheck() {
397
406
debugMessage (DebugLevel::Info, " ArduinoCloud.reconnect(): %d" , ret_code_reconnect);
398
407
if (ret_code_reconnect == CONNECT_SUCCESS) {
399
408
setIoTConnectionState (ArduinoIoTConnectionStatus::CONNECTED);
409
+ execCloudConnectionEventCallback (_on_connect_event_callback, 0 /* callback_arg */ );
400
410
CloudSerial.begin (9600 );
401
411
CloudSerial.println (" Hello from Cloud Serial!" );
402
412
}
@@ -407,6 +417,7 @@ void ArduinoIoTCloudClass::connectionCheck() {
407
417
debugMessage (DebugLevel::Verbose, " ArduinoCloud.connect(): %d" , ret_code_connect);
408
418
if (ret_code_connect == CONNECT_SUCCESS) {
409
419
setIoTConnectionState (ArduinoIoTConnectionStatus::CONNECTED);
420
+ execCloudConnectionEventCallback (_on_connect_event_callback, 0 /* callback_arg */ );
410
421
CloudSerial.begin (9600 );
411
422
CloudSerial.println (" Hello from Cloud Serial!" );
412
423
} else if (ret_code_connect == CONNECT_FAILURE_SUBSCRIBE) {
@@ -435,4 +446,18 @@ void ArduinoIoTCloudClass::printDebugInfo() {
435
446
debugMessage (DebugLevel::Info, " MQTT Broker: %s:%d" , _brokerAddress.c_str (), _brokerPort);
436
447
}
437
448
449
+ void ArduinoIoTCloudClass::addCallback (ArduinoIoTCloudConnectionEvent const event, OnCloudConnectionEventCallback callback) {
450
+ switch (event) {
451
+ case ArduinoIoTCloudConnectionEvent::SYNC: _on_sync_event_callback = callback; break ;
452
+ case ArduinoIoTCloudConnectionEvent::CONNECT: _on_connect_event_callback = callback; break ;
453
+ case ArduinoIoTCloudConnectionEvent::DISCONNECT: _on_disconnect_event_callback = callback; break ;
454
+ }
455
+ }
456
+
457
+ void ArduinoIoTCloudClass::execCloudConnectionEventCallback (OnCloudConnectionEventCallback & callback, void * callback_arg) {
458
+ if (callback) {
459
+ (*callback)(callback_arg);
460
+ }
461
+ }
462
+
438
463
ArduinoIoTCloudClass ArduinoCloud;
0 commit comments