From 90312b9c3e0c5a0682ff6df8dca23e8b88ab4f88 Mon Sep 17 00:00:00 2001 From: Laurent Garnier Date: Sat, 16 Oct 2021 16:37:23 +0200 Subject: [PATCH] [sonyprojector] Allow translation of exception messages that can be displayed in MainUI Signed-off-by: Laurent Garnier --- .../internal/SonyProjectorException.java | 64 +++++++++++++++++-- .../internal/SonyProjectorHandlerFactory.java | 12 +++- .../communication/SonyProjectorConnector.java | 52 ++++++++------- .../sdcp/SonyProjectorSdcpConnector.java | 20 ++++-- .../sdcp/SonyProjectorSdcpSimuConnector.java | 9 ++- .../serial/SonyProjectorSerialConnector.java | 36 +++++------ .../SonyProjectorSerialOverIpConnector.java | 14 ++-- .../SonyProjectorSerialSimuConnector.java | 9 ++- .../handler/SonyProjectorHandler.java | 46 ++++++++----- .../OH-INF/i18n/sonyprojector.properties | 7 ++ .../OH-INF/i18n/sonyprojector_fr.properties | 7 ++ 11 files changed, 193 insertions(+), 83 deletions(-) diff --git a/bundles/org.openhab.binding.sonyprojector/src/main/java/org/openhab/binding/sonyprojector/internal/SonyProjectorException.java b/bundles/org.openhab.binding.sonyprojector/src/main/java/org/openhab/binding/sonyprojector/internal/SonyProjectorException.java index 6743dd761a595..a300067aeec49 100644 --- a/bundles/org.openhab.binding.sonyprojector/src/main/java/org/openhab/binding/sonyprojector/internal/SonyProjectorException.java +++ b/bundles/org.openhab.binding.sonyprojector/src/main/java/org/openhab/binding/sonyprojector/internal/SonyProjectorException.java @@ -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 @@ -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; + } } diff --git a/bundles/org.openhab.binding.sonyprojector/src/main/java/org/openhab/binding/sonyprojector/internal/SonyProjectorHandlerFactory.java b/bundles/org.openhab.binding.sonyprojector/src/main/java/org/openhab/binding/sonyprojector/internal/SonyProjectorHandlerFactory.java index ad85245fcf886..aa0d5f29e83f8 100644 --- a/bundles/org.openhab.binding.sonyprojector/src/main/java/org/openhab/binding/sonyprojector/internal/SonyProjectorHandlerFactory.java +++ b/bundles/org.openhab.binding.sonyprojector/src/main/java/org/openhab/binding/sonyprojector/internal/SonyProjectorHandlerFactory.java @@ -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; @@ -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 @@ -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; diff --git a/bundles/org.openhab.binding.sonyprojector/src/main/java/org/openhab/binding/sonyprojector/internal/communication/SonyProjectorConnector.java b/bundles/org.openhab.binding.sonyprojector/src/main/java/org/openhab/binding/sonyprojector/internal/communication/SonyProjectorConnector.java index c319217ade22a..9b8e06a91c2f4 100644 --- a/bundles/org.openhab.binding.sonyprojector/src/main/java/org/openhab/binding/sonyprojector/internal/communication/SonyProjectorConnector.java +++ b/bundles/org.openhab.binding.sonyprojector/src/main/java/org/openhab/binding/sonyprojector/internal/communication/SonyProjectorConnector.java @@ -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; @@ -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 */ @@ -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; } @@ -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); } } @@ -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()); @@ -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()); @@ -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; } /** @@ -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); } } @@ -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); } } diff --git a/bundles/org.openhab.binding.sonyprojector/src/main/java/org/openhab/binding/sonyprojector/internal/communication/sdcp/SonyProjectorSdcpConnector.java b/bundles/org.openhab.binding.sonyprojector/src/main/java/org/openhab/binding/sonyprojector/internal/communication/sdcp/SonyProjectorSdcpConnector.java index 21d7a39417791..0a11363dbfa29 100644 --- a/bundles/org.openhab.binding.sonyprojector/src/main/java/org/openhab/binding/sonyprojector/internal/communication/sdcp/SonyProjectorSdcpConnector.java +++ b/bundles/org.openhab.binding.sonyprojector/src/main/java/org/openhab/binding/sonyprojector/internal/communication/sdcp/SonyProjectorSdcpConnector.java @@ -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; @@ -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); } /** @@ -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; @@ -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"); } } } @@ -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); } } diff --git a/bundles/org.openhab.binding.sonyprojector/src/main/java/org/openhab/binding/sonyprojector/internal/communication/sdcp/SonyProjectorSdcpSimuConnector.java b/bundles/org.openhab.binding.sonyprojector/src/main/java/org/openhab/binding/sonyprojector/internal/communication/sdcp/SonyProjectorSdcpSimuConnector.java index 608882e45dd4f..2377adc70af29 100644 --- a/bundles/org.openhab.binding.sonyprojector/src/main/java/org/openhab/binding/sonyprojector/internal/communication/sdcp/SonyProjectorSdcpSimuConnector.java +++ b/bundles/org.openhab.binding.sonyprojector/src/main/java/org/openhab/binding/sonyprojector/internal/communication/sdcp/SonyProjectorSdcpSimuConnector.java @@ -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; @@ -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 diff --git a/bundles/org.openhab.binding.sonyprojector/src/main/java/org/openhab/binding/sonyprojector/internal/communication/serial/SonyProjectorSerialConnector.java b/bundles/org.openhab.binding.sonyprojector/src/main/java/org/openhab/binding/sonyprojector/internal/communication/serial/SonyProjectorSerialConnector.java index 673c86182d184..77ba03007586b 100644 --- a/bundles/org.openhab.binding.sonyprojector/src/main/java/org/openhab/binding/sonyprojector/internal/communication/serial/SonyProjectorSerialConnector.java +++ b/bundles/org.openhab.binding.sonyprojector/src/main/java/org/openhab/binding/sonyprojector/internal/communication/serial/SonyProjectorSerialConnector.java @@ -15,7 +15,6 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.io.UnsupportedEncodingException; import java.util.Arrays; import java.util.TooManyListenersException; import java.util.concurrent.TimeUnit; @@ -26,6 +25,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.io.transport.serial.PortInUseException; import org.openhab.core.io.transport.serial.SerialPort; import org.openhab.core.io.transport.serial.SerialPortEvent; @@ -68,10 +69,12 @@ public class SonyProjectorSerialConnector extends SonyProjectorConnector impleme * @param serialPortManager the serial port manager * @param serialPortName the serial port name to be used * @param model the projector model in use + * @param i18nProvider the translation provider + * @param localeProvider the locale provider to get the language */ public SonyProjectorSerialConnector(SerialPortManager serialPortManager, String serialPortName, - SonyProjectorModel model) { - this(serialPortManager, serialPortName, model, false); + SonyProjectorModel model, TranslationProvider i18nProvider, LocaleProvider localeProvider) { + this(serialPortManager, serialPortName, model, i18nProvider, localeProvider, false); } /** @@ -80,11 +83,13 @@ public SonyProjectorSerialConnector(SerialPortManager serialPortManager, String * @param serialPortManager the serial port manager * @param serialPortName the serial port name to be used * @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 SonyProjectorSerialConnector(SerialPortManager serialPortManager, String serialPortName, - SonyProjectorModel model, boolean simu) { - super(model, simu); + SonyProjectorModel model, TranslationProvider i18nProvider, LocaleProvider localeProvider, boolean simu) { + super(model, i18nProvider, localeProvider, simu); this.serialPortManager = serialPortManager; this.serialPortName = serialPortName; @@ -97,8 +102,8 @@ public synchronized void open() throws SonyProjectorException { try { SerialPortIdentifier portIdentifier = serialPortManager.getIdentifier(serialPortName); if (portIdentifier == null) { - logger.debug("Opening serial connection failed: No Such Port: {}", serialPortName); - throw new SonyProjectorException("Opening serial connection failed: No Such Port"); + throw new SonyProjectorException(i18nProvider, localeProvider, "exception.invalid-serial-port", + serialPortName); } SerialPort commPort = portIdentifier.open(this.getClass().getName(), 2000); @@ -139,20 +144,9 @@ public synchronized void open() throws SonyProjectorException { connected = true; logger.debug("Serial connection opened"); - } catch (PortInUseException e) { - logger.debug("Opening serial connection failed: Port in Use Exception: {}", e.getMessage(), e); - throw new SonyProjectorException("Opening serial connection failed: Port in Use Exception"); - } catch (UnsupportedCommOperationException e) { - logger.debug("Opening serial connection failed: Unsupported Comm Operation Exception: {}", - e.getMessage(), e); - throw new SonyProjectorException( - "Opening serial connection failed: Unsupported Comm Operation Exception"); - } catch (UnsupportedEncodingException e) { - logger.debug("Opening serial connection failed: Unsupported Encoding Exception: {}", e.getMessage(), e); - throw new SonyProjectorException("Opening serial connection failed: Unsupported Encoding Exception"); - } catch (IOException e) { - logger.debug("Opening serial connection failed: IO Exception: {}", e.getMessage(), e); - throw new SonyProjectorException("Opening serial connection failed: IO Exception"); + } catch (PortInUseException | UnsupportedCommOperationException | IOException e) { + throw new SonyProjectorException(e, i18nProvider, localeProvider, + "exception.opening-serial-connection-failed"); } } } diff --git a/bundles/org.openhab.binding.sonyprojector/src/main/java/org/openhab/binding/sonyprojector/internal/communication/serial/SonyProjectorSerialOverIpConnector.java b/bundles/org.openhab.binding.sonyprojector/src/main/java/org/openhab/binding/sonyprojector/internal/communication/serial/SonyProjectorSerialOverIpConnector.java index 6487941b0b89b..b4807ffb83fd8 100644 --- a/bundles/org.openhab.binding.sonyprojector/src/main/java/org/openhab/binding/sonyprojector/internal/communication/serial/SonyProjectorSerialOverIpConnector.java +++ b/bundles/org.openhab.binding.sonyprojector/src/main/java/org/openhab/binding/sonyprojector/internal/communication/serial/SonyProjectorSerialOverIpConnector.java @@ -23,6 +23,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.io.transport.serial.SerialPortManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -49,10 +51,12 @@ public class SonyProjectorSerialOverIpConnector extends SonyProjectorSerialConne * @param address the IP address of the projector * @param port the TCP port to be used * @param model the projector model in use + * @param i18nProvider the translation provider + * @param localeProvider the locale provider to get the language */ public SonyProjectorSerialOverIpConnector(SerialPortManager serialPortManager, String address, Integer port, - SonyProjectorModel model) { - super(serialPortManager, "dummyPort", model); + SonyProjectorModel model, TranslationProvider i18nProvider, LocaleProvider localeProvider) { + super(serialPortManager, "dummyPort", model, i18nProvider, localeProvider); this.address = address; this.port = port; @@ -75,8 +79,8 @@ public synchronized void open() throws SonyProjectorException { logger.debug("Serial over IP connection opened"); } catch (IOException | SecurityException | IllegalArgumentException e) { - logger.debug("Opening serial over IP connection failed: {}", e.getMessage()); - throw new SonyProjectorException("Opening serial over IP connection failed: " + e.getMessage()); + throw new SonyProjectorException(e, i18nProvider, localeProvider, + "exception.opening-serial-over-ip-connection-failed"); } } } @@ -122,7 +126,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); } } } diff --git a/bundles/org.openhab.binding.sonyprojector/src/main/java/org/openhab/binding/sonyprojector/internal/communication/serial/SonyProjectorSerialSimuConnector.java b/bundles/org.openhab.binding.sonyprojector/src/main/java/org/openhab/binding/sonyprojector/internal/communication/serial/SonyProjectorSerialSimuConnector.java index 43cfeaf76c5ef..cb7709c1cbcbd 100644 --- a/bundles/org.openhab.binding.sonyprojector/src/main/java/org/openhab/binding/sonyprojector/internal/communication/serial/SonyProjectorSerialSimuConnector.java +++ b/bundles/org.openhab.binding.sonyprojector/src/main/java/org/openhab/binding/sonyprojector/internal/communication/serial/SonyProjectorSerialSimuConnector.java @@ -15,6 +15,8 @@ import org.eclipse.jdt.annotation.NonNullByDefault; 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.io.transport.serial.SerialPortManager; import org.openhab.core.util.HexUtils; import org.slf4j.Logger; @@ -35,9 +37,12 @@ public class SonyProjectorSerialSimuConnector extends SonyProjectorSerialConnect * * @param serialPortManager the serial port manager * @param model the projector model in use + * @param i18nProvider the translation provider + * @param localeProvider the locale provider to get the language */ - public SonyProjectorSerialSimuConnector(SerialPortManager serialPortManager, SonyProjectorModel model) { - super(serialPortManager, "dummyPort", model, true); + public SonyProjectorSerialSimuConnector(SerialPortManager serialPortManager, SonyProjectorModel model, + TranslationProvider i18nProvider, LocaleProvider localeProvider) { + super(serialPortManager, "dummyPort", model, i18nProvider, localeProvider, true); } @Override diff --git a/bundles/org.openhab.binding.sonyprojector/src/main/java/org/openhab/binding/sonyprojector/internal/handler/SonyProjectorHandler.java b/bundles/org.openhab.binding.sonyprojector/src/main/java/org/openhab/binding/sonyprojector/internal/handler/SonyProjectorHandler.java index d4d5f7996002e..3f8ffb868d192 100644 --- a/bundles/org.openhab.binding.sonyprojector/src/main/java/org/openhab/binding/sonyprojector/internal/handler/SonyProjectorHandler.java +++ b/bundles/org.openhab.binding.sonyprojector/src/main/java/org/openhab/binding/sonyprojector/internal/handler/SonyProjectorHandler.java @@ -33,6 +33,8 @@ import org.openhab.binding.sonyprojector.internal.configuration.SonyProjectorSerialConfiguration; import org.openhab.binding.sonyprojector.internal.configuration.SonyProjectorSerialOverIpConfiguration; import org.openhab.core.cache.ExpiringCacheMap; +import org.openhab.core.i18n.LocaleProvider; +import org.openhab.core.i18n.TranslationProvider; import org.openhab.core.io.transport.serial.SerialPortManager; import org.openhab.core.library.types.DecimalType; import org.openhab.core.library.types.OnOffType; @@ -60,16 +62,21 @@ @NonNullByDefault public class SonyProjectorHandler extends BaseThingHandler { - private final Logger logger = LoggerFactory.getLogger(SonyProjectorHandler.class); - private static final SonyProjectorModel DEFAULT_MODEL = SonyProjectorModel.VW520; private static final long POLLING_INTERVAL = TimeUnit.SECONDS.toSeconds(15); + private final Logger logger = LoggerFactory.getLogger(SonyProjectorHandler.class); + + private final TranslationProvider i18nProvider; + private final LocaleProvider localeProvider; + + private final ExpiringCacheMap cache; + private @Nullable ScheduledFuture refreshJob; private boolean identifyProjector; private SonyProjectorModel projectorModel = DEFAULT_MODEL; - private SonyProjectorConnector connector = new SonyProjectorSdcpSimuConnector(DEFAULT_MODEL); + private SonyProjectorConnector connector; private SonyProjectorStateDescriptionOptionProvider stateDescriptionProvider; private SerialPortManager serialPortManager; @@ -78,14 +85,15 @@ public class SonyProjectorHandler extends BaseThingHandler { private final Object commandLock = new Object(); - private final ExpiringCacheMap cache; - public SonyProjectorHandler(Thing thing, SonyProjectorStateDescriptionOptionProvider stateDescriptionProvider, - SerialPortManager serialPortManager) { + SerialPortManager serialPortManager, TranslationProvider i18nProvider, LocaleProvider localeProvider) { super(thing); this.stateDescriptionProvider = stateDescriptionProvider; this.serialPortManager = serialPortManager; + this.i18nProvider = i18nProvider; + this.localeProvider = localeProvider; this.cache = new ExpiringCacheMap<>(TimeUnit.SECONDS.toMillis(POLLING_INTERVAL)); + this.connector = new SonyProjectorSdcpSimuConnector(DEFAULT_MODEL, i18nProvider, localeProvider); } @Override @@ -104,7 +112,7 @@ public void handleCommand(ChannelUID channelUID, Command command) { connector.open(); } catch (SonyProjectorException e) { logger.debug("Command {} from channel {} failed: {}", command, channel, e.getMessage()); - updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage()); + updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getLocalizedMessage()); return; } try { @@ -311,8 +319,9 @@ public void initialize() { } else { configOk = true; - connector = simu ? new SonyProjectorSdcpSimuConnector(DEFAULT_MODEL) - : new SonyProjectorSdcpConnector(config.host, config.port, config.community, DEFAULT_MODEL); + connector = simu ? new SonyProjectorSdcpSimuConnector(DEFAULT_MODEL, i18nProvider, localeProvider) + : new SonyProjectorSdcpConnector(config.host, config.port, config.community, DEFAULT_MODEL, + i18nProvider, localeProvider); identifyProjector = "AUTO".equals(configModel); projectorModel = switchToModel("AUTO".equals(configModel) ? null : configModel, true); @@ -335,8 +344,11 @@ public void initialize() { } else { configOk = true; - connector = simu ? new SonyProjectorSerialSimuConnector(serialPortManager, DEFAULT_MODEL) - : new SonyProjectorSerialConnector(serialPortManager, config.port, DEFAULT_MODEL); + connector = simu + ? new SonyProjectorSerialSimuConnector(serialPortManager, DEFAULT_MODEL, i18nProvider, + localeProvider) + : new SonyProjectorSerialConnector(serialPortManager, config.port, DEFAULT_MODEL, i18nProvider, + localeProvider); identifyProjector = false; projectorModel = switchToModel(configModel, true); @@ -363,9 +375,11 @@ public void initialize() { } else { configOk = true; - connector = simu ? new SonyProjectorSerialSimuConnector(serialPortManager, DEFAULT_MODEL) + connector = simu + ? new SonyProjectorSerialSimuConnector(serialPortManager, DEFAULT_MODEL, i18nProvider, + localeProvider) : new SonyProjectorSerialOverIpConnector(serialPortManager, config.host, config.port, - DEFAULT_MODEL); + DEFAULT_MODEL, i18nProvider, localeProvider); identifyProjector = false; projectorModel = switchToModel(configModel, true); @@ -374,7 +388,7 @@ public void initialize() { } if (!configOk) { - connector = new SonyProjectorSdcpSimuConnector(DEFAULT_MODEL); + connector = new SonyProjectorSdcpSimuConnector(DEFAULT_MODEL, i18nProvider, localeProvider); } else { ScheduledFuture refreshJob = this.refreshJob; if (refreshJob == null || refreshJob.isCancelled()) { @@ -406,8 +420,8 @@ private void pollProjector() { try { connector.open(); } catch (SonyProjectorException e) { - logger.debug("Poll projector failed: {}", e.getMessage()); - updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage()); + logger.debug("Poll projector failed: {}", e.getMessage(), e); + updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getLocalizedMessage()); return; } diff --git a/bundles/org.openhab.binding.sonyprojector/src/main/resources/OH-INF/i18n/sonyprojector.properties b/bundles/org.openhab.binding.sonyprojector/src/main/resources/OH-INF/i18n/sonyprojector.properties index 1f6e51989db6d..70dad31f4d20e 100644 --- a/bundles/org.openhab.binding.sonyprojector/src/main/resources/OH-INF/i18n/sonyprojector.properties +++ b/bundles/org.openhab.binding.sonyprojector/src/main/resources/OH-INF/i18n/sonyprojector.properties @@ -296,3 +296,10 @@ offline.config-error-unknown-port = Undefined port configuration setting offline.config-error-invalid-port = Invalid port configuration setting offline.config-error-unknown-model = Undefined model configuration setting offline.config-error-invalid-thing-type = Use serial over IP connection thing type + + Exceptions + +exception.invalid-serial-port = Opening serial connection failed: no port {0} +exception.opening-serial-connection-failed = Opening serial connection failed +exception.opening-serial-over-ip-connection-failed = Opening serial over IP connection failed +exception.opening-sdcp-connection-failed = Opening SDCP connection failed diff --git a/bundles/org.openhab.binding.sonyprojector/src/main/resources/OH-INF/i18n/sonyprojector_fr.properties b/bundles/org.openhab.binding.sonyprojector/src/main/resources/OH-INF/i18n/sonyprojector_fr.properties index c41a02a839308..1c1dcf1874fc8 100644 --- a/bundles/org.openhab.binding.sonyprojector/src/main/resources/OH-INF/i18n/sonyprojector_fr.properties +++ b/bundles/org.openhab.binding.sonyprojector/src/main/resources/OH-INF/i18n/sonyprojector_fr.properties @@ -296,3 +296,10 @@ offline.config-error-unknown-port = Paramètre de port indéfini offline.config-error-invalid-port = Paramètre de port invalide offline.config-error-unknown-model = Paramètre de modèle indéfini offline.config-error-invalid-thing-type = Utiliser le type connexion série sur IP + + Exceptions + +exception.invalid-serial-port = Echec de l''ouverture de la connexion série: pas de port {0} +exception.opening-serial-connection-failed = Echec de l''ouverture de la connexion série +exception.opening-serial-over-ip-connection-failed = Echec de l''ouverture de la connexion série sur IP +exception.opening-sdcp-connection-failed = Echec de l''ouverture de la connexion SDCP