Skip to content

Commit

Permalink
[remoteopenhab] Add all default translations to properties file (open…
Browse files Browse the repository at this point in the history
…hab#11373)

Allows translating the remote openHAB binding strings with Crowdin.

Signed-off-by: Laurent Garnier <[email protected]>
  • Loading branch information
lolodomo authored and Nemer_Daud committed Jan 28, 2022
1 parent 5ea2245 commit 0fb5310
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import org.openhab.binding.remoteopenhab.internal.handler.RemoteopenhabBridgeHandler;
import org.openhab.binding.remoteopenhab.internal.handler.RemoteopenhabThingHandler;
import org.openhab.core.config.core.Configuration;
import org.openhab.core.i18n.LocaleProvider;
import org.openhab.core.i18n.TranslationProvider;
import org.openhab.core.io.net.http.HttpClientFactory;
import org.openhab.core.thing.Bridge;
import org.openhab.core.thing.Thing;
Expand Down Expand Up @@ -77,6 +79,8 @@ public class RemoteopenhabHandlerFactory extends BaseThingHandlerFactory {
private final RemoteopenhabStateDescriptionOptionProvider stateDescriptionProvider;
private final RemoteopenhabCommandDescriptionOptionProvider commandDescriptionProvider;
private final Gson jsonParser;
private final TranslationProvider i18nProvider;
private final LocaleProvider localeProvider;

private HttpClient httpClientTrustingCert;

Expand All @@ -85,7 +89,8 @@ public RemoteopenhabHandlerFactory(final @Reference HttpClientFactory httpClient
final @Reference ClientBuilder clientBuilder, final @Reference SseEventSourceFactory eventSourceFactory,
final @Reference RemoteopenhabChannelTypeProvider channelTypeProvider,
final @Reference RemoteopenhabStateDescriptionOptionProvider stateDescriptionProvider,
final @Reference RemoteopenhabCommandDescriptionOptionProvider commandDescriptionProvider) {
final @Reference RemoteopenhabCommandDescriptionOptionProvider commandDescriptionProvider,
final @Reference TranslationProvider i18nProvider, final @Reference LocaleProvider localeProvider) {
this.httpClient = httpClientFactory.getCommonHttpClient();
this.httpClientTrustingCert = httpClientFactory.createHttpClient(RemoteopenhabBindingConstants.BINDING_ID);
this.clientBuilder = clientBuilder;
Expand All @@ -94,6 +99,8 @@ public RemoteopenhabHandlerFactory(final @Reference HttpClientFactory httpClient
this.stateDescriptionProvider = stateDescriptionProvider;
this.commandDescriptionProvider = commandDescriptionProvider;
this.jsonParser = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.IDENTITY).create();
this.i18nProvider = i18nProvider;
this.localeProvider = localeProvider;

try {
SSLContext sslContext = SSLContext.getInstance("SSL");
Expand Down Expand Up @@ -200,7 +207,7 @@ public boolean supportsThingType(ThingTypeUID thingTypeUID) {
if (thingTypeUID.equals(RemoteopenhabBindingConstants.BRIDGE_TYPE_SERVER)) {
return new RemoteopenhabBridgeHandler((Bridge) thing, httpClient, httpClientTrustingCert, clientBuilder,
eventSourceFactory, channelTypeProvider, stateDescriptionProvider, commandDescriptionProvider,
jsonParser);
jsonParser, i18nProvider, localeProvider);
} else if (RemoteopenhabBindingConstants.SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
return new RemoteopenhabThingHandler(thing);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,10 @@ private boolean matchLocalIpAddress(String ipAddress) {
String restPath = service.getPropertyString("uri");
ThingUID thingUID = getThingUID(service);
if (thingUID != null && ip != null && restPath != null) {
String label = "openHAB server";
logger.debug("Create a DiscoveryResult for remote openHAB server {} with IP {}", thingUID, ip);
Map<String, Object> properties = Map.of(HOST, ip, REST_PATH, restPath);
result = DiscoveryResultBuilder.create(thingUID).withProperties(properties).withRepresentationProperty(HOST)
.withLabel(label).build();
.withLabel("@text/discovery.server.label").build();
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ protected void startScan() {
createDiscoveryResult(thing, bridgeUID);
}
} catch (RemoteopenhabException e) {
logger.debug("{}", e.getMessage());
logger.debug("Scan for remote things failed", e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.i18n.AbstractI18nException;

/**
* Exceptions thrown by this binding.
Expand All @@ -22,17 +23,17 @@
*/
@NonNullByDefault
@SuppressWarnings("serial")
public class RemoteopenhabException extends Exception {
public class RemoteopenhabException extends AbstractI18nException {

public RemoteopenhabException(@Nullable String message) {
super(message);
public RemoteopenhabException(Throwable cause) {
super(cause);
}

public RemoteopenhabException(@Nullable String message, @Nullable Throwable cause) {
super(message, cause);
public RemoteopenhabException(String message, @Nullable Object @Nullable... msgParams) {
super(message, msgParams);
}

public RemoteopenhabException(@Nullable Throwable cause) {
super(cause);
public RemoteopenhabException(String message, Throwable cause, @Nullable Object @Nullable... msgParams) {
super(message, cause, msgParams);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
import org.openhab.binding.remoteopenhab.internal.listener.RemoteopenhabItemsDataListener;
import org.openhab.binding.remoteopenhab.internal.listener.RemoteopenhabStreamingDataListener;
import org.openhab.binding.remoteopenhab.internal.rest.RemoteopenhabRestClient;
import org.openhab.core.i18n.LocaleProvider;
import org.openhab.core.i18n.TranslationProvider;
import org.openhab.core.library.CoreItemFactory;
import org.openhab.core.library.types.DateTimeType;
import org.openhab.core.library.types.DecimalType;
Expand Down Expand Up @@ -79,6 +81,8 @@
import org.openhab.core.types.StateOption;
import org.openhab.core.types.TypeParser;
import org.openhab.core.types.UnDefType;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;
import org.osgi.service.jaxrs.client.SseEventSourceFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -106,6 +110,9 @@ public class RemoteopenhabBridgeHandler extends BaseBridgeHandler
private final RemoteopenhabChannelTypeProvider channelTypeProvider;
private final RemoteopenhabStateDescriptionOptionProvider stateDescriptionProvider;
private final RemoteopenhabCommandDescriptionOptionProvider commandDescriptionProvider;
private final TranslationProvider i18nProvider;
private final LocaleProvider localeProvider;
private final Bundle bundle;

private final Object updateThingLock = new Object();

Expand All @@ -120,13 +127,18 @@ public RemoteopenhabBridgeHandler(Bridge bridge, HttpClient httpClient, HttpClie
ClientBuilder clientBuilder, SseEventSourceFactory eventSourceFactory,
RemoteopenhabChannelTypeProvider channelTypeProvider,
RemoteopenhabStateDescriptionOptionProvider stateDescriptionProvider,
RemoteopenhabCommandDescriptionOptionProvider commandDescriptionProvider, final Gson jsonParser) {
RemoteopenhabCommandDescriptionOptionProvider commandDescriptionProvider, final Gson jsonParser,
final TranslationProvider i18nProvider, final LocaleProvider localeProvider) {
super(bridge);
this.httpClientTrustingCert = httpClientTrustingCert;
this.channelTypeProvider = channelTypeProvider;
this.stateDescriptionProvider = stateDescriptionProvider;
this.commandDescriptionProvider = commandDescriptionProvider;
this.restClient = new RemoteopenhabRestClient(httpClient, clientBuilder, eventSourceFactory, jsonParser);
this.i18nProvider = i18nProvider;
this.localeProvider = localeProvider;
this.bundle = FrameworkUtil.getBundle(this.getClass());
this.restClient = new RemoteopenhabRestClient(httpClient, clientBuilder, eventSourceFactory, jsonParser,
i18nProvider);
}

@Override
Expand All @@ -138,21 +150,21 @@ public void initialize() {
String host = config.host.trim();
if (host.length() == 0) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"Undefined server address setting in the thing configuration");
"@text/offline.config-error-undefined-host");
return;
}
String path = config.restPath.trim();
if (path.length() == 0 || !path.startsWith("/")) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"Invalid REST API path setting in the thing configuration");
"@text/offline.config-error-invalid-rest-path");
return;
}
URL url;
try {
url = new URL(config.useHttps ? "https" : "http", host, config.port, path);
} catch (MalformedURLException e) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"Invalid REST URL built from the settings in the thing configuration");
"@text/offline.config-error-invalid-rest-url");
return;
}

Expand Down Expand Up @@ -201,7 +213,8 @@ public void handleCommand(ChannelUID channelUID, Command command) {
channelUID.getId());
}
} catch (RemoteopenhabException e) {
logger.debug("{}", e.getMessage());
logger.debug("Handling command for channel {} failed: {}", channelUID.getId(),
e.getMessage(bundle, i18nProvider));
}
}

Expand Down Expand Up @@ -235,12 +248,18 @@ private boolean createChannels(List<RemoteopenhabItem> items, boolean replace) {
ChannelType channelType = channelTypeProvider.getChannelType(itemType, readOnly, pattern);
String label;
String description;
String defaultValue;
if (channelType == null) {
channelTypeUID = channelTypeProvider.buildNewChannelTypeUID(itemType);
logger.trace("Create the channel type {} for item type {} ({} and with pattern {})",
channelTypeUID, itemType, readOnly ? "read only" : "read write", pattern);
label = String.format("Remote %s Item", itemType);
description = String.format("An item of type %s from the remote server.", itemType);
defaultValue = String.format("Remote %s Item", itemType);
label = i18nProvider.getText(bundle, "channel-type.label", defaultValue,
localeProvider.getLocale(), itemType);
label = label != null && !label.isBlank() ? label : defaultValue;
description = i18nProvider.getText(bundle, "channel-type.description", defaultValue,
localeProvider.getLocale(), itemType);
description = description != null && !description.isBlank() ? description : defaultValue;
StateDescriptionFragmentBuilder stateDescriptionBuilder = StateDescriptionFragmentBuilder
.create().withReadOnly(readOnly);
if (!pattern.isEmpty()) {
Expand All @@ -257,8 +276,13 @@ private boolean createChannels(List<RemoteopenhabItem> items, boolean replace) {
}
ChannelUID channelUID = new ChannelUID(getThing().getUID(), item.name);
logger.trace("Create the channel {} of type {}", channelUID, channelTypeUID);
label = "Item " + item.name;
description = String.format("Item %s from the remote server.", item.name);
defaultValue = String.format("Item %s", item.name);
label = i18nProvider.getText(bundle, "channel.label", defaultValue, localeProvider.getLocale(),
item.name);
label = label != null && !label.isBlank() ? label : defaultValue;
description = i18nProvider.getText(bundle, "channel.description", defaultValue,
localeProvider.getLocale(), item.name);
description = description != null && !description.isBlank() ? description : defaultValue;
channels.add(ChannelBuilder.create(channelUID, itemType).withType(channelTypeUID)
.withKind(ChannelKind.STATE).withLabel(label).withDescription(description).build());
}
Expand Down Expand Up @@ -356,7 +380,7 @@ public void checkConnection(boolean restartSse) {
restClient.tryApi();
if (restClient.getRestApiVersion() == null) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"OH 1.x server not supported by the binding");
"@text/offline.config-error-unsupported-server");
} else if (getThing().getStatus() != ThingStatus.ONLINE) {
List<RemoteopenhabItem> items = restClient.getRemoteItems("name,type,groupType,state,stateDescription");

Expand All @@ -370,17 +394,17 @@ public void checkConnection(boolean restartSse) {

restartStreamingUpdates();
} else {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE,
"Dynamic creation of the channels for the remote server items failed");
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE, "@text/offline.error-channels-creation");
stopStreamingUpdates();
}
} else if (restartSse) {
logger.debug("The SSE connection is restarted because there was no recent event received");
restartStreamingUpdates();
}
} catch (RemoteopenhabException e) {
logger.debug("{}", e.getMessage());
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
logger.debug("checkConnection for thing {} failed: {}", getThing().getUID(),
e.getMessage(bundle, i18nProvider), e);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getRawMessage());
stopStreamingUpdates();
}
}
Expand Down Expand Up @@ -454,12 +478,15 @@ public void onConnected() {

@Override
public void onDisconnected() {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Disconected from the remote server");
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"@text/offline.comm-error-disconnected");
}

@Override
public void onError(String message) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, message);
logger.debug("onError: {}", message);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"@text/offline.comm-error-receiving-events");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private void initializeThing(@Nullable ThingStatus bridgeStatus) {
String uid = getConfigThingUID();
if (uid.length() == 0) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"Undefined thing UID setting in the thing configuration");
"@text/offline.config-error-undefined-thing-uid");
} else {
RemoteopenhabRestClient client = ((RemoteopenhabBridgeHandler) bridgeHandler).gestRestClient();
client.addThingsDataListener(this);
Expand All @@ -114,8 +114,7 @@ private void initializeThing(@Nullable ThingStatus bridgeStatus) {
updateThingStatus(uid, statusInfo);
}
} catch (RemoteopenhabException e) {
logger.debug("{}", e.getMessage());
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getRawMessage());
}
});
}
Expand Down
Loading

0 comments on commit 0fb5310

Please sign in to comment.