]> git.basschouten.com Git - openhab-addons.git/blob
87cb88325b42eaf6f42b057413242a964f42b957
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.hue.internal.action;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.hue.internal.handler.Clip2ThingHandler;
18 import org.openhab.core.automation.annotation.ActionInput;
19 import org.openhab.core.automation.annotation.RuleAction;
20 import org.openhab.core.library.types.QuantityType;
21 import org.openhab.core.library.unit.MetricPrefix;
22 import org.openhab.core.library.unit.Units;
23 import org.openhab.core.thing.binding.ThingActions;
24 import org.openhab.core.thing.binding.ThingActionsScope;
25 import org.openhab.core.thing.binding.ThingHandler;
26 import org.openhab.core.types.Command;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Implementation of the {@link ThingActions} interface used for sending 'dynamics' commands to Hue API v2 devices,
32  * rooms or zones.
33  *
34  * @author Andrew Fiddian-Green - Initial contribution
35  */
36 @ThingActionsScope(name = "hue")
37 @NonNullByDefault
38 public class DynamicsActions implements ThingActions {
39
40     private final Logger logger = LoggerFactory.getLogger(DynamicsActions.class);
41
42     private @Nullable Clip2ThingHandler handler;
43
44     public static void dynamicCommand(ThingActions actions, @Nullable String channelId, @Nullable Command command,
45             @Nullable Long durationMs) {
46         ((DynamicsActions) actions).dynamicCommand(channelId, command, durationMs);
47     }
48
49     @RuleAction(label = "@text/dynamics.action.label", description = "@text/dynamics.action.description")
50     public void dynamicCommand(
51             @ActionInput(name = "channelId", label = "@text/dynamics.channel.label", description = "@text/dynamics.channel.description") @Nullable String channelId,
52             @ActionInput(name = "command", label = "@text/dynamics.command.label", description = "@text/dynamics.command.description") @Nullable Command command,
53             @ActionInput(name = "durationMs", label = "@text/dynamics.duration.label", description = "@text/dynamics.duration.description") @Nullable Long durationMs) {
54         //
55         Clip2ThingHandler handler = this.handler;
56         if (handler == null) {
57             logger.warn("ThingHandler is null.");
58             return;
59         }
60         if (channelId == null) {
61             logger.debug("Channel ID is null.");
62             return;
63         }
64         if (command == null) {
65             logger.debug("Command is null.");
66             return;
67         }
68         if (durationMs == null || durationMs.longValue() <= 0) {
69             logger.debug("Duration is null, zero or negative.");
70             return;
71         }
72         handler.handleDynamicsCommand(channelId, command,
73                 new QuantityType<>(durationMs.longValue(), MetricPrefix.MILLI(Units.SECOND)));
74         logger.debug("Dynamic command '{}' sent to channelId '{}' with duration {}ms.", command, channelId, durationMs);
75     }
76
77     @Override
78     public @Nullable ThingHandler getThingHandler() {
79         return handler;
80     }
81
82     @Override
83     public void setThingHandler(@Nullable ThingHandler handler) {
84         this.handler = (Clip2ThingHandler) handler;
85     }
86 }