]> git.basschouten.com Git - openhab-addons.git/commitdiff
[neeo] improve code (#9028)
authorJ-N-K <J-N-K@users.noreply.github.com>
Wed, 18 Nov 2020 00:25:46 +0000 (01:25 +0100)
committerGitHub <noreply@github.com>
Wed, 18 Nov 2020 00:25:46 +0000 (16:25 -0800)
* create forwardactionsservlet only if forwardchain is not empty
* remove dependency on apache commons

Signed-off-by: Jan N. Klug <jan.n.klug@rub.de>
21 files changed:
bundles/org.openhab.binding.neeo/src/main/java/org/openhab/binding/neeo/internal/NeeoBrainApi.java
bundles/org.openhab.binding.neeo/src/main/java/org/openhab/binding/neeo/internal/NeeoBrainConfig.java
bundles/org.openhab.binding.neeo/src/main/java/org/openhab/binding/neeo/internal/NeeoRoomProtocol.java
bundles/org.openhab.binding.neeo/src/main/java/org/openhab/binding/neeo/internal/NeeoUtil.java
bundles/org.openhab.binding.neeo/src/main/java/org/openhab/binding/neeo/internal/UidUtils.java
bundles/org.openhab.binding.neeo/src/main/java/org/openhab/binding/neeo/internal/discovery/NeeoBrainDiscovery.java
bundles/org.openhab.binding.neeo/src/main/java/org/openhab/binding/neeo/internal/discovery/NeeoDeviceDiscoveryService.java
bundles/org.openhab.binding.neeo/src/main/java/org/openhab/binding/neeo/internal/discovery/NeeoRoomDiscoveryService.java
bundles/org.openhab.binding.neeo/src/main/java/org/openhab/binding/neeo/internal/handler/ChannelUtils.java
bundles/org.openhab.binding.neeo/src/main/java/org/openhab/binding/neeo/internal/handler/NeeoBrainHandler.java
bundles/org.openhab.binding.neeo/src/main/java/org/openhab/binding/neeo/internal/handler/NeeoDeviceHandler.java
bundles/org.openhab.binding.neeo/src/main/java/org/openhab/binding/neeo/internal/handler/NeeoForwardActionsServlet.java
bundles/org.openhab.binding.neeo/src/main/java/org/openhab/binding/neeo/internal/handler/NeeoRoomHandler.java
bundles/org.openhab.binding.neeo/src/main/java/org/openhab/binding/neeo/internal/models/NeeoDeviceDetails.java
bundles/org.openhab.binding.neeo/src/main/java/org/openhab/binding/neeo/internal/models/NeeoDevices.java
bundles/org.openhab.binding.neeo/src/main/java/org/openhab/binding/neeo/internal/models/NeeoMacros.java
bundles/org.openhab.binding.neeo/src/main/java/org/openhab/binding/neeo/internal/models/NeeoRecipes.java
bundles/org.openhab.binding.neeo/src/main/java/org/openhab/binding/neeo/internal/models/NeeoRooms.java
bundles/org.openhab.binding.neeo/src/main/java/org/openhab/binding/neeo/internal/models/NeeoScenarios.java
bundles/org.openhab.binding.neeo/src/main/java/org/openhab/binding/neeo/internal/net/HttpResponse.java
bundles/org.openhab.binding.neeo/src/main/resources/OH-INF/thing/thing-types.xml

index 26ffb6ac9bd5020b3d83c84579d14bfe762eeef6..9350a5e82a08df6cc1c83da3f4badb70c2da4bad 100644 (file)
 package org.openhab.binding.neeo.internal;
 
 import java.io.IOException;
+import java.lang.reflect.Type;
 import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Objects;
 import java.util.concurrent.atomic.AtomicReference;
 
@@ -31,6 +34,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import com.google.gson.Gson;
+import com.google.gson.reflect.TypeToken;
 
 /**
  * The class provides the API for communicating with a NEEO brain
@@ -162,7 +166,7 @@ public class NeeoBrainApi implements AutoCloseable {
      * @return the non-null, possibly empty list of active scenarios keys
      * @throws IOException Signals that an I/O exception has occurred.
      */
-    public String[] getActiveScenarios() throws IOException {
+    public List<String> getActiveScenarios() throws IOException {
         final String url = urlBuilder.append(NeeoConstants.GET_ACTIVESCENARIOS).toString();
 
         final HttpRequest rqst = request.get();
@@ -171,7 +175,9 @@ public class NeeoBrainApi implements AutoCloseable {
             throw resp.createException();
         }
 
-        return Objects.requireNonNull(gson.fromJson(resp.getContent(), String[].class));
+        Type arrayListType = new TypeToken<ArrayList<String>>() {
+        }.getType();
+        return Objects.requireNonNull(gson.fromJson(resp.getContent(), arrayListType));
     }
 
     /**
index 555ee0c0e7f72c06e1222213e1ee45f3c2a7d832..089ac6661f4036e48f793c01aeb3a8a316d1f78a 100644 (file)
@@ -132,4 +132,11 @@ public class NeeoBrainConfig {
     public void setCheckStatusInterval(int checkStatusInterval) {
         this.checkStatusInterval = checkStatusInterval;
     }
+
+    @Override
+    public String toString() {
+        return "NeeoBrainConfig{" + "ipAddress='" + ipAddress + '\'' + ", enableForwardActions=" + enableForwardActions
+                + ", forwardChain='" + forwardChain + '\'' + ", discoverEmptyRooms=" + discoverEmptyRooms
+                + ", checkStatusInterval=" + checkStatusInterval + '}';
+    }
 }
index e4abd30f59afe90d859b22d4f27c6b31cb8612c5..f8d014744a5a1fd73b819fa9e4f24c64eb86a75f 100644 (file)
 package org.openhab.binding.neeo.internal;
 
 import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Objects;
 import java.util.concurrent.atomic.AtomicReference;
 
-import org.apache.commons.lang.ArrayUtils;
-import org.apache.commons.lang.StringUtils;
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
 import org.openhab.binding.neeo.internal.models.ExecuteResult;
@@ -53,7 +53,7 @@ public class NeeoRoomProtocol {
     private final NeeoRoom neeoRoom;
 
     /** The currently active scenarios */
-    private final AtomicReference<String[]> activeScenarios = new AtomicReference<>(new String[0]);
+    private final AtomicReference<List<String>> activeScenarios = new AtomicReference<>(new ArrayList<>());
 
     /**
      * Instantiates a new neeo room protocol.
@@ -95,8 +95,8 @@ public class NeeoRoomProtocol {
         Objects.requireNonNull(action, "action cannot be null");
 
         final NeeoRecipes recipes = neeoRoom.getRecipes();
-        final boolean launch = StringUtils.equalsIgnoreCase(NeeoRecipe.LAUNCH, action.getAction());
-        final boolean poweroff = StringUtils.equalsIgnoreCase(NeeoRecipe.POWEROFF, action.getAction());
+        final boolean launch = NeeoRecipe.LAUNCH.equalsIgnoreCase(action.getAction());
+        final boolean poweroff = NeeoRecipe.POWEROFF.equalsIgnoreCase(action.getAction());
 
         // Can't be both true but if both false - it's neither one
         if (launch == poweroff) {
@@ -107,7 +107,7 @@ public class NeeoRoomProtocol {
         final NeeoRecipe recipe = recipeName == null ? null : recipes.getRecipeByName(recipeName);
         final String scenarioKey = recipe == null ? null : recipe.getScenarioKey();
 
-        if (scenarioKey != null && StringUtils.isNotEmpty(scenarioKey)) {
+        if (scenarioKey != null && !scenarioKey.isEmpty()) {
             processScenarioChange(scenarioKey, launch);
         } else {
             logger.debug("Could not find a recipe named '{}' for the action {}", recipeName, action);
@@ -123,18 +123,27 @@ public class NeeoRoomProtocol {
     private void processScenarioChange(String scenarioKey, boolean launch) {
         NeeoUtil.requireNotEmpty(scenarioKey, "scenarioKey cannot be empty");
 
-        final String[] activeScenarios = this.activeScenarios.get();
-        final int idx = ArrayUtils.indexOf(activeScenarios, scenarioKey);
+        List<String> oldActiveScenarios;
+        List<String> newActiveScenarios;
 
-        // already set that way
-        if ((idx < 0 && !launch) || (idx >= 0 && launch)) {
-            return;
-        }
+        do {
+            oldActiveScenarios = this.activeScenarios.get();
+            newActiveScenarios = new ArrayList<>(oldActiveScenarios);
 
-        final String[] newScenarios = idx >= 0 ? (String[]) ArrayUtils.remove(activeScenarios, idx)
-                : (String[]) ArrayUtils.add(activeScenarios, scenarioKey);
-
-        this.activeScenarios.set(newScenarios);
+            if (newActiveScenarios.contains(scenarioKey)) {
+                if (launch) {
+                    return;
+                } else {
+                    newActiveScenarios.remove(scenarioKey);
+                }
+            } else {
+                if (launch) {
+                    newActiveScenarios.add(scenarioKey);
+                } else {
+                    return;
+                }
+            }
+        } while (!this.activeScenarios.compareAndSet(oldActiveScenarios, newActiveScenarios));
 
         refreshScenarioStatus(scenarioKey);
     }
@@ -247,10 +256,9 @@ public class NeeoRoomProtocol {
 
         final NeeoScenario scenario = neeoRoom.getScenarios().getScenario(scenarioKey);
         if (scenario != null) {
-            final String[] active = activeScenarios.get();
-            final boolean isActive = ArrayUtils.contains(active, scenarioKey);
+            final boolean isActive = activeScenarios.get().contains(scenarioKey);
             callback.stateChanged(UidUtils.createChannelId(NeeoConstants.ROOM_GROUP_SCENARIO_ID,
-                    NeeoConstants.ROOM_CHANNEL_STATUS, scenarioKey), isActive ? OnOffType.ON : OnOffType.OFF);
+                    NeeoConstants.ROOM_CHANNEL_STATUS, scenarioKey), OnOffType.from(isActive));
         }
     }
 
@@ -263,19 +271,13 @@ public class NeeoRoomProtocol {
             logger.debug("API is null [likely bridge is offline]");
         } else {
             try {
-                final String[] activeScenarios = api.getActiveScenarios();
-                final String[] oldScenarios = this.activeScenarios.getAndSet(activeScenarios);
-
-                if (!ArrayUtils.isEquals(activeScenarios, oldScenarios)) {
-                    for (String scenario : activeScenarios) {
-                        refreshScenarioStatus(scenario);
-                    }
+                final List<String> activeScenarios = api.getActiveScenarios();
+                final List<String> oldScenarios = this.activeScenarios.getAndSet(activeScenarios);
 
-                    for (String oldScenario : oldScenarios) {
-                        if (!ArrayUtils.contains(activeScenarios, oldScenario)) {
-                            refreshScenarioStatus(oldScenario);
-                        }
-                    }
+                if (!activeScenarios.equals(oldScenarios)) {
+                    activeScenarios.forEach(this::refreshScenarioStatus);
+                    oldScenarios.removeIf(activeScenarios::contains);
+                    oldScenarios.forEach(this::refreshScenarioStatus);
                 }
             } catch (IOException e) {
                 logger.debug("Exception requesting active scenarios: {}", e.getMessage(), e);
@@ -292,7 +294,7 @@ public class NeeoRoomProtocol {
     private void sendCurrentStepTrigger(@Nullable String step) {
         callback.triggerEvent(
                 UidUtils.createChannelId(NeeoConstants.ROOM_GROUP_STATE_ID, NeeoConstants.ROOM_CHANNEL_CURRENTSTEP),
-                step == null || StringUtils.isEmpty(step) ? "" : step);
+                step == null || step.isEmpty() ? "" : step);
     }
 
     /**
@@ -312,10 +314,10 @@ public class NeeoRoomProtocol {
 
             if (recipe != null) {
                 if (recipe.isEnabled()) {
-                    final boolean isLaunch = StringUtils.equalsIgnoreCase(NeeoRecipe.LAUNCH, recipe.getType());
+                    final boolean isLaunch = NeeoRecipe.LAUNCH.equalsIgnoreCase(recipe.getType());
 
                     try {
-                        if (isLaunch || scenarioKey == null || StringUtils.isEmpty(scenarioKey)) {
+                        if (isLaunch || scenarioKey == null || scenarioKey.isEmpty()) {
                             handleExecuteResult(scenarioKey, recipeKey, true, api.executeRecipe(roomKey, recipeKey));
                         } else {
                             handleExecuteResult(scenarioKey, recipeKey, false, api.stopScenario(roomKey, scenarioKey));
@@ -345,7 +347,7 @@ public class NeeoRoomProtocol {
                 start ? NeeoRecipe.LAUNCH : NeeoRecipe.POWEROFF);
         final String recipeKey = recipe == null ? null : recipe.getKey();
 
-        if (recipe != null && recipeKey != null && StringUtils.isNotEmpty(recipeKey)) {
+        if (recipe != null && recipeKey != null && !recipeKey.isEmpty()) {
             if (recipe.isEnabled()) {
                 startRecipe(recipeKey);
             } else {
@@ -370,7 +372,7 @@ public class NeeoRoomProtocol {
         NeeoUtil.requireNotEmpty(recipeKey, "recipeKey cannot be empty");
 
         int nextStep = 0;
-        if (scenarioKey != null && StringUtils.isNotEmpty(scenarioKey)) {
+        if (scenarioKey != null && !scenarioKey.isEmpty()) {
             callback.scheduleTask(() -> {
                 processScenarioChange(scenarioKey, launch);
             }, 1);
index c393276da0721184de08f7eaaabd27543f5d1bb2..ee3a8e610b261138e46aeffdbfea192d4ea5c3b7 100644 (file)
  */
 package org.openhab.binding.neeo.internal;
 
-import java.util.Objects;
 import java.util.concurrent.Future;
 
-import org.apache.commons.lang.StringUtils;
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
 import org.openhab.binding.neeo.internal.models.NeeoDevices;
@@ -104,8 +102,7 @@ public class NeeoUtil {
      * @throws IllegalArgumentException if value is an empty string
      */
     public static void requireNotEmpty(@Nullable String value, String msg) {
-        Objects.requireNonNull(value, msg);
-        if (StringUtils.isEmpty(value)) {
+        if (value == null || value.isEmpty()) {
             throw new IllegalArgumentException(msg);
         }
     }
index 5a23a567c140feca7f63b0bbfc829973a86e1f48..fed0a432466e7d48994d8c342e3430f4f35dd270 100644 (file)
@@ -12,9 +12,6 @@
  */
 package org.openhab.binding.neeo.internal;
 
-import java.util.Objects;
-
-import org.apache.commons.lang.StringUtils;
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
 import org.openhab.binding.neeo.internal.models.NeeoDevice;
@@ -66,8 +63,6 @@ public class UidUtils {
      * @return the non-null, non empty (only 1 or 2 element) list of parts
      */
     public static String[] parseChannelId(ChannelUID uid) {
-        Objects.requireNonNull(uid, "uid cannot be null");
-
         final String channelId = uid.getIdWithoutGroup();
         final int idx = channelId.indexOf(DELIMITER);
         if (idx < 0 || idx == channelId.length() - 1) {
@@ -101,8 +96,8 @@ public class UidUtils {
     public static String createChannelId(@Nullable String groupId, String channelId, @Nullable String channelKey) {
         NeeoUtil.requireNotEmpty(channelId, "channelId cannot be empty");
 
-        return (StringUtils.isEmpty(groupId) ? "" : (groupId + "#"))
-                + (StringUtils.isEmpty(channelKey) ? channelId : (channelId + DELIMITER + channelKey));
+        return ((groupId == null || groupId.isEmpty()) ? "" : (groupId + "#"))
+                + ((channelKey == null || channelKey.isEmpty()) ? channelId : (channelId + DELIMITER + channelKey));
     }
 
     /**
@@ -129,6 +124,6 @@ public class UidUtils {
     public static ChannelUID createChannelUID(ThingUID thingUid, String groupId, String channelId,
             @Nullable String channelKey) {
         return new ChannelUID(thingUid, groupId,
-                channelId + (StringUtils.isEmpty(channelKey) ? "" : (DELIMITER + channelKey)));
+                channelId + ((channelKey == null || channelKey.isEmpty()) ? "" : (DELIMITER + channelKey)));
     }
 }
index bfdb2b834f7507ab1fc2443426e1c55a3bceb85f..e77d14a63d11001542d75ebaa2721ba3d98e3e8b 100644 (file)
@@ -24,7 +24,6 @@ import java.util.Set;
 
 import javax.jmdns.ServiceInfo;
 
-import org.apache.commons.lang.StringUtils;
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
 import org.openhab.binding.neeo.internal.NeeoConstants;
@@ -100,7 +99,7 @@ public class NeeoBrainDiscovery implements MDNSDiscoveryParticipant {
         }
 
         logger.debug("getThingUID is evaluating: {}", service);
-        if (!StringUtils.equals("neeo", service.getApplication())) {
+        if (!"neeo".equals(service.getApplication())) {
             logger.debug("Application not 'neeo' in MDNS serviceinfo: {}", service);
             return null;
         }
index 91aeb9f44169c69865ad0e04b777800b2d3a7fe3..820cc44311c1a1e3673cf4fe892bf68e70899b14 100644 (file)
@@ -17,7 +17,6 @@ import java.util.Collections;
 import java.util.Objects;
 import java.util.Set;
 
-import org.apache.commons.lang.StringUtils;
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.openhab.binding.neeo.internal.NeeoBrainApi;
 import org.openhab.binding.neeo.internal.NeeoConstants;
@@ -73,7 +72,7 @@ public class NeeoDeviceDiscoveryService extends AbstractDiscoveryService {
         final ThingUID roomUid = roomBridge.getUID();
 
         final String brainId = roomHandler.getNeeoBrainId();
-        if (brainId == null || StringUtils.isEmpty(brainId)) {
+        if (brainId == null || brainId.isEmpty()) {
             logger.debug("Unknown brain ID for roomHandler: {}", roomHandler);
             return;
         }
@@ -86,7 +85,7 @@ public class NeeoDeviceDiscoveryService extends AbstractDiscoveryService {
 
         final NeeoRoomConfig config = roomBridge.getConfiguration().as(NeeoRoomConfig.class);
         final String roomKey = config.getRoomKey();
-        if (roomKey == null || StringUtils.isEmpty(roomKey)) {
+        if (roomKey == null || roomKey.isEmpty()) {
             logger.debug("RoomKey wasn't configured for {} - skipping", brainId);
             return;
         }
@@ -103,7 +102,7 @@ public class NeeoDeviceDiscoveryService extends AbstractDiscoveryService {
             logger.debug("Room {} found, scanning {} devices in it", room.getName(), devices.length);
             for (NeeoDevice device : devices) {
                 final String deviceKey = device.getKey();
-                if (deviceKey == null || StringUtils.isEmpty(deviceKey)) {
+                if (deviceKey == null || deviceKey.isEmpty()) {
                     logger.debug("Device key wasn't found for device: {}", device);
                     continue;
                 }
index c863e63ef644896d79f1800b795b35b47e719cea..773b9e04d82abc9b087a8bd03527ccc4fdbadd4e 100644 (file)
@@ -17,7 +17,6 @@ import java.util.Collections;
 import java.util.Objects;
 import java.util.Set;
 
-import org.apache.commons.lang.StringUtils;
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.openhab.binding.neeo.internal.NeeoBrainApi;
 import org.openhab.binding.neeo.internal.NeeoBrainConfig;
@@ -91,7 +90,7 @@ public class NeeoRoomDiscoveryService extends AbstractDiscoveryService {
             logger.debug("Brain {} ({}) found, scanning {} rooms in it", brain.getName(), brainId, rooms.length);
             for (NeeoRoom room : rooms) {
                 final String roomKey = room.getKey();
-                if (roomKey == null || StringUtils.isEmpty(roomKey)) {
+                if (roomKey == null || roomKey.isEmpty()) {
                     logger.debug("Room didn't have a room key: {}", room);
                     continue;
                 }
index 76f70c13c9e312ca4b0c689b07a25c016c208836..e9ab97d126699fd4db1549d6cf078bc38078fd98 100644 (file)
@@ -14,9 +14,7 @@ package org.openhab.binding.neeo.internal.handler;
 
 import java.util.ArrayList;
 import java.util.List;
-import java.util.Objects;
 
-import org.apache.commons.lang.StringUtils;
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.openhab.binding.neeo.internal.NeeoConstants;
 import org.openhab.binding.neeo.internal.UidUtils;
@@ -47,18 +45,16 @@ class ChannelUtils {
      * @return a non-null but possibly empty list of {@link Channel} s
      */
     static List<Channel> generateChannels(ThingUID thingUid, NeeoDevice device) {
-        Objects.requireNonNull(thingUid, "thingUid cannot be null");
-        Objects.requireNonNull(device, "device cannot be null");
-
         final List<Channel> channels = new ArrayList<>();
         for (NeeoMacro macro : device.getMacros().getMacros()) {
             final String key = macro.getKey();
-            if (key != null && StringUtils.isNotEmpty(key)) {
-                final String label = StringUtils.isEmpty(macro.getName()) ? macro.getLabel() : macro.getName();
+            if (key != null && !key.isEmpty()) {
+                String name = macro.getName();
+                final String label = (name == null || name.isEmpty()) ? macro.getLabel() : name;
                 channels.add(ChannelBuilder
                         .create(UidUtils.createChannelUID(thingUid, NeeoConstants.DEVICE_GROUP_MACROS_ID,
                                 NeeoConstants.DEVICE_CHANNEL_STATUS, key), "Switch")
-                        .withLabel(label == null || StringUtils.isEmpty(label) ? key : label)
+                        .withLabel((label == null || label.isEmpty()) ? key : label)
                         .withType(NeeoConstants.DEVICE_MACRO_STATUS_UID).build());
             }
         }
@@ -74,9 +70,6 @@ class ChannelUtils {
      * @return a non-null but possibly empty list of {@link Channel} s
      */
     static List<Channel> generateChannels(ThingUID thingUid, NeeoRoom room) {
-        Objects.requireNonNull(thingUid, "thingUid cannot be null");
-        Objects.requireNonNull(room, "room cannot be null");
-
         final List<Channel> channels = new ArrayList<>();
         channels.addAll(generateStateChannels(thingUid));
         channels.addAll(generateScenarioChannels(thingUid, room.getScenarios()));
@@ -91,8 +84,6 @@ class ChannelUtils {
      * @return a non-null but possibly empty list of {@link Channel} s
      */
     private static List<Channel> generateStateChannels(ThingUID thingUid) {
-        Objects.requireNonNull(thingUid, "thingUid cannot be null");
-
         final List<Channel> channels = new ArrayList<>();
         channels.add(ChannelBuilder
                 .create(UidUtils.createChannelUID(thingUid, NeeoConstants.ROOM_GROUP_STATE_ID,
@@ -109,20 +100,15 @@ class ChannelUtils {
      * @return a non-null but possibly empty list of {@link Channel} s
      */
     private static List<Channel> generateScenarioChannels(ThingUID thingUid, NeeoScenarios scenarios) {
-        Objects.requireNonNull(thingUid, "thingUid cannot be null");
-        Objects.requireNonNull(scenarios, "scenarios cannot be null");
-
         final List<Channel> channels = new ArrayList<>();
         for (NeeoScenario scenario : scenarios.getScenarios()) {
             final String key = scenario.getKey();
-            if (key != null && StringUtils.isNotEmpty(key)) {
-                final String scenarioLabel = StringUtils.isEmpty(scenario.getName()) ? null : scenario.getName();
-                final String nameLabel = (scenarioLabel == null || StringUtils.isEmpty(scenarioLabel) ? key
-                        : scenarioLabel) + " Name";
-                final String configuredLabel = (scenarioLabel == null || StringUtils.isEmpty(scenarioLabel) ? key
-                        : scenarioLabel) + " Configured";
-                final String statusLabel = (scenarioLabel == null || StringUtils.isEmpty(scenarioLabel) ? key
-                        : scenarioLabel) + " Status";
+            if (key != null && !key.isEmpty()) {
+                final String name = scenario.getName();
+                final String scenarioLabel = (name == null || name.isEmpty()) ? key : name;
+                final String nameLabel = scenarioLabel + " Name";
+                final String configuredLabel = scenarioLabel + " Configured";
+                final String statusLabel = scenarioLabel + " Status";
 
                 channels.add(ChannelBuilder
                         .create(UidUtils.createChannelUID(thingUid, NeeoConstants.ROOM_GROUP_SCENARIO_ID,
@@ -149,22 +135,16 @@ class ChannelUtils {
      * @return a non-null but possibly empty list of {@link Channel} s
      */
     private static List<Channel> generateRecipeChannels(ThingUID thingUid, NeeoRecipes recipes) {
-        Objects.requireNonNull(thingUid, "thingUid cannot be null");
-        Objects.requireNonNull(recipes, "recipes cannot be null");
-
         final List<Channel> channels = new ArrayList<>();
         for (NeeoRecipe recipe : recipes.getRecipes()) {
             final String key = recipe.getKey();
-            if (key != null && StringUtils.isNotEmpty(key)) {
-                final String recipeLabel = StringUtils.isEmpty(recipe.getName()) ? null : recipe.getName();
-                final String nameLabel = (recipeLabel == null || StringUtils.isEmpty(recipeLabel) ? key : recipeLabel)
-                        + " Name (" + recipe.getType() + ")";
-                final String typeLabel = (recipeLabel == null || StringUtils.isEmpty(recipeLabel) ? key : recipeLabel)
-                        + " Type (" + recipe.getType() + ")";
-                final String enabledLabel = (recipeLabel == null || StringUtils.isEmpty(recipeLabel) ? key
-                        : recipeLabel) + " Enabled (" + recipe.getType() + ")";
-                final String statusLabel = (recipeLabel == null || StringUtils.isEmpty(recipeLabel) ? key : recipeLabel)
-                        + " Status (" + recipe.getType() + ")";
+            if (key != null && !key.isEmpty()) {
+                final String name = recipe.getName();
+                final String recipeLabel = (name == null || name.isEmpty()) ? key : name;
+                final String nameLabel = recipeLabel + " Name (" + recipe.getType() + ")";
+                final String typeLabel = recipeLabel + " Type (" + recipe.getType() + ")";
+                final String enabledLabel = recipeLabel + " Enabled (" + recipe.getType() + ")";
+                final String statusLabel = recipeLabel + " Status (" + recipe.getType() + ")";
 
                 channels.add(ChannelBuilder
                         .create(UidUtils.createChannelUID(thingUid, NeeoConstants.ROOM_GROUP_RECIPE_ID,
index 3c797a4ffb64bfadfb0d85f970f8394b2df035c4..a71482bb74d5b5c5c64986d3dee70f93df110b51 100644 (file)
@@ -31,7 +31,6 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
 import javax.servlet.ServletException;
 import javax.ws.rs.client.ClientBuilder;
 
-import org.apache.commons.lang.StringUtils;
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
 import org.openhab.binding.neeo.internal.NeeoBrainApi;
@@ -130,7 +129,7 @@ public class NeeoBrainHandler extends BaseBridgeHandler {
     }
 
     /**
-     * Handles any {@Commands} sent - this bridge has no commands and does nothing
+     * Handles any {@link Command} sent - this bridge has no commands and does nothing
      *
      * @see
      *      org.openhab.core.thing.binding.ThingHandler#handleCommand(org.openhab.core.thing.ChannelUID,
@@ -163,8 +162,10 @@ public class NeeoBrainHandler extends BaseBridgeHandler {
             NeeoUtil.checkInterrupt();
 
             final NeeoBrainConfig config = getBrainConfig();
+            logger.trace("Brain-UID {}: config is {}", thing.getUID(), config);
+
             final String ipAddress = config.getIpAddress();
-            if (ipAddress == null || StringUtils.isEmpty(ipAddress)) {
+            if (ipAddress == null || ipAddress.isEmpty()) {
                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
                         "Brain IP Address must be specified");
                 return;
@@ -186,7 +187,8 @@ public class NeeoBrainHandler extends BaseBridgeHandler {
             addProperty(properties, "Last Change", String.valueOf(brain.getLastChange()));
             updateProperties(properties);
 
-            if (config.isEnableForwardActions()) {
+            String forwardChain = config.getForwardChain();
+            if (config.isEnableForwardActions() && forwardChain != null && !forwardChain.isEmpty()) {
                 NeeoUtil.checkInterrupt();
 
                 forwardActionServlet = new NeeoForwardActionsServlet(scheduler, json -> {
@@ -200,7 +202,7 @@ public class NeeoBrainHandler extends BaseBridgeHandler {
                             ((NeeoRoomHandler) th).processAction(action);
                         }
                     }
-                }, config.getForwardChain(), clientBuilder);
+                }, forwardChain, clientBuilder);
 
                 NeeoUtil.checkInterrupt();
                 try {
@@ -240,7 +242,7 @@ public class NeeoBrainHandler extends BaseBridgeHandler {
             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
                     "Exception occurred connecting to brain: " + e.getMessage());
         } catch (InterruptedException e) {
-            logger.debug("Initializtion was interrupted", e);
+            logger.debug("Initialization was interrupted", e);
             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.HANDLER_INITIALIZING_ERROR,
                     "Initialization was interrupted");
         } finally {
@@ -256,9 +258,7 @@ public class NeeoBrainHandler extends BaseBridgeHandler {
      * @param value a possibly null, possibly empty key
      */
     private void addProperty(Map<String, String> properties, String key, @Nullable String value) {
-        Objects.requireNonNull(properties, "properties cannot be null");
-        NeeoUtil.requireNotEmpty(key, "key cannot be empty");
-        if (value != null && StringUtils.isNotEmpty(value)) {
+        if (value != null && !value.isEmpty()) {
             properties.put(key, value);
         }
     }
index 634c290e485a85d3a83cd0a3bba0a7bbdf55832c..17bef68723ea83b824cb74800f69cfa435c92fbc 100644 (file)
@@ -13,6 +13,7 @@
 package org.openhab.binding.neeo.internal.handler;
 
 import java.io.IOException;
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Objects;
@@ -20,8 +21,8 @@ import java.util.concurrent.Future;
 import java.util.concurrent.ScheduledFuture;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicReference;
+import java.util.stream.Collectors;
 
-import org.apache.commons.lang.StringUtils;
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
 import org.openhab.binding.neeo.internal.NeeoBrainApi;
@@ -104,11 +105,11 @@ public class NeeoDeviceHandler extends BaseThingHandler {
         }
 
         final String localGroupId = channelUID.getGroupId();
-        final String groupId = localGroupId == null || StringUtils.isEmpty(localGroupId) ? "" : localGroupId;
+        final String groupId = localGroupId == null || localGroupId.isEmpty() ? "" : localGroupId;
         final String channelId = channelIds[0];
         final String channelKey = channelIds.length > 1 ? channelIds[1] : "";
 
-        if (StringUtils.isEmpty(groupId)) {
+        if (groupId.isEmpty()) {
             logger.debug("GroupID for channel is null - ignoring command: {}", channelUID);
             return;
         }
@@ -176,14 +177,14 @@ public class NeeoDeviceHandler extends BaseThingHandler {
         final NeeoDeviceConfig config = getConfigAs(NeeoDeviceConfig.class);
 
         final String roomKey = getRoomKey();
-        if (roomKey == null || StringUtils.isEmpty(roomKey)) {
+        if (roomKey == null || roomKey.isEmpty()) {
             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
                     "Room key (from the parent room bridge) was not found");
             return;
         }
 
         final String deviceKey = config.getDeviceKey();
-        if (deviceKey == null || StringUtils.isEmpty(deviceKey)) {
+        if (deviceKey == null || deviceKey.isEmpty()) {
             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
                     "Device key was not found or empty");
             return;
@@ -225,7 +226,8 @@ public class NeeoDeviceHandler extends BaseThingHandler {
                     properties.put("Shutdown Delay", toString(timing.getShutdownDelay()));
                 }
 
-                properties.put("Device Capabilities", StringUtils.join(details.getDeviceCapabilities(), ','));
+                properties.put("Device Capabilities",
+                        Arrays.stream(details.getDeviceCapabilities()).collect(Collectors.joining(",")));
             }
 
             final ThingBuilder thingBuilder = editThing();
@@ -292,7 +294,7 @@ public class NeeoDeviceHandler extends BaseThingHandler {
     private void addProperty(Map<String, String> properties, String key, @Nullable String value) {
         Objects.requireNonNull(properties, "properties cannot be null");
         NeeoUtil.requireNotEmpty(key, "key cannot be empty");
-        if (value != null && StringUtils.isNotEmpty(value)) {
+        if (value != null && !value.isEmpty()) {
             properties.put(key, value);
         }
     }
index 5c2565b5ec1c285a1de646c261bfad949acc4a46..299d820263c6203a0f67aed9660c7f5c9b730fdb 100644 (file)
 package org.openhab.binding.neeo.internal.handler;
 
 import java.io.IOException;
-import java.util.Objects;
 import java.util.concurrent.ScheduledExecutorService;
+import java.util.stream.Collectors;
 
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.ws.rs.client.ClientBuilder;
 
-import org.apache.commons.io.IOUtils;
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
 import org.eclipse.jetty.http.HttpStatus;
@@ -48,7 +47,6 @@ public class NeeoForwardActionsServlet extends HttpServlet {
     private final Callback callback;
 
     /** The forwarding chain */
-    @Nullable
     private final String forwardChain;
 
     /** The {@link ClientBuilder} to use */
@@ -64,13 +62,10 @@ public class NeeoForwardActionsServlet extends HttpServlet {
      * @param callback a non-null {@link Callback}
      * @param forwardChain a possibly null, possibly empty forwarding chain
      */
-    NeeoForwardActionsServlet(ScheduledExecutorService scheduler, Callback callback, @Nullable String forwardChain,
+    NeeoForwardActionsServlet(ScheduledExecutorService scheduler, Callback callback, String forwardChain,
             ClientBuilder clientBuilder) {
         super();
 
-        Objects.requireNonNull(scheduler, "scheduler cannot be null");
-        Objects.requireNonNull(callback, "callback cannot be null");
-
         this.scheduler = scheduler;
         this.callback = callback;
         this.forwardChain = forwardChain;
@@ -78,7 +73,7 @@ public class NeeoForwardActionsServlet extends HttpServlet {
     }
 
     /**
-     * Processes the post action from the NEEO brain. Simply get's the specified json and then forwards it on (if
+     * Processes the post action from the NEEO brain. Simply gets the specified json and then forwards it on (if
      * needed)
      *
      * @param req the non-null request
@@ -91,27 +86,24 @@ public class NeeoForwardActionsServlet extends HttpServlet {
             return;
         }
 
-        final String json = IOUtils.toString(req.getReader());
+        final String json = req.getReader().lines().collect(Collectors.joining("\n"));
         logger.debug("handleForwardActions {}", json);
 
         callback.post(json);
 
-        final String fc = forwardChain;
-        if (fc != null && !fc.isEmpty()) {
-            scheduler.execute(() -> {
-                try (final HttpRequest request = new HttpRequest(clientBuilder)) {
-                    for (final String forwardUrl : fc.split(",")) {
-                        if (forwardUrl != null && !forwardUrl.isEmpty()) {
-                            final HttpResponse httpResponse = request.sendPostJsonCommand(forwardUrl, json);
-                            if (httpResponse.getHttpCode() != HttpStatus.OK_200) {
-                                logger.debug("Cannot forward event {} to {}: {}", json, forwardUrl,
-                                        httpResponse.getHttpCode());
-                            }
+        scheduler.execute(() -> {
+            try (final HttpRequest request = new HttpRequest(clientBuilder)) {
+                for (final String forwardUrl : forwardChain.split(",")) {
+                    if (forwardUrl != null && !forwardUrl.isEmpty()) {
+                        final HttpResponse httpResponse = request.sendPostJsonCommand(forwardUrl, json);
+                        if (httpResponse.getHttpCode() != HttpStatus.OK_200) {
+                            logger.debug("Cannot forward event {} to {}: {}", json, forwardUrl,
+                                    httpResponse.getHttpCode());
                         }
                     }
                 }
-            });
-        }
+            }
+        });
     }
 
     interface Callback {
index 0b2b1a43c7c36abcbf9a8d2cba1b871656af9fc7..71e134696d3831acb6f4899151fb0e2c67d45620 100644 (file)
@@ -20,7 +20,6 @@ import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicReference;
 
-import org.apache.commons.lang.StringUtils;
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
 import org.openhab.binding.neeo.internal.NeeoBrainApi;
@@ -99,7 +98,7 @@ public class NeeoRoomHandler extends BaseBridgeHandler {
         }
 
         final String localGroupId = channelUID.getGroupId();
-        final String groupId = localGroupId == null || StringUtils.isEmpty(localGroupId) ? "" : localGroupId;
+        final String groupId = localGroupId == null || localGroupId.isEmpty() ? "" : localGroupId;
         final String channelId = channelIds[0];
         final String channelKey = channelIds.length > 1 ? channelIds[1] : "";
 
@@ -195,7 +194,7 @@ public class NeeoRoomHandler extends BaseBridgeHandler {
         final NeeoRoomConfig config = getConfigAs(NeeoRoomConfig.class);
 
         final String roomKey = config.getRoomKey();
-        if (roomKey == null || StringUtils.isEmpty(roomKey)) {
+        if (roomKey == null || roomKey.isEmpty()) {
             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
                     "Room key (from the parent room bridge) was not found");
             return;
index 78f40143a13fe182079fa5604d2592ab990baa63..4a4818982997609644ad85e5b0e067c55ee6071c 100644 (file)
@@ -12,7 +12,8 @@
  */
 package org.openhab.binding.neeo.internal.models;
 
-import org.apache.commons.lang.StringUtils;
+import java.util.Arrays;
+
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
 
@@ -123,8 +124,8 @@ public class NeeoDeviceDetails {
 
     @Override
     public String toString() {
-        return "NeeoDeviceDetails [sourceName=" + sourceName + ", adapterName=" + adapterName + ", type=" + type
-                + ", manufacturer=" + manufacturer + ", name=" + name + ", timing=" + timing + ", deviceCapabilities="
-                + StringUtils.join(deviceCapabilities, ',') + "]";
+        return "NeeoDeviceDetails{" + "sourceName='" + sourceName + '\'' + ", adapterName='" + adapterName + '\''
+                + ", type='" + type + '\'' + ", manufacturer='" + manufacturer + '\'' + ", name='" + name + '\''
+                + ", timing=" + timing + ", deviceCapabilities=" + Arrays.toString(deviceCapabilities) + '}';
     }
 }
index 31c7e62d943035732898b050b28f14d260132800..f86a99600c0a2aefdd9357c5eebb2f9ac9850626 100644 (file)
@@ -15,7 +15,6 @@ package org.openhab.binding.neeo.internal.models;
 import java.util.Arrays;
 import java.util.Objects;
 
-import org.apache.commons.lang.StringUtils;
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
 
@@ -59,7 +58,7 @@ public class NeeoDevices {
     @Nullable
     public NeeoDevice getDevice(String key) {
         for (NeeoDevice device : getDevices()) {
-            if (StringUtils.equalsIgnoreCase(key, device.getKey())) {
+            if (key.equalsIgnoreCase(device.getKey())) {
                 return device;
             }
         }
index 9c73342332dfbfdb22a5d970cd60191628438eb7..2d371590e0baf07478c2b5b171f4d6a3e36a91db 100644 (file)
@@ -15,7 +15,6 @@ package org.openhab.binding.neeo.internal.models;
 import java.util.Arrays;
 import java.util.Objects;
 
-import org.apache.commons.lang.StringUtils;
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
 
@@ -59,7 +58,7 @@ public class NeeoMacros {
     @Nullable
     public NeeoMacro getMacro(String key) {
         for (NeeoMacro macro : getMacros()) {
-            if (StringUtils.equalsIgnoreCase(key, macro.getKey())) {
+            if (key.equalsIgnoreCase(macro.getKey())) {
                 return macro;
             }
         }
index bbaa183dbae99624c1b46e1f2acff8d27fd1bb5c..68451210d9a8371ea2900c884f0d9a1b170f3035 100644 (file)
@@ -13,9 +13,7 @@
 package org.openhab.binding.neeo.internal.models;
 
 import java.util.Arrays;
-import java.util.Objects;
 
-import org.apache.commons.lang.StringUtils;
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
 
@@ -28,7 +26,7 @@ import org.eclipse.jdt.annotation.Nullable;
 public class NeeoRecipes {
 
     /** The recipes. */
-    private NeeoRecipe @Nullable [] recipes;
+    private NeeoRecipe[] recipes;
 
     /**
      * Creates the recipes from the given recipes
@@ -36,7 +34,6 @@ public class NeeoRecipes {
      * @param recipes the recipes
      */
     NeeoRecipes(NeeoRecipe[] recipes) {
-        Objects.requireNonNull(recipes, "recipes cannot be null");
         this.recipes = recipes;
     }
 
@@ -46,8 +43,7 @@ public class NeeoRecipes {
      * @return the recipes
      */
     public NeeoRecipe[] getRecipes() {
-        final NeeoRecipe[] localRecipes = recipes;
-        return localRecipes == null ? new NeeoRecipe[0] : localRecipes;
+        return recipes;
     }
 
     /**
@@ -58,12 +54,12 @@ public class NeeoRecipes {
      */
     @Nullable
     public NeeoRecipe getRecipe(String key) {
-        if (recipes == null || StringUtils.isEmpty(key)) {
+        if (key.isEmpty()) {
             return null;
         }
 
-        for (NeeoRecipe recipe : getRecipes()) {
-            if (StringUtils.equalsIgnoreCase(key, recipe.getKey())) {
+        for (NeeoRecipe recipe : recipes) {
+            if (key.equalsIgnoreCase(recipe.getKey())) {
                 return recipe;
             }
         }
@@ -79,13 +75,12 @@ public class NeeoRecipes {
      */
     @Nullable
     public NeeoRecipe getRecipeByScenarioKey(String key, String type) {
-        if (recipes == null || StringUtils.isEmpty(key)) {
+        if (key.isEmpty()) {
             return null;
         }
 
-        for (NeeoRecipe recipe : getRecipes()) {
-            if (StringUtils.equalsIgnoreCase(key, recipe.getScenarioKey())
-                    && StringUtils.equalsIgnoreCase(type, recipe.getType())) {
+        for (NeeoRecipe recipe : recipes) {
+            if (key.equalsIgnoreCase(recipe.getScenarioKey()) && type.equalsIgnoreCase(recipe.getType())) {
                 return recipe;
             }
         }
@@ -100,12 +95,12 @@ public class NeeoRecipes {
      */
     @Nullable
     public NeeoRecipe getRecipeByName(String name) {
-        if (recipes == null || StringUtils.isEmpty(name)) {
+        if (name.isEmpty()) {
             return null;
         }
 
-        for (NeeoRecipe recipe : getRecipes()) {
-            if (StringUtils.equalsIgnoreCase(name, recipe.getName())) {
+        for (NeeoRecipe recipe : recipes) {
+            if (name.equalsIgnoreCase(recipe.getName())) {
                 return recipe;
             }
         }
index 877c6ff721588d6d95a8be6fe7767fc34b231add..f40999ee8c865b4933e1101919b2afcf4f6ff23b 100644 (file)
@@ -15,7 +15,6 @@ package org.openhab.binding.neeo.internal.models;
 import java.util.Arrays;
 import java.util.Objects;
 
-import org.apache.commons.lang.StringUtils;
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
 
@@ -58,7 +57,7 @@ public class NeeoRooms {
      */
     NeeoRoom getRoom(String key) {
         for (NeeoRoom room : getRooms()) {
-            if (StringUtils.equalsIgnoreCase(key, room.getKey())) {
+            if (key.equalsIgnoreCase(room.getKey())) {
                 return room;
             }
         }
index e854bcbd129c36e8a83a576caad007af70bf7807..ca816d0154cf916310a90181284df60a162af58f 100644 (file)
@@ -15,7 +15,6 @@ package org.openhab.binding.neeo.internal.models;
 import java.util.Arrays;
 import java.util.Objects;
 
-import org.apache.commons.lang.StringUtils;
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
 
@@ -59,7 +58,7 @@ public class NeeoScenarios {
     @Nullable
     public NeeoScenario getScenario(String key) {
         for (NeeoScenario scenario : getScenarios()) {
-            if (StringUtils.equalsIgnoreCase(key, scenario.getKey())) {
+            if (key.equalsIgnoreCase(scenario.getKey())) {
                 return scenario;
             }
         }
index 909dddaeb8d93b5d3d0cd8b56e1f83906d88b694..42489786f1adc62dad3cab7f9748e06e008232a1 100644 (file)
@@ -21,7 +21,6 @@ import java.util.Objects;
 
 import javax.ws.rs.core.Response;
 
-import org.apache.commons.io.IOUtils;
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
 
@@ -59,7 +58,7 @@ public class HttpResponse {
 
         if (response.hasEntity()) {
             InputStream is = response.readEntity(InputStream.class);
-            contents = IOUtils.toByteArray(is);
+            contents = is.readAllBytes();
         } else {
             contents = null;
         }
index 6a4d92ad2c7c995590ee144ba0da2ae640ca6f39..30c3b26655f2b97354d562126cfe42e0ad8d9ca8 100644 (file)
@@ -30,7 +30,6 @@
                        <parameter name="forwardChain" type="text">
                                <label>Forward Chaining</label>
                                <description>Comma delimited list of URLs to forward NEEO brain actions to</description>
-                               <default>true</default>
                                <advanced>true</advanced>
                        </parameter>
                        <parameter name="checkStatusInterval" type="integer">