Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include time parameter in request #68

Merged
merged 3 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<jackson-annotations.version>2.15.3</jackson-annotations.version>
<guava.version>32.1.3-jre</guava.version>
<logback.version>1.4.11</logback.version>
<LGoodDatePicker.version>11.2.1</LGoodDatePicker.version>

<junit-jupiter.version>5.10.0</junit-jupiter.version>
<mockito-core.version>5.6.0</mockito-core.version>
Expand Down Expand Up @@ -54,6 +55,11 @@
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>com.github.lgooddatepicker</groupId>
<artifactId>LGoodDatePicker</artifactId>
<version>${LGoodDatePicker.version}</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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;

Expand All @@ -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;
Expand All @@ -58,7 +63,23 @@ 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 -> {
LocalTime rightNow = LocalTime.now();
timePicker.setTime(rightNow);
liveBoardController.refreshLiveBoard(this, liveBoard.stationInfo().id(), rightNow);
});

JPanel pnlContent = new JPanel(new BorderLayout());
Departures departures = liveBoard.departures();
Expand All @@ -67,6 +88,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();
Expand Down Expand Up @@ -122,12 +144,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"));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand All @@ -16,4 +17,14 @@ public interface LiveBoardService {
* @return a Live Board for a given Station
*/
Optional<LiveBoard> 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<LiveBoard> getForStation(String id, String language, LocalTime localTime);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Stream;
Expand All @@ -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;

Expand All @@ -42,8 +46,13 @@ public LiveBoardServiceImpl(OkHttpClient client, ObjectMapper objectMapper, Lang

@Override
public Optional<LiveBoard> getForStation(String id, String language) {
return getForStation(id, language, LocalTime.now());
}

@Override
public Optional<LiveBoard> 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)) {
Expand All @@ -58,11 +67,17 @@ public Optional<LiveBoard> 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 = Optional.ofNullable(localTime)
.orElse(LocalTime.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;
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
refresh.automatic.tooltip=Toggles automatic refresh
live.board.time.picker.tooltip=Fetch data for a specific time.
3 changes: 2 additions & 1 deletion src/main/resources/messages_de.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
refresh.automatic=Automatische Aktualisierung
live.board.time.picker.tooltip=Daten für eine bestimmte Zeit abrufen.
3 changes: 2 additions & 1 deletion src/main/resources/messages_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
refresh.automatic.tooltip=Toggles automatic refresh
live.board.time.picker.tooltip=Fetch data for a specific time.
3 changes: 2 additions & 1 deletion src/main/resources/messages_fr.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
refresh.automatic=Actualisation automatique
live.board.time.picker.tooltip=Récupérer des données pour une heure spécifique.
Loading