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
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;
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 {
import java.io.IOException;
import java.util.concurrent.ScheduledExecutorService;
+import java.util.function.Consumer;
import java.util.stream.Collectors;
import javax.servlet.http.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;
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;
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);
+ });
+ }
}
}