diff --git a/sncf-station/src/TrainsTable.js b/sncf-station/src/TrainsTable.js index 68a5904..e2ab7f1 100644 --- a/sncf-station/src/TrainsTable.js +++ b/sncf-station/src/TrainsTable.js @@ -79,7 +79,7 @@ function TrainsTableBody({stop, date, time, tableType}) { const nullRef = useRef(null) let table_rows = trains.map((train) => - + ) return <> @@ -91,7 +91,7 @@ function TrainsTableBody({stop, date, time, tableType}) { } -function TrainRow({train, tableType}) { +function TrainRow({train, tableType, date, time}) { const tripQuery = useQuery({ queryKey: ['trip', train.trip], queryFn: () => fetch(`/api/gtfs/trip/${train.trip}/`) @@ -138,6 +138,19 @@ function TrainRow({train, tableType}) { stopsFilter = (stop_time) => stop_time.stop_sequence < train.stop_sequence && stop_time.pickup_type === 0 let stopsNames = stops.filter(stopsFilter).map(stopTime => stopTime?.stop.name ?? "").join(", ") ?? "" + const realtimeQuery = useQuery({ + queryKey: ['realtime', train.id, date, time], + queryFn: () => fetch(`/api/gtfs-rt/stop_time_update/${train.id}/`) + .then(response => response.json()), + enabled: !!train.id, + }) + const realtimeData = realtimeQuery.data ?? {} + if (!realtimeQuery.isError) + console.log(realtimeData) + const delay = tableType === "departures" ? realtimeData.departure_delay : realtimeData.arrival_delay + const prettyDelay = delay ? getPrettyDelay(delay) : "" + const visibleDelay = delay ? `${prettyDelay}` : "" + return <> @@ -165,8 +178,15 @@ function TrainRow({train, tableType}) { - - {getDisplayTime(train, tableType)} + + + + {getDisplayTime(train, tableType)} + + + {visibleDelay} + + @@ -227,4 +247,12 @@ function getDisplayTime(train, tableType) { return day_split[day_split.length - 1].substring(0, 5) } +function getPrettyDelay(delay) { + let delay_split = delay.split(':') + let hours = parseInt(delay_split[0]) + let minutes = parseInt(delay_split[1]) + let full_minutes = hours * 60 + minutes + return full_minutes ? `+${full_minutes} min` : "À l'heure" +} + export default TrainsTable; \ No newline at end of file diff --git a/sncf/api/serializers.py b/sncf/api/serializers.py index c763cb5..e61d56c 100644 --- a/sncf/api/serializers.py +++ b/sncf/api/serializers.py @@ -1,7 +1,7 @@ from rest_framework import serializers from sncfgtfs.models import Agency, Stop, Route, Trip, StopTime, Calendar, CalendarDate, \ - Transfer, FeedInfo + Transfer, FeedInfo, TripUpdate, StopTimeUpdate class AgencySerializer(serializers.ModelSerializer): @@ -61,3 +61,15 @@ class FeedInfoSerializer(serializers.ModelSerializer): class Meta: model = FeedInfo fields = '__all__' + + +class TripUpdateSerializer(serializers.ModelSerializer): + class Meta: + model = TripUpdate + fields = '__all__' + + +class StopTimeUpdateSerializer(serializers.ModelSerializer): + class Meta: + model = StopTimeUpdate + fields = '__all__' diff --git a/sncf/api/views.py b/sncf/api/views.py index 904ac01..5ded803 100644 --- a/sncf/api/views.py +++ b/sncf/api/views.py @@ -10,10 +10,9 @@ from rest_framework.filters import OrderingFilter, SearchFilter from sncf.api.serializers import AgencySerializer, StopSerializer, RouteSerializer, TripSerializer, \ StopTimeSerializer, CalendarSerializer, CalendarDateSerializer, TransferSerializer, \ - FeedInfoSerializer + FeedInfoSerializer, TripUpdateSerializer, StopTimeUpdateSerializer from sncfgtfs.models import Agency, Stop, Route, Trip, StopTime, Calendar, CalendarDate, \ - Transfer, FeedInfo - + Transfer, FeedInfo, TripUpdate, StopTimeUpdate CACHE_CONTROL = cache_control(max_age=7200) LAST_MODIFIED = last_modified(lambda *args, **kwargs: datetime.fromisoformat(FeedInfo.objects.get().version)) @@ -102,6 +101,20 @@ class FeedInfoViewSet(viewsets.ReadOnlyModelViewSet): filterset_fields = '__all__' +class TripUpdateViewSet(viewsets.ReadOnlyModelViewSet): + queryset = TripUpdate.objects.all() + serializer_class = TripUpdateSerializer + filter_backends = [DjangoFilterBackend] + filterset_fields = '__all__' + + +class StopTimeUpdateViewSet(viewsets.ReadOnlyModelViewSet): + queryset = StopTimeUpdate.objects.all() + serializer_class = StopTimeUpdateSerializer + filter_backends = [DjangoFilterBackend] + filterset_fields = '__all__' + + class NextDeparturesViewSet(viewsets.ReadOnlyModelViewSet): queryset = StopTime.objects.none() serializer_class = StopTimeSerializer diff --git a/sncf/urls.py b/sncf/urls.py index 00520ee..e1dae15 100644 --- a/sncf/urls.py +++ b/sncf/urls.py @@ -19,7 +19,8 @@ from django.urls import path, include from rest_framework import routers from sncf.api.views import AgencyViewSet, StopViewSet, RouteViewSet, TripViewSet, StopTimeViewSet, \ - CalendarViewSet, CalendarDateViewSet, TransferViewSet, FeedInfoViewSet, NextDeparturesViewSet, NextArrivalsViewSet + CalendarViewSet, CalendarDateViewSet, TransferViewSet, FeedInfoViewSet, NextDeparturesViewSet, NextArrivalsViewSet, \ + TripUpdateViewSet, StopTimeUpdateViewSet router = routers.DefaultRouter() router.register("gtfs/agency", AgencyViewSet) @@ -31,6 +32,8 @@ router.register("gtfs/calendar", CalendarViewSet) router.register("gtfs/calendar_date", CalendarDateViewSet) router.register("gtfs/transfer", TransferViewSet) router.register("gtfs/feed_info", FeedInfoViewSet) +router.register("gtfs-rt/trip_update", TripUpdateViewSet) +router.register("gtfs-rt/stop_time_update", StopTimeUpdateViewSet) router.register("station/next_departures", NextDeparturesViewSet, basename="next_departures") router.register("station/next_arrivals", NextArrivalsViewSet, basename="next_arrivals")