]> git.basschouten.com Git - openhab-addons.git/blob
74493647cbd21bb498d54e6a1d7a904b6e2b655b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.tr064.internal.phonebook;
14
15 import java.util.Collection;
16 import java.util.Optional;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.tr064.internal.Tr064RootHandler;
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  * The {@link PhonebookActions} is responsible for handling phonebook actions
32  *
33  * @author Jan N. Klug - Initial contribution
34  */
35 @ThingActionsScope(name = "tr064")
36 @NonNullByDefault
37 @SuppressWarnings("unused")
38 public class PhonebookActions implements ThingActions {
39     private final Logger logger = LoggerFactory.getLogger(PhonebookActions.class);
40
41     private @Nullable Tr064RootHandler handler;
42
43     @RuleAction(label = "@text/phonebookLookupActionLabel", description = "@text/phonebookLookupActionDescription")
44     public @ActionOutput(name = "name", type = "java.lang.String") String phonebookLookup(
45             @ActionInput(name = "phonenumber") @Nullable String phonenumber,
46             @ActionInput(name = "matches") @Nullable Integer matchCount) {
47         return phonebookLookup(phonenumber, null, matchCount);
48     }
49
50     @RuleAction(label = "@text/phonebookLookupActionLabel", description = "@text/phonebookLookupActionDescription")
51     public @ActionOutput(name = "name", type = "java.lang.String") String phonebookLookup(
52             @ActionInput(name = "phonenumber") @Nullable String phonenumber) {
53         return phonebookLookup(phonenumber, null, null);
54     }
55
56     @RuleAction(label = "@text/phonebookLookupActionLabel", description = "@text/phonebookLookupActionDescription")
57     public @ActionOutput(name = "name", type = "java.lang.String") String phonebookLookup(
58             @ActionInput(name = "phonenumber") @Nullable String phonenumber,
59             @ActionInput(name = "phonebook") @Nullable String phonebook) {
60         return phonebookLookup(phonenumber, phonebook, null);
61     }
62
63     @RuleAction(label = "@text/phonebookLookupActionLabel", description = "@text/phonebookLookupActionDescription")
64     public @ActionOutput(name = "name", type = "java.lang.String") String phonebookLookup(
65             @ActionInput(name = "phonenumber") @Nullable String phonenumber,
66             @ActionInput(name = "phonebook") @Nullable String phonebook,
67             @ActionInput(name = "matches") @Nullable Integer matchCount) {
68         if (phonenumber == null) {
69             logger.warn("Cannot lookup a missing number.");
70             return "";
71         }
72
73         final Tr064RootHandler handler = this.handler;
74         if (handler == null) {
75             logger.info("Handler is null, cannot lookup number.");
76             return phonenumber;
77         } else {
78             int matchCountInt = matchCount == null ? 0 : matchCount;
79             if (phonebook != null && !phonebook.isEmpty()) {
80                 return handler.getPhonebookByName(phonebook).flatMap(p -> p.lookupNumber(phonenumber, matchCountInt))
81                         .orElse(phonenumber);
82             } else {
83                 Collection<Phonebook> phonebooks = handler.getPhonebooks();
84                 return phonebooks.stream().map(p -> p.lookupNumber(phonenumber, matchCountInt))
85                         .filter(Optional::isPresent).map(Optional::get).findAny().orElse(phonenumber);
86             }
87         }
88     }
89
90     public static String phonebookLookup(ThingActions actions, @Nullable String phonenumber,
91             @Nullable Integer matchCount) {
92         return phonebookLookup(actions, phonenumber, null, matchCount);
93     }
94
95     public static String phonebookLookup(ThingActions actions, @Nullable String phonenumber) {
96         return phonebookLookup(actions, phonenumber, null, null);
97     }
98
99     public static String phonebookLookup(ThingActions actions, @Nullable String phonenumber,
100             @Nullable String phonebook) {
101         return phonebookLookup(actions, phonenumber, phonebook, null);
102     }
103
104     public static String phonebookLookup(ThingActions actions, @Nullable String phonenumber, @Nullable String phonebook,
105             @Nullable Integer matchCount) {
106         return ((PhonebookActions) actions).phonebookLookup(phonenumber, phonebook, matchCount);
107     }
108
109     @Override
110     public void setThingHandler(@Nullable ThingHandler handler) {
111         if (handler instanceof Tr064RootHandler) {
112             this.handler = (Tr064RootHandler) handler;
113         }
114     }
115
116     @Override
117     public @Nullable ThingHandler getThingHandler() {
118         return handler;
119     }
120 }