]> git.basschouten.com Git - openhab-addons.git/blob
054e09c0cfc344100c7f91dbfb4e4ebfe6277b22
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.net.URI;
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.Locale;
19 import java.util.Map;
20 import java.util.concurrent.ConcurrentHashMap;
21 import java.util.stream.Collectors;
22 import java.util.stream.Stream;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.core.config.core.ConfigOptionProvider;
27 import org.openhab.core.config.core.ParameterOption;
28 import org.openhab.core.thing.ThingUID;
29 import org.openhab.core.thing.profiles.Profile;
30 import org.openhab.core.thing.profiles.ProfileCallback;
31 import org.openhab.core.thing.profiles.ProfileContext;
32 import org.openhab.core.thing.profiles.ProfileFactory;
33 import org.openhab.core.thing.profiles.ProfileType;
34 import org.openhab.core.thing.profiles.ProfileTypeProvider;
35 import org.openhab.core.thing.profiles.ProfileTypeUID;
36 import org.openhab.core.util.UIDUtils;
37 import org.osgi.service.component.annotations.Component;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  * The {@link PhonebookProfileFactory} class is used to create phonebook profiles
43  *
44  * @author Jan N. Klug - Initial contribution
45  */
46 @NonNullByDefault
47 @Component(service = { ProfileFactory.class, ProfileTypeProvider.class, PhonebookProfileFactory.class,
48         ConfigOptionProvider.class })
49 public class PhonebookProfileFactory implements ProfileFactory, ProfileTypeProvider, ConfigOptionProvider {
50     private final Logger logger = LoggerFactory.getLogger(PhonebookProfileFactory.class);
51     private final Map<ThingUID, PhonebookProvider> phonebookProviders = new ConcurrentHashMap<>();
52
53     @Override
54     public @Nullable Profile createProfile(ProfileTypeUID profileTypeUID, ProfileCallback callback,
55             ProfileContext profileContext) {
56         return new PhonebookProfile(callback, profileContext, phonebookProviders);
57     }
58
59     @Override
60     public Collection<ProfileTypeUID> getSupportedProfileTypeUIDs() {
61         return Collections.singleton(PhonebookProfile.PHONEBOOK_PROFILE_TYPE_UID);
62     }
63
64     @Override
65     public Collection<ProfileType> getProfileTypes(@Nullable Locale locale) {
66         return Collections.singleton(PhonebookProfile.PHONEBOOK_PROFILE_TYPE);
67     }
68
69     /**
70      * register a phonebook provider
71      *
72      * @param phonebookProvider the provider that shall be added
73      */
74     public void registerPhonebookProvider(PhonebookProvider phonebookProvider) {
75         if (phonebookProviders.put(phonebookProvider.getUID(), phonebookProvider) != null) {
76             logger.warn("Tried to register a phonebook provider with UID '{}' for the second time.",
77                     phonebookProvider.getUID());
78         }
79     }
80
81     /**
82      * unregister a phonebook provider
83      *
84      * @param phonebookProvider the provider that shall be removed
85      */
86     public void unregisterPhonebookProvider(PhonebookProvider phonebookProvider) {
87         if (phonebookProviders.remove(phonebookProvider.getUID()) == null) {
88             logger.warn("Tried to unregister a phonebook provider with UID '{}' but it was not found.",
89                     phonebookProvider.getUID());
90         }
91     }
92
93     private Stream<ParameterOption> createPhonebookList(Map.Entry<ThingUID, PhonebookProvider> entry) {
94         String thingUid = UIDUtils.encode(entry.getKey().toString());
95         String thingName = entry.getValue().getFriendlyName();
96
97         Stream<ParameterOption> parameterOptions = entry.getValue().getPhonebooks().stream()
98                 .map(phonebook -> new ParameterOption(thingUid + ":" + UIDUtils.encode(phonebook.getName()),
99                         thingName + " " + phonebook.getName()));
100
101         if (parameterOptions.count() > 0) {
102             return Stream.concat(Stream.of(new ParameterOption(thingUid, thingName)), parameterOptions);
103         }
104
105         return parameterOptions;
106     }
107
108     @Override
109     public @Nullable Collection<ParameterOption> getParameterOptions(URI uri, String s, @Nullable String s1,
110             @Nullable Locale locale) {
111         if (uri.getSchemeSpecificPart().equals(PhonebookProfile.PHONEBOOK_PROFILE_TYPE_UID.toString())
112                 && s.equals(PhonebookProfile.PHONEBOOK_PARAM)) {
113             return phonebookProviders.entrySet().stream().flatMap(this::createPhonebookList)
114                     .collect(Collectors.toSet());
115         }
116         return null;
117     }
118 }