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;
15 import java.io.IOException;
16 import java.nio.file.FileSystems;
17 import java.nio.file.Files;
18 import java.nio.file.Path;
19 import java.time.LocalDateTime;
20 import java.time.format.DateTimeFormatter;
21 import java.util.Collection;
23 import java.util.Objects;
24 import java.util.Optional;
25 import java.util.concurrent.ExecutionException;
26 import java.util.concurrent.TimeoutException;
28 import javax.xml.soap.SOAPMessage;
30 import org.eclipse.jdt.annotation.NonNullByDefault;
31 import org.eclipse.jdt.annotation.Nullable;
32 import org.eclipse.jetty.client.api.ContentResponse;
33 import org.openhab.binding.tr064.internal.dto.scpd.root.SCPDServiceType;
34 import org.openhab.binding.tr064.internal.phonebook.Phonebook;
35 import org.openhab.binding.tr064.internal.soap.SOAPRequest;
36 import org.openhab.binding.tr064.internal.util.SCPDUtil;
37 import org.openhab.binding.tr064.internal.util.Util;
38 import org.openhab.core.automation.annotation.ActionInput;
39 import org.openhab.core.automation.annotation.ActionOutput;
40 import org.openhab.core.automation.annotation.RuleAction;
41 import org.openhab.core.thing.binding.ThingActions;
42 import org.openhab.core.thing.binding.ThingActionsScope;
43 import org.openhab.core.thing.binding.ThingHandler;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 * The {@link FritzboxActions} is responsible for handling phone book actions
50 * @author Jan N. Klug - Initial contribution
52 @ThingActionsScope(name = "tr064")
54 public class FritzboxActions implements ThingActions {
55 private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("dd.MM.yyyy_HHmm");
57 private final Logger logger = LoggerFactory.getLogger(FritzboxActions.class);
59 private @Nullable Tr064RootHandler handler;
61 @RuleAction(label = "@text/phonebookLookupActionLabel", description = "@text/phonebookLookupActionDescription")
62 public @ActionOutput(name = "name", label = "@text/phonebookLookupActionOutputLabel", description = "@text/phonebookLookupActionOutputDescription", type = "java.lang.String") String phonebookLookup(
63 @ActionInput(name = "phonenumber", label = "@text/phonebookLookupActionInputPhoneNumberLabel", description = "@text/phonebookLookupActionInputPhoneNumberDescription", type = "java.lang.String", required = true) @Nullable String phonenumber,
64 @ActionInput(name = "matches", label = "@text/phonebookLookupActionInputMatchesLabel", description = "@text/phonebookLookupActionInputMatchesDescription", type = "java.lang.Integer") @Nullable Integer matchCount) {
65 return phonebookLookup(phonenumber, null, matchCount);
68 @RuleAction(label = "@text/phonebookLookupActionLabel", description = "@text/phonebookLookupActionDescription")
69 public @ActionOutput(name = "name", label = "@text/phonebookLookupActionOutputLabel", description = "@text/phonebookLookupActionOutputDescription", type = "java.lang.String") String phonebookLookup(
70 @ActionInput(name = "phonenumber", label = "@text/phonebookLookupActionInputPhoneNumberLabel", description = "@text/phonebookLookupActionInputPhoneNumberDescription", type = "java.lang.String", required = true) @Nullable String phonenumber) {
71 return phonebookLookup(phonenumber, null, null);
74 @RuleAction(label = "@text/phonebookLookupActionLabel", description = "@text/phonebookLookupActionDescription")
75 public @ActionOutput(name = "name", label = "@text/phonebookLookupActionOutputLabel", description = "@text/phonebookLookupActionOutputDescription", type = "java.lang.String") String phonebookLookup(
76 @ActionInput(name = "phonenumber", label = "@text/phonebookLookupActionInputPhoneNumberLabel", description = "@text/phonebookLookupActionInputPhoneNumberDescription", type = "java.lang.String", required = true) @Nullable String phonenumber,
77 @ActionInput(name = "phonebook", label = "@text/phonebookLookupActionInputPhoneBookLabel", description = "@text/phonebookLookupActionInputPhoneBookDescription", type = "java.lang.String") @Nullable String phonebook) {
78 return phonebookLookup(phonenumber, phonebook, null);
81 @RuleAction(label = "@text/phonebookLookupActionLabel", description = "@text/phonebookLookupActionDescription")
82 public @ActionOutput(name = "name", label = "@text/phonebookLookupActionOutputLabel", description = "@text/phonebookLookupActionOutputDescription", type = "java.lang.String") String phonebookLookup(
83 @ActionInput(name = "phonenumber", label = "@text/phonebookLookupActionInputPhoneNumberLabel", description = "@text/phonebookLookupActionInputPhoneNumberDescription", type = "java.lang.String", required = true) @Nullable String phonenumber,
84 @ActionInput(name = "phonebook", label = "@text/phonebookLookupActionInputPhoneBookLabel", description = "@text/phonebookLookupActionInputPhoneBookDescription", type = "java.lang.String") @Nullable String phonebook,
85 @ActionInput(name = "matches", label = "@text/phonebookLookupActionInputMatchesLabel", description = "@text/phonebookLookupActionInputMatchesDescription", type = "java.lang.Integer") @Nullable Integer matchCount) {
86 if (phonenumber == null) {
87 logger.warn("Cannot lookup a missing number.");
91 final Tr064RootHandler handler = this.handler;
92 if (handler == null) {
93 logger.info("Handler is null, cannot lookup number.");
96 int matchCountInt = matchCount == null ? 0 : matchCount;
97 if (phonebook != null && !phonebook.isEmpty()) {
98 return Objects.requireNonNull(handler.getPhonebookByName(phonebook)
99 .flatMap(p -> p.lookupNumber(phonenumber, matchCountInt)).orElse(phonenumber));
101 Collection<Phonebook> phonebooks = handler.getPhonebooks();
102 return Objects.requireNonNull(phonebooks.stream().map(p -> p.lookupNumber(phonenumber, matchCountInt))
103 .filter(Optional::isPresent).map(Optional::get).findAny().orElse(phonenumber));
108 @RuleAction(label = "create configuration backup", description = "Creates a configuration backup")
109 public void createConfigurationBackup() {
110 Tr064RootHandler handler = this.handler;
112 if (handler == null) {
113 logger.warn("TR064 action service ThingHandler is null!");
117 SCPDUtil scpdUtil = handler.getSCPDUtil();
118 if (scpdUtil == null) {
119 logger.warn("Could not get SCPDUtil, handler seems to be uninitialized.");
123 Optional<SCPDServiceType> scpdService = scpdUtil.getDevice("")
124 .flatMap(deviceType -> deviceType.getServiceList().stream().filter(
125 service -> service.getServiceId().equals("urn:DeviceConfig-com:serviceId:DeviceConfig1"))
127 if (scpdService.isEmpty()) {
128 logger.warn("Could not get service.");
132 BackupConfiguration configuration = handler.getBackupConfiguration();
134 SOAPRequest soapRequest = new SOAPRequest(scpdService.get(), "X_AVM-DE_GetConfigFile",
135 Map.of("NewX_AVM-DE_Password", configuration.password));
136 SOAPMessage soapMessage = handler.getSOAPConnector().doSOAPRequestUncached(soapRequest);
137 String configBackupURL = Util.getSOAPElement(soapMessage, "NewX_AVM-DE_ConfigFileUrl")
138 .orElseThrow(() -> new Tr064CommunicationException("Empty URL"));
140 ContentResponse content = handler.getUrl(configBackupURL);
142 String fileName = String.format("%s %s.export", handler.getFriendlyName(),
143 DATE_TIME_FORMATTER.format(LocalDateTime.now()));
144 Path filePath = FileSystems.getDefault().getPath(configuration.directory, fileName);
145 Path folder = filePath.getParent();
146 if (folder != null) {
147 Files.createDirectories(folder);
149 Files.write(filePath, content.getContent());
150 } catch (Tr064CommunicationException e) {
151 logger.warn("Failed to get configuration backup URL: {}", e.getMessage());
152 } catch (InterruptedException | ExecutionException | TimeoutException e) {
153 logger.warn("Failed to get remote backup file: {}", e.getMessage());
154 } catch (IOException e) {
155 logger.warn("Failed to create backup file: {}", e.getMessage());
159 public static String phonebookLookup(ThingActions actions, @Nullable String phonenumber,
160 @Nullable Integer matchCount) {
161 return phonebookLookup(actions, phonenumber, null, matchCount);
164 public static String phonebookLookup(ThingActions actions, @Nullable String phonenumber) {
165 return phonebookLookup(actions, phonenumber, null, null);
168 public static String phonebookLookup(ThingActions actions, @Nullable String phonenumber,
169 @Nullable String phonebook) {
170 return phonebookLookup(actions, phonenumber, phonebook, null);
173 public static String phonebookLookup(ThingActions actions, @Nullable String phonenumber, @Nullable String phonebook,
174 @Nullable Integer matchCount) {
175 return ((FritzboxActions) actions).phonebookLookup(phonenumber, phonebook, matchCount);
178 public static void createConfigurationBackup(ThingActions actions) {
179 ((FritzboxActions) actions).createConfigurationBackup();
183 public void setThingHandler(@Nullable ThingHandler handler) {
184 this.handler = (Tr064RootHandler) handler;
188 public @Nullable ThingHandler getThingHandler() {
192 public record BackupConfiguration(String directory, String password) {