From 1a0e335902e87f2092499cc24f4ad14399b722d5 Mon Sep 17 00:00:00 2001 From: clinique Date: Sat, 20 Nov 2021 13:19:49 +0100 Subject: [PATCH] Code review correctiong Added i18n data to get prepared for crowdin Signed-off-by: clinique --- bundles/org.openhab.binding.sncf/README.md | 15 +++++--- .../sncf/internal/SncfBindingConstants.java | 2 +- .../discovery/SncfDiscoveryService.java | 11 +++--- .../binding/sncf/internal/dto/Coord.java | 1 + .../binding/sncf/internal/dto/Route.java | 1 + .../binding/sncf/internal/dto/StopArea.java | 2 + .../sncf/internal/dto/StopDateTime.java | 1 + .../binding/sncf/internal/dto/StopPoint.java | 1 + .../internal/dto/VJDisplayInformation.java | 2 + .../internal/handler/SncfBridgeHandler.java | 22 +++++------ .../sncf/internal/handler/StationHandler.java | 10 ++++- .../main/resources/OH-INF/config/config.xml | 1 - .../resources/OH-INF/i18n/sncf.properties | 29 ++++++++++++++ .../main/resources/OH-INF/thing/bridge.xml | 1 - .../main/resources/OH-INF/thing/station.xml | 9 ----- pom.xml | 38 +++++++++++++++++++ 16 files changed, 107 insertions(+), 39 deletions(-) create mode 100644 bundles/org.openhab.binding.sncf/src/main/resources/OH-INF/i18n/sncf.properties diff --git a/bundles/org.openhab.binding.sncf/README.md b/bundles/org.openhab.binding.sncf/README.md index 110d025b6348e..338a1ac5cfff8 100644 --- a/bundles/org.openhab.binding.sncf/README.md +++ b/bundles/org.openhab.binding.sncf/README.md @@ -3,24 +3,27 @@ The SNCF binding provides real-time data for each train, bus, tramway... station in France. This is based on live API provided by DIGITALSNCF. -Get you API key at https://www.digital.sncf.com/startup/api/token-developpeur +Get your API key on [DIGITALSNCF web site](https://www.digital.sncf.com/startup/api/token-developpeur) -Note : SNCF Api is based on the open [API Navitia](https://doc.navitia.io/#getting-started). This binding uses a very small subset of it, restricted to its primary purpose. +Note : SNCF Api is based on the open [API Navitia](https://doc.navitia.io/#getting-started). +This binding uses a very small subset of it, restricted to its primary purpose. ## Supported Things -Bridge: The binding supports a bridge to connect to the [DIGITALSNCF service](https://www.digital.sncf.com/startup/api/token-developpeur). A bridge uses the thing ID "api". +Bridge: The binding supports a bridge to connect to the [DIGITALSNCF service](https://www.digital.sncf.com/startup/api/token developpeur). +A bridge uses the thing ID "api". Station: Represents a given bus, train station. -Of course, you can add multiple as many stations as needed. +Of course, you can add as many stations as needed. ## Discovery -This binding takes care of auto discovery. This method is strongly recommended as its the only way to get proper station ID depending upon transportation type. +This binding takes care of auto discovery. This method is strongly recommended as it is the only way to get proper station ID depending upon transportation type. -To enable auto-discovery, your location system setting must be defined. Once done, at first launch discovery will search every station in a radius of 2500 m around the system, extending it by step of 500 m until it finds a first set of results. +To enable auto-discovery, your location system setting must be defined. +Once done, at first launch, discovery will search every station in a radius of 2000 m around the system, extending it by step of 500 m until it finds a first set of results. Every following manual successive launch will extend this radius by 500 m, increasing the number of stations discovered. diff --git a/bundles/org.openhab.binding.sncf/src/main/java/org/openhab/binding/sncf/internal/SncfBindingConstants.java b/bundles/org.openhab.binding.sncf/src/main/java/org/openhab/binding/sncf/internal/SncfBindingConstants.java index 5ebac06e06893..36f237998f354 100644 --- a/bundles/org.openhab.binding.sncf/src/main/java/org/openhab/binding/sncf/internal/SncfBindingConstants.java +++ b/bundles/org.openhab.binding.sncf/src/main/java/org/openhab/binding/sncf/internal/SncfBindingConstants.java @@ -50,6 +50,6 @@ public class SncfBindingConstants { public static final ThingTypeUID APIBRIDGE_THING_TYPE = new ThingTypeUID(BINDING_ID, "api"); public static final ThingTypeUID STATION_THING_TYPE = new ThingTypeUID(BINDING_ID, "station"); - // List of all adressable things in OH = SUPPORTED_DEVICE_THING_TYPES_UIDS + the virtual bridge + // List of all adressable things public static final Set SUPPORTED_THING_TYPES_UIDS = Set.of(APIBRIDGE_THING_TYPE, STATION_THING_TYPE); } diff --git a/bundles/org.openhab.binding.sncf/src/main/java/org/openhab/binding/sncf/internal/discovery/SncfDiscoveryService.java b/bundles/org.openhab.binding.sncf/src/main/java/org/openhab/binding/sncf/internal/discovery/SncfDiscoveryService.java index 887d076440e19..b2a5ea4e1f849 100644 --- a/bundles/org.openhab.binding.sncf/src/main/java/org/openhab/binding/sncf/internal/discovery/SncfDiscoveryService.java +++ b/bundles/org.openhab.binding.sncf/src/main/java/org/openhab/binding/sncf/internal/discovery/SncfDiscoveryService.java @@ -42,10 +42,10 @@ @Component(service = ThingHandlerService.class) @NonNullByDefault public class SncfDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService { - private final Logger logger = LoggerFactory.getLogger(SncfDiscoveryService.class); - private static final int SEARCH_TIME = 2; + private final Logger logger = LoggerFactory.getLogger(SncfDiscoveryService.class); + private @Nullable LocationProvider locationProvider; private @Nullable SncfBridgeHandler bridgeHandler; @@ -68,7 +68,7 @@ public void startScan() { if (provider != null && handler != null) { PointType location = provider.getLocation(); if (location != null) { - ThingUID bridgeId = handler.getThing().getUID(); + ThingUID bridgeUID = handler.getThing().getUID(); searchRange += 500; try { List places = handler.discoverNearby(location, searchRange); @@ -77,8 +77,8 @@ public void startScan() { String placeId = place.id; String thingId = placeId.replace(":", "_").replace("-", "_").replace("stop_point_", ""); thingDiscovered( - DiscoveryResultBuilder.create(new ThingUID(STATION_THING_TYPE, bridgeId, thingId)) - .withLabel(place.stopPoint.name).withBridge(bridgeId) + DiscoveryResultBuilder.create(new ThingUID(STATION_THING_TYPE, bridgeUID, thingId)) + .withLabel(place.stopPoint.name).withBridge(bridgeUID) .withRepresentationProperty(STOP_POINT_ID) .withProperty(STOP_POINT_ID, placeId).build()); }); @@ -93,7 +93,6 @@ public void startScan() { logger.info("Please set a system location to enable station discovery"); } } - stopScan(); } @Override diff --git a/bundles/org.openhab.binding.sncf/src/main/java/org/openhab/binding/sncf/internal/dto/Coord.java b/bundles/org.openhab.binding.sncf/src/main/java/org/openhab/binding/sncf/internal/dto/Coord.java index 0e16bc337db64..7441c5c7f7b6d 100644 --- a/bundles/org.openhab.binding.sncf/src/main/java/org/openhab/binding/sncf/internal/dto/Coord.java +++ b/bundles/org.openhab.binding.sncf/src/main/java/org/openhab/binding/sncf/internal/dto/Coord.java @@ -13,6 +13,7 @@ package org.openhab.binding.sncf.internal.dto; /** + * The {@link Coord} class holds latitude and longitude of a point * * @author Gaël L'hopital - Initial contribution */ diff --git a/bundles/org.openhab.binding.sncf/src/main/java/org/openhab/binding/sncf/internal/dto/Route.java b/bundles/org.openhab.binding.sncf/src/main/java/org/openhab/binding/sncf/internal/dto/Route.java index d186384f51479..417b5e894f6fb 100644 --- a/bundles/org.openhab.binding.sncf/src/main/java/org/openhab/binding/sncf/internal/dto/Route.java +++ b/bundles/org.openhab.binding.sncf/src/main/java/org/openhab/binding/sncf/internal/dto/Route.java @@ -13,6 +13,7 @@ package org.openhab.binding.sncf.internal.dto; /** + * The {@link Route} class holds Route details * * @author Gaël L'hopital - Initial contribution */ diff --git a/bundles/org.openhab.binding.sncf/src/main/java/org/openhab/binding/sncf/internal/dto/StopArea.java b/bundles/org.openhab.binding.sncf/src/main/java/org/openhab/binding/sncf/internal/dto/StopArea.java index db503f2cc5893..706874a1f8536 100644 --- a/bundles/org.openhab.binding.sncf/src/main/java/org/openhab/binding/sncf/internal/dto/StopArea.java +++ b/bundles/org.openhab.binding.sncf/src/main/java/org/openhab/binding/sncf/internal/dto/StopArea.java @@ -13,6 +13,8 @@ package org.openhab.binding.sncf.internal.dto; /** + * The {@link StopArea} class holds informations for a Stop Area + * (usually a train station) * * @author Gaël L'hopital - Initial contribution */ diff --git a/bundles/org.openhab.binding.sncf/src/main/java/org/openhab/binding/sncf/internal/dto/StopDateTime.java b/bundles/org.openhab.binding.sncf/src/main/java/org/openhab/binding/sncf/internal/dto/StopDateTime.java index 8f7a3bbdc5ef4..0bcce5cd81bbf 100644 --- a/bundles/org.openhab.binding.sncf/src/main/java/org/openhab/binding/sncf/internal/dto/StopDateTime.java +++ b/bundles/org.openhab.binding.sncf/src/main/java/org/openhab/binding/sncf/internal/dto/StopDateTime.java @@ -13,6 +13,7 @@ package org.openhab.binding.sncf.internal.dto; /** + * The {@link StopDateTime} class holds informations for a transportation stop * * @author Gaël L'hopital - Initial contribution */ diff --git a/bundles/org.openhab.binding.sncf/src/main/java/org/openhab/binding/sncf/internal/dto/StopPoint.java b/bundles/org.openhab.binding.sncf/src/main/java/org/openhab/binding/sncf/internal/dto/StopPoint.java index c6649eb3e2df2..e372dc15e5629 100644 --- a/bundles/org.openhab.binding.sncf/src/main/java/org/openhab/binding/sncf/internal/dto/StopPoint.java +++ b/bundles/org.openhab.binding.sncf/src/main/java/org/openhab/binding/sncf/internal/dto/StopPoint.java @@ -13,6 +13,7 @@ package org.openhab.binding.sncf.internal.dto; /** + * The {@link StopPoint} class holds informations for a train station * * @author Gaël L'hopital - Initial contribution */ diff --git a/bundles/org.openhab.binding.sncf/src/main/java/org/openhab/binding/sncf/internal/dto/VJDisplayInformation.java b/bundles/org.openhab.binding.sncf/src/main/java/org/openhab/binding/sncf/internal/dto/VJDisplayInformation.java index b57895c6199cd..a3200c7d6f80f 100644 --- a/bundles/org.openhab.binding.sncf/src/main/java/org/openhab/binding/sncf/internal/dto/VJDisplayInformation.java +++ b/bundles/org.openhab.binding.sncf/src/main/java/org/openhab/binding/sncf/internal/dto/VJDisplayInformation.java @@ -13,6 +13,8 @@ package org.openhab.binding.sncf.internal.dto; /** + * The {@link VJDisplayInformation} class holds informations displayed + * to traveller regarding a stop in the station * * @author Gaël L'hopital - Initial contribution */ diff --git a/bundles/org.openhab.binding.sncf/src/main/java/org/openhab/binding/sncf/internal/handler/SncfBridgeHandler.java b/bundles/org.openhab.binding.sncf/src/main/java/org/openhab/binding/sncf/internal/handler/SncfBridgeHandler.java index 4e497d6d8255a..c92f39836c894 100644 --- a/bundles/org.openhab.binding.sncf/src/main/java/org/openhab/binding/sncf/internal/handler/SncfBridgeHandler.java +++ b/bundles/org.openhab.binding.sncf/src/main/java/org/openhab/binding/sncf/internal/handler/SncfBridgeHandler.java @@ -28,7 +28,6 @@ import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jetty.client.HttpClient; -import org.eclipse.jetty.client.HttpResponseException; import org.eclipse.jetty.client.api.ContentResponse; import org.openhab.binding.sncf.internal.SncfException; import org.openhab.binding.sncf.internal.discovery.SncfDiscoveryService; @@ -45,6 +44,7 @@ import org.openhab.core.thing.Bridge; import org.openhab.core.thing.ChannelUID; import org.openhab.core.thing.ThingStatus; +import org.openhab.core.thing.ThingStatusDetail; import org.openhab.core.thing.binding.BaseBridgeHandler; import org.openhab.core.thing.binding.ThingHandlerService; import org.openhab.core.types.Command; @@ -84,7 +84,11 @@ public SncfBridgeHandler(Bridge bridge, Gson gson, LocationProvider locationProv public void initialize() { logger.debug("Initializing SNCF API bridge handler."); apiId = (String) getConfig().get("apiID"); - updateStatus(ThingStatus.ONLINE); + if (apiId != null && apiId.length() != 0) { + updateStatus(ThingStatus.ONLINE); + } else { + updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "@text/null-or-empty-api-key"); + } } @Override @@ -130,16 +134,7 @@ private String getResponse(String url) throws SncfException { logger.debug("SNCF Api server responded with status code {}: {}", httpStatus, content); throw new SncfException(content); } - } catch (ExecutionException e) { - String errorMessage = e.getLocalizedMessage(); - logger.trace("Exception occurred during execution: {}", errorMessage, e); - if (e.getCause() instanceof HttpResponseException) { - logger.debug("SNCF Api server responded with status code {}: Invalid API key.", UNAUTHORIZED_401); - throw new SncfException("@text/offline.conf-error-invalid-apikey", e.getCause()); - } else { - throw new SncfException(errorMessage, e.getCause()); - } - } catch (TimeoutException e) { + } catch (TimeoutException | ExecutionException e) { logger.debug("Exception occurred during execution: {}", e.getLocalizedMessage(), e); throw new SncfException(e.getLocalizedMessage(), e.getCause()); } catch (InterruptedException e) { @@ -167,7 +162,8 @@ public Optional getNextPassage(String stopPointId, String expected) thr String URL = String.format(Locale.US, "%s/stop_points/%s/%s?disable_geojson=true&count=1", SERVICE_URL, stopPointId, expected); List passages = getResponseFromCache(URL, Passages.class).passages; - return passages != null ? Optional.ofNullable(passages.get(0)) : Optional.empty(); + return passages != null ? passages.size() > 0 ? Optional.ofNullable(passages.get(0)) : Optional.empty() + : Optional.empty(); } public LocationProvider getLocationProvider() { diff --git a/bundles/org.openhab.binding.sncf/src/main/java/org/openhab/binding/sncf/internal/handler/StationHandler.java b/bundles/org.openhab.binding.sncf/src/main/java/org/openhab/binding/sncf/internal/handler/StationHandler.java index 6d86f5691999e..e5830d78d2adf 100644 --- a/bundles/org.openhab.binding.sncf/src/main/java/org/openhab/binding/sncf/internal/handler/StationHandler.java +++ b/bundles/org.openhab.binding.sncf/src/main/java/org/openhab/binding/sncf/internal/handler/StationHandler.java @@ -130,7 +130,7 @@ private void queryApiAndUpdateChannels() { SncfBridgeHandler bridgeHandler = getBridgeHandler(); if (bridgeHandler != null) { try { - bridgeHandler.getNextPassage(stationId, GROUP_DEPARTURE).ifPresent(departure -> { + bridgeHandler.getNextPassage(stationId, GROUP_DEPARTURE).ifPresentOrElse(departure -> { getThing().getChannels().stream().map(Channel::getUID).filter( channelUID -> isLinked(channelUID) && GROUP_DEPARTURE.equals(channelUID.getGroupId())) .forEach(channelUID -> { @@ -138,8 +138,11 @@ private void queryApiAndUpdateChannels() { updateState(channelUID, state); }); scheduleRefresh(fromDTO(departure.stopDateTime.departureDateTime)); + }, () -> { + logger.debug("No {} available", GROUP_DEPARTURE); + scheduleRefresh(ZonedDateTime.now().plusMinutes(5)); }); - bridgeHandler.getNextPassage(stationId, GROUP_ARRIVAL).ifPresent(arrival -> { + bridgeHandler.getNextPassage(stationId, GROUP_ARRIVAL).ifPresentOrElse(arrival -> { getThing().getChannels().stream().map(Channel::getUID) .filter(channelUID -> isLinked(channelUID) && GROUP_ARRIVAL.equals(channelUID.getGroupId())) .forEach(channelUID -> { @@ -147,6 +150,9 @@ private void queryApiAndUpdateChannels() { updateState(channelUID, state); }); scheduleRefresh(fromDTO(arrival.stopDateTime.arrivalDateTime)); + }, () -> { + logger.debug("No {} available", GROUP_ARRIVAL); + scheduleRefresh(ZonedDateTime.now().plusMinutes(5)); }); updateStatus(ThingStatus.ONLINE); } catch (SncfException e) { diff --git a/bundles/org.openhab.binding.sncf/src/main/resources/OH-INF/config/config.xml b/bundles/org.openhab.binding.sncf/src/main/resources/OH-INF/config/config.xml index cbe4de3613431..13d5fd0a9692e 100644 --- a/bundles/org.openhab.binding.sncf/src/main/resources/OH-INF/config/config.xml +++ b/bundles/org.openhab.binding.sncf/src/main/resources/OH-INF/config/config.xml @@ -8,7 +8,6 @@ - Your Sncf API ID true password diff --git a/bundles/org.openhab.binding.sncf/src/main/resources/OH-INF/i18n/sncf.properties b/bundles/org.openhab.binding.sncf/src/main/resources/OH-INF/i18n/sncf.properties new file mode 100644 index 0000000000000..f19218090f870 --- /dev/null +++ b/bundles/org.openhab.binding.sncf/src/main/resources/OH-INF/i18n/sncf.properties @@ -0,0 +1,29 @@ + +binding.sncf.name = SNCF Binding +binding.sncf.description = Retrieves French railway informations + +config.thing-type.sncf.api.apiID.label = API ID +config.thing-type.sncf.api.apiID.description = Your Sncf API ID + +thing-type.sncf.api.label = SNCF API +thing-type.sncf.api.description = This bridge represents the gateway to Sncf API. + +thing-type.sncf.station.label = Station +thing-type.sncf.station.description = This represents a station hosting some transportation mode. + +channel-type.sncf.direction.label = Direction +channel-type.sncf.direction.description = The direction of this route. +channel-type.sncf.code.label = Code +channel-type.sncf.code.description = Code of the line. +channel-type.sncf.name.label = Name +channel-type.sncf.name.description = Name of the line. +channel-type.sncf.network.label = Network +channel-type.sncf.network.description = Name of the transportation network. +channel-type.sncf.commercialMode.label = Commercial Mode +channel-type.sncf.commercialMode.description = Commercial modes are close from physical modes, but not normalized and can refer to a brand, something that can be specific to a network, and known to the traveler. +channel-type.sncf.timestamp.label = Timestamp +channel-type.sncf.timestamp.description = Timestamp of the future event. + +# Error messages +null-or-empty-api-key = Null or empty API ID +error-invalid-apikey = Invalid API ID \ No newline at end of file diff --git a/bundles/org.openhab.binding.sncf/src/main/resources/OH-INF/thing/bridge.xml b/bundles/org.openhab.binding.sncf/src/main/resources/OH-INF/thing/bridge.xml index 7ef00e39c499b..ab99f53c25924 100644 --- a/bundles/org.openhab.binding.sncf/src/main/resources/OH-INF/thing/bridge.xml +++ b/bundles/org.openhab.binding.sncf/src/main/resources/OH-INF/thing/bridge.xml @@ -6,7 +6,6 @@ - This bridge represents the gateway to Sncf API. diff --git a/bundles/org.openhab.binding.sncf/src/main/resources/OH-INF/thing/station.xml b/bundles/org.openhab.binding.sncf/src/main/resources/OH-INF/thing/station.xml index 783d9bd506d15..727b8d12448cf 100644 --- a/bundles/org.openhab.binding.sncf/src/main/resources/OH-INF/thing/station.xml +++ b/bundles/org.openhab.binding.sncf/src/main/resources/OH-INF/thing/station.xml @@ -25,7 +25,6 @@ - Identifier of the station @@ -40,7 +39,6 @@ - Date et heure de l'évènement @@ -48,43 +46,36 @@ String - The direction of this route String - Code name of the line String - Name of the line String - Name of the network String - Commercial modes are close from physical modes, but not normalized and can refer to a brand, something - that can be specific to a network, and known to the traveler. DateTime - Data timestamp diff --git a/pom.xml b/pom.xml index a25687bcdb1fb..b6164c91e35c2 100644 --- a/pom.xml +++ b/pom.xml @@ -520,6 +520,44 @@ Import-Package: \\ + + org.codehaus.mojo + xml-maven-plugin + 1.0.2 + + + + transform + + + + + + + ${project.basedir}/src/main/resources/OH-INF/ + + + **/*.xml + + ${project.basedir}/src/main/resources/OH-INF/i18n + ${project.basedir}/../../tools/i18n/i18n.xsl + + + omit-xml-declaration + yes + + + + + properties + + + + + + + org.openhab.tools.sat sat-plugin