Skip to content

Commit

Permalink
add support for stream listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarolb committed Sep 1, 2021
1 parent 707ff37 commit e953660
Showing 1 changed file with 65 additions and 2 deletions.
67 changes: 65 additions & 2 deletions src/thinger/thinger_resource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,18 @@
#include "thinger_message.hpp"

#ifdef __has_include
# if __has_include(<functional>) || defined(ESP8266)
# if __has_include(<functional>)
# undef min
# undef max
# include <functional>
# define THINGER_USE_FUNCTIONAL
# endif
#endif

#ifndef THINGER_DISABLE_STREAM_LISTENER
#define THINGER_ENABLE_STREAM_LISTENER
#endif

namespace thinger{


Expand All @@ -48,7 +52,8 @@ class thinger_resource {
run = 1,
pson_in = 2,
pson_out = 3,
pson_in_pson_out = 4
pson_in_pson_out = 4,
pson_stream = 5
};

enum access_type{
Expand Down Expand Up @@ -84,6 +89,7 @@ class thinger_resource {

#endif


// used for defining the resource
io_type io_type_;
access_type access_type_;
Expand All @@ -96,6 +102,14 @@ class thinger_resource {
unsigned long streaming_freq_;
unsigned long last_streaming_;

#ifdef THINGER_ENABLE_STREAM_LISTENER
#ifdef THINGER_USE_FUNCTIONAL
std::function<void(uint16_t, unsigned long, bool enabled)> stream_listener_;
#else
void (*stream_listener_)(uint16_t, unsigned long, bool enabled) = nullptr;
#endif
#endif

// TODO change to pointer so it is not using more than a pointer size if not used?
thinger_map<thinger_resource> sub_resources_;

Expand All @@ -106,6 +120,13 @@ class thinger_resource {
}else if(streaming_freq_>0 && streaming_freq==0){
get_streaming_counter()--;
}

#ifdef THINGER_ENABLE_STREAM_LISTENER
if(stream_listener_){
stream_listener_(stream_id, streaming_freq, true);
}
#endif

streaming_freq_ = streaming_freq;
last_streaming_ = 0;
}
Expand All @@ -115,6 +136,11 @@ class thinger_resource {
{}

void disable_streaming(){
#ifdef THINGER_ENABLE_STREAM_LISTENER
if(stream_listener_){
stream_listener_(stream_id_, streaming_freq_, false);
}
#endif
stream_id_ = 0;
if(streaming_freq_>0){
get_streaming_counter()--;
Expand Down Expand Up @@ -155,6 +181,10 @@ class thinger_resource {
return *this;
}

void set_io_type(io_type type){
io_type_ = type;
}

io_type get_io_type(){
return io_type_;
}
Expand Down Expand Up @@ -266,6 +296,23 @@ class thinger_resource {
callback_.pson_in_pson_out = pson_in_pson_out_function;
}


#ifdef THINGER_ENABLE_STREAM_LISTENER
/**
* Establish a function for receiving stream listening events
*/
void set_stream_listener(std::function<void(uint16_t, unsigned long, bool enabled)> stream_listener){
stream_listener_ = stream_listener;
}

/**
* Establish a function for receiving stream listening events
*/
std::function<void(uint16_t, unsigned long, bool enabled)> get_stream_listener(){
return stream_listener_;
}
#endif

#else

/**
Expand Down Expand Up @@ -332,6 +379,19 @@ class thinger_resource {
callback_.pson_in_pson_out = pson_in_pson_out_function;
}

#ifdef THINGER_ENABLE_STREAM_LISTENER
/**
* Establish a function for receiving stream listening events
*/
void set_stream_listener(void (*stream_listener)(uint16_t, unsigned long, bool enabled)){
stream_listener_ = stream_listener;
}

void (*stream_listener)(uint16_t, unsigned long, bool enabled) get_stream_listener(){
return stream_listener_;
}
#endif

#endif

/**
Expand All @@ -354,6 +414,9 @@ class thinger_resource {
case pson_in_pson_out:
callback_.pson_in_pson_out(request, response);
break;
case pson_stream:
callback_.pson(request);
break;
case none:
break;
}
Expand Down

0 comments on commit e953660

Please sign in to comment.