@@ -162,9 +162,9 @@ int NotecardConnectionHandler::setWiFiCredentials (const String & ssid_, const S
162162 int result;
163163
164164 // Validate the connection state is not in an initialization state
165- if (check () == NetworkConnectionState::INIT)
165+ if (NetworkConnectionState::INIT == _current_net_connection_state )
166166 {
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 ." ));
168168 result = NotecardCommunicationError::NOTECARD_ERROR_GENERIC;
169169 } else if (J *req = _notecard.newRequest (" card.wifi" )) {
170170 JAddStringToObject (req, " ssid" , ssid_.c_str ());
@@ -195,10 +195,11 @@ int NotecardConnectionHandler::setWiFiCredentials (const String & ssid_, const S
195195
196196const String & NotecardConnectionHandler::syncArduinoDeviceId (const String & device_id_)
197197{
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))
200201 {
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 ." ));
202203 return device_id_;
203204 }
204205
@@ -249,23 +250,29 @@ int NotecardConnectionHandler::syncSecretDeviceKey (const String & secret_device
249250{
250251 int result;
251252
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))
254256 {
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 ." ));
256258 result = NotecardCommunicationError::NOTECARD_ERROR_GENERIC;
257259 } else if (J *req = _notecard.newRequest (" var.set" )) {
258260 JAddStringToObject (req, " file" , NOTEFILE_SECURE_DATABASE);
259261 JAddStringToObject (req, " name" , " secret_device_key" );
260262 if (secret_device_key_.length () > 0 ) {
261263 JAddStringToObject (req, " text" , secret_device_key_.c_str ());
262264 }
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+ }
265269 if (J *rsp = _notecard.requestAndResponse (req)) {
266270 // Check the response for errors
267271 if (NoteResponseError (rsp)) {
268272 const char *err = JGetString (rsp, " err" );
273+ if (NoteErrorContains (err, " {hub-not-connected}" )) {
274+ _current_net_connection_state = NetworkConnectionState::DISCONNECTED;
275+ }
269276 Debug.print (DBG_ERROR, F (" %s" ), err);
270277 Debug.print (DBG_ERROR, F (" Failed to sync Secret Device Key." ));
271278 result = NotecardCommunicationError::NOTECARD_ERROR_GENERIC;
@@ -372,13 +379,19 @@ int NotecardConnectionHandler::write(const uint8_t * buf_, size_t size_)
372379{
373380 int result;
374381
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" )) {
376389 JAddStringToObject (req, " file" , NOTEFILE_SECURE_OUTBOUND);
377390 if (buf_) {
378391 JAddBinaryToObject (req, " payload" , buf_, size_);
379392 }
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) ) {
382395 JAddBoolToObject (req, " live" , true );
383396 JAddBoolToObject (req, " sync" , true );
384397 }
@@ -387,6 +400,9 @@ int NotecardConnectionHandler::write(const uint8_t * buf_, size_t size_)
387400 J * rsp = _notecard.requestAndResponse (req);
388401 if (NoteResponseError (rsp)) {
389402 const char *err = JGetString (rsp, " err" );
403+ if (NoteErrorContains (err, " {hub-not-connected}" )) {
404+ _current_net_connection_state = NetworkConnectionState::DISCONNECTED;
405+ }
390406 Debug.print (DBG_ERROR, F (" %s\n " ), err);
391407 result = NotecardCommunicationError::NOTECARD_ERROR_GENERIC;
392408 } else {
@@ -786,11 +802,11 @@ bool NotecardConnectionHandler::configureConnection (bool connect_) const
786802 // Configure the connection mode based on the `connect_` parameter
787803 if (connect_) {
788804 JAddStringToObject (req, " mode" , " continuous" );
789- JAddIntToObject (req, " inbound" , 15 ); // Unnecessary fail safe value
805+ JAddIntToObject (req, " inbound" , 15 ); // Fail- safe (theoretically unnecessary)
790806 JAddBoolToObject (req, " sync" , true );
791807 } else {
792808 JAddStringToObject (req, " mode" , " periodic" );
793- JAddIntToObject (req, " inbound" , 1440 ); // TODO: Revisit this value
809+ JAddIntToObject (req, " inbound" , 1440 ); // Once daily
794810 JAddIntToObject (req, " outbound" , -1 );
795811 JAddStringToObject (req, " vinbound" , " -" );
796812 JAddStringToObject (req, " voutbound" , " -" );
@@ -939,16 +955,23 @@ int NotecardConnectionHandler::initiateNotehubSync (void) const
939955int NotecardConnectionHandler::notehubLogging (bool enable_) const
940956{
941957 int result;
958+ Debug.print (DBG_INFO, F (" %sabling Notehub logging..." ), (enable_ ? " En" : " Dis" ));
942959
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" )) {
944967 JAddStringToObject (req, " file" , NOTEFILE_SECURE_OUTBOUND);
945968 if (enable_) {
946969 JAddBinaryToObject (req, " payload" , " 1" , sizeof (" 1" ));
947970 } else {
948971 JAddBinaryToObject (req, " payload" , " 0" , sizeof (" 0" ));
949972 }
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) ) {
952975 JAddBoolToObject (req, " live" , true );
953976 JAddBoolToObject (req, " sync" , true );
954977 }
@@ -957,6 +980,9 @@ int NotecardConnectionHandler::notehubLogging (bool enable_) const
957980 J * rsp = _notecard.requestAndResponse (req);
958981 if (NoteResponseError (rsp)) {
959982 const char *err = JGetString (rsp, " err" );
983+ if (NoteErrorContains (err, " {hub-not-connected}" )) {
984+ _current_net_connection_state = NetworkConnectionState::DISCONNECTED;
985+ }
960986 Debug.print (DBG_ERROR, F (" %s\n " ), err);
961987 result = NotecardCommunicationError::NOTECARD_ERROR_GENERIC;
962988 } else {
0 commit comments