Commit 52179526 authored by Volker Krause's avatar Volker Krause
Browse files

Also detect vehicle types for some non GBFS-compliant feeds

parent 8a9b9db2
Pipeline #78624 passed with stage
in 18 seconds
......@@ -74,7 +74,9 @@ static RentalVehicle::VehicleType gbfs2kptVehicleType(const GBFSVehicleType &veh
{ GBFSVehicleType::Bicycle, GBFSVehicleType::ElectricAssist, RentalVehicle::Pedelec },
{ GBFSVehicleType::Scooter, GBFSVehicleType::Electric, RentalVehicle::ElectricKickScooter },
{ GBFSVehicleType::Scooter, GBFSVehicleType::ElectricAssist, RentalVehicle::ElectricKickScooter },
{ GBFSVehicleType::Scooter, GBFSVehicleType::UndefinedPropulsion, RentalVehicle::ElectricKickScooter },
{ GBFSVehicleType::Moped, GBFSVehicleType::Electric, RentalVehicle::ElectricMoped },
{ GBFSVehicleType::Moped, GBFSVehicleType::UndefinedPropulsion, RentalVehicle::ElectricMoped },
{ GBFSVehicleType::Car, GBFSVehicleType::Electric, RentalVehicle::Car },
{ GBFSVehicleType::Car, GBFSVehicleType::Combustion, RentalVehicle::Car },
};
......@@ -190,7 +192,12 @@ static void appendResults(const GBFSService &service, const LocationRequest &req
// TODO remaining range, deep rental links
RentalVehicle vehicle;
vehicle.setNetwork(network);
const auto vehicleType = vehicleTypes.vehicleType(bike.value(QLatin1String("vehicle_type_id")).toString());
auto vehicleTypeId = bike.value(QLatin1String("vehicle_type_id")).toString();
if (vehicleTypeId.isEmpty()) { // non-compliant format used eg. by Lime
vehicleTypeId = bike.value(QLatin1String("vehicle_type")).toString();
}
const auto vehicleType = vehicleTypes.vehicleType(vehicleTypeId);
vehicle.setType(gbfs2kptVehicleType(vehicleType));
loc.setData(vehicle);
......
......@@ -63,6 +63,20 @@ static T lookupValue(const value_map_entry<T>(&map)[N], QStringView name)
return {};
}
// some services don't prove a vehicle_types file but use somewhat descriptive fixed values
// try to support that as well to the extend possible
struct fallback_entry {
GBFSVehicleType::FormFactor formFactor;
GBFSVehicleType::PropulsionType propulsionType;
};
static constexpr const value_map_entry<fallback_entry> fallback_type_map[] = {
{ "bike", { GBFSVehicleType::Bicycle, GBFSVehicleType::Human } },
{ "moped", { GBFSVehicleType::Moped, GBFSVehicleType::UndefinedPropulsion } },
{ "scooter", { GBFSVehicleType::Scooter, GBFSVehicleType::UndefinedPropulsion } },
{ "ebike", { GBFSVehicleType::Bicycle, GBFSVehicleType::ElectricAssist } },
{ "electric_moped", { GBFSVehicleType::Moped, GBFSVehicleType::Electric } },
};
GBFSVehicleType GBFSVehicleType::fromJson(const QJsonObject &obj)
{
GBFSVehicleType v;
......@@ -94,9 +108,27 @@ GBFSVehicleTypes::~GBFSVehicleTypes() = default;
GBFSVehicleType GBFSVehicleTypes::vehicleType(QStringView typeId) const
{
if (typeId.empty()) {
return {};
}
const auto it = std::lower_bound(m_vehicleTypes.begin(), m_vehicleTypes.end(), typeId);
if (it != m_vehicleTypes.end() && (*it).typeId == typeId) {
return (*it);
}
// fallback for non-compliant services without vehicle_types file
if (m_vehicleTypes.empty()) {
for (const auto &val : fallback_type_map) {
if (QLatin1String(val.name) == typeId) {
GBFSVehicleType v;
v.formFactor = val.value.formFactor;
v.propulsionType = val.value.propulsionType;
return v;
}
}
qDebug() << "unknown fallback vehicle type:" << typeId;
}
return {};
}
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