@@ -450,30 +450,30 @@ bool Snapshotter::writeTopic(
450450
451451 MessageQueue::range_t range = message_queue.rangeFromTimes (req->start_time , req->stop_time );
452452
453- /* TODO(jwhitleywork): FIX
454- // open bag if this the first valid topic and there is data
455- if (!bag.isOpen() && range.second > range.first) {
456- try {
457- bag.open(req->filename, rosbag::bagmode::Write);
458- } catch (rosbag::BagException const & err) {
459- res->success = false;
460- res->message = string("failed to open bag: ") + err.what();
453+ rosbag2_storage::TopicMetadata tm;
454+ tm.name = topic_details.name ;
455+ tm.type = topic_details.type ;
456+ tm.serialization_format = " cdr" ;
457+
458+ bag_writer.create_topic (tm);
459+
460+ for (auto msg_it = range.first ; msg_it != range.second ; ++msg_it) {
461+ // Create BAG message
462+ auto bag_message = std::make_shared<rosbag2_storage::SerializedBagMessage>();
463+ auto ret = rcutils_system_time_now (&bag_message->time_stamp );
464+ if (ret != RCL_RET_OK) {
465+ RCLCPP_ERROR (get_logger (), " Failed to assign time to rosbag message." );
461466 return false ;
462467 }
463- RCLCPP_INFO(get_logger(), "Writing snapshot to %s", req->filename.c_str());
464- }
465468
466- // write queue
467- try {
468- for (MessageQueue::range_t::first_type msg_it = range.first; msg_it != range.second; ++msg_it) {
469- SnapshotMessage const & msg = *msg_it;
470- bag.write(topic, msg.time, msg.msg);
471- }
472- } catch (rosbag::BagException const & err) {
473- res->success = false;
474- res->message = string("failed to write bag: ") + err.what();
469+ bag_message->topic_name = tm.name ;
470+ bag_message->serialized_data = std::make_shared<rcutils_uint8_array_t >(
471+ msg_it->msg ->get_rcl_serialized_message ()
472+ );
473+
474+ bag_writer.write (bag_message);
475475 }
476- */
476+
477477 return true ;
478478}
479479
0 commit comments