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 22, 2021
1 parent c6a89be commit fc0068c
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/**
* Copyright (c) 2010-2021 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
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.TranslationProvider;
import org.osgi.framework.Bundle;

/**
* The {@link I18nException} class is a generic exception class implementing the internationalization of
* exception message in the context of openHAB.
*
* @author Laurent Garnier - Initial contribution
*/
@NonNullByDefault
public class I18nException extends Exception {
private static final long serialVersionUID = 1L;

private String msgKey;
private @Nullable Object @Nullable [] msgParams;
private @Nullable Bundle bundle;
private @Nullable TranslationProvider i18nProvider;
private @Nullable Locale locale;

public I18nException(@Nullable String message, @Nullable Throwable cause) {
super(message, cause);
this.msgKey = "";
}

public I18nException(@Nullable Throwable cause, String msgKey, @Nullable Object @Nullable... msgParams) {
super(null, cause);
this.msgKey = msgKey;
this.msgParams = msgParams;
}

public void setupI18n(Bundle bundle, TranslationProvider i18nProvider) {
setupI18n(bundle, i18nProvider, null);
}

public void setupI18n(Bundle bundle, TranslationProvider i18nProvider, @Nullable Locale locale) {
this.bundle = bundle;
this.i18nProvider = i18nProvider;
this.locale = locale;
}

@Override
public @Nullable String getMessage() {
Bundle localeBundle = this.bundle;
TranslationProvider localI18nProvider = this.i18nProvider;
if (msgKey.isBlank() || localeBundle == null || localI18nProvider == null) {
return getMessage();
} else {
return localI18nProvider.getText(localeBundle, msgKey, null, Locale.ENGLISH, msgParams);
}
}

@Override
public @Nullable String getLocalizedMessage() {
Bundle localeBundle = this.bundle;
TranslationProvider localI18nProvider = this.i18nProvider;
Locale localLocale = this.locale;
if (msgKey.isBlank() || localeBundle == null || localI18nProvider == null || localLocale == null) {
return getMessage();
} else {
return localI18nProvider.getText(localeBundle, msgKey, null, localLocale, msgParams);
}
}

public @Nullable String getMessageOrKeyWithParams() {
if (msgKey.isBlank()) {
return getMessage();
}
String result = "@text/" + msgKey;
Object @Nullable [] params = msgParams;
if (params != null && params.length > 0) {
result += " [";
boolean first = true;
for (Object param : params) {
if (first) {
first = false;
} else {
result += ",";
}
result += String.format(" \"%s\"", param == null ? "" : param.toString());
}
result += " ]";
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,33 @@

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

/**
* The {@link SonyProjectorException} class is used for any exception thrown by the binding
*
* @author Markus Wehrle - Initial contribution
* @author Laurent Garnier - use I18nException for message internationalization
*/
@NonNullByDefault
public class SonyProjectorException extends Exception {
public class SonyProjectorException extends I18nException {

private static final long serialVersionUID = 1L;

// Parameterless Constructor
public SonyProjectorException() {
public SonyProjectorException(String message) {
super(message, null);
}

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

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

// Constructor that accepts a message
public SonyProjectorException(@Nullable String message) {
super(message);
public void setupI18n(TranslationProvider i18nProvider) {
setupI18n(FrameworkUtil.getBundle(getClass()).getBundleContext().getBundle(), i18nProvider);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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.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 +50,15 @@ public class SonyProjectorHandlerFactory extends BaseThingHandlerFactory {
private final SerialPortManager serialPortManager;

private final SonyProjectorStateDescriptionOptionProvider stateDescriptionProvider;
private final TranslationProvider i18nProvider;

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

@Override
Expand All @@ -67,7 +71,7 @@ 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);
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -898,8 +898,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 @@ -917,8 +916,7 @@ private void setSetting(SonyProjectorItem item, byte[] data) throws SonyProjecto
try {
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 @@ -951,12 +949,10 @@ private synchronized void sendIR(SonyProjectorItem item) throws SonyProjectorExc
close();
}
} 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 +971,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 +1057,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 +1082,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 @@ -130,8 +130,7 @@ 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, "exception.opening-sdcp-connection-failed");
}
}
}
Expand Down Expand Up @@ -197,7 +196,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 @@ -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;
Expand Down Expand Up @@ -97,8 +96,7 @@ 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(null, "exception.invalid-serial-port", serialPortName);
}

SerialPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
Expand Down Expand Up @@ -139,20 +137,8 @@ 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, "exception.opening-serial-connection-failed");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ 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, "exception.opening-serial-over-ip-connection-failed");
}
}
}
Expand Down Expand Up @@ -122,7 +121,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);
}
}
}
Loading

0 comments on commit fc0068c

Please sign in to comment.