Commit 5f8544bc authored by Volker Krause's avatar Volker Krause

Basic timer management for auto-polling

parent 33e6bbfa
......@@ -54,6 +54,9 @@ LiveDataManager::LiveDataManager(QObject *parent)
{
m_ptMgr.reset(new KPublicTransport::Manager);
m_pollTimer.setSingleShot(true);
connect(&m_pollTimer, &QTimer::timeout, this, &LiveDataManager::poll);
}
LiveDataManager::~LiveDataManager() = default;
......@@ -78,6 +81,7 @@ void LiveDataManager::setReservationManager(ReservationManager *resMgr)
});
loadPublicTransportData();
m_pollTimer.setInterval(nextPollTime());
}
void LiveDataManager::setPkPassManager(PkPassManager *pkPassMgr)
......@@ -87,7 +91,12 @@ void LiveDataManager::setPkPassManager(PkPassManager *pkPassMgr)
void LiveDataManager::setPollingEnabled(bool pollingEnabled)
{
// TODO
if (pollingEnabled) {
m_pollTimer.setInterval(nextPollTime());
m_pollTimer.start();
} else {
m_pollTimer.stop();
}
}
void LiveDataManager::setAllowInsecureServices(bool allowInsecure)
......@@ -346,4 +355,57 @@ void LiveDataManager::reservationRemoved(const QString &resId)
}
}
void LiveDataManager::poll()
{
qCDebug(Log);
// TODO
}
int LiveDataManager::nextPollTime() const
{
int t = std::numeric_limits<int>::max();
for (const auto &resId : m_reservations) {
t = std::min(t, nextPollTimeForReservation(resId));
}
qCDebug(Log) << "next auto-update in" << (t/1000) << "secs";
return t;
}
struct {
int distance; // secs
int pollInterval; // secs
} static const pollIntervalTable[] = {
{ 3600, 5*60 }, // for <1h we poll every 5 minutes
{ 4 * 3600, 15 * 60 }, // for <4h we poll every 15 minutes
{ 24 * 3600, 3600 }, // for <1d we poll once per hour
{ 4 * 24 * 3600, 24 * 3600 }, // for <4d we poll once per day
};
int LiveDataManager::nextPollTimeForReservation(const QString& resId) const
{
const auto res = m_resMgr->reservation(resId);
const auto now = QDateTime::currentDateTime();
auto dist = now.secsTo(SortUtil::startDateTime(res));
if (dist < 0) {
dist = now.secsTo(SortUtil::endtDateTime(res));
}
if (dist < 0) {
return std::numeric_limits<int>::max();
}
// TODO consider delayed but still pending departures/arrivals too
const auto it = std::lower_bound(std::begin(pollIntervalTable), std::end(pollIntervalTable), dist, [](const auto &lhs, const auto rhs) {
return lhs.distance < rhs;
});
if (it == std::end(pollIntervalTable)) {
return std::numeric_limits<int>::max();
}
// TODO check last poll time for this reservation, and skip stuff that isn't outdated yet
return it->pollInterval * 1000; // we need msecs
}
#include "moc_livedatamanager.cpp"
......@@ -23,6 +23,7 @@
#include <QDateTime>
#include <QHash>
#include <QObject>
#include <QTimer>
#include <memory>
#include <vector>
......@@ -85,12 +86,18 @@ private:
void loadPublicTransportData();
void loadPublicTransportData(const QString &prefix, QHash<QString, TrainChange>& data) const;
void poll();
int nextPollTime() const;
int nextPollTimeForReservation(const QString &resId) const;
ReservationManager *m_resMgr;
PkPassManager *m_pkPassMgr;
std::unique_ptr<KPublicTransport::Manager> m_ptMgr;
std::vector<QString> m_reservations;
QHash <QString, TrainChange> m_arrivals;
QHash <QString, TrainChange> m_departures;
QTimer m_pollTimer;
};
#endif // LIVEDATAMANAGER_H
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment