From 824897f63304e07b4d91a8ca09a9dbaead6b8c17 Mon Sep 17 00:00:00 2001 From: Thibstars Date: Tue, 24 Oct 2023 18:03:21 +0200 Subject: [PATCH 1/3] Include time parameter in request --- .../btsd/irail/client/LiveBoardServiceImpl.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/github/thibstars/btsd/irail/client/LiveBoardServiceImpl.java b/src/main/java/com/github/thibstars/btsd/irail/client/LiveBoardServiceImpl.java index 3589a6b..e505c03 100644 --- a/src/main/java/com/github/thibstars/btsd/irail/client/LiveBoardServiceImpl.java +++ b/src/main/java/com/github/thibstars/btsd/irail/client/LiveBoardServiceImpl.java @@ -5,6 +5,8 @@ import com.github.thibstars.btsd.irail.helper.LanguageService; import com.github.thibstars.btsd.irail.model.LiveBoard; import java.io.IOException; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; import java.util.Objects; import java.util.Optional; import java.util.stream.Stream; @@ -26,7 +28,9 @@ public class LiveBoardServiceImpl implements LiveBoardService { private static final String LANG_PLACEHOLDER = "${lang}"; - private static final String URL = "https://api.irail.be/liveboard/?id=" + ID_PLACEHOLDER + "&arrdep=departure&lang=" + LANG_PLACEHOLDER + "&format=json&alerts=false"; + private static final String TIME_PLACEHOLDER = "${time}"; + + private static final String URL = "https://api.irail.be/liveboard/?id=" + ID_PLACEHOLDER + "&arrdep=departure&lang=" + LANG_PLACEHOLDER + "&format=json&alerts=false&time=" + TIME_PLACEHOLDER; private final OkHttpClient client; @@ -61,8 +65,13 @@ public Optional getForStation(String id, String language) { private LiveBoard fetchLiveBoard(String id, String language) throws IOException { LOGGER.info("Fetching live board for station: {}", id); + String timeString = LocalDateTime.now() + .format(DateTimeFormatter.ofPattern("HHmm")); + Request request = new Request.Builder() - .url(URL.replace(ID_PLACEHOLDER, id).replace(LANG_PLACEHOLDER, languageService.getLanguageOrFallback(language))) + .url(URL.replace(ID_PLACEHOLDER, id) + .replace(LANG_PLACEHOLDER, languageService.getLanguageOrFallback(language)) + .replace(TIME_PLACEHOLDER, timeString)) .build(); ResponseBody responseBody; From 79406a3606db494df3fcb0fb8066e37a0198b511 Mon Sep 17 00:00:00 2001 From: Thibstars Date: Mon, 20 Nov 2023 22:18:49 +0100 Subject: [PATCH 2/3] Allow for time selection in UI --- pom.xml | 6 ++++ .../liveboard/LiveBoardController.java | 9 +++++ .../desktop/liveboard/LiveBoardPanel.java | 33 +++++++++++++++---- .../btsd/irail/client/LiveBoardService.java | 11 +++++++ .../irail/client/LiveBoardServiceImpl.java | 16 ++++++--- src/main/resources/messages.properties | 3 +- src/main/resources/messages_de.properties | 3 +- src/main/resources/messages_en.properties | 3 +- src/main/resources/messages_fr.properties | 3 +- 9 files changed, 71 insertions(+), 16 deletions(-) diff --git a/pom.xml b/pom.xml index f421129..599f028 100644 --- a/pom.xml +++ b/pom.xml @@ -23,6 +23,7 @@ 2.15.3 32.1.3-jre 1.4.11 + 11.2.1 5.10.0 5.6.0 @@ -54,6 +55,11 @@ logback-classic ${logback.version} + + com.github.lgooddatepicker + LGoodDatePicker + ${LGoodDatePicker.version} + org.junit.jupiter diff --git a/src/main/java/com/github/thibstars/btsd/desktop/liveboard/LiveBoardController.java b/src/main/java/com/github/thibstars/btsd/desktop/liveboard/LiveBoardController.java index 537c73e..86f0c1a 100644 --- a/src/main/java/com/github/thibstars/btsd/desktop/liveboard/LiveBoardController.java +++ b/src/main/java/com/github/thibstars/btsd/desktop/liveboard/LiveBoardController.java @@ -5,6 +5,7 @@ import com.github.thibstars.btsd.irail.client.LiveBoardService; import java.awt.Dimension; import java.time.LocalDateTime; +import java.time.LocalTime; import java.time.format.DateTimeFormatter; import javax.swing.JOptionPane; import javax.swing.SwingUtilities; @@ -53,6 +54,14 @@ protected void refreshLiveBoard(LiveBoardPanel liveBoardPanel, String id) { ); } + public void refreshLiveBoard(LiveBoardPanel liveBoardPanel, String id, LocalTime localTime) { + liveBoardService.getForStation(id, i18NController.getPreferredLocale().getLanguage(), localTime) + .ifPresentOrElse( + liveBoardPanel::update, + () -> LOGGER.warn("Unable to refresh live board.") + ); + } + protected String getMessage(String key) { return i18NController.getMessage(key); } diff --git a/src/main/java/com/github/thibstars/btsd/desktop/liveboard/LiveBoardPanel.java b/src/main/java/com/github/thibstars/btsd/desktop/liveboard/LiveBoardPanel.java index 928e152..8f050d1 100644 --- a/src/main/java/com/github/thibstars/btsd/desktop/liveboard/LiveBoardPanel.java +++ b/src/main/java/com/github/thibstars/btsd/desktop/liveboard/LiveBoardPanel.java @@ -1,5 +1,6 @@ package com.github.thibstars.btsd.desktop.liveboard; +import com.github.lgooddatepicker.components.TimePicker; import com.github.thibstars.btsd.desktop.components.CaptionedLabel; import com.github.thibstars.btsd.desktop.components.RefreshPanel; import com.github.thibstars.btsd.desktop.i18n.I18NController; @@ -15,6 +16,7 @@ import java.awt.BorderLayout; import java.lang.reflect.Field; import java.time.LocalDateTime; +import java.time.LocalTime; import java.time.ZoneId; import java.util.Arrays; import java.util.Date; @@ -24,6 +26,7 @@ import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.ScrollPaneConstants; +import javax.swing.SpinnerNumberModel; import javax.swing.border.EmptyBorder; import javax.swing.table.DefaultTableModel; @@ -42,6 +45,8 @@ public class LiveBoardPanel extends JPanel implements LocaleChangeListener { private final RefreshPanel pnlRefresh; + private final TimePicker timePicker; + private final CaptionedLabel lblDepartureNumber; private final DefaultTableModel departuresModel; @@ -58,7 +63,19 @@ public LiveBoardPanel(LiveBoardController liveBoardController, LiveBoard liveBoa this.pnlRefresh = new RefreshPanel(liveBoardFrame); pnlRefresh.setLastRefresh(liveBoardController.formatDateTime(LocalDateTime.now())); - pnlRefresh.addRefreshListener(actionEvent -> liveBoardController.refreshLiveBoard(this, liveBoard.stationInfo().id())); + + SpinnerNumberModel hourMinuteModel = new SpinnerNumberModel(); + hourMinuteModel.setMinimum(0); + hourMinuteModel.setMaximum(2359); + LocalDateTime now = LocalDateTime.now(); + hourMinuteModel.setValue(Integer.valueOf(now.getHour() + "" + now.getMinute())); + + this.timePicker = new TimePicker(); + + timePicker.setTime(LocalTime.now()); + timePicker.addTimeChangeListener(event -> liveBoardController.refreshLiveBoard(this, liveBoard.stationInfo().id(), event.getNewTime())); + timePicker.setToolTipText(liveBoardController.getMessage("live.board.time.picker.tooltip")); + pnlRefresh.addRefreshListener(actionEvent -> liveBoardController.refreshLiveBoard(this, liveBoard.stationInfo().id(), timePicker.getTime())); JPanel pnlContent = new JPanel(new BorderLayout()); Departures departures = liveBoard.departures(); @@ -67,6 +84,7 @@ public LiveBoardPanel(LiveBoardController liveBoardController, LiveBoard liveBoa JPanel pnlTop = new JPanel(); pnlTop.add(lblDepartureNumber); pnlTop.add(pnlRefresh); + pnlTop.add(timePicker); pnlContent.add(pnlTop, BorderLayout.LINE_START); this.tblDepartures = new JTable(); @@ -122,12 +140,13 @@ private void setDepartures(Departures departures) { public void localeChanged(Locale locale, I18NController i18NController) { pnlStation.localeChanged(locale, i18NController); pnlRefresh.localeChanged(locale, i18NController); - departuresModel.setColumnIdentifiers( - Arrays.stream(Departure.class.getDeclaredFields()) - .map(Field::getName) - .map(fieldName -> i18NController.getMessage("live.board.departures." + fieldName)) - .toArray() - ); + timePicker.setToolTipText(i18NController.getMessage("live.board.time.picker.tooltip")); + departuresModel.setColumnIdentifiers( + Arrays.stream(Departure.class.getDeclaredFields()) + .map(Field::getName) + .map(fieldName -> i18NController.getMessage("live.board.departures." + fieldName)) + .toArray() + ); lblDepartureNumber.setCaption(i18NController.getMessage("live.board.departures")); } diff --git a/src/main/java/com/github/thibstars/btsd/irail/client/LiveBoardService.java b/src/main/java/com/github/thibstars/btsd/irail/client/LiveBoardService.java index b831fc3..8c76358 100644 --- a/src/main/java/com/github/thibstars/btsd/irail/client/LiveBoardService.java +++ b/src/main/java/com/github/thibstars/btsd/irail/client/LiveBoardService.java @@ -1,6 +1,7 @@ package com.github.thibstars.btsd.irail.client; import com.github.thibstars.btsd.irail.model.LiveBoard; +import java.time.LocalTime; import java.util.Optional; /** @@ -16,4 +17,14 @@ public interface LiveBoardService { * @return a Live Board for a given Station */ Optional getForStation(String id, String language); + + /** + * Retrieves a Live Board for a given Station at a specified time. + * + * @param id the Station's id + * @param language the language to use + * @param localTime the time to use + * @return a Live Board for a given Station at a specified time + */ + Optional getForStation(String id, String language, LocalTime localTime); } diff --git a/src/main/java/com/github/thibstars/btsd/irail/client/LiveBoardServiceImpl.java b/src/main/java/com/github/thibstars/btsd/irail/client/LiveBoardServiceImpl.java index e505c03..c33e2c8 100644 --- a/src/main/java/com/github/thibstars/btsd/irail/client/LiveBoardServiceImpl.java +++ b/src/main/java/com/github/thibstars/btsd/irail/client/LiveBoardServiceImpl.java @@ -5,7 +5,7 @@ import com.github.thibstars.btsd.irail.helper.LanguageService; import com.github.thibstars.btsd.irail.model.LiveBoard; import java.io.IOException; -import java.time.LocalDateTime; +import java.time.LocalTime; import java.time.format.DateTimeFormatter; import java.util.Objects; import java.util.Optional; @@ -46,8 +46,13 @@ public LiveBoardServiceImpl(OkHttpClient client, ObjectMapper objectMapper, Lang @Override public Optional getForStation(String id, String language) { + return getForStation(id, language, LocalTime.now()); + } + + @Override + public Optional getForStation(String id, String language, LocalTime localTime) { try { - LiveBoard liveBoard = fetchLiveBoard(id, language); + LiveBoard liveBoard = fetchLiveBoard(id, language, localTime); if (Stream.of(liveBoard.station(), liveBoard.stationInfo(), liveBoard.departures()) .allMatch(Objects::isNull)) { @@ -62,10 +67,11 @@ public Optional getForStation(String id, String language) { } } - private LiveBoard fetchLiveBoard(String id, String language) throws IOException { - LOGGER.info("Fetching live board for station: {}", id); + private LiveBoard fetchLiveBoard(String id, String language, LocalTime localTime) throws IOException { + LOGGER.info("Fetching live board for station: {} and local time: {}", id, localTime); - String timeString = LocalDateTime.now() + String timeString = Optional.ofNullable(localTime) + .orElse(LocalTime.now()) .format(DateTimeFormatter.ofPattern("HHmm")); Request request = new Request.Builder() diff --git a/src/main/resources/messages.properties b/src/main/resources/messages.properties index 6225712..857ec31 100644 --- a/src/main/resources/messages.properties +++ b/src/main/resources/messages.properties @@ -40,4 +40,5 @@ live.board.warning.failed.fetch.title=Unable to retrieve live board refresh.last.refresh=Last refresh refresh.refresh=Refresh refresh.automatic=Automatic refresh -refresh.automatic.tooltip=Toggles automatic refresh \ No newline at end of file +refresh.automatic.tooltip=Toggles automatic refresh +live.board.time.picker.tooltip=Fetch data for a specific time. \ No newline at end of file diff --git a/src/main/resources/messages_de.properties b/src/main/resources/messages_de.properties index 8ab86ee..73f65ac 100644 --- a/src/main/resources/messages_de.properties +++ b/src/main/resources/messages_de.properties @@ -40,4 +40,5 @@ live.board.warning.failed.fetch.message=Das Live-Board konnte nicht abgerufen we live.board.warning.failed.fetch.title=Live-Board kann nicht abgerufen werden refresh.refresh=Aktualisierung refresh.automatic.tooltip=Schaltet die automatische Aktualisierung um -refresh.automatic=Automatische Aktualisierung \ No newline at end of file +refresh.automatic=Automatische Aktualisierung +live.board.time.picker.tooltip=Daten für eine bestimmte Zeit abrufen. \ No newline at end of file diff --git a/src/main/resources/messages_en.properties b/src/main/resources/messages_en.properties index d45793a..b0b64ec 100644 --- a/src/main/resources/messages_en.properties +++ b/src/main/resources/messages_en.properties @@ -40,4 +40,5 @@ live.board.warning.failed.fetch.message=Could not retrieve live board. Try again live.board.warning.failed.fetch.title=Unable to retrieve live board refresh.refresh=Refresh refresh.automatic=Automatic refresh -refresh.automatic.tooltip=Toggles automatic refresh \ No newline at end of file +refresh.automatic.tooltip=Toggles automatic refresh +live.board.time.picker.tooltip=Fetch data for a specific time. \ No newline at end of file diff --git a/src/main/resources/messages_fr.properties b/src/main/resources/messages_fr.properties index b472003..a71b142 100644 --- a/src/main/resources/messages_fr.properties +++ b/src/main/resources/messages_fr.properties @@ -42,4 +42,5 @@ Impossible de récupérer le tableau en direct. Réessayez plus tard. live.board.warning.failed.fetch.title=Impossible de récupérer le tableau en direct refresh.refresh=Rafraîchir refresh.automatic.tooltip=Active/désactive l'actualisation automatique -refresh.automatic=Actualisation automatique \ No newline at end of file +refresh.automatic=Actualisation automatique +live.board.time.picker.tooltip=Récupérer des données pour une heure spécifique. \ No newline at end of file From b2f99addd190d97078a9e4f12710a8ea1072d356 Mon Sep 17 00:00:00 2001 From: Thibstars Date: Mon, 20 Nov 2023 22:31:22 +0100 Subject: [PATCH 3/3] Force auto refresh on current time --- .../thibstars/btsd/desktop/liveboard/LiveBoardPanel.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/github/thibstars/btsd/desktop/liveboard/LiveBoardPanel.java b/src/main/java/com/github/thibstars/btsd/desktop/liveboard/LiveBoardPanel.java index 8f050d1..a47f4f2 100644 --- a/src/main/java/com/github/thibstars/btsd/desktop/liveboard/LiveBoardPanel.java +++ b/src/main/java/com/github/thibstars/btsd/desktop/liveboard/LiveBoardPanel.java @@ -75,7 +75,11 @@ public LiveBoardPanel(LiveBoardController liveBoardController, LiveBoard liveBoa timePicker.setTime(LocalTime.now()); timePicker.addTimeChangeListener(event -> liveBoardController.refreshLiveBoard(this, liveBoard.stationInfo().id(), event.getNewTime())); timePicker.setToolTipText(liveBoardController.getMessage("live.board.time.picker.tooltip")); - pnlRefresh.addRefreshListener(actionEvent -> liveBoardController.refreshLiveBoard(this, liveBoard.stationInfo().id(), timePicker.getTime())); + pnlRefresh.addRefreshListener(actionEvent -> { + LocalTime rightNow = LocalTime.now(); + timePicker.setTime(rightNow); + liveBoardController.refreshLiveBoard(this, liveBoard.stationInfo().id(), rightNow); + }); JPanel pnlContent = new JPanel(new BorderLayout()); Departures departures = liveBoard.departures();