2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.mpd.internal.action;
15 import java.lang.reflect.Method;
16 import java.lang.reflect.Proxy;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.mpd.internal.handler.MPDHandler;
21 import org.openhab.core.automation.annotation.ActionInput;
22 import org.openhab.core.automation.annotation.RuleAction;
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.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * The {@link @MPDActions} defines rule actions for the Music Player Daemon binding.
32 * @author Stefan Röllin - Initial contribution
34 @ThingActionsScope(name = "mpd")
36 public class MPDActions implements ThingActions, IMPDActions {
38 private final Logger logger = LoggerFactory.getLogger(MPDActions.class);
40 private @Nullable MPDHandler handler = null;
43 public void setThingHandler(@Nullable ThingHandler handler) {
44 if (handler instanceof MPDHandler) {
45 this.handler = (MPDHandler) handler;
50 public @Nullable ThingHandler getThingHandler() {
55 @RuleAction(label = "MPD : Send command", description = "Send a command to the Music Player Daemon.")
56 public void sendCommand(@ActionInput(name = "command") @Nullable String command,
57 @ActionInput(name = "parameter") @Nullable String parameter) {
58 logger.debug("sendCommand called with {}", command);
60 MPDHandler handler = this.handler;
61 if (handler != null) {
62 handler.sendCommand(command, parameter);
64 logger.warn("MPD Action service ThingHandler is null!");
69 @RuleAction(label = "MPD : Send command", description = "Send a command to the Music Player Daemon.")
70 public void sendCommand(@ActionInput(name = "command") @Nullable String command) {
71 logger.debug("sendCommand called with {}", command);
73 MPDHandler handler = this.handler;
74 if (handler != null) {
75 handler.sendCommand(command);
77 logger.warn("MPD Action service ThingHandler is null!");
81 private static IMPDActions invokeMethodOf(@Nullable ThingActions actions) {
82 if (actions == null) {
83 throw new IllegalArgumentException("actions cannot be null");
85 if (actions.getClass().getName().equals(MPDActions.class.getName())) {
86 if (actions instanceof IMPDActions) {
87 return (IMPDActions) actions;
89 return (IMPDActions) Proxy.newProxyInstance(IMPDActions.class.getClassLoader(),
90 new Class[] { IMPDActions.class }, (Object proxy, Method method, Object[] args) -> {
91 Method m = actions.getClass().getDeclaredMethod(method.getName(),
92 method.getParameterTypes());
93 return m.invoke(actions, args);
97 throw new IllegalArgumentException("Actions is not an instance of MPDActions");
100 public static void sendCommand(@Nullable ThingActions actions, @Nullable String command,
101 @Nullable String parameter) {
102 invokeMethodOf(actions).sendCommand(command, parameter);
105 public static void sendCommand(@Nullable ThingActions actions, @Nullable String command) {
106 invokeMethodOf(actions).sendCommand(command);