Skip to content

Commit

Permalink
add state listener
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarolb committed Aug 31, 2021
1 parent 6b17ee0 commit ab2a571
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions src/ThingerClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,23 @@ emyPxgcYxn/eR44/KJ4EBs+lVDR3veyJm+kXQ99b21/+jh5Xos1AnX5iItreGCc=
class ThingerClient : public thinger::thinger {

public:

enum THINGER_STATE{
NETWORK_CONNECTING,
NETWORK_CONNECTED,
NETWORK_CONNECT_ERROR,
SOCKET_CONNECTING,
SOCKET_CONNECTED,
SOCKET_CONNECTION_ERROR,
SOCKET_DISCONNECTED,
SOCKET_TIMEOUT,
SOCKET_ERROR,
THINGER_AUTHENTICATING,
THINGER_AUTHENTICATED,
THINGER_AUTH_FAILED,
THINGER_STOP_REQUEST
};

ThingerClient(Client& client, const char* user, const char* device, const char* device_credential) :
client_(client),
username_(user),
Expand Down Expand Up @@ -352,22 +369,6 @@ class ThingerClient : public thinger::thinger {
for (;;) {}
}

enum THINGER_STATE{
NETWORK_CONNECTING,
NETWORK_CONNECTED,
NETWORK_CONNECT_ERROR,
SOCKET_CONNECTING,
SOCKET_CONNECTED,
SOCKET_CONNECTION_ERROR,
SOCKET_DISCONNECTED,
SOCKET_TIMEOUT,
SOCKET_ERROR,
THINGER_AUTHENTICATING,
THINGER_AUTHENTICATED,
THINGER_AUTH_FAILED,
THINGER_STOP_REQUEST
};

virtual void thinger_state_listener(THINGER_STATE state){
#ifdef _DEBUG_
switch(state){
Expand Down Expand Up @@ -421,6 +422,7 @@ class ThingerClient : public thinger::thinger {
break;
}
#endif
if(state_listener_) state_listener_(state);
}

bool handle_connection()
Expand Down Expand Up @@ -527,6 +529,10 @@ class ThingerClient : public thinger::thinger {
return root_ca_;
}

void set_state_listener(std::function<void(THINGER_STATE)> state_listener){
state_listener_ = state_listener;
}

Client& get_client(){
return client_;
}
Expand All @@ -540,6 +546,7 @@ class ThingerClient : public thinger::thinger {
const char* host_;
const char* root_ca_;
bool reboot_ = false;
std::function<void(THINGER_STATE)> state_listener_;
#ifndef THINGER_DISABLE_OUTPUT_BUFFER
uint8_t * out_buffer_;
size_t out_size_;
Expand Down

0 comments on commit ab2a571

Please sign in to comment.