Skip to content

Commit

Permalink
[sonyprojector] Allow translation of exception messages that can be d…
Browse files Browse the repository at this point in the history
…isplayed in MainUI

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
  • Loading branch information
lolodomo committed Oct 16, 2021
1 parent c6a89be commit 90312b9
Show file tree
Hide file tree
Showing 11 changed files with 193 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,15 @@
*/
package org.openhab.binding.sonyprojector.internal;

import java.util.Locale;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.i18n.LocaleProvider;
import org.openhab.core.i18n.TranslationProvider;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;
import org.slf4j.LoggerFactory;

/**
* The {@link SonyProjectorException} class is used for any exception thrown by the binding
Expand All @@ -24,12 +31,59 @@
public class SonyProjectorException extends Exception {
private static final long serialVersionUID = 1L;

// Parameterless Constructor
public SonyProjectorException() {
}
private @Nullable TranslationProvider i18nProvider;
private @Nullable LocaleProvider localeProvider;
private @Nullable String messageKey;
private @Nullable Object param1;

// Constructor that accepts a message
public SonyProjectorException(@Nullable String message) {
public SonyProjectorException(String message) {
super(message);
}

public SonyProjectorException(String message, Throwable cause) {
super(message, cause);
}

public SonyProjectorException(TranslationProvider i18nProvider, LocaleProvider localeProvider, String message,
@Nullable Object... params) {
super(getLocalizedText(i18nProvider, Locale.ENGLISH, message, params));
this.i18nProvider = i18nProvider;
this.localeProvider = localeProvider;
this.messageKey = message;
this.param1 = params.length > 0 ? params[0] : null;
}

public SonyProjectorException(Throwable cause, TranslationProvider i18nProvider, LocaleProvider localeProvider,
String message, @Nullable Object... params) {
super(getLocalizedText(i18nProvider, Locale.ENGLISH, message, params), cause);
this.i18nProvider = i18nProvider;
this.localeProvider = localeProvider;
this.messageKey = message;
this.param1 = params.length > 0 ? params[0] : null;
}

@Override
public @Nullable String getLocalizedMessage() {
TranslationProvider translationProvider = i18nProvider;
LocaleProvider theLocaleProvider = localeProvider;
String key = messageKey;
if (translationProvider == null || theLocaleProvider == null || key == null) {
return getMessage();
} else {
return getLocalizedText(translationProvider, theLocaleProvider.getLocale(), key, param1);
}
}

private static String getLocalizedText(TranslationProvider i18nProvider, Locale locale, String key,
@Nullable Object... arguments) {
String result = null;
try {
Bundle bundle = FrameworkUtil.getBundle(SonyProjectorException.class).getBundleContext().getBundle();
String defaultText = i18nProvider.getText(bundle, key, key, Locale.ENGLISH, arguments);
result = i18nProvider.getText(bundle, key, defaultText, locale, arguments);
} catch (IllegalArgumentException e) {
LoggerFactory.getLogger(SonyProjectorException.class).warn("Can't load text for key {}", key);
}
return result != null ? result : key;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.sonyprojector.internal.handler.SonyProjectorHandler;
import org.openhab.core.i18n.LocaleProvider;
import org.openhab.core.i18n.TranslationProvider;
import org.openhab.core.io.transport.serial.SerialPortManager;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingTypeUID;
Expand Down Expand Up @@ -49,12 +51,17 @@ public class SonyProjectorHandlerFactory extends BaseThingHandlerFactory {
private final SerialPortManager serialPortManager;

private final SonyProjectorStateDescriptionOptionProvider stateDescriptionProvider;
private final TranslationProvider i18nProvider;
private final LocaleProvider localeProvider;

@Activate
public SonyProjectorHandlerFactory(final @Reference SerialPortManager serialPortManager,
final @Reference SonyProjectorStateDescriptionOptionProvider stateDescriptionProvider) {
final @Reference SonyProjectorStateDescriptionOptionProvider stateDescriptionProvider,
final @Reference TranslationProvider i18nProvider, final @Reference LocaleProvider localeProvider) {
this.serialPortManager = serialPortManager;
this.stateDescriptionProvider = stateDescriptionProvider;
this.i18nProvider = i18nProvider;
this.localeProvider = localeProvider;
}

@Override
Expand All @@ -67,7 +74,8 @@ public boolean supportsThingType(ThingTypeUID thingTypeUID) {
ThingTypeUID thingTypeUID = thing.getThingTypeUID();

if (SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID)) {
return new SonyProjectorHandler(thing, stateDescriptionProvider, serialPortManager);
return new SonyProjectorHandler(thing, stateDescriptionProvider, serialPortManager, i18nProvider,
localeProvider);
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.sonyprojector.internal.SonyProjectorException;
import org.openhab.binding.sonyprojector.internal.SonyProjectorModel;
import org.openhab.core.i18n.LocaleProvider;
import org.openhab.core.i18n.TranslationProvider;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.util.HexUtils;
import org.slf4j.Logger;
Expand Down Expand Up @@ -48,6 +50,9 @@ public abstract class SonyProjectorConnector {
private static final byte[] XVCOLOR_ON = new byte[] { 0x00, 0x01 };
private static final byte[] XVCOLOR_OFF = new byte[] { 0x00, 0x00 };

protected final TranslationProvider i18nProvider;
protected final LocaleProvider localeProvider;

private SonyProjectorModel model;

/** The output stream */
Expand All @@ -65,10 +70,15 @@ public abstract class SonyProjectorConnector {
* Constructor
*
* @param model the projector model in use
* @param i18nProvider the translation provider
* @param localeProvider the locale provider to get the language
* @param simu whether the communication is simulated or real
*/
public SonyProjectorConnector(SonyProjectorModel model, boolean simu) {
public SonyProjectorConnector(SonyProjectorModel model, TranslationProvider i18nProvider,
LocaleProvider localeProvider, boolean simu) {
this.model = model;
this.i18nProvider = i18nProvider;
this.localeProvider = localeProvider;
this.simu = simu;
}

Expand Down Expand Up @@ -899,7 +909,7 @@ protected byte[] getSetting(SonyProjectorItem item) throws SonyProjectorExceptio
return result;
} catch (SonyProjectorException e) {
logger.debug("Get setting {} failed: {}", item.getName(), e.getMessage());
throw new SonyProjectorException("Get setting " + item.getName() + " failed: " + e.getMessage());
throw new SonyProjectorException("Get setting " + item.getName() + " failed", e);
}
}

