[neeo] fix forwardActionServlet and code improvements (#9929)
Signed-off-by: Jan N. Klug <jan.n.klug@rub.de>
This commit is contained in:
@@ -14,10 +14,9 @@ package org.openhab.binding.neeo.internal;
|
|||||||
|
|
||||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
import org.eclipse.jdt.annotation.Nullable;
|
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
|
* @author Tim Roberts - initial contribution
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -14,12 +14,11 @@ package org.openhab.binding.neeo.internal;
|
|||||||
|
|
||||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
import org.eclipse.jdt.annotation.Nullable;
|
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
|
@NonNullByDefault
|
||||||
public class NeeoDeviceConfig {
|
public class NeeoDeviceConfig {
|
||||||
|
|||||||
@@ -16,17 +16,16 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
|
|||||||
import org.eclipse.jdt.annotation.Nullable;
|
import org.eclipse.jdt.annotation.Nullable;
|
||||||
import org.openhab.core.thing.ThingStatus;
|
import org.openhab.core.thing.ThingStatus;
|
||||||
import org.openhab.core.thing.ThingStatusDetail;
|
import org.openhab.core.thing.ThingStatusDetail;
|
||||||
import org.openhab.core.thing.binding.ThingHandler;
|
|
||||||
import org.openhab.core.types.State;
|
import org.openhab.core.types.State;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* This interface is used to provide a callback mechanism between a @link {@link ThingHandler} and the assoicated
|
* This interface is used to provide a callback mechanism between a {@link org.openhab.core.thing.binding.ThingHandler}
|
||||||
* protocol.
|
* 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
|
* This is necessary since the status and state of a bridge/thing is private and the protocol handler cannot access it
|
||||||
* directly.
|
* directly.
|
||||||
*
|
*
|
||||||
* @author Tim Roberts - initial contribution
|
* @author Tim Roberts - Initial contribution
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@NonNullByDefault
|
@NonNullByDefault
|
||||||
|
|||||||
@@ -14,10 +14,9 @@ package org.openhab.binding.neeo.internal;
|
|||||||
|
|
||||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
import org.eclipse.jdt.annotation.Nullable;
|
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
|
* @author Tim Roberts - initial contribution
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -46,7 +46,6 @@ import org.openhab.core.thing.Thing;
|
|||||||
import org.openhab.core.thing.ThingStatus;
|
import org.openhab.core.thing.ThingStatus;
|
||||||
import org.openhab.core.thing.ThingStatusDetail;
|
import org.openhab.core.thing.ThingStatusDetail;
|
||||||
import org.openhab.core.thing.binding.BaseBridgeHandler;
|
import org.openhab.core.thing.binding.BaseBridgeHandler;
|
||||||
import org.openhab.core.thing.binding.ThingHandler;
|
|
||||||
import org.openhab.core.types.Command;
|
import org.openhab.core.types.Command;
|
||||||
import org.osgi.service.http.HttpService;
|
import org.osgi.service.http.HttpService;
|
||||||
import org.osgi.service.http.NamespaceException;
|
import org.osgi.service.http.NamespaceException;
|
||||||
@@ -187,22 +186,16 @@ public class NeeoBrainHandler extends BaseBridgeHandler {
|
|||||||
addProperty(properties, "Last Change", String.valueOf(brain.getLastChange()));
|
addProperty(properties, "Last Change", String.valueOf(brain.getLastChange()));
|
||||||
updateProperties(properties);
|
updateProperties(properties);
|
||||||
|
|
||||||
String forwardChain = config.getForwardChain();
|
if (config.isEnableForwardActions()) {
|
||||||
if (config.isEnableForwardActions() && forwardChain != null && !forwardChain.isEmpty()) {
|
|
||||||
NeeoUtil.checkInterrupt();
|
NeeoUtil.checkInterrupt();
|
||||||
|
|
||||||
forwardActionServlet = new NeeoForwardActionsServlet(scheduler, json -> {
|
forwardActionServlet = new NeeoForwardActionsServlet(scheduler, json -> {
|
||||||
triggerChannel(NeeoConstants.CHANNEL_BRAIN_FOWARDACTIONS, json);
|
triggerChannel(NeeoConstants.CHANNEL_BRAIN_FOWARDACTIONS, json);
|
||||||
|
|
||||||
final NeeoAction action = Objects.requireNonNull(gson.fromJson(json, NeeoAction.class));
|
final NeeoAction action = Objects.requireNonNull(gson.fromJson(json, NeeoAction.class));
|
||||||
|
getThing().getThings().stream().map(Thing::getHandler).filter(NeeoRoomHandler.class::isInstance)
|
||||||
for (final Thing child : getThing().getThings()) {
|
.forEach(h -> ((NeeoRoomHandler) h).processAction(action));
|
||||||
final ThingHandler th = child.getHandler();
|
}, config.getForwardChain(), clientBuilder);
|
||||||
if (th instanceof NeeoRoomHandler) {
|
|
||||||
((NeeoRoomHandler) th).processAction(action);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}, forwardChain, clientBuilder);
|
|
||||||
|
|
||||||
NeeoUtil.checkInterrupt();
|
NeeoUtil.checkInterrupt();
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ package org.openhab.binding.neeo.internal.handler;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.concurrent.ScheduledExecutorService;
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
import java.util.function.Consumer;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServlet;
|
import javax.servlet.http.HttpServlet;
|
||||||
@@ -44,10 +45,10 @@ public class NeeoForwardActionsServlet extends HttpServlet {
|
|||||||
private final Logger logger = LoggerFactory.getLogger(NeeoForwardActionsServlet.class);
|
private final Logger logger = LoggerFactory.getLogger(NeeoForwardActionsServlet.class);
|
||||||
|
|
||||||
/** The event publisher */
|
/** The event publisher */
|
||||||
private final Callback callback;
|
private final Consumer<String> callback;
|
||||||
|
|
||||||
/** The forwarding chain */
|
/** The forwarding chain */
|
||||||
private final String forwardChain;
|
private final @Nullable String forwardChain;
|
||||||
|
|
||||||
/** The {@link ClientBuilder} to use */
|
/** The {@link ClientBuilder} to use */
|
||||||
private final ClientBuilder clientBuilder;
|
private final ClientBuilder clientBuilder;
|
||||||
@@ -56,14 +57,14 @@ public class NeeoForwardActionsServlet extends HttpServlet {
|
|||||||
private final ScheduledExecutorService scheduler;
|
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 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
|
* @param forwardChain a possibly null, possibly empty forwarding chain
|
||||||
*/
|
*/
|
||||||
NeeoForwardActionsServlet(ScheduledExecutorService scheduler, Callback callback, String forwardChain,
|
NeeoForwardActionsServlet(ScheduledExecutorService scheduler, Consumer<String> callback,
|
||||||
ClientBuilder clientBuilder) {
|
@Nullable String forwardChain, ClientBuilder clientBuilder) {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
this.scheduler = scheduler;
|
this.scheduler = scheduler;
|
||||||
@@ -89,24 +90,22 @@ public class NeeoForwardActionsServlet extends HttpServlet {
|
|||||||
final String json = req.getReader().lines().collect(Collectors.joining("\n"));
|
final String json = req.getReader().lines().collect(Collectors.joining("\n"));
|
||||||
logger.debug("handleForwardActions {}", json);
|
logger.debug("handleForwardActions {}", json);
|
||||||
|
|
||||||
callback.post(json);
|
callback.accept(json);
|
||||||
|
|
||||||
scheduler.execute(() -> {
|
if (forwardChain != null && !forwardChain.isEmpty()) {
|
||||||
try (final HttpRequest request = new HttpRequest(clientBuilder)) {
|
scheduler.execute(() -> {
|
||||||
for (final String forwardUrl : forwardChain.split(",")) {
|
try (final HttpRequest request = new HttpRequest(clientBuilder)) {
|
||||||
if (forwardUrl != null && !forwardUrl.isEmpty()) {
|
for (final String forwardUrl : forwardChain.split(",")) {
|
||||||
final HttpResponse httpResponse = request.sendPostJsonCommand(forwardUrl, json);
|
if (!forwardUrl.isEmpty()) {
|
||||||
if (httpResponse.getHttpCode() != HttpStatus.OK_200) {
|
final HttpResponse httpResponse = request.sendPostJsonCommand(forwardUrl, json);
|
||||||
logger.debug("Cannot forward event {} to {}: {}", json, forwardUrl,
|
if (httpResponse.getHttpCode() != HttpStatus.OK_200) {
|
||||||
httpResponse.getHttpCode());
|
logger.debug("Cannot forward event {} to {}: {}", json, forwardUrl,
|
||||||
|
httpResponse.getHttpCode());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
});
|
}
|
||||||
}
|
|
||||||
|
|
||||||
interface Callback {
|
|
||||||
void post(String json);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user