]> git.basschouten.com Git - openhab-addons.git/commitdiff
[neeo] fix forwardActionServlet and code improvements (#9929)
authorJ-N-K <J-N-K@users.noreply.github.com>
Sun, 24 Jan 2021 19:55:48 +0000 (20:55 +0100)
committerGitHub <noreply@github.com>
Sun, 24 Jan 2021 19:55:48 +0000 (11:55 -0800)
Signed-off-by: Jan N. Klug <jan.n.klug@rub.de>
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/NeeoDeviceConfig.java
bundles/org.openhab.binding.neeo/src/main/java/org/openhab/binding/neeo/internal/NeeoHandlerCallback.java
bundles/org.openhab.binding.neeo/src/main/java/org/openhab/binding/neeo/internal/NeeoRoomConfig.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/NeeoForwardActionsServlet.java

index 52fc4173b285a2c2fa327416f1f102010655f503..843e63d3b2b618a5f85bde366e733dd5233636e5 100644 (file)
@@ -14,10 +14,9 @@ package org.openhab.binding.neeo.internal;
 
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
-import org.openhab.binding.neeo.internal.handler.NeeoBrainHandler;
 
 /**
- * The configuration class a neeo brain and used by {@link NeeoBrainHandler}
+ * Configuration used by {@link org.openhab.binding.neeo.internal.handler.NeeoBrainHandler}
  *
  * @author Tim Roberts - initial contribution
  */
index 2be34c3afe77c4681084ee06f22b3a48af1898b7..5f1244f6cf775dad689bea123b3b980e7e68a7ed 100644 (file)
@@ -14,12 +14,11 @@ package org.openhab.binding.neeo.internal;
 
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
-import org.openhab.binding.neeo.internal.handler.NeeoDeviceHandler;
 
 /**
- * THe configuration class for the device used by {@link NeeoDeviceHandler}
+ * Configuration used by {@link org.openhab.binding.neeo.internal.handler.NeeoDeviceHandler}
  *
- * @author Tim Roberts - initial contribution
+ * @author Tim Roberts - Initial contribution
  */
 @NonNullByDefault
 public class NeeoDeviceConfig {
index bd5307dc2a3048bd21fe23997fdbbcc159f3a0b1..5794fffa971b269cb8150a2a9dd99f6410c75fb7 100644 (file)
@@ -16,17 +16,16 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
 import org.openhab.core.thing.ThingStatus;
 import org.openhab.core.thing.ThingStatusDetail;
-import org.openhab.core.thing.binding.ThingHandler;
 import org.openhab.core.types.State;
 
 /**
  *
- * This interface is used to provide a callback mechanism between a @link {@link ThingHandler} and the assoicated
- * protocol.
+ * This interface is used to provide a callback mechanism between a {@link org.openhab.core.thing.binding.ThingHandler}
+ * and the associated protocol.
  * This is necessary since the status and state of a bridge/thing is private and the protocol handler cannot access it
  * directly.
  *
- * @author Tim Roberts - initial contribution
+ * @author Tim Roberts - Initial contribution
  *
  */
 @NonNullByDefault
index 720e62779c595e80de5279e233edea0e0c3f69ad..dfeec4c86fbe60ab66ea4ad5a32d3de6752f5ff3 100644 (file)
@@ -14,10 +14,9 @@ package org.openhab.binding.neeo.internal;
 
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
-import org.openhab.binding.neeo.internal.handler.NeeoRoomHandler;
 
 /**
- * THe configuration class for the room used by {@link NeeoRoomHandler}
+ * Configuration used by {@link org.openhab.binding.neeo.internal.handler.NeeoRoomHandler}
  *
  * @author Tim Roberts - initial contribution
  */
index b907cc15ed0da801fa052d73a8bd562e3d59e584..9892ca882ed888a476b37f909ebe5aefea59a691 100644 (file)
@@ -46,7 +46,6 @@ import org.openhab.core.thing.Thing;
 import org.openhab.core.thing.ThingStatus;
 import org.openhab.core.thing.ThingStatusDetail;
 import org.openhab.core.thing.binding.BaseBridgeHandler;
-import org.openhab.core.thing.binding.ThingHandler;
 import org.openhab.core.types.Command;
 import org.osgi.service.http.HttpService;
 import org.osgi.service.http.NamespaceException;
@@ -187,22 +186,16 @@ public class NeeoBrainHandler extends BaseBridgeHandler {
             addProperty(properties, "Last Change", String.valueOf(brain.getLastChange()));
             updateProperties(properties);
 
-            String forwardChain = config.getForwardChain();
-            if (config.isEnableForwardActions() && forwardChain != null && !forwardChain.isEmpty()) {
+            if (config.isEnableForwardActions()) {
                 NeeoUtil.checkInterrupt();
 
                 forwardActionServlet = new NeeoForwardActionsServlet(scheduler, json -> {
                     triggerChannel(NeeoConstants.CHANNEL_BRAIN_FOWARDACTIONS, json);
 
                     final NeeoAction action = Objects.requireNonNull(gson.fromJson(json, NeeoAction.class));
-
-                    for (final Thing child : getThing().getThings()) {
-                        final ThingHandler th = child.getHandler();
-                        if (th instanceof NeeoRoomHandler) {
-                            ((NeeoRoomHandler) th).processAction(action);
-                        }
-                    }
-                }, forwardChain, clientBuilder);
+                    getThing().getThings().stream().map(Thing::getHandler).filter(NeeoRoomHandler.class::isInstance)
+                            .forEach(h -> ((NeeoRoomHandler) h).processAction(action));
+                }, config.getForwardChain(), clientBuilder);
 
                 NeeoUtil.checkInterrupt();
                 try {
index dc1815d17ea774c41497bcfad4c9be6524e60e08..1fe60c5167bda09bdbd4acae97e0e3b639c06da8 100644 (file)
@@ -14,6 +14,7 @@ package org.openhab.binding.neeo.internal.handler;
 
 import java.io.IOException;
 import java.util.concurrent.ScheduledExecutorService;
+import java.util.function.Consumer;
 import java.util.stream.Collectors;
 
 import javax.servlet.http.HttpServlet;
@@ -44,10 +45,10 @@ public class NeeoForwardActionsServlet extends HttpServlet {
     private final Logger logger = LoggerFactory.getLogger(NeeoForwardActionsServlet.class);
 
     /** The event publisher */
-    private final Callback callback;
+    private final Consumer<String> callback;
 
     /** The forwarding chain */
-    private final String forwardChain;
+    private final @Nullable String forwardChain;
 
     /** The {@link ClientBuilder} to use */
     private final ClientBuilder clientBuilder;
@@ -56,14 +57,14 @@ public class NeeoForwardActionsServlet extends HttpServlet {
     private final ScheduledExecutorService scheduler;
 
     /**
-     * Creates the servlet the will process foward action events from the NEEO brain.
+     * Creates the servlet the will process forward action events from the NEEO brain.
      *
      * @param scheduler a non-null {@link ScheduledExecutorService} to schedule forward actions
-     * @param callback a non-null {@link Callback}
+     * @param callback a non-null String consumer
      * @param forwardChain a possibly null, possibly empty forwarding chain
      */
-    NeeoForwardActionsServlet(ScheduledExecutorService scheduler, Callback callback, String forwardChain,
-            ClientBuilder clientBuilder) {
+    NeeoForwardActionsServlet(ScheduledExecutorService scheduler, Consumer<String> callback,
+            @Nullable String forwardChain, ClientBuilder clientBuilder) {
         super();
 
         this.scheduler = scheduler;
@@ -89,24 +90,22 @@ public class NeeoForwardActionsServlet extends HttpServlet {
         final String json = req.getReader().lines().collect(Collectors.joining("\n"));
         logger.debug("handleForwardActions {}", json);
 
-        callback.post(json);
-
-        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());
+        callback.accept(json);
+
+        if (forwardChain != null && !forwardChain.isEmpty()) {
+            scheduler.execute(() -> {
+                try (final HttpRequest request = new HttpRequest(clientBuilder)) {
+                    for (final String forwardUrl : forwardChain.split(",")) {
+                        if (!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 {
-        void post(String json);
+            });
+        }
     }
 }