]> git.basschouten.com Git - openhab-addons.git/blob
e3dc89f207fb1c8be81e2ecd50383f615b357c9c
[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.mpd.internal.action;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.mpd.internal.handler.MPDHandler;
18 import org.openhab.core.automation.annotation.ActionInput;
19 import org.openhab.core.automation.annotation.RuleAction;
20 import org.openhab.core.thing.binding.ThingActions;
21 import org.openhab.core.thing.binding.ThingActionsScope;
22 import org.openhab.core.thing.binding.ThingHandler;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * The {@link MPDActions} defines rule actions for the Music Player Daemon binding.
28  *
29  * @author Stefan Röllin - Initial contribution
30  */
31 @ThingActionsScope(name = "mpd")
32 @NonNullByDefault
33 public class MPDActions implements ThingActions {
34
35     private final Logger logger = LoggerFactory.getLogger(MPDActions.class);
36
37     private @Nullable MPDHandler handler = null;
38
39     @Override
40     public void setThingHandler(@Nullable ThingHandler handler) {
41         if (handler instanceof MPDHandler mpdHandler) {
42             this.handler = mpdHandler;
43         }
44     }
45
46     @Override
47     public @Nullable ThingHandler getThingHandler() {
48         return handler;
49     }
50
51     @RuleAction(label = "send a command with a parameter", description = "Send a command to the Music Player Daemon.")
52     public void sendCommand(@ActionInput(name = "command") @Nullable String command,
53             @ActionInput(name = "parameter") @Nullable String parameter) {
54         logger.debug("sendCommand called with {}", command);
55
56         MPDHandler handler = this.handler;
57         if (handler != null) {
58             handler.sendCommand(command, parameter);
59         } else {
60             logger.warn("MPD Action service ThingHandler is null!");
61         }
62     }
63
64     @RuleAction(label = "send a command", description = "Send a command to the Music Player Daemon.")
65     public void sendCommand(@ActionInput(name = "command") @Nullable String command) {
66         logger.debug("sendCommand called with {}", command);
67
68         MPDHandler handler = this.handler;
69         if (handler != null) {
70             handler.sendCommand(command);
71         } else {
72             logger.warn("MPD Action service ThingHandler is null!");
73         }
74     }
75
76     public static void sendCommand(ThingActions actions, @Nullable String command, @Nullable String parameter) {
77         ((MPDActions) actions).sendCommand(command, parameter);
78     }
79
80     public static void sendCommand(ThingActions actions, @Nullable String command) {
81         ((MPDActions) actions).sendCommand(command);
82     }
83 }