From 083c651513c63946b5e62dca3d32113fe6f142f1 Mon Sep 17 00:00:00 2001 From: andan67 Date: Sun, 20 Jun 2021 17:40:23 +0200 Subject: [PATCH] Code clean-up --- bundles/org.openhab.binding.sony/README.md | 4 +- .../sony/internal/AbstractThingHandler.java | 42 +++++-- .../sony/internal/SonyAuthCookieStore.java | 9 +- .../binding/sony/internal/SonyUtil.java | 32 +++-- .../providers/SonyDefinitionProviderImpl.java | 10 +- .../SonyDeviceCapabilitySerializer.java | 6 +- .../providers/sources/SonyFolderSource.java | 9 +- .../providers/sources/SonyGithubSource.java | 19 ++- .../internal/scalarweb/ScalarWebHandler.java | 6 +- .../scalarweb/models/ScalarWebService.java | 2 +- .../scalarweb/models/api/Notifications.java | 4 +- .../scalarweb/models/api/ServiceProtocol.java | 1 - .../scalarweb/models/api/SupportedApi.java | 12 +- .../protocols/AbstractScalarWebProtocol.java | 25 ++-- .../protocols/ScalarWebAvContentProtocol.java | 21 +++- .../protocols/ScalarWebBrowserProtocol.java | 6 +- .../internal/transports/SonyAuthFilter.java | 13 +- .../binding/sony/internal/SonyUtilTest.java | 119 ------------------ 18 files changed, 167 insertions(+), 173 deletions(-) delete mode 100644 bundles/org.openhab.binding.sony/src/test/java/org/openhab/binding/sony/internal/SonyUtilTest.java diff --git a/bundles/org.openhab.binding.sony/README.md b/bundles/org.openhab.binding.sony/README.md index 1b9aef535573c..9ae0080b3e368 100644 --- a/bundles/org.openhab.binding.sony/README.md +++ b/bundles/org.openhab.binding.sony/README.md @@ -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 @@ -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. diff --git a/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/AbstractThingHandler.java b/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/AbstractThingHandler.java index aa9c8909a19b0..d6b5d990b7ea5 100644 --- a/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/AbstractThingHandler.java +++ b/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/AbstractThingHandler.java @@ -1,13 +1,13 @@ /** * 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.sony.internal; @@ -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; @@ -60,8 +63,8 @@ public abstract class AbstractThingHandler 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); @@ -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 * @@ -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 { diff --git a/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/SonyAuthCookieStore.java b/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/SonyAuthCookieStore.java index 710243afc3b7d..d2dfb66ada5d8 100644 --- a/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/SonyAuthCookieStore.java +++ b/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/SonyAuthCookieStore.java @@ -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; @@ -30,10 +29,10 @@ @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 hostAuthCookieMap = new ConcurrentHashMap<>(); + private final ConcurrentMap hostAuthCookieMap = new ConcurrentHashMap<>(); /** * Gets single instance @@ -41,7 +40,7 @@ public class SonyAuthCookieStore { * @return the instance */ public static SonyAuthCookieStore getInstance() { - return instance; + return INSTANCE; } /** @@ -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); } } diff --git a/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/SonyUtil.java b/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/SonyUtil.java index 994aebd278d21..82aadcf3e8607 100644 --- a/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/SonyUtil.java +++ b/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/SonyUtil.java @@ -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; @@ -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; } @@ -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; } @@ -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; } @@ -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)); } @@ -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); + } } /** diff --git a/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/providers/SonyDefinitionProviderImpl.java b/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/providers/SonyDefinitionProviderImpl.java index fef2a0a9f1576..e27f464d2ac05 100644 --- a/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/providers/SonyDefinitionProviderImpl.java +++ b/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/providers/SonyDefinitionProviderImpl.java @@ -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; diff --git a/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/providers/sources/SonyDeviceCapabilitySerializer.java b/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/providers/sources/SonyDeviceCapabilitySerializer.java index f7280f529ee7d..b031cdd3f3f5f 100644 --- a/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/providers/sources/SonyDeviceCapabilitySerializer.java +++ b/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/providers/sources/SonyDeviceCapabilitySerializer.java @@ -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) diff --git a/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/providers/sources/SonyFolderSource.java b/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/providers/sources/SonyFolderSource.java index a1285c9e3ff4c..4dbd727055b26 100644 --- a/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/providers/sources/SonyFolderSource.java +++ b/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/providers/sources/SonyFolderSource.java @@ -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; diff --git a/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/providers/sources/SonyGithubSource.java b/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/providers/sources/SonyGithubSource.java index a6c4dd345dfe8..ec4af9380a18d 100644 --- a/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/providers/sources/SonyGithubSource.java +++ b/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/providers/sources/SonyGithubSource.java @@ -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; @@ -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); @@ -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 { diff --git a/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/scalarweb/ScalarWebHandler.java b/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/scalarweb/ScalarWebHandler.java index 22e706172b94e..be0b8bc61b4c3 100644 --- a/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/scalarweb/ScalarWebHandler.java +++ b/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/scalarweb/ScalarWebHandler.java @@ -1,13 +1,13 @@ /** * 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.sony.internal.scalarweb; diff --git a/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/scalarweb/models/ScalarWebService.java b/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/scalarweb/models/ScalarWebService.java index 9c8e34de58ba7..9851be67d2b62 100644 --- a/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/scalarweb/models/ScalarWebService.java +++ b/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/scalarweb/models/ScalarWebService.java @@ -144,7 +144,7 @@ public List 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 { diff --git a/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/scalarweb/models/api/Notifications.java b/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/scalarweb/models/api/Notifications.java index b65b85cae89c8..22b52d77ae80a 100644 --- a/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/scalarweb/models/api/Notifications.java +++ b/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/scalarweb/models/api/Notifications.java @@ -52,8 +52,8 @@ public Notifications(final List enabled, final List // 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); } /** diff --git a/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/scalarweb/models/api/ServiceProtocol.java b/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/scalarweb/models/api/ServiceProtocol.java index 2c99a20c41e4a..6ae99d7ee1a7b 100644 --- a/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/scalarweb/models/api/ServiceProtocol.java +++ b/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/scalarweb/models/api/ServiceProtocol.java @@ -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; /** diff --git a/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/scalarweb/models/api/SupportedApi.java b/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/scalarweb/models/api/SupportedApi.java index 3249216f61913..578737ecd222d 100644 --- a/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/scalarweb/models/api/SupportedApi.java +++ b/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/scalarweb/models/api/SupportedApi.java @@ -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; diff --git a/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/scalarweb/protocols/AbstractScalarWebProtocol.java b/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/scalarweb/protocols/AbstractScalarWebProtocol.java index 915d2bf57709b..55d3246ce9949 100644 --- a/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/scalarweb/protocols/AbstractScalarWebProtocol.java +++ b/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/scalarweb/protocols/AbstractScalarWebProtocol.java @@ -15,7 +15,18 @@ 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.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; @@ -518,7 +529,7 @@ protected void addGeneralSettingsDescriptor(final List 0 ? GeneralSetting.ENUMTARGET : GeneralSetting.STRINGTARGET; + settingType = !candidates.isEmpty() ? GeneralSetting.ENUMTARGET : GeneralSetting.STRINGTARGET; } } @@ -527,7 +538,7 @@ protected void addGeneralSettingsDescriptor(final List 0) { + if (!candidates.isEmpty()) { final GeneralSettingsCandidate candidate = candidates.get(0); if (candidate != null && candidate.getMax() != null && candidate.getMin() != null && candidate.getStep() != null) { @@ -540,7 +551,7 @@ protected void addGeneralSettingsDescriptor(final List 0) { + if (SonyUtil.equals(settingType, GeneralSetting.BOOLEANTARGET) && !candidates.isEmpty()) { if (candidates.size() != 2) { settingType = GeneralSetting.ENUMTARGET; } else { @@ -584,7 +595,7 @@ protected void addGeneralSettingsDescriptor(final List 0) { + if (!candidates.isEmpty()) { final GeneralSettingsCandidate candidate = candidates.get(0); final Double min = candidate.getMin(), max = candidate.getMax(), step = candidate.getStep(); @@ -628,7 +639,7 @@ protected void addGeneralSettingsDescriptor(final List 0) { + if (!candidates.isEmpty()) { final GeneralSettingsCandidate candidate = candidates.get(0); final Double min = candidate.getMin(), max = candidate.getMax(), step = candidate.getStep(); diff --git a/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/scalarweb/protocols/ScalarWebAvContentProtocol.java b/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/scalarweb/protocols/ScalarWebAvContentProtocol.java index 3fde45b09ea81..110e3732f8517 100644 --- a/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/scalarweb/protocols/ScalarWebAvContentProtocol.java +++ b/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/scalarweb/protocols/ScalarWebAvContentProtocol.java @@ -17,8 +17,20 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; import java.util.Map.Entry; +import java.util.Objects; +import java.util.Optional; +import java.util.Scanner; +import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import java.util.concurrent.atomic.AtomicReference; @@ -2888,7 +2900,7 @@ private void refreshPresetChannelStateDescription(final List c ScalarWebChannel.createChannelId(chl.getCategory(), chl.getId()) + "_" + thingId + ".csv"); if (Files.exists(path)) { try { - // regex to parse csv formatted lines (with limitations + // regex to parse csv formatted lines (with limitations) String regexCSV = ",(?=([^\"]*\"[^\"]*\")*[^\"]*$)"; String regexQuotes = "^\"|\"$"; final String content = Files.readString(path); @@ -2900,7 +2912,6 @@ private void refreshPresetChannelStateDescription(final List c final String line = scanner.nextLine(); final String[] values = line.split(regexCSV); final String dispNum = values[1].trim().replaceAll(regexQuotes, ""); - final String uri = values[3].trim().replaceAll(regexQuotes, ""); final Integer rank = Integer.parseInt(values[4].trim().replaceAll(regexQuotes, "")); rankMap.put(dispNum, rank); } catch (final Exception ex) { @@ -2921,7 +2932,9 @@ private void refreshPresetChannelStateDescription(final List c Optional si = Optional.empty(); if (dispNum != null && !dispNum.isEmpty() && uri != null && !uri.isEmpty()) { si = Optional.of(new StateOption(dispNum, SonyUtil.defaultIfEmpty(title, dispNum))); - displayNumberUriMap.put(dispNum, uri); + if (displayNumberUriMap != null) { + displayNumberUriMap.put(dispNum, uri); + } } return si; }).filter(Optional::isPresent).map(Optional::get) diff --git a/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/scalarweb/protocols/ScalarWebBrowserProtocol.java b/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/scalarweb/protocols/ScalarWebBrowserProtocol.java index dd3210339e5e4..89b8cdaedf6dd 100644 --- a/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/scalarweb/protocols/ScalarWebBrowserProtocol.java +++ b/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/scalarweb/protocols/ScalarWebBrowserProtocol.java @@ -1,13 +1,13 @@ /** * 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.sony.internal.scalarweb.protocols; diff --git a/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/transports/SonyAuthFilter.java b/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/transports/SonyAuthFilter.java index 794e639d24610..f60d450f95f1a 100644 --- a/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/transports/SonyAuthFilter.java +++ b/bundles/org.openhab.binding.sony/src/main/java/org/openhab/binding/sony/internal/transports/SonyAuthFilter.java @@ -15,7 +15,11 @@ import java.io.IOException; import java.net.ConnectException; import java.net.URI; -import java.util.*; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import java.util.Map; +import java.util.Objects; import java.util.concurrent.atomic.AtomicBoolean; import javax.ws.rs.ProcessingException; @@ -110,11 +114,12 @@ public void filter(final @Nullable ClientRequestContext requestCtx) throws IOExc // logger.debug("Apply filter"); if (!authCookieStore.getAuthCookieForHost(host).getValue().isEmpty()) { - logger.debug(authCookieStore.getAuthCookieForHost(host).toString()); + final NewCookie authCookie = authCookieStore.getAuthCookieForHost(host); + logger.debug("Got cookie: {} for host: {}", authCookie, host); // Has the cookie expired... - final Date expiryDate = authCookieStore.getAuthCookieForHost(host).getExpiry(); + final Date expiryDate = authCookie.getExpiry(); if (expiryDate != null && new Date().after(expiryDate)) { - logger.debug("expired"); + logger.debug("Cookie expired"); } else { authNeeded = false; } diff --git a/bundles/org.openhab.binding.sony/src/test/java/org/openhab/binding/sony/internal/SonyUtilTest.java b/bundles/org.openhab.binding.sony/src/test/java/org/openhab/binding/sony/internal/SonyUtilTest.java deleted file mode 100644 index 0c3d63305121f..0000000000000 --- a/bundles/org.openhab.binding.sony/src/test/java/org/openhab/binding/sony/internal/SonyUtilTest.java +++ /dev/null @@ -1,119 +0,0 @@ -package org.openhab.binding.sony.internal; - -import static org.junit.jupiter.api.Assertions.*; - -import java.util.*; -import java.util.stream.Collectors; - -import org.junit.jupiter.api.Test; -import org.openhab.core.types.StateOption; - -class SonyUtilTest { - - @Test - public void toBooleanObjectTest() { - assertTrue(SonyUtil.toBooleanObject("true")); - assertFalse(SonyUtil.toBooleanObject("false")); - assertTrue(!Boolean.FALSE.equals(SonyUtil.toBooleanObject("true"))); - assertFalse(!Boolean.FALSE.equals(SonyUtil.toBooleanObject("false"))); - assertTrue(!Boolean.FALSE.equals(SonyUtil.toBooleanObject(null))); - } - - @Test - public void csvScannerTest() { - String testCSV = "Source, DispNum, Title, Uri, Rank\n" - + "\"tv:dvbt\",\"001\",\"Das Erste\",\"tv:dvbt?trip=8468.38912.64&srvName=Das%20Erste\",\"0\"\n" - + "\"tv:dvbt\",\"002\",\"ZDF\",\"tv:dvbt?trip=8468.514.514&srvName=ZDF\",\"0\"\n" - + "\"tv:dvbt\",003, \"3sat\",\"tv:dvbt?trip=8468.514.515&srvName=3sat\",\"0\""; - - Scanner scanner; - String regex = "(?:,|\\n|^)(\"(?:(?:\"\")*[^\"]*)*\"|[^\",\\n]*|(?:\\n|$))"; - String regex2 = ",(?=([^\"]*\"[^\"]*\")*[^\"]*$)"; - String regex3 = "^\"|\"$"; - - try { - scanner = new Scanner(testCSV); - scanner.nextLine(); - while (scanner.hasNextLine()) { - String line = scanner.nextLine(); - String[] values = line.split(regex2); - Arrays.stream(values).forEach(v -> System.out.println(v.trim().replaceAll(regex3, ""))); - } - } catch (Exception ex) { - System.out.printf("Exception %s", ex.getMessage()); - } - } - - @Test - public void presetFavoriteTest() { - String initialCSV = "Source,DispNum,Title,Uri,Rank\n" - + "\"tv:dvbs\",\"100\",\"Sky 1 HD\",\"tv:dvbs?trip=133.8.147&srvName=Sky%201%20HD\",\"0\"\n" - + "\"tv:dvbs\",\"101\",\"Sky Serien & Shows HD\",\"tv:dvbs?trip=133.4.117&srvName=Sky%20Serien%20%26%20Shows%20HD\",\"3\"\n" - + "\"tv:dvbs\",\"103\",\"Sky Atlantic HD\",\"tv:dvbs?trip=133.13.110&srvName=Sky%20Atlantic%20HD\",\"2\"\n" - + "\"tv:dvbs\",\"104\",\"Fox HD\",\"tv:dvbs?trip=133.10.124&srvName=Fox%20HD\",\"0\"\n" - + "\"tv:dvbs\",\"105\",\"TNT Serie HD\",\"tv:dvbs?trip=133.9.123&srvName=TNT%20Serie%20HD\",\"1\"\n" - + "\"tv:dvbs\",\"106\",\"TNT Comedy HD\",\"tv:dvbs?trip=133.14.136&srvName=TNT%20Comedy%20HD\",\"-1\""; - - String channelCSV = "Source,DispNum,Title,Uri,Rank\n" - + "\"tv:dvbs\",\"100\",\"Sky 1 HD\",\"tv:dvbs?trip=133.8.147&srvName=Sky%201%20HD\",\"0\"\n" - + "\"tv:dvbs\",\"101\",\"Sky Serien & Shows HD\",\"tv:dvbs?trip=133.4.117&srvName=Sky%20Serien%20%26%20Shows%20HD\",\"0\"\n" - + "\"tv:dvbs\",\"104\",\"Fox HD\",\"tv:dvbs?trip=133.10.124&srvName=Fox%20HD\",\"0\"\n" - + "\"tv:dvbs\",\"105\",\"TNT Serie HD\",\"tv:dvbs?trip=133.9.123&srvName=TNT%20Serie%20HD\",\"0\"\n" - + "\"tv:dvbs\",\"106\",\"TNT Comedy HD\",\"tv:dvbs?trip=133.14.136&srvName=TNT%20Comedy%20HD\",\"0\"\n" - + "\"tv:dvbs\",\"107\",\"SYFY HD\",\"tv:dvbs?trip=133.12.126&srvName=SYFY%20HD\",\"0\"\n" - + "\"tv:dvbs\",\"108\",\"13th Street HD\",\"tv:dvbs?trip=133.13.127&srvName=13th%20Street%20HD\",\"0\""; - - HashMap uriToRankMap = new HashMap<>(); - String regexCSV = ",(?=([^\"]*\"[^\"]*\")*[^\"]*$)"; - String regexQuotes = "^\"|\"$"; - Scanner scanner = new Scanner(initialCSV); - scanner.nextLine(); - while (scanner.hasNextLine()) { - try { - final String line = scanner.nextLine(); - final String[] values = line.split(regexCSV); - final String uri = values[3].trim().replaceAll(regexQuotes, ""); - final Integer rank = Integer.parseInt(values[4].trim().replaceAll(regexQuotes, "")); - uriToRankMap.put(uri, rank); - } catch (final Exception ex) { - // ignore - } - } - List uriList = new ArrayList<>(); - List stateOptions = new ArrayList<>(); - scanner = new Scanner(channelCSV); - scanner.nextLine(); - while (scanner.hasNextLine()) { - try { - final String line = scanner.nextLine(); - final String[] values = line.split(regexCSV); - final String uri = values[3].trim().replaceAll(regexQuotes, ""); - final String title = values[2].trim().replaceAll(regexQuotes, ""); - uriList.add(uri); - stateOptions.add(new StateOption(uri, title)); - } catch (final Exception ex) { - // ignore - } - } - - System.out.println(uriToRankMap.size()); - List filteredAndSortedList = uriList.stream() - .filter(uri -> (uriToRankMap.getOrDefault(uri, Integer.MAX_VALUE)) >= 0) - .sorted(Comparator.comparing(uri -> uriToRankMap.getOrDefault(uri, Integer.MAX_VALUE))) - .collect(Collectors.toList()); - List stateOptionsFilteredAndSorted = stateOptions.stream() - .filter(a -> (uriToRankMap.getOrDefault(a.getValue(), Integer.MAX_VALUE)) >= 0) - // .sorted(Comparator.comparing(StateOption::getValue).thenComparing(StateOption::getLabel)) - // .sorted(Comparator.comparing(a -> uriToRankMap.getOrDefault(a.getValue(), Integer.MAX_VALUE))). - // .sorted(Comparator - // . comparing( - // a -> uriToRankMap.getOrDefault(a.getValue(), Integer.MAX_VALUE)) - // .thenComparing(a -> SonyUtil.defaultIfEmpty(a.getLabel(), ""))) - .sorted(Comparator. comparingInt(a -> { - Integer r = uriToRankMap.getOrDefault(a.getValue(), 0); - return r == 0 ? Integer.MAX_VALUE : r; - }).thenComparing(a -> SonyUtil.defaultIfEmpty(a.getLabel(), ""))).collect(Collectors.toList()); - System.out.println(filteredAndSortedList.size()); - System.out.println(stateOptionsFilteredAndSorted.size()); - } -}