2 * Copyright (c) 2010-2021 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 @SuppressWarnings("unused")
38 public class PhonebookActions implements ThingActions {
39 private final Logger logger = LoggerFactory.getLogger(PhonebookActions.class);
41 private @Nullable Tr064RootHandler handler;
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);
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);
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);
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.");
73 final Tr064RootHandler handler = this.handler;
74 if (handler == null) {
75 logger.info("Handler is null, cannot lookup number.");
78 int matchCountInt = matchCount == null ? 0 : matchCount;
79 if (phonebook != null && !phonebook.isEmpty()) {
80 return handler.getPhonebookByName(phonebook).flatMap(p -> p.lookupNumber(phonenumber, matchCountInt))
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);
90 public static String phonebookLookup(ThingActions actions, @Nullable String phonenumber,
91 @Nullable Integer matchCount) {
92 return phonebookLookup(actions, phonenumber, null, matchCount);
95 public static String phonebookLookup(ThingActions actions, @Nullable String phonenumber) {
96 return phonebookLookup(actions, phonenumber, null, null);
99 public static String phonebookLookup(ThingActions actions, @Nullable String phonenumber,
100 @Nullable String phonebook) {
101 return phonebookLookup(actions, phonenumber, phonebook, null);
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);
110 public void setThingHandler(@Nullable ThingHandler handler) {
111 if (handler instanceof Tr064RootHandler) {
112 this.handler = (Tr064RootHandler) handler;
117 public @Nullable ThingHandler getThingHandler() {