]> git.basschouten.com Git - openhab-addons.git/blob
358adc0df791539d0af96d2695bfdffa652c1118
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.deconz.internal.action;
14
15 import java.util.Map;
16 import java.util.Objects;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.eclipse.jetty.http.HttpMethod;
21 import org.openhab.binding.deconz.internal.Util;
22 import org.openhab.binding.deconz.internal.handler.DeconzBridgeHandler;
23 import org.openhab.core.automation.annotation.ActionInput;
24 import org.openhab.core.automation.annotation.RuleAction;
25 import org.openhab.core.thing.binding.ThingActions;
26 import org.openhab.core.thing.binding.ThingActionsScope;
27 import org.openhab.core.thing.binding.ThingHandler;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * The {@link BridgeActions} provides actions for managing scenes in groups
33  *
34  * @author Jan N. Klug - Initial contribution
35  */
36 @ThingActionsScope(name = "deconz")
37 @NonNullByDefault
38 public class BridgeActions implements ThingActions {
39     private final Logger logger = LoggerFactory.getLogger(BridgeActions.class);
40
41     private @Nullable DeconzBridgeHandler handler;
42
43     @RuleAction(label = "@text/action.permit-join-network.label", description = "@text/action.permit-join-network.description")
44     public void permitJoin(
45             @ActionInput(name = "duration", label = "@text/action.permit-join-network.duration.label", description = "@text/action.permit-join-network.duration.description") @Nullable Integer duration) {
46         DeconzBridgeHandler handler = this.handler;
47
48         if (handler == null) {
49             logger.warn("Deconz BridgeActions service ThingHandler is null!");
50             return;
51         }
52
53         int searchDuration = Util.constrainToRange(Objects.requireNonNullElse(duration, 120), 1, 240);
54
55         Object object = Map.of("permitjoin", searchDuration);
56         handler.sendObject("config", object, HttpMethod.PUT).thenAccept(v -> {
57             if (v.getResponseCode() != java.net.HttpURLConnection.HTTP_OK) {
58                 logger.warn("Sending {} via PUT to config failed: {} - {}", object, v.getResponseCode(), v.getBody());
59             } else {
60                 logger.trace("Result code={}, body={}", v.getResponseCode(), v.getBody());
61                 logger.info("Enabled device searching for {} seconds on bridge {}.", searchDuration,
62                         handler.getThing().getUID());
63             }
64         }).exceptionally(e -> {
65             logger.warn("Sending {} via PUT to config failed: {} - {}", object, e.getClass(), e.getMessage());
66             return null;
67         });
68     }
69
70     public static void permitJoin(ThingActions actions, @Nullable Integer duration) {
71         if (actions instanceof BridgeActions bridgeActions) {
72             bridgeActions.permitJoin(duration);
73         }
74     }
75
76     @Override
77     public void setThingHandler(@Nullable ThingHandler handler) {
78         if (handler instanceof DeconzBridgeHandler bridgeHandler) {
79             this.handler = bridgeHandler;
80         }
81     }
82
83     @Override
84     public @Nullable ThingHandler getThingHandler() {
85         return handler;
86     }
87 }