Expand All @@ -918,7 +928,7 @@ private void setSetting(SonyProjectorItem item, byte[] data) throws SonyProjecto
executeCommand(item, false, data);
} catch (SonyProjectorException e) {
logger.debug("Set setting {} failed: {}", item.getName(), e.getMessage());
throw new SonyProjectorException("Set setting " + item.getName() + " failed: " + e.getMessage());
throw new SonyProjectorException("Set setting " + item.getName() + " failed", e);
}

logger.debug("Set setting {} succeeded", item.getName());
Expand Down Expand Up @@ -952,11 +962,11 @@ private synchronized void sendIR(SonyProjectorItem item) throws SonyProjectorExc
}
} catch (SonyProjectorException e) {
logger.debug("Send IR {} failed: {}", item.getName(), e.getMessage());
throw new SonyProjectorException("Send IR " + item.getName() + " failed: " + e.getMessage());
throw new SonyProjectorException("Send IR " + item.getName() + " failed", e);
} catch (InterruptedException e) {
logger.debug("Send IR {} interrupted: {}", item.getName(), e.getMessage());
Thread.currentThread().interrupt();
throw new SonyProjectorException("Send IR " + item.getName() + " interrupted: " + e.getMessage());
throw new SonyProjectorException("Send IR " + item.getName() + " interrupted", e);
}

logger.debug("Send IR {} succeeded", item.getName());
Expand All @@ -975,28 +985,24 @@ private synchronized void sendIR(SonyProjectorItem item) throws SonyProjectorExc
*/
private synchronized byte[] executeCommand(SonyProjectorItem item, boolean getCommand, byte[] data)
throws SonyProjectorException {
try {
boolean runningSession = connected;
boolean runningSession = connected;

open();
open();

// Build the message and send it
writeCommand(buildMessage(item, getCommand, data));
// Build the message and send it
writeCommand(buildMessage(item, getCommand, data));

// Read the response
byte[] responseMessage = readResponse();
// Read the response
byte[] responseMessage = readResponse();

if (!runningSession) {
close();
}
if (!runningSession) {
close();
}

// Validate the content of the response
validateResponse(responseMessage, item);
// Validate the content of the response
validateResponse(responseMessage, item);

return responseMessage;
} catch (SonyProjectorException e) {
throw new SonyProjectorException(e.getMessage());
}
return responseMessage;
}

