[neeo] fix forwardActionServlet and code improvements (#9929)

Signed-off-by: Jan N. Klug <jan.n.klug@rub.de>
This commit is contained in:
J-N-K 2021-01-24 20:55:48 +01:00 committed by GitHub
parent 7df9290b5d
commit 506d21face
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 31 additions and 43 deletions

View 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
*/

View 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 {

View 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

View 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
*/

View 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 {

View 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);
callback.accept(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());
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);
});
}
}
}