]> git.basschouten.com Git - openhab-addons.git/blob
79f00578160ee3cb859ad34730b955a7530c8ccf
[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.lang.reflect.Type;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.concurrent.CompletableFuture;
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.TimeUnit;
21 import java.util.concurrent.TimeoutException;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.eclipse.jetty.http.HttpMethod;
26 import org.openhab.binding.deconz.internal.dto.NewSceneResponse;
27 import org.openhab.binding.deconz.internal.handler.GroupThingHandler;
28 import org.openhab.core.automation.annotation.ActionInput;
29 import org.openhab.core.automation.annotation.ActionOutput;
30 import org.openhab.core.automation.annotation.RuleAction;
31 import org.openhab.core.thing.binding.ThingActions;
32 import org.openhab.core.thing.binding.ThingActionsScope;
33 import org.openhab.core.thing.binding.ThingHandler;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 import com.google.gson.Gson;
38 import com.google.gson.JsonParseException;
39 import com.google.gson.reflect.TypeToken;
40
41 /**
42  * The {@link GroupActions} provides actions for managing scenes in groups
43  *
44  * @author Jan N. Klug - Initial contribution
45  */
46 @ThingActionsScope(name = "deconz")
47 @NonNullByDefault
48 public class GroupActions implements ThingActions {
49     private static final String NEW_SCENE_ID_OUTPUT = "newSceneId";
50     private static final Type NEW_SCENE_RESPONSE_TYPE = new TypeToken<List<NewSceneResponse>>() {
51     }.getType();
52
53     private final Logger logger = LoggerFactory.getLogger(GroupActions.class);
54     private final Gson gson = new Gson();
55
56     private @Nullable GroupThingHandler handler;
57
58     @RuleAction(label = "@text/action.create-scene.label", description = "@text/action.create-scene.description")
59     public @ActionOutput(name = NEW_SCENE_ID_OUTPUT, type = "java.lang.Integer") Map<String, Object> createScene(
60             @ActionInput(name = "name", label = "@text/action.create-scene.name.label", description = "@text/action.create-scene.name.description") @Nullable String name) {
61         GroupThingHandler handler = this.handler;
62
63         if (handler == null) {
64             logger.warn("Deconz GroupActions service ThingHandler is null!");
65             return Map.of();
66         }
67
68         if (name == null) {
69             logger.debug("Skipping scene creation due to missing scene name");
70             return Map.of();
71         }
72
73         CompletableFuture<String> newSceneId = new CompletableFuture<>();
74         handler.doNetwork(Map.of("name", name), "scenes", HttpMethod.POST, newSceneId::complete);
75
76         try {
77             String returnedJson = newSceneId.get(2000, TimeUnit.MILLISECONDS);
78             List<NewSceneResponse> newSceneResponses = gson.fromJson(returnedJson, NEW_SCENE_RESPONSE_TYPE);
79             if (newSceneResponses != null && !newSceneResponses.isEmpty()) {
80                 return Map.of(NEW_SCENE_ID_OUTPUT, newSceneResponses.get(0).success.id);
81             }
82             throw new IllegalStateException("response is empty");
83         } catch (InterruptedException | ExecutionException | TimeoutException | JsonParseException
84                 | IllegalStateException e) {
85             logger.warn("Couldn't get newSceneId", e);
86             return Map.of();
87         }
88     }
89
90     public static Map<String, Object> createScene(ThingActions actions, @Nullable String name) {
91         if (actions instanceof GroupActions groupActions) {
92             return groupActions.createScene(name);
93         }
94         return Map.of();
95     }
96
97     @RuleAction(label = "@text/action.delete-scene.label", description = "@text/action.delete-scene.description")
98     public void deleteScene(
99             @ActionInput(name = "sceneId", label = "@text/action.delete-scene.sceneId.label", description = "@text/action.delete-scene.sceneId.description") @Nullable Integer sceneId) {
100         GroupThingHandler handler = this.handler;
101
102         if (handler == null) {
103             logger.warn("Deconz GroupActions service ThingHandler is null!");
104             return;
105         }
106
107         if (sceneId == null) {
108             logger.warn("Skipping scene deletion due to missing scene id");
109             return;
110         }
111
112         handler.doNetwork(null, "scenes/" + sceneId, HttpMethod.DELETE, null);
113     }
114
115     public static void deleteScene(ThingActions actions, @Nullable Integer sceneId) {
116         if (actions instanceof GroupActions groupActions) {
117             groupActions.deleteScene(sceneId);
118         }
119     }
120
121     @RuleAction(label = "@text/action.store-as-scene.label", description = "@text/action.store-as-scene.description")
122     public void storeScene(
123             @ActionInput(name = "sceneId", label = "@text/action.store-as-scene.sceneId.label", description = "@text/action.store-as-scene.sceneId.description") @Nullable Integer sceneId) {
124         GroupThingHandler handler = this.handler;
125
126         if (handler == null) {
127             logger.warn("Deconz GroupActions service ThingHandler is null!");
128             return;
129         }
130
131         if (sceneId == null) {
132             logger.warn("Skipping scene storage due to missing scene id");
133             return;
134         }
135
136         handler.doNetwork(null, "scenes/" + sceneId + "/store", HttpMethod.PUT, null);
137     }
138
139     public static void storeScene(ThingActions actions, @Nullable Integer sceneId) {
140         if (actions instanceof GroupActions groupActions) {
141             groupActions.storeScene(sceneId);
142         }
143     }
144
145     @Override
146     public void setThingHandler(@Nullable ThingHandler handler) {
147         if (handler instanceof GroupThingHandler) {
148             this.handler = (GroupThingHandler) handler;
149         }
150     }
151
152     @Override
153     public @Nullable ThingHandler getThingHandler() {
154         return handler;
155     }
156 }