Skip to content

Commit

Permalink
final version
Browse files Browse the repository at this point in the history
  • Loading branch information
facontidavide committed Aug 30, 2023
1 parent fe78e5d commit aac2bbf
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 15 deletions.
66 changes: 65 additions & 1 deletion plotjuggler_plugins/ToolboxRemote/toolbox_remote.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
#include <QMessageBox>
#include <array>
#include <math.h>
#include "fmt/format.h"

#define MCAP_IMPLEMENTATION
#include "mcap/writer.hpp"

#include "ui_dialog_file_selection.h"

Expand Down Expand Up @@ -39,6 +41,11 @@ ToolboxRemote::ToolboxRemote() : _widget(new RemoteLoad)
this, &ToolboxRemote::onShowFilesToSelect);

connect(_widget, &RemoteLoad::rejected, this, [this]() {
if(_mcap_writer)
{
_mcap_writer->close();
_mcap_writer.reset();
}
emit closed();
});

Expand All @@ -64,6 +71,11 @@ ToolboxRemote::ToolboxRemote() : _widget(new RemoteLoad)
_progress_dialog->close();
_progress_dialog = nullptr;
}
if(_mcap_writer)
{
_mcap_writer->close();
_mcap_writer.reset();
}
emit closed();
});

Expand All @@ -73,6 +85,7 @@ ToolboxRemote::ToolboxRemote() : _widget(new RemoteLoad)

ToolboxRemote::~ToolboxRemote()
{
delete _widget;
}

void ToolboxRemote::init(PJ::PlotDataMapRef& /*src_data*/, PJ::TransformsMap& /*transform_map*/)
Expand Down Expand Up @@ -220,6 +233,13 @@ void ToolboxRemote::onLoadTopics(QStringList topics)

connect(_progress_dialog, &QProgressDialog::canceled,
&_client, &mcap_client::MCAPClient::cancelDownload);


auto filepath = _widget->getSaveFile();
if(!filepath.isEmpty())
{
saveData(filepath, topics);
}
}

void ToolboxRemote::onChunkReceived(const mcap_api::ChunkView &chunk)
Expand All @@ -230,6 +250,18 @@ void ToolboxRemote::onChunkReceived(const mcap_api::ChunkView &chunk)
{
double timestamp_sec = double(msg_view.timestamp) * 1e-9;

if(_mcap_writer)
{
mcap::Message msg;
const auto channel_id = _channeld_id_remapping.at(msg_view.channel_id);
msg.channelId = channel_id;
msg.logTime = msg_view.timestamp;
msg.publishTime = msg_view.timestamp;
msg.data = msg_view.data;
msg.dataSize = msg_view.data_size;
auto res = _mcap_writer->write(msg);
}

auto parser_it = _parsers_by_channel.find(msg_view.channel_id);
if( parser_it == _parsers_by_channel.end() )
{
Expand All @@ -241,3 +273,35 @@ void ToolboxRemote::onChunkReceived(const mcap_api::ChunkView &chunk)
parser->parseMessage(msg, timestamp_sec);
}
}

bool ToolboxRemote::saveData(QString filepath, QStringList topics)
{
_mcap_writer = std::make_shared<mcap::McapWriter>();

auto options = mcap::McapWriterOptions("MCAP PlotJuggler Cache");
const auto res = _mcap_writer->open(filepath.toStdString(), options);
if (!res.ok())
{
QMessageBox::warning(_widget, "MCAP cache saving",
QString("Failed to open the file: \n%0").arg(filepath));
return false;
}

for(auto const& info: _statistics.channels)
{
if(!topics.contains(QString::fromStdString(info.topic)))
{
continue;
}

mcap::Schema schema(info.schema_name,
info.schema_encoding,
info.schema_data);
_mcap_writer->addSchema(schema);
mcap::Channel channel(info.topic, info.message_encoding, schema.id);
_mcap_writer->addChannel(channel);
_channeld_id_remapping.insert({ info.id, channel.id});
}

return true;
}
11 changes: 11 additions & 0 deletions plotjuggler_plugins/ToolboxRemote/toolbox_remote.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@

#include <QDialog>

namespace mcap
{
class McapWriter;
}

class ToolboxRemote : public PJ::ToolboxPlugin
{
Q_OBJECT
Expand Down Expand Up @@ -61,4 +66,10 @@ private slots:
QProgressDialog* _progress_dialog = nullptr;

QString _current_filename;

std::shared_ptr<mcap::McapWriter> _mcap_writer;
// from old ID to new one
std::unordered_map<int, int> _channeld_id_remapping;

bool saveData(QString filepath, QStringList topics);
};
76 changes: 65 additions & 11 deletions plotjuggler_plugins/ToolboxRemote/widget_remote_load.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#include "ui_widget_remote_load.h"
#include <QTableWidgetItem>
#include <QSettings>
#include <QFileInfo>
#include <QFileDialog>
#include <QDateTime>

RemoteLoad::RemoteLoad(QWidget *parent) :
QDialog(parent),
Expand All @@ -19,29 +22,30 @@ RemoteLoad::RemoteLoad(QWidget *parent) :
connect (ui->buttonBox, &QDialogButtonBox::rejected, this, &RemoteLoad::rejected);

QSettings settings;
settings.beginGroup("RemoteLoadWidget");
ui->spinBox->setValue( settings.value("RemoteLoad::max_array_size", 500).toInt());

ui->spinBox->setValue( settings.value("max_array_size", 500).toInt());

