@@ -162,9 +162,9 @@ int NotecardConnectionHandler::setWiFiCredentials (const String & ssid_, const S
162
162
int result;
163
163
164
164
// Validate the connection state is not in an initialization state
165
- if (check () == NetworkConnectionState::INIT)
165
+ if (NetworkConnectionState::INIT == _current_net_connection_state )
166
166
{
167
- Debug.print (DBG_ERROR, F (" Failed to set WiFi credentials. Connection has not been initialized ." ));
167
+ Debug.print (DBG_ERROR, F (" Unable to set WiFi credentials. Connection to Notecard uninitialized ." ));
168
168
result = NotecardCommunicationError::NOTECARD_ERROR_GENERIC;
169
169
} else if (J *req = _notecard.newRequest (" card.wifi" )) {
170
170
JAddStringToObject (req, " ssid" , ssid_.c_str ());
@@ -195,10 +195,11 @@ int NotecardConnectionHandler::setWiFiCredentials (const String & ssid_, const S
195
195
196
196
const String & NotecardConnectionHandler::syncArduinoDeviceId (const String & device_id_)
197
197
{
198
- // Validate the connection state is not in an initialization state
199
- if (check () == NetworkConnectionState::INIT)
198
+ // Validate the connection state is not uninitialized or in error state
199
+ if ((NetworkConnectionState::INIT == _current_net_connection_state)
200
+ || (NetworkConnectionState::ERROR == _current_net_connection_state))
200
201
{
201
- Debug.print (DBG_ERROR, F (" Failed to sync Arduino Device ID. Connection has not been initialized ." ));
202
+ Debug.print (DBG_ERROR, F (" Unable to sync Arduino Device ID. Connection to Notecard uninitialized or in error state ." ));
202
203
return device_id_;
203
204
}
204
205
@@ -249,23 +250,29 @@ int NotecardConnectionHandler::syncSecretDeviceKey (const String & secret_device
249
250
{
250
251
int result;
251
252
252
- // Validate the connection state is not in an initialization state
253
- if (check () == NetworkConnectionState::INIT)
253
+ // Validate the connection state is not uninitialized or in error state
254
+ if ((NetworkConnectionState::INIT == _current_net_connection_state)
255
+ || (NetworkConnectionState::ERROR == _current_net_connection_state))
254
256
{
255
- Debug.print (DBG_ERROR, F (" Failed to sync Secret Device Key. Connection has not been initialized ." ));
257
+ Debug.print (DBG_ERROR, F (" Unable to sync Secret Device Key. Connection to Notecard uninitialized or in error state ." ));
256
258
result = NotecardCommunicationError::NOTECARD_ERROR_GENERIC;
257
259
} else if (J *req = _notecard.newRequest (" var.set" )) {
258
260
JAddStringToObject (req, " file" , NOTEFILE_SECURE_DATABASE);
259
261
JAddStringToObject (req, " name" , " secret_device_key" );
260
262
if (secret_device_key_.length () > 0 ) {
261
263
JAddStringToObject (req, " text" , secret_device_key_.c_str ());
262
264
}
263
- JAddBoolToObject (req, " live" , true );
264
- JAddBoolToObject (req, " sync" , true );
265
+ if (NetworkConnectionState::CONNECTED == _current_net_connection_state) {
266
+ JAddBoolToObject (req, " live" , true );
267
+ JAddBoolToObject (req, " sync" , true );
268
+ }
265
269
if (J *rsp = _notecard.requestAndResponse (req)) {
266
270
// Check the response for errors
267
271
if (NoteResponseError (rsp)) {
268
272
const char *err = JGetString (rsp, " err" );
273
+ if (NoteErrorContains (err, " {hub-not-connected}" )) {
274
+ _current_net_connection_state = NetworkConnectionState::DISCONNECTED;
275
+ }
269
276
Debug.print (DBG_ERROR, F (" %s" ), err);
270
277
Debug.print (DBG_ERROR, F (" Failed to sync Secret Device Key." ));
271
278
result = NotecardCommunicationError::NOTECARD_ERROR_GENERIC;
@@ -372,13 +379,19 @@ int NotecardConnectionHandler::write(const uint8_t * buf_, size_t size_)
372
379
{
373
380
int result;
374
381
375
- if (J * req = _notecard.newRequest (" note.add" )) {
382
+ // Validate the connection state is not uninitialized or in error state
383
+ if ((NetworkConnectionState::INIT == _current_net_connection_state)
384
+ || (NetworkConnectionState::ERROR == _current_net_connection_state))
385
+ {
386
+ Debug.print (DBG_ERROR, F (" Unable to write message. Connection to Notecard uninitialized or in error state." ));
387
+ result = NotecardCommunicationError::NOTECARD_ERROR_GENERIC;
388
+ } else if (J * req = _notecard.newRequest (" note.add" )) {
376
389
JAddStringToObject (req, " file" , NOTEFILE_SECURE_OUTBOUND);
377
390
if (buf_) {
378
391
JAddBinaryToObject (req, " payload" , buf_, size_);
379
392
}
380
- // Queue the Note when `_keep_alive` is disabled
381
- if (_keep_alive) {
393
+ // Queue the Note when `_keep_alive` is disabled or not connected to Notehub
394
+ if (_keep_alive && (NetworkConnectionState::CONNECTED == _current_net_connection_state) ) {
382
395
JAddBoolToObject (req, " live" , true );
383
396
JAddBoolToObject (req, " sync" , true );
384
397
}
@@ -387,6 +400,9 @@ int NotecardConnectionHandler::write(const uint8_t * buf_, size_t size_)
387
400
J * rsp = _notecard.requestAndResponse (req);
388
401
if (NoteResponseError (rsp)) {
389
402
const char *err = JGetString (rsp, " err" );
403
+ if (NoteErrorContains (err, " {hub-not-connected}" )) {
404
+ _current_net_connection_state = NetworkConnectionState::DISCONNECTED;
405
+ }
390
406
Debug.print (DBG_ERROR, F (" %s\n " ), err);
391
407
result = NotecardCommunicationError::NOTECARD_ERROR_GENERIC;
392
408
} else {
@@ -786,11 +802,11 @@ bool NotecardConnectionHandler::configureConnection (bool connect_) const
786
802
// Configure the connection mode based on the `connect_` parameter
787
803
if (connect_) {
788
804
JAddStringToObject (req, " mode" , " continuous" );
789
- JAddIntToObject (req, " inbound" , 15 ); // Unnecessary fail safe value
805
+ JAddIntToObject (req, " inbound" , 15 ); // Fail- safe (theoretically unnecessary)
790
806
JAddBoolToObject (req, " sync" , true );
791
807
} else {
792
808
JAddStringToObject (req, " mode" , " periodic" );
793
- JAddIntToObject (req, " inbound" , 1440 ); // TODO: Revisit this value
809
+ JAddIntToObject (req, " inbound" , 1440 ); // Once daily
794
810
JAddIntToObject (req, " outbound" , -1 );
795
811
JAddStringToObject (req, " vinbound" , " -" );
796
812
JAddStringToObject (req, " voutbound" , " -" );
@@ -939,16 +955,23 @@ int NotecardConnectionHandler::initiateNotehubSync (void) const
939
955
int NotecardConnectionHandler::notehubLogging (bool enable_) const
940
956
{
941
957
int result;
958
+ Debug.print (DBG_INFO, F (" %sabling Notehub logging..." ), (enable_ ? " En" : " Dis" ));
942
959
943
- if (J * req = _notecard.newRequest (" note.add" )) {
960
+ // Validate the connection state is not uninitialized or in error state
961
+ if ((NetworkConnectionState::INIT == _current_net_connection_state)
962
+ || (NetworkConnectionState::ERROR == _current_net_connection_state))
963
+ {
964
+ Debug.print (DBG_ERROR, F (" Unable to %sable Notehub logging. Connection to Notecard uninitialized or in error state." ), (enable_ ? " en" : " dis" ));
965
+ result = NotecardCommunicationError::NOTECARD_ERROR_GENERIC;
966
+ } else if (J * req = _notecard.newRequest (" note.add" )) {
944
967
JAddStringToObject (req, " file" , NOTEFILE_SECURE_OUTBOUND);
945
968
if (enable_) {
946
969
JAddBinaryToObject (req, " payload" , " 1" , sizeof (" 1" ));
947
970
} else {
948
971
JAddBinaryToObject (req, " payload" , " 0" , sizeof (" 0" ));
949
972
}
950
- // Queue the Note when `_keep_alive` is disabled
951
- if (_keep_alive) {
973
+ // Queue the Note when `_keep_alive` is disabled or not connected to Notehub
974
+ if (_keep_alive && (NetworkConnectionState::CONNECTED == _current_net_connection_state) ) {
952
975
JAddBoolToObject (req, " live" , true );
953
976
JAddBoolToObject (req, " sync" , true );
954
977
}
@@ -957,6 +980,9 @@ int NotecardConnectionHandler::notehubLogging (bool enable_) const
957
980
J * rsp = _notecard.requestAndResponse (req);
958
981
if (NoteResponseError (rsp)) {
959
982
const char *err = JGetString (rsp, " err" );
983
+ if (NoteErrorContains (err, " {hub-not-connected}" )) {
984
+ _current_net_connection_state = NetworkConnectionState::DISCONNECTED;
985
+ }
960
986
Debug.print (DBG_ERROR, F (" %s\n " ), err);
961
987
result = NotecardCommunicationError::NOTECARD_ERROR_GENERIC;
962
988
} else {
0 commit comments