/**
Expand Down Expand Up @@ -1065,7 +1071,7 @@ protected int readInput(byte[] dataBuffer) throws SonyProjectorException {
return dataIn.read(dataBuffer);
} catch (IOException e) {
logger.debug("readInput failed: {}", e.getMessage());
throw new SonyProjectorException("readInput failed: " + e.getMessage());
throw new SonyProjectorException("readInput failed", e);
}
}

Expand All @@ -1090,7 +1096,7 @@ protected void writeCommand(byte[] message) throws SonyProjectorException {
dataOut.flush();
} catch (IOException e) {
logger.debug("writeCommand failed: {}", e.getMessage());
throw new SonyProjectorException("writeCommand failed: " + e.getMessage());
throw new SonyProjectorException("writeCommand failed", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import org.openhab.binding.sonyprojector.internal.SonyProjectorModel;
import org.openhab.binding.sonyprojector.internal.communication.SonyProjectorConnector;
import org.openhab.binding.sonyprojector.internal.communication.SonyProjectorItem;
import org.openhab.core.i18n.LocaleProvider;
import org.openhab.core.i18n.TranslationProvider;
import org.openhab.core.util.HexUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -68,10 +70,12 @@ public class SonyProjectorSdcpConnector extends SonyProjectorConnector {
* @param port the TCP port to be used
* @param community the community name of the equipment
* @param model the projector model in use
* @param i18nProvider the translation provider
* @param localeProvider the locale provider to get the language
*/
public SonyProjectorSdcpConnector(String address, @Nullable Integer port, @Nullable String community,
SonyProjectorModel model) {
this(address, port, community, model, false);
SonyProjectorModel model, TranslationProvider i18nProvider, LocaleProvider localeProvider) {
this(address, port, community, model, i18nProvider, localeProvider, false);
}

/**
Expand All @@ -81,11 +85,13 @@ public SonyProjectorSdcpConnector(String address, @Nullable Integer port, @Nulla
* @param port the TCP port to be used
* @param community the community name of the equipment
* @param model the projector model in use
* @param i18nProvider the translation provider
* @param localeProvider the locale provider to get the language
* @param simu whether the communication is simulated or real
*/
protected SonyProjectorSdcpConnector(String address, @Nullable Integer port, @Nullable String community,
SonyProjectorModel model, boolean simu) {
super(model, false);
SonyProjectorModel model, TranslationProvider i18nProvider, LocaleProvider localeProvider, boolean simu) {
super(model, i18nProvider, localeProvider, false);

this.address = address;

Expand Down Expand Up @@ -130,8 +136,8 @@ public synchronized void open() throws SonyProjectorException {

logger.debug("SDCP connection opened");
} catch (IOException | SecurityException | IllegalArgumentException e) {
logger.debug("Opening SDCP connection failed: {}", e.getMessage());
throw new SonyProjectorException("Opening SDCP connection failed: " + e.getMessage());
throw new SonyProjectorException(e, i18nProvider, localeProvider,
"exception.opening-sdcp-connection-failed");
}
}
}
Expand Down Expand Up @@ -197,7 +203,7 @@ protected int readInput(byte[] dataBuffer) throws SonyProjectorException {
return 0;
} catch (IOException e) {
logger.debug("readInput failed: {}", e.getMessage());
throw new SonyProjectorException("readInput failed: " + e.getMessage());
throw new SonyProjectorException("readInput failed", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import org.openhab.binding.sonyprojector.internal.SonyProjectorException;
import org.openhab.binding.sonyprojector.internal.SonyProjectorModel;
import org.openhab.binding.sonyprojector.internal.communication.SonyProjectorItem;
import org.openhab.core.i18n.LocaleProvider;
import org.openhab.core.i18n.TranslationProvider;
import org.openhab.core.util.HexUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -38,9 +40,12 @@ public class SonyProjectorSdcpSimuConnector extends SonyProjectorSdcpConnector {
* Constructor
*
* @param model the projector model in use
* @param i18nProvider the translation provider
* @param localeProvider the locale provider to get the language
*/
public SonyProjectorSdcpSimuConnector(SonyProjectorModel model) {
super("127.0.0.1", null, null, model, true);
public SonyProjectorSdcpSimuConnector(SonyProjectorModel model, TranslationProvider i18nProvider,
LocaleProvider localeProvider) {
super("127.0.0.1", null, null, model, i18nProvider, localeProvider, true);
}

@Override
Expand Down
Loading

0 comments on commit 90312b9

Please sign in to comment.