if(settings.value("clamp_large_array_size", true).toBool())
if(settings.value("RemoteLoad::clamp_large_array_size", true).toBool())
{
ui->radioClamp->setChecked(true);
}
else {
ui->radioSkip->setChecked(true);
}
ui->lineHost->setText( settings.value("host", "localhost").toString() );
ui->linePort->setText( QString::number( settings.value("port", 1667).toInt()) );
ui->lineHost->setText( settings.value("RemoteLoad::host", "localhost").toString() );
ui->linePort->setText( QString::number( settings.value("RemoteLoad::port", 1667).toInt()) );
ui->lineDestination->setText(settings.value("RemoteLoad::setPath", QString()).toString());
ui->checkBoxAppendDate->setChecked(settings.value("RemoteLoad::appendDate", false).toBool());
}

RemoteLoad::~RemoteLoad()
{
QSettings settings;
settings.beginGroup("RemoteLoadWidget");
settings.setValue("max_array_size", ui->spinBox->value() );
settings.setValue("clamp_large_array_size", ui->radioClamp->isChecked() );
settings.setValue("host", ui->lineHost->text() );
settings.setValue("port", ui->linePort->text().toInt() );
settings.setValue("RemoteLoad::max_array_size", ui->spinBox->value() );
settings.setValue("RemoteLoad::clamp_large_array_size", ui->radioClamp->isChecked() );
settings.setValue("RemoteLoad::host", ui->lineHost->text() );
settings.setValue("RemoteLoad::port", ui->linePort->text().toInt() );
settings.setValue("RemoteLoad::setPath", ui->lineDestination->text());
settings.setValue("RemoteLoad::appendDate", ui->checkBoxAppendDate->isChecked());

delete ui;
}
Expand Down Expand Up @@ -114,6 +118,31 @@ std::pair<double, double> RemoteLoad::getTimeRange()
return {ui->timeMin->value(), ui->timeMax->value()};
}

QString RemoteLoad::getSaveFile() const
{
if(!ui->checkBoxSaveMCAP->isChecked())
{
return {};
}

QString filepath = ui->lineDestination->text();
if(filepath.isEmpty())
{
return {};
}
if(ui->checkBoxAppendDate->isChecked())
{
const auto info = QFileInfo(filepath);
auto filename = info.baseName() +
QDateTime::currentDateTime().toString("-yyyy_MM_dd-hh_mm") +
".mcap";
return info.dir().filePath(filename);
}
else {
return filepath;
}
}

void RemoteLoad::on_buttonOpenFile_clicked()
{
QString host = ui->lineHost->text();
Expand Down Expand Up @@ -144,3 +173,28 @@ void RemoteLoad::on_buttonResetTimes_clicked()
ui->timeMax->setValue(ui->timeMax->maximum());
}

void RemoteLoad::on_checkBoxSaveMCAP_toggled(bool checked)
{
ui->widgetSave->setEnabled(checked);
}


void RemoteLoad::on_buttonSaveDestination_clicked()
{
QString filepath = ui->lineDestination->text();
QString directory = filepath.isEmpty() ? QDir::currentPath() : filepath;

auto save_path = QFileDialog::getSaveFileName(this,
"Save downloaded data to...",
directory, "*.mcap");

if(!save_path.isEmpty())
{
if(QFileInfo(save_path).suffix() != "mcap")
{
save_path += ".mcap";
}
ui->lineDestination->setText(save_path);
}
}

6 changes: 6 additions & 0 deletions plotjuggler_plugins/ToolboxRemote/widget_remote_load.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class RemoteLoad : public QDialog

std::pair<double, double> getTimeRange();

QString getSaveFile() const;

signals:

void openFile(QString host, int port);
Expand All @@ -46,6 +48,10 @@ private slots:

void on_buttonResetTimes_clicked();

void on_checkBoxSaveMCAP_toggled(bool checked);

void on_buttonSaveDestination_clicked();

private:
Ui::RemoteLoad *ui;

Expand Down
39 changes: 36 additions & 3 deletions plotjuggler_plugins/ToolboxRemote/widget_remote_load.ui
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@
</widget>
</item>
<item>
<widget class="QWidget" name="" native="true">
<widget class="QWidget" name="widget" native="true">
<layout class="QHBoxLayout" name="horizontalLayout_3">
<property name="leftMargin">
<number>2</number>
Expand All @@ -367,14 +367,14 @@
</font>
</property>
<property name="text">
<string>Select the topics that you want to load:</string>
<string>Select the topics that you want to download:</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="buttonLoadData">
<property name="text">
<string>Load</string>
<string>Download</string>
</property>
</widget>
</item>
Expand Down Expand Up @@ -437,6 +437,39 @@
</column>
</widget>
</item>
<item>
<widget class="QCheckBox" name="checkBoxSaveMCAP">
<property name="text">
<string>Save locally a MCAP file containing the download data</string>
</property>
</widget>
</item>
<item>
<widget class="QWidget" name="widgetSave" native="true">
<layout class="QHBoxLayout" name="horizontalLayout_5">
<property name="topMargin">
<number>1</number>
</property>
<item>
<widget class="QPushButton" name="buttonSaveDestination">
<property name="text">
<string>Set destination</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineDestination"/>
</item>
<item>
<widget class="QCheckBox" name="checkBoxAppendDate">
<property name="text">
<string>Append date time to filename</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</item>
Expand Down

0 comments on commit aac2bbf

Please sign in to comment.