-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[tr064] add rule action for phonebook #9505
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
...ng.tr064/src/main/java/org/openhab/binding/tr064/internal/phonebook/PhonebookActions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* Copyright (c) 2010-2020 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.binding.tr064.internal.phonebook; | ||
|
||
import java.util.*; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
import org.openhab.binding.tr064.internal.Tr064RootHandler; | ||
import org.openhab.core.automation.annotation.ActionInput; | ||
import org.openhab.core.automation.annotation.ActionOutput; | ||
import org.openhab.core.automation.annotation.RuleAction; | ||
import org.openhab.core.thing.binding.ThingActions; | ||
import org.openhab.core.thing.binding.ThingActionsScope; | ||
import org.openhab.core.thing.binding.ThingHandler; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* The {@link PhonebookActions} is responsible for handling phonebook actions | ||
* | ||
* @author Jan N. Klug - Initial contribution | ||
*/ | ||
@ThingActionsScope(name = "tr064") | ||
@NonNullByDefault | ||
@SuppressWarnings("unused") | ||
public class PhonebookActions implements ThingActions { | ||
private final Logger logger = LoggerFactory.getLogger(PhonebookActions.class); | ||
|
||
private @Nullable Tr064RootHandler handler; | ||
|
||
@RuleAction(label = "@text/phonebookLookupActionLabel", description = "@text/phonebookLookupActionDescription") | ||
public @ActionOutput(name = "name", type = "java.lang.String") String phonebookLookup( | ||
@ActionInput(name = "phonenumber") @Nullable String phonenumber, | ||
@ActionInput(name = "phonebook") @Nullable String phonebook, | ||
@ActionInput(name = "matches") @Nullable Integer matchCount) { | ||
if (phonenumber == null) { | ||
logger.warn("Cannot lookup a missing number."); | ||
return ""; | ||
} | ||
|
||
final Tr064RootHandler handler = this.handler; | ||
if (handler == null) { | ||
logger.info("Handler is null, cannot lookup number."); | ||
return phonenumber; | ||
} else { | ||
int matchCountInt = matchCount == null ? 0 : matchCount; | ||
if (phonebook != null && !phonebook.isEmpty()) { | ||
return handler.getPhonebookByName(phonebook).flatMap(p -> p.lookupNumber(phonenumber, matchCountInt)) | ||
.orElse(phonenumber); | ||
} else { | ||
Collection<Phonebook> phonebooks = handler.getPhonebooks(); | ||
return phonebooks.stream().map(p -> p.lookupNumber(phonenumber, matchCountInt)) | ||
.filter(Optional::isPresent).map(Optional::get).findAny().orElse(phonenumber); | ||
} | ||
} | ||
} | ||
|
||
public static String phonebookLookup(ThingActions actions, @Nullable String phonenumber, @Nullable String phonebook, | ||
@Nullable Integer matchCount) { | ||
return ((PhonebookActions) actions).phonebookLookup(phonenumber, phonebook, matchCount); | ||
} | ||
|
||
@Override | ||
public void setThingHandler(@Nullable ThingHandler handler) { | ||
if (handler instanceof Tr064RootHandler) { | ||
this.handler = (Tr064RootHandler) handler; | ||
} | ||
} | ||
|
||
@Override | ||
public @Nullable ThingHandler getThingHandler() { | ||
return handler; | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
bundles/org.openhab.binding.tr064/src/main/resources/OH-INF/i18n/tr064.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# actions | ||
phonebookLookupActionLabel = lookup a phonenumber | ||
phonebookLookupActionDescription = Lookup a phone number. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just tested those changes and based on the description here would have expected that something like
would actually work as the other parameters are described as optional.
But the above rule code results in an error like
Using
val result = tr064Actions.phonebookLookup("01234567", null, null)
works fine. Maybe might be good to update the readme here, so it's clear the parameters can't be ommited, but need to be set tonull
instead - or update the code if the parameters should be ommitable.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably adding those methods would be the better option. Will do that tomorrow. Is it working otherwise as expected?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only did some simple lookups of incoming call numbers in the phonebook. That worked as expected. But didn't test (yet) if the
phonebook
andmatchCount
parameters are working correctly.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just tested looking up with specific phonebooks. Seems to work as expected. But might be useful to mention it's selected by the phonebooks name (and not its id)