|
|
@ -1,10 +1,8 @@ |
|
|
|
#include "tools/cabana/streams/livestream.h" |
|
|
|
#include "tools/cabana/streams/livestream.h" |
|
|
|
|
|
|
|
|
|
|
|
LiveStream::LiveStream(QObject *parent, QString address) : zmq_address(address), AbstractStream(parent, true) { |
|
|
|
#include <QTimer> |
|
|
|
timer = new QTimer(this); |
|
|
|
|
|
|
|
timer->callOnTimeout(this, &LiveStream::removeExpiredEvents); |
|
|
|
|
|
|
|
timer->start(3 * 1000); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LiveStream::LiveStream(QObject *parent, QString address) : zmq_address(address), AbstractStream(parent, true) { |
|
|
|
stream_thread = new QThread(this); |
|
|
|
stream_thread = new QThread(this); |
|
|
|
QObject::connect(stream_thread, &QThread::started, [=]() { streamThread(); }); |
|
|
|
QObject::connect(stream_thread, &QThread::started, [=]() { streamThread(); }); |
|
|
|
QObject::connect(stream_thread, &QThread::finished, stream_thread, &QThread::deleteLater); |
|
|
|
QObject::connect(stream_thread, &QThread::finished, stream_thread, &QThread::deleteLater); |
|
|
@ -15,8 +13,6 @@ LiveStream::~LiveStream() { |
|
|
|
stream_thread->requestInterruption(); |
|
|
|
stream_thread->requestInterruption(); |
|
|
|
stream_thread->quit(); |
|
|
|
stream_thread->quit(); |
|
|
|
stream_thread->wait(); |
|
|
|
stream_thread->wait(); |
|
|
|
for (Event *e : can_events) ::delete e; |
|
|
|
|
|
|
|
for (auto m : messages) delete m; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void LiveStream::streamThread() { |
|
|
|
void LiveStream::streamThread() { |
|
|
@ -35,11 +31,8 @@ void LiveStream::streamThread() { |
|
|
|
QThread::msleep(50); |
|
|
|
QThread::msleep(50); |
|
|
|
continue; |
|
|
|
continue; |
|
|
|
} |
|
|
|
} |
|
|
|
AlignedBuffer *buf = messages.emplace_back(new AlignedBuffer()); |
|
|
|
std::lock_guard lk(lock); |
|
|
|
Event *evt = ::new Event(buf->align(msg)); |
|
|
|
handleEvent(messages.emplace_back(msg).event); |
|
|
|
delete msg; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
handleEvent(evt); |
|
|
|
|
|
|
|
// TODO: write stream to log file to replay it with cabana --data_dir flag.
|
|
|
|
// TODO: write stream to log file to replay it with cabana --data_dir flag.
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -53,11 +46,10 @@ void LiveStream::handleEvent(Event *evt) { |
|
|
|
emit streamStarted(); |
|
|
|
emit streamStarted(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
std::lock_guard lk(lock); |
|
|
|
received.push_back(evt); |
|
|
|
can_events.push_back(evt); |
|
|
|
|
|
|
|
if (!pause_) { |
|
|
|
if (!pause_) { |
|
|
|
if (speed_ < 1 && last_update_ts > 0) { |
|
|
|
if (speed_ < 1 && last_update_ts > 0) { |
|
|
|
auto it = std::upper_bound(can_events.cbegin(), can_events.cend(), current_ts, [](uint64_t ts, auto &e) { |
|
|
|
auto it = std::upper_bound(received.cbegin(), received.cend(), current_ts, [](uint64_t ts, auto &e) { |
|
|
|
return ts < e->mono_time; |
|
|
|
return ts < e->mono_time; |
|
|
|
}); |
|
|
|
}); |
|
|
|
if (it != can_events.cend()) { |
|
|
|
if (it != can_events.cend()) { |
|
|
@ -73,27 +65,20 @@ void LiveStream::handleEvent(Event *evt) { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void LiveStream::removeExpiredEvents() { |
|
|
|
void LiveStream::process(QHash<MessageId, CanData> *last_messages) { |
|
|
|
std::lock_guard lk(lock); |
|
|
|
{ |
|
|
|
if (can_events.size() > 0) { |
|
|
|
std::lock_guard lk(lock); |
|
|
|
const uint64_t max_ns = settings.max_cached_minutes * 60 * 1e9; |
|
|
|
uint64_t last_ts = can_events.empty() ? 0 : can_events.back()->mono_time; |
|
|
|
const uint64_t last_ns = can_events.back()->mono_time; |
|
|
|
auto first = std::upper_bound(received.cbegin(), received.cend(), last_ts, [](uint64_t ts, auto &e) { |
|
|
|
while (!can_events.empty() && (last_ns - can_events.front()->mono_time) > max_ns) { |
|
|
|
return ts < e->mono_time; |
|
|
|
::delete can_events.front(); |
|
|
|
}); |
|
|
|
delete messages.front(); |
|
|
|
can_events.insert(can_events.end(), first, received.cend()); |
|
|
|
can_events.pop_front(); |
|
|
|
if (speed_ == 1) { |
|
|
|
messages.pop_front(); |
|
|
|
received.clear(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
emit eventsMerged(); |
|
|
|
|
|
|
|
AbstractStream::process(last_messages); |
|
|
|
const std::vector<Event *> *LiveStream::events() const { |
|
|
|
|
|
|
|
std::lock_guard lk(lock); |
|
|
|
|
|
|
|
if (events_vector.capacity() <= can_events.size()) { |
|
|
|
|
|
|
|
events_vector.reserve(can_events.size() * 2); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
events_vector.assign(can_events.begin(), can_events.end()); |
|
|
|
|
|
|
|
return &events_vector; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void LiveStream::pause(bool pause) { |
|
|
|
void LiveStream::pause(bool pause) { |
|
|
|