Skip to content

Commit

Permalink
change device on-device-log to signal
Browse files Browse the repository at this point in the history
  • Loading branch information
maloel committed Dec 14, 2023
1 parent bd449da commit ff1dd12
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 27 deletions.
2 changes: 1 addition & 1 deletion third-party/realdds/include/realdds/dds-device.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class dds_device
typedef std::function< void(
dds_time const & timestamp, char type, std::string const & text, nlohmann::json const & data ) >
on_device_log_callback;
void on_device_log( on_device_log_callback cb );
rsutils::subscription on_device_log( on_device_log_callback && cb );

typedef std::function< void( std::string const & id, nlohmann::json const & ) > on_notification_callback;
rsutils::subscription on_notification( on_notification_callback && );
Expand Down
12 changes: 7 additions & 5 deletions third-party/realdds/py/pyrealdds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -908,11 +908,13 @@ PYBIND11_MODULE(NAME, m) {
[&self, callback]( std::shared_ptr< const nlohmann::json > const & pj )
{ FN_FWD_CALL( dds_device, "on_metadata_available", callback( self, json_to_py( *pj ) ); ) } ) );
} )
.def( FN_FWD( dds_device,
on_device_log,
(dds_device &, dds_time const &, char, std::string const &, py::object && ),
(dds_time const & timestamp, char type, std::string const & text, nlohmann::json const & data),
callback( self, timestamp, type, text, json_to_py( data ) ); ) )
.def( "on_device_log",
[]( dds_device & self, std::function< void( dds_device &, dds_time const &, char, std::string const &, py::object && ) > callback )
{
return std::make_shared< subscription >( self.on_device_log(
[&self, callback]( dds_time const & timestamp, char type, std::string const & text, nlohmann::json const & data )
{ FN_FWD_CALL( dds_device, "on_device_log", callback( self, timestamp, type, text, json_to_py( data ) ); ) } ) );
} )
.def( "on_notification",
[]( dds_device & self, std::function< void( dds_device &, std::string const &, py::object && ) > callback )
{
Expand Down
20 changes: 6 additions & 14 deletions third-party/realdds/src/dds-device-impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,25 +348,17 @@ void dds_device::impl::on_log( nlohmann::json const & j, eprosima::fastdds::dds:
{
if( ! entry.is_array() )
throw std::runtime_error( "not an array" );
if( entry.size() > 4 )
throw std::runtime_error( "too long" );
if( entry.size() < 3 || entry.size() > 4 )
throw std::runtime_error( "bad array length" );
auto timestamp = time_from( rsutils::json::get< dds_nsec >( entry, 0 ) );
auto const stype = rsutils::json::get< std::string >( entry, 1 );
auto const & stype = rsutils::json::string_ref( entry[1] );
if( stype.length() != 1 || ! strchr( "EWID", stype[0] ) )
throw std::runtime_error( "type not one of 'EWID'" );
char const type = stype[0];
auto const text = rsutils::json::get< std::string >( entry, 2 );
nlohmann::json data;
if( entry.size() > 3 )
{
data = entry.at( 3 );
if( ! data.is_object() )
throw std::runtime_error( "data is not an object" );
}
auto const & text = rsutils::json::string_ref( entry[2] );
nlohmann::json const & data = entry.size() > 3 ? entry[3] : rsutils::json::null_json;

if( _on_device_log )
_on_device_log( timestamp, type, text, data );
else
if( ! _on_device_log.raise( timestamp, type, text, data ) )
LOG_DEBUG( "[" << debug_name() << "][" << timestr( timestamp ) << "][" << type << "] " << text
<< " [" << data << "]" );
}
Expand Down
15 changes: 10 additions & 5 deletions third-party/realdds/src/dds-device-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,15 @@ class dds_device::impl
return _on_metadata_available.subscribe( std::move( cb ) );
}

typedef std::function< void(
dds_time const & timestamp, char type, std::string const & text, nlohmann::json const & data ) >
on_device_log_callback;
void on_device_log( on_device_log_callback cb ) { _on_device_log = cb; }
using on_device_log_signal = rsutils::signal< dds_time const &, // timestamp
char, // type
std::string const &, // text
nlohmann::json const & >; // data
using on_device_log_callback = on_device_log_signal::callback;
rsutils::subscription on_device_log( on_device_log_callback && cb )
{
_on_device_log.subscribe( std::move( cb ) );
}

using on_notification_signal = rsutils::signal< std::string const &, nlohmann::json const & >;
using on_notification_callback = on_notification_signal::callback;
Expand Down Expand Up @@ -128,7 +133,7 @@ class dds_device::impl
void handle_notification( nlohmann::json const &, eprosima::fastdds::dds::SampleInfo const & );

on_metadata_available_signal _on_metadata_available;
on_device_log_callback _on_device_log;
on_device_log_signal _on_device_log;
on_notification_signal _on_notification;
};

Expand Down
4 changes: 2 additions & 2 deletions third-party/realdds/src/dds-device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ rsutils::subscription dds_device::on_metadata_available( on_metadata_available_c
return _impl->on_metadata_available( std::move( cb ) );
}

void dds_device::on_device_log( on_device_log_callback cb )
rsutils::subscription dds_device::on_device_log( on_device_log_callback && cb )
{
_impl->on_device_log( cb );
return _impl->on_device_log( std::move( cb ) );
}

rsutils::subscription dds_device::on_notification( on_notification_callback && cb )
Expand Down

0 comments on commit ff1dd12

Please sign in to comment.