Skip to content

Commit

Permalink
Code clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
andan67 committed Jun 20, 2021
1 parent 784d75f commit 083c651
Show file tree
Hide file tree
Showing 18 changed files with 167 additions and 173 deletions.
4 changes: 3 additions & 1 deletion bundles/org.openhab.binding.sony/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ You best action would be to turn the device ON when you are trying to set it up

1. IRCC/Scalar on Blurays will not be auto discovered if the device is off (however DIAL will be discovered).
Both these services are turned off when the device is turned off.
2. Audio service on certain devices will either be turned off or limited in scope.
2. Audio service on certain devices will either be turned off or limited in scope.

If the audio service is off, you will either see no audio channels (volume, etc) or will be missing audio channels (like headphone volume for Bravias)

### Wireless Interface
Expand Down Expand Up @@ -209,6 +210,7 @@ The following is a list of common configuration options for all services
1. See IP Address Configuration above
2. Only specify if the device support wake on lan (WOL)
3. Only specify if the device provides status information.

Set to negative to disable (-1).

```refresh``` is the time between checking the state of the device.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/**
* Copyright (c) 2010-2021 Contributors to the openHAB project
* <p>
*
* See the NOTICE file(s) distributed with this work for additional
* information.
* <p>
*
* 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
* <p>
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.sony.internal;
Expand All @@ -28,7 +28,10 @@

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.thing.*;
import org.openhab.core.thing.ChannelUID;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingStatus;
import org.openhab.core.thing.ThingStatusDetail;
import org.openhab.core.thing.binding.BaseThingHandler;
import org.openhab.core.types.Command;
import org.openhab.core.types.RefreshType;
Expand Down Expand Up @@ -60,8 +63,8 @@ public abstract class AbstractThingHandler<C extends AbstractConfig> extends Bas
private final AtomicReference<@Nullable Future<?>> retryConnection = new AtomicReference<>(null);

/** The delay for trying to reconnect after command has been sent to offline thing */
private final int AUTO_RECONNECT_DELAY = 30;
private final int MAX_AUTO_RECONNECT = 5;
private static final int AUTO_RECONNECT_DELAY = 30;
private static final int MAX_AUTO_RECONNECT = 5;
private final AtomicInteger autoRetryCount = new AtomicInteger(0);
private final AtomicBoolean isAutoRetryActive = new AtomicBoolean(false);

Expand Down Expand Up @@ -127,6 +130,31 @@ private void doConnect() {
connect();
}

/**
* Helper method to start a reconnection attempt
*/
private void doReconnect() {
updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.NONE, "Reconnecting ...");
String ipAddress = "";
int port = 0;
try {
URL url;
url = getCheckStatusUrl();
ipAddress = url.getHost();
final int urlPort = url.getPort();
port = urlPort == -1 ? url.getDefaultPort() : urlPort;
Socket soc = new Socket();
soc.connect(new InetSocketAddress(ipAddress, port), 5000);
} catch (IOException e) {
logger.debug("Checking connectivity to {}:{} - unsuccessful. Giving up reconnection attempt", ipAddress,
port);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"Error reconnecting to device: " + e.getMessage());
return;
}
connect();
}

/**
* Helper method to determine if we should auto reconnect on a command
*
Expand Down Expand Up @@ -321,7 +349,7 @@ private void scheduleReconnect(@Nullable Integer retryPolling) {
SonyUtil.cancel(retryConnection.getAndSet(this.scheduler.schedule(() -> {
if (!SonyUtil.isInterrupted() && !isRemoved()) {
logger.debug("Do reconnect");
doConnect();
doReconnect();
}
}, retryPolling, TimeUnit.SECONDS)));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import javax.ws.rs.core.NewCookie;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -30,18 +29,18 @@
@NonNullByDefault
public class SonyAuthCookieStore {
private final Logger logger = LoggerFactory.getLogger(SonyAuthCookieStore.class);
private static final SonyAuthCookieStore instance = new SonyAuthCookieStore();
private static final SonyAuthCookieStore INSTANCE = new SonyAuthCookieStore();
private static final NewCookie EMPTY_COOKIE = new NewCookie("auth", "");

private final ConcurrentMap<String, @NonNull NewCookie> hostAuthCookieMap = new ConcurrentHashMap<>();
private final ConcurrentMap<String, NewCookie> hostAuthCookieMap = new ConcurrentHashMap<>();

/**
* Gets single instance
*
* @return the instance
*/
public static SonyAuthCookieStore getInstance() {
return instance;
return INSTANCE;
}

