]> git.basschouten.com Git - openhab-addons.git/blob
1f919c930050fc45106eafd4fcaad4351acc3a40
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.internal.action;
14
15 import java.util.HashMap;
16 import java.util.Map;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.satel.internal.handler.SatelEventLogHandler;
21 import org.openhab.core.automation.annotation.ActionInput;
22 import org.openhab.core.automation.annotation.ActionOutput;
23 import org.openhab.core.automation.annotation.RuleAction;
24 import org.openhab.core.thing.binding.ThingActions;
25 import org.openhab.core.thing.binding.ThingActionsScope;
26 import org.openhab.core.thing.binding.ThingHandler;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Automation action handler service for reading Satel event log.
32  *
33  * @author Krzysztof Goworek - Initial contribution
34  * @see SatelEventLogHandler
35  */
36 @ThingActionsScope(name = "satel")
37 @NonNullByDefault
38 public class SatelEventLogActions implements ThingActions {
39
40     private final Logger logger = LoggerFactory.getLogger(getClass());
41
42     private @Nullable SatelEventLogHandler handler;
43
44     @Override
45     public void setThingHandler(@Nullable ThingHandler handler) {
46         if (handler instanceof SatelEventLogHandler) {
47             this.handler = (SatelEventLogHandler) handler;
48         }
49     }
50
51     @Override
52     public @Nullable ThingHandler getThingHandler() {
53         return handler;
54     }
55
56     @RuleAction(label = "@text/actionReadEventLabel", description = "@text/actionReadEventDesc")
57     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(
58             @ActionInput(name = "index", label = "@text/actionInputIndexLabel", description = "@text/actionInputIndexDesc") @Nullable Number index) {
59         logger.debug("satel.readEvent called with input: index={}", index);
60
61         Map<String, Object> result = new HashMap<>();
62         SatelEventLogHandler handler = this.handler;
63         if (handler != null) {
64             handler.readEvent(index == null ? -1 : index.intValue()).ifPresent(event -> {
65                 result.put("index", event.getIndex());
66                 result.put("prev_index", event.getPrevIndex());
67                 result.put("timestamp", event.getTimestamp());
68                 result.put("description", event.getDescription());
69                 result.put("details", event.getDetails());
70             });
71         }
72         return result;
73     }
74
75     public static Map<String, Object> readEvent(ThingActions actions, @Nullable Number index) {
76         return ((SatelEventLogActions) actions).readEvent(index);
77     }
78 }