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.satel.action;
15 import java.lang.reflect.Method;
16 import java.lang.reflect.Proxy;
17 import java.util.HashMap;
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;
33 * Automation action handler service for reading Satel event log.
35 * @author Krzysztof Goworek - Initial contribution
36 * @see SatelEventLogHandler
38 @ThingActionsScope(name = "satel")
40 public class SatelEventLogActions implements ThingActions, ISatelEventLogActions {
42 private final Logger logger = LoggerFactory.getLogger(getClass());
44 private @Nullable SatelEventLogHandler handler;
47 public void setThingHandler(@Nullable ThingHandler handler) {
48 if (handler instanceof SatelEventLogHandler) {
49 this.handler = (SatelEventLogHandler) handler;
54 public @Nullable ThingHandler getThingHandler() {
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);
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());
77 public static Map<String, Object> readEvent(@Nullable ThingActions actions, @Nullable Number index) {
78 return invokeMethodOf(actions).readEvent(index);
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);
93 throw new IllegalArgumentException("actions is not an instance of ISatelEventLogActions");