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