2 * Copyright (c) 2010-2023 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.tr064.internal.phonebook;
15 import java.util.Collection;
16 import java.util.Optional;
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;
31 * The {@link PhonebookActions} is responsible for handling phonebook actions
33 * @author Jan N. Klug - Initial contribution
35 @ThingActionsScope(name = "tr064")
37 public class PhonebookActions implements ThingActions {
38 private final Logger logger = LoggerFactory.getLogger(PhonebookActions.class);
40 private @Nullable Tr064RootHandler handler;
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);
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);
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);
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.");
72 final Tr064RootHandler handler = this.handler;
73 if (handler == null) {
74 logger.info("Handler is null, cannot lookup number.");
77 int matchCountInt = matchCount == null ? 0 : matchCount;
78 if (phonebook != null && !phonebook.isEmpty()) {
79 return handler.getPhonebookByName(phonebook).flatMap(p -> p.lookupNumber(phonenumber, matchCountInt))
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);
89 public static String phonebookLookup(ThingActions actions, @Nullable String phonenumber,
90 @Nullable Integer matchCount) {
91 return phonebookLookup(actions, phonenumber, null, matchCount);
94 public static String phonebookLookup(ThingActions actions, @Nullable String phonenumber) {
95 return phonebookLookup(actions, phonenumber, null, null);
98 public static String phonebookLookup(ThingActions actions, @Nullable String phonenumber,
99 @Nullable String phonebook) {
100 return phonebookLookup(actions, phonenumber, phonebook, null);
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);
109 public void setThingHandler(@Nullable ThingHandler handler) {
110 if (handler instanceof Tr064RootHandler) {
111 this.handler = (Tr064RootHandler) handler;
116 public @Nullable ThingHandler getThingHandler() {