diff options
author | Birte Kristina Friesel <derf@finalrewind.org> | 2025-06-09 13:13:56 +0200 |
---|---|---|
committer | Birte Kristina Friesel <derf@finalrewind.org> | 2025-06-09 13:13:56 +0200 |
commit | ccdfc0206f849ccaeee8f5a5093c51d1274d2652 (patch) | |
tree | a30deb0179ef95a01360161020dd85dfe1d1823d /lib/Travelynx/Model/Stations.pm | |
parent | 3322ca23669871fff79a229b9167f2e3169c4352 (diff) |
Add (possibly still somewhat experimental) MOTIS support
Squashed commit of the following:
commit c7c8b2ec5d8254eefb548bfe7763a7d8c9558be4
Author: Birte Kristina Friesel <derf@finalrewind.org>
Date: Mon Jun 9 13:08:57 2025 +0200
fix another merge issue
commit d2ae55c901ab59284263ad3070ba425e03cee833
Author: Birte Kristina Friesel <derf@finalrewind.org>
Date: Mon Jun 9 13:08:39 2025 +0200
Stations: get_by_external_id is a slow function
commit 725174413300e71c350d2f1dcfbeacd751def977
Author: Birte Kristina Friesel <derf@finalrewind.org>
Date: Mon Jun 9 13:05:48 2025 +0200
... I accidentally commited a merge conflict
commit c695494dbd6aaf252199da42ad763bdffa1d64b9
Merge: e5da62b 3322ca2
Author: Birte Kristina Friesel <derf@finalrewind.org>
Date: Mon Jun 9 12:46:08 2025 +0200
Merge branch 'main' into motis
commit e5da62bcfc7953d5109ba53ae1fcc34f509f251b
Author: Birte Kristina Friesel <derf@finalrewind.org>
Date: Wed Apr 30 18:15:39 2025 +0200
cpanfile: add Travel::Status::MOTIS dependency
commit 180723a9e0e2f0aede0bc6352d5eee601183ccef
Merge: 479373b c90ae4c
Author: Birte Kristina Friesel <derf@finalrewind.org>
Date: Wed Apr 30 18:13:45 2025 +0200
Merge branch 'main' into motis
commit 479373b14eaadbc022199df246c9fb523a87188c
Author: Birte Kristina Friesel <derf@finalrewind.org>
Date: Wed Apr 30 18:06:41 2025 +0200
database: remove duplicate users_with_backend migration
commit 94c8b5a7d1e2cb7f73b0eca7e33d916775504cd4
Author: Birte Kristina Friesel <derf@finalrewind.org>
Date: Wed Apr 30 18:06:04 2025 +0200
Do not store train colours in database. They're only supported by MOTIS.
commit d58f23c3c7b06cc0243c1945dacd8673d2d2e428
Author: networkException <git@nwex.de>
Date: Fri Apr 18 11:47:02 2025 +0200
Initial MOTIS backend support
This patch adds support for checkins using MOTIS backends
using the Travel::Status::MOTIS module.
With this travelynx supports the two services currently
exposed by the module, RNV for local transit in Mannheim,
Germany and surrounding cities and transitous for worldwide
crowdsourced tranit feeds.
This implementation supports realtime predictions,
cancellations and polylines as well as custom route colors
if available.
As MOTIS doesn't expose names of indivial trips currently,
displaying transports is mostly limited to route names.
MOTIS uses strings for stop ids, based on the used GTFS
source feeds. As travelynx's data model currently assumes
interger station ids, this patch adds a mapping table
to the database.
This patch assumes support for MOTIS in db-fakedisplay.
Note that while träwelling has migrated to tranitous fully
sync remains unsupported for now.
See https://github.com/Traewelling/traewelling/issues/3345
Diffstat (limited to 'lib/Travelynx/Model/Stations.pm')
-rw-r--r-- | lib/Travelynx/Model/Stations.pm | 96 |
1 files changed, 95 insertions, 1 deletions
diff --git a/lib/Travelynx/Model/Stations.pm b/lib/Travelynx/Model/Stations.pm index 3d6549f..761c5de 100644 --- a/lib/Travelynx/Model/Stations.pm +++ b/lib/Travelynx/Model/Stations.pm @@ -1,6 +1,7 @@ package Travelynx::Model::Stations; # Copyright (C) 2022 Birte Kristina Friesel +# Copyright (C) 2025 networkException <git@nwex.de> # # SPDX-License-Identifier: AGPL-3.0-or-later @@ -28,6 +29,9 @@ sub get_backend_id { if ( $opt{dbris} and $self->{backend_id}{dbris}{ $opt{dbris} } ) { return $self->{backend_id}{dbris}{ $opt{dbris} }; } + if ( $opt{motis} and $self->{backend_id}{motis}{ $opt{motis} } ) { + return $self->{backend_id}{motis}{ $opt{motis} }; + } my $db = $opt{db} // $self->{pg}->db; my $backend_id = 0; @@ -54,6 +58,17 @@ sub get_backend_id { )->hash->{id}; $self->{backend_id}{hafas}{ $opt{hafas} } = $backend_id; } + elsif ( $opt{motis} ) { + $backend_id = $db->select( + 'backends', + ['id'], + { + motis => 1, + name => $opt{motis} + } + )->hash->{id}; + $self->{backend_id}{motis}{ $opt{motis} } = $backend_id; + } return $backend_id; } @@ -85,7 +100,7 @@ sub get_backends { $opt{db} //= $self->{pg}->db; my $res = $opt{db} - ->select( 'backends', [ 'id', 'name', 'iris', 'hafas', 'dbris' ] ); + ->select( 'backends', [ 'id', 'name', 'iris', 'hafas', 'dbris', 'motis' ] ); my @ret; while ( my $row = $res->hash ) { @@ -97,6 +112,7 @@ sub get_backends { iris => $row->{iris}, dbris => $row->{dbris}, hafas => $row->{hafas}, + motis => $row->{motis}, } ); } @@ -149,6 +165,61 @@ sub add_or_update { return; } + if ( $opt{motis} ) { + if ( + my $s = $self->get_by_external_id( + external_id => $stop->id, + db => $opt{db}, + backend_id => $opt{backend_id} + ) + ) + { + $opt{db}->update( + 'stations', + { + name => $stop->name, + lat => $stop->lat, + lon => $stop->lon, + archived => 0 + }, + { + eva => $s->{eva}, + source => $opt{backend_id} + } + ); + + $stop->{eva} = $s->{eva}; + + return; + } + + my $s = $opt{db}->query( + qq { + with new_station as ( + insert into stations_external_ids (backend_id, external_id) + values (?, ?) + returning eva, backend_id + ) + + insert into stations (eva, name, lat, lon, source, archived) + values ((select eva from new_station), ?, ?, ?, (select backend_id from new_station), ?) + returning * + }, + ( + $opt{backend_id}, + $stop->id, + $stop->name, + $stop->lat, + $stop->lon, + 0, + ) + ); + + $stop->{eva} = $s->hash->{eva}; + + return; + } + my $loc = $stop->loc; if ( my $s = $self->get_by_eva( @@ -184,6 +255,8 @@ sub add_or_update { archived => 0 } ); + + return; } sub add_meta { @@ -276,6 +349,27 @@ sub get_by_eva { )->hash; } +# Slow +sub get_by_external_id { + my ( $self, %opt ) = @_; + + if ( not $opt{external_id} ) { + return; + } + + $opt{db} //= $self->{pg}->db; + $opt{backend_id} //= $self->get_backend_id(%opt); + + return $opt{db}->select( + 'stations_with_external_ids', + '*', + { + external_id => $opt{external_id}, + source => $opt{backend_id}, + } + )->hash; +} + # Fast sub get_by_evas { my ( $self, %opt ) = @_; |