Commit 1f60bc50 authored by Volker Krause's avatar Volker Krause
Browse files

Use cached location results for Hafas departure queries

parent 89e58567
......@@ -54,6 +54,7 @@ private slots:
Location loc;
loc.setName(QLatin1String("Randa"));
loc.setCoordinate(7.6, 46.1);
loc.setIdentifier(QLatin1String("uic"), QLatin1String("85xxxxx"));
Cache::addLocationCacheEntry(QLatin1String("unittest"), req.cacheKey(), {loc});
entry = Cache::lookupLocation(QLatin1String("unittest"), req.cacheKey());
......@@ -62,6 +63,9 @@ private slots:
QCOMPARE(entry.data[0].name(), loc.name());
QCOMPARE(entry.data[0].latitude(), 7.6f);
QCOMPARE(entry.data[0].longitude(), 46.1f);
QCOMPARE(entry.data[0].identifiers().size(), 1);
QCOMPARE(entry.data[0].identifier(QLatin1String("uic")), QLatin1String("85xxxxx"));
QCOMPARE(entry.data[0].timeZone().isValid(), false);
}
};
......
......@@ -70,6 +70,23 @@ bool HafasMgateBackend::queryDeparture(DepartureReply *reply, QNetworkAccessMana
locReq.setCoordinate(request.stop().latitude(), request.stop().longitude());
locReq.setName(request.stop().name());
// TODO set max result = 1
// check if this location query is cached already
const auto cacheEntry = Cache::lookupLocation(backendId(), locReq.cacheKey());
switch (cacheEntry.type) {
case CacheHitType::Negative:
addError(reply, Reply::NotFoundError, {});
return false;
case CacheHitType::Positive:
if (cacheEntry.data.size() >= 1) {
queryDeparture(reply, cacheEntry.data[0].identifier(locationIdentifierType()), nam);
return true;
}
break;
case CacheHitType::Miss:
break;
}
const auto locReply = postLocationQuery(locReq, nam);
if (!locReply) {
return false;
......
......@@ -159,6 +159,15 @@ QJsonObject Location::toJson(const Location &loc)
obj.insert(QLatin1String("name"), loc.name());
obj.insert(QLatin1String("latitude"), loc.latitude());
obj.insert(QLatin1String("longitude"), loc.longitude());
if (loc.timeZone().isValid()) {
obj.insert(QLatin1String("timezone"), QString::fromUtf8(loc.timeZone().id()));
}
QJsonObject ids;
for (auto it = loc.d->ids.begin(); it != loc.d->ids.end(); ++it) {
ids.insert(it.key(), it.value());
}
obj.insert(QLatin1String("identifier"), ids);
return obj;
}
......@@ -176,6 +185,16 @@ static Location fromJsonObject(const QJsonObject &obj)
Location loc;
loc.setName(obj.value(QLatin1String("name")).toString());
loc.setCoordinate(obj.value(QLatin1String("latitude")).toDouble(), obj.value(QLatin1String("longitude")).toDouble());
const auto tz = obj.value(QLatin1String("timezone")).toString();
if (!tz.isEmpty()) {
loc.setTimeZone(QTimeZone(tz.toUtf8()));
}
const auto ids = obj.value(QLatin1String("identifier")).toObject();
for (auto it = ids.begin(); it != ids.end(); ++it) {
loc.setIdentifier(it.key(), it.value().toString());
}
return loc;
}
......
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