]> git.basschouten.com Git - openhab-addons.git/blob
7a907277ffdc65bc61515ed955d9c5b1fe69da32
[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.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 public class PhonebookActions implements ThingActions {
38     private final Logger logger = LoggerFactory.getLogger(PhonebookActions.class);
39
40     private @Nullable Tr064RootHandler handler;
41
42     @RuleAction(label = "@text/phonebookLookupActionLabel", description = "@text/phonebookLookupActionDescription")
43     public @ActionOutput(name = "name", label = "@text/phonebookLookupActionOutputLabel", description = "@text/phonebookLookupActionOutputDescription", type = "java.lang.String") String phonebookLookup(
44             @ActionInput(name = "phonenumber", label = "@text/phonebookLookupActionInputPhoneNumberLabel", description = "@text/phonebookLookupActionInputPhoneNumberDescription", type = "java.lang.String", required = true) @Nullable String phonenumber,
45             @ActionInput(name = "matches", label = "@text/phonebookLookupActionInputMatchesLabel", description = "@text/phonebookLookupActionInputMatchesDescription", type = "java.lang.Integer") @Nullable Integer matchCount) {
46         return phonebookLookup(phonenumber, null, matchCount);
47     }
48
49     @RuleAction(label = "@text/phonebookLookupActionLabel", description = "@text/phonebookLookupActionDescription")
50     public @ActionOutput(name = "name", label = "@text/phonebookLookupActionOutputLabel", description = "@text/phonebookLookupActionOutputDescription", type = "java.lang.String") String phonebookLookup(
51             @ActionInput(name = "phonenumber", label = "@text/phonebookLookupActionInputPhoneNumberLabel", description = "@text/phonebookLookupActionInputPhoneNumberDescription", type = "java.lang.String", required = true) @Nullable String phonenumber) {
52         return phonebookLookup(phonenumber, null, null);
53     }
54
55     @RuleAction(label = "@text/phonebookLookupActionLabel", description = "@text/phonebookLookupActionDescription")
56     public @ActionOutput(name = "name", label = "@text/phonebookLookupActionOutputLabel", description = "@text/phonebookLookupActionOutputDescription", type = "java.lang.String") String phonebookLookup(
57             @ActionInput(name = "phonenumber", label = "@text/phonebookLookupActionInputPhoneNumberLabel", description = "@text/phonebookLookupActionInputPhoneNumberDescription", type = "java.lang.String", required = true) @Nullable String phonenumber,
58             @ActionInput(name = "phonebook", label = "@text/phonebookLookupActionInputPhoneBookLabel", description = "@text/phonebookLookupActionInputPhoneBookDescription", type = "java.lang.String") @Nullable String phonebook) {
59         return phonebookLookup(phonenumber, phonebook, null);
60     }
61
62     @RuleAction(label = "@text/phonebookLookupActionLabel", description = "@text/phonebookLookupActionDescription")
63     public @ActionOutput(name = "name", label = "@text/phonebookLookupActionOutputLabel", description = "@text/phonebookLookupActionOutputDescription", type = "java.lang.String") String phonebookLookup(
64             @ActionInput(name = "phonenumber", label = "@text/phonebookLookupActionInputPhoneNumberLabel", description = "@text/phonebookLookupActionInputPhoneNumberDescription", type = "java.lang.String", required = true) @Nullable String phonenumber,
65             @ActionInput(name = "phonebook", label = "@text/phonebookLookupActionInputPhoneBookLabel", description = "@text/phonebookLookupActionInputPhoneBookDescription", type = "java.lang.String") @Nullable String phonebook,
66             @ActionInput(name = "matches", label = "@text/phonebookLookupActionInputMatchesLabel", description = "@text/phonebookLookupActionInputMatchesDescription", type = "java.lang.Integer") @Nullable Integer matchCount) {
67         if (phonenumber == null) {
68             logger.warn("Cannot lookup a missing number.");
69             return "";
70         }
71
72         final Tr064RootHandler handler = this.handler;
73         if (handler == null) {
74             logger.info("Handler is null, cannot lookup number.");
75             return phonenumber;
76         } else {
77             int matchCountInt = matchCount == null ? 0 : matchCount;
78             if (phonebook != null && !phonebook.isEmpty()) {
79                 return handler.getPhonebookByName(phonebook).flatMap(p -> p.lookupNumber(phonenumber, matchCountInt))
80                         .orElse(phonenumber);
81             } else {
82                 Collection<Phonebook> phonebooks = handler.getPhonebooks();
83                 return phonebooks.stream().map(p -> p.lookupNumber(phonenumber, matchCountInt))
84                         .filter(Optional::isPresent).map(Optional::get).findAny().orElse(phonenumber);
85             }
86         }
87     }
88
89     public static String phonebookLookup(ThingActions actions, @Nullable String phonenumber,
90             @Nullable Integer matchCount) {
91         return phonebookLookup(actions, phonenumber, null, matchCount);
92     }
93
94     public static String phonebookLookup(ThingActions actions, @Nullable String phonenumber) {
95         return phonebookLookup(actions, phonenumber, null, null);
96     }
97
98     public static String phonebookLookup(ThingActions actions, @Nullable String phonenumber,
99             @Nullable String phonebook) {
100         return phonebookLookup(actions, phonenumber, phonebook, null);
101     }
102
103     public static String phonebookLookup(ThingActions actions, @Nullable String phonenumber, @Nullable String phonebook,
104             @Nullable Integer matchCount) {
105         return ((PhonebookActions) actions).phonebookLookup(phonenumber, phonebook, matchCount);
106     }
107
108     @Override
109     public void setThingHandler(@Nullable ThingHandler handler) {
110         if (handler instanceof Tr064RootHandler) {
111             this.handler = (Tr064RootHandler) handler;
112         }
113     }
114
115     @Override
116     public @Nullable ThingHandler getThingHandler() {
117         return handler;
118     }
119 }