]> git.basschouten.com Git - openhab-addons.git/blob
95d2a4732f646b8dae16e4fd43075e7860963880
[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.satel.action;
14
15 import java.lang.reflect.Method;
16 import java.lang.reflect.Proxy;
17 import java.util.HashMap;
18 import java.util.Map;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.satel.internal.handler.SatelEventLogHandler;
23 import org.openhab.core.automation.annotation.ActionInput;
24 import org.openhab.core.automation.annotation.ActionOutput;
25 import org.openhab.core.automation.annotation.RuleAction;
26 import org.openhab.core.thing.binding.ThingActions;
27 import org.openhab.core.thing.binding.ThingActionsScope;
28 import org.openhab.core.thing.binding.ThingHandler;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * Automation action handler service for reading Satel event log.
34  *
35  * @author Krzysztof Goworek - Initial contribution
36  * @see SatelEventLogHandler
37  */
38 @ThingActionsScope(name = "satel")
39 @NonNullByDefault
40 public class SatelEventLogActions implements ThingActions, ISatelEventLogActions {
41
42     private final Logger logger = LoggerFactory.getLogger(getClass());
43
44     private @Nullable SatelEventLogHandler handler;
45
46     @Override
47     public void setThingHandler(@Nullable ThingHandler handler) {
48         if (handler instanceof SatelEventLogHandler) {
49             this.handler = (SatelEventLogHandler) handler;
50         }
51     }
52
53     @Override
54     public @Nullable ThingHandler getThingHandler() {
55         return handler;
56     }
57
58     @Override
59     @RuleAction(label = "@text/actionReadEventLabel", description = "@text/actionReadEventDesc")
60     public @ActionOutput(name = "index", type = "java.lang.Integer", label = "@text/actionOutputIndexLabel", description = "@text/actionOutputIndexDesc") @ActionOutput(name = "prev_index", type = "java.lang.Integer", label = "@text/actionOutputPrevIndexLabel", description = "@text/actionOutputPrevIndexDesc") @ActionOutput(name = "timestamp", type = "java.time.ZonedDateTime", label = "@text/actionOutputTimestampLabel", description = "@text/actionOutputTimestampDesc") @ActionOutput(name = "description", type = "java.lang.String", label = "@text/actionOutputDescriptionLabel", description = "@text/actionOutputDescriptionDesc") @ActionOutput(name = "details", type = "java.lang.String", label = "@text/actionOutputDetailsLabel", description = "@text/actionOutputDetailsDesc") Map<String, Object> readEvent(
61             @ActionInput(name = "index", label = "@text/actionInputIndexLabel", description = "@text/actionInputIndexDesc") @Nullable Number index) {
62         logger.debug("satel.readEvent called with input: index={}", index);
63
64         Map<String, Object> result = new HashMap<>();
65         if (handler != null) {
66             handler.readEvent(index == null ? -1 : index.intValue()).ifPresent(event -> {
67                 result.put("index", event.getIndex());
68                 result.put("prev_index", event.getPrevIndex());
69                 result.put("timestamp", event.getTimestamp());
70                 result.put("description", event.getDescription());
71                 result.put("details", event.getDetails());
72             });
73         }
74         return result;
75     }
76
77     public static Map<String, Object> readEvent(@Nullable ThingActions actions, @Nullable Number index) {
78         return invokeMethodOf(actions).readEvent(index);
79     }
80
81     private static ISatelEventLogActions invokeMethodOf(@Nullable ThingActions actions) {
82         if (actions == null) {
83             throw new IllegalArgumentException("actions cannot be null");
84         } else if (actions instanceof ISatelEventLogActions) {
85             return (ISatelEventLogActions) actions;
86         } else if (actions.getClass().getName().equals(SatelEventLogActions.class.getName())) {
87             return (ISatelEventLogActions) Proxy.newProxyInstance(ISatelEventLogActions.class.getClassLoader(),
88                     new Class[] { ISatelEventLogActions.class }, (Object proxy, Method method, Object[] args) -> {
89                         Method m = actions.getClass().getDeclaredMethod(method.getName(), method.getParameterTypes());
90                         return m.invoke(actions, args);
91                     });
92         }
93         throw new IllegalArgumentException("actions is not an instance of ISatelEventLogActions");
94     }
95 }