/**
Expand All @@ -67,7 +66,7 @@ public NewCookie getAuthCookieForHost(String host) {
* @param authCookie the non-null cookie
*/
public void setAuthCookieForHost(String host, NewCookie authCookie) {
// logger.debug("setAuthCookieForHost: host: {} authCookie: {} instance: {}", host, authCookie, instance);
logger.debug("setAuthCookieForHost: host: {} authCookie: {} instance: {}", host, authCookie, INSTANCE);
hostAuthCookieMap.put(host, authCookie);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.Future;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -371,14 +379,18 @@ public static boolean equals(final @Nullable String str1, final @Nullable String
* @return null if string is null, TRUE if string represents a true boolean, FALSE otherwise
*/
public static @Nullable Boolean toBooleanObject(@Nullable String str) {
if (str == null)
if (str == null) {
return null;
if (str.equalsIgnoreCase("true"))
}
if ("true".equalsIgnoreCase(str)) {
return Boolean.TRUE;
if (str.equalsIgnoreCase("yes"))
}
if ("yes".equalsIgnoreCase(str)) {
return Boolean.TRUE;
if (str.equalsIgnoreCase("on"))
}
if ("on".equalsIgnoreCase(str)) {
return Boolean.TRUE;
}
return Boolean.FALSE;
}

Expand All @@ -393,7 +405,7 @@ public static boolean isNumeric(@Nullable String str) {
return false;
}
try {
long l = Long.parseLong(str);
Long.parseLong(str);
} catch (NumberFormatException nfe) {
return false;
}
Expand All @@ -411,7 +423,7 @@ public static boolean isNumber(@Nullable String str) {
return false;
}
try {
double d = Double.parseDouble(str);
Double.parseDouble(str);
} catch (NumberFormatException nfe) {
return false;
}
Expand Down Expand Up @@ -521,8 +533,9 @@ public static String stripEnd(final String str, final String stripChars) {

public static String join(final String delimiter, final @Nullable String @Nullable [] strArray) {
// public static String join(final String delimiter, final String[] strArray) {
if (strArray == null)
if (strArray == null) {
return "";
}
return Arrays.stream(strArray).map(s -> s == null ? "" : s).collect(Collectors.joining(delimiter));
}

Expand Down Expand Up @@ -786,8 +799,9 @@ public static boolean endsWithIgnoreCase(final String str, final String suffix)
* @param message the message
*/
public static void validateNotEmpty(final String str, final String message) {
if (SonyUtil.isEmpty(str))
if (SonyUtil.isEmpty(str)) {
throw new IllegalArgumentException(message);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@
package org.openhab.binding.sony.internal.providers;

import java.math.BigDecimal;
import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ScheduledExecutorService;
import java.util.function.Predicate;
import java.util.stream.Collectors;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.sony.internal.providers.models.SonyDeviceCapability;

import com.google.gson.*;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

/**
* This class represents the serializer for a SonyDeviceCapability that will remove the baseURL (private information)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.Future;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,19 @@
import java.nio.file.StandardOpenOption;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -554,8 +566,9 @@ private MetaInfo getMetaInfo() throws JsonSyntaxException, IOException {
final String content = resp.getContent();
logger.trace("Got new meta info for etag {}: {}", metaEtag, content);
final @Nullable MetaInfo metaInfoFromJson = gson.fromJson(content, MetaInfo.class);
if (metaInfoFromJson != null)
if (metaInfoFromJson != null) {
metaInfo = metaInfoFromJson;
}
return metaInfo;
} else if (resp.getHttpCode() == HttpStatus.NOT_MODIFIED_304) {
logger.trace("Metainfo was not modified - returning last version from etag {}", metaEtag);
Expand Down Expand Up @@ -903,7 +916,7 @@ public void addListener(final String modelName, final ThingTypeUID currentThingT
// ... it doesn't exist - get the definition and write it
// If not found
// ... add it to the waiting list
if (fileName != null && !fileName.isEmpty()) {
if (!fileName.isEmpty()) {
final File theFile = thingTypePath.resolve(fileName).toFile();
if (!theFile.exists()) {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/**
* Copyright (c) 2010-2021 Contributors to the openHAB project
* <p>
*
* See the NOTICE file(s) distributed with this work for additional
* information.
* <p>
*
* 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
* <p>
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.sony.internal.scalarweb;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public List<ScalarWebMethod> getMethods() {
// Retrieve the api versions for the service
versions.addAll(execute(new ScalarWebRequest(ScalarWebMethod.GETVERSIONS, version)).asArray(String.class));
} catch (final IOException e) {
if (e.getMessage().contains(String.valueOf(HttpStatus.NOT_FOUND_404))) {
if (e.getMessage() != null && e.getMessage().contains(String.valueOf(HttpStatus.NOT_FOUND_404))) {
logger.debug("Could not retrieve method versions - missing method {}: {}", ScalarWebMethod.GETVERSIONS,
e.getMessage());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public Notifications(final List<Notification> enabled, final List<Notification>

// note: if empty - set to null. Sony has a bad habit of ignoring
// the request if one or the other array is an empty array (both can be empty however - go figure)
this.enabled = enabled.size() == 0 && disabled.size() > 0 ? null : new ArrayList<>(enabled);
this.disabled = enabled.size() > 0 && disabled.size() == 0 ? null : new ArrayList<>(disabled);
this.enabled = enabled.isEmpty() && !disabled.isEmpty() ? null : new ArrayList<>(enabled);
this.disabled = !enabled.isEmpty() && disabled.isEmpty() ? null : new ArrayList<>(disabled);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.sony.internal.SonyUtil;
import org.openhab.binding.sony.internal.transports.SonyTransport;
import org.openhab.binding.sony.internal.transports.SonyTransportFactory;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,17 @@
package org.openhab.binding.sony.internal.scalarweb.models.api;

import java.io.IOException;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down
Loading

0 comments on commit 083c651

Please sign in to comment.