]> git.basschouten.com Git - openhab-addons.git/blob
85450853798c3859de24b205de82acd734a6176d
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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 java.lang.reflect.Method;
16 import java.lang.reflect.Proxy;
17
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;
28
29 /**
30  * The {@link @MPDActions} defines rule actions for the Music Player Daemon binding.
31  *
32  * @author Stefan Röllin - Initial contribution
33  */
34 @ThingActionsScope(name = "mpd")
35 @NonNullByDefault
36 public class MPDActions implements ThingActions, IMPDActions {
37
38     private final Logger logger = LoggerFactory.getLogger(MPDActions.class);
39
40     private @Nullable MPDHandler handler = null;
41
42     @Override
43     public void setThingHandler(@Nullable ThingHandler handler) {
44         if (handler instanceof MPDHandler) {
45             this.handler = (MPDHandler) handler;
46         }
47     }
48
49     @Override
50     public @Nullable ThingHandler getThingHandler() {
51         return handler;
52     }
53
54     @Override
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);
59
60         MPDHandler handler = this.handler;
61         if (handler != null) {
62             handler.sendCommand(command, parameter);
63         } else {
64             logger.warn("MPD Action service ThingHandler is null!");
65         }
66     }
67
68     @Override
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);
72
73         MPDHandler handler = this.handler;
74         if (handler != null) {
75             handler.sendCommand(command);
76         } else {
77             logger.warn("MPD Action service ThingHandler is null!");
78         }
79     }
80
81     private static IMPDActions invokeMethodOf(@Nullable ThingActions actions) {
82         if (actions == null) {
83             throw new IllegalArgumentException("actions cannot be null");
84         }
85         if (actions.getClass().getName().equals(MPDActions.class.getName())) {
86             if (actions instanceof IMPDActions) {
87                 return (IMPDActions) actions;
88             } else {
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);
94                         });
95             }
96         }
97         throw new IllegalArgumentException("Actions is not an instance of MPDActions");
98     }
99
100     public static void sendCommand(@Nullable ThingActions actions, @Nullable String command,
101             @Nullable String parameter) {
102         invokeMethodOf(actions).sendCommand(command, parameter);
103     }
104
105     public static void sendCommand(@Nullable ThingActions actions, @Nullable String command) {
106         invokeMethodOf(actions).sendCommand(command);
107     }
108 }