]> git.basschouten.com Git - openhab-addons.git/blob
f10359f76ab1daecf75174d5f2549db7221895c4
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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 static java.util.Comparator.comparing;
16
17 import java.net.URI;
18 import java.util.*;
19 import java.util.concurrent.ConcurrentHashMap;
20 import java.util.stream.Collectors;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.core.config.core.ConfigOptionProvider;
25 import org.openhab.core.config.core.ParameterOption;
26 import org.openhab.core.i18n.LocalizedKey;
27 import org.openhab.core.thing.ThingUID;
28 import org.openhab.core.thing.profiles.Profile;
29 import org.openhab.core.thing.profiles.ProfileCallback;
30 import org.openhab.core.thing.profiles.ProfileContext;
31 import org.openhab.core.thing.profiles.ProfileFactory;
32 import org.openhab.core.thing.profiles.ProfileType;
33 import org.openhab.core.thing.profiles.ProfileTypeProvider;
34 import org.openhab.core.thing.profiles.ProfileTypeUID;
35 import org.openhab.core.thing.profiles.i18n.ProfileTypeI18nLocalizationService;
36 import org.openhab.core.util.BundleResolver;
37 import org.openhab.core.util.UIDUtils;
38 import org.osgi.framework.Bundle;
39 import org.osgi.service.component.annotations.Activate;
40 import org.osgi.service.component.annotations.Component;
41 import org.osgi.service.component.annotations.Reference;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 /**
46  * The {@link PhonebookProfileFactory} class is used to create phonebook profiles
47  *
48  * @author Jan N. Klug - Initial contribution
49  */
50 @NonNullByDefault
51 @Component(service = { ProfileFactory.class, ProfileTypeProvider.class, PhonebookProfileFactory.class,
52         ConfigOptionProvider.class })
53 public class PhonebookProfileFactory implements ProfileFactory, ProfileTypeProvider, ConfigOptionProvider {
54     private final Logger logger = LoggerFactory.getLogger(PhonebookProfileFactory.class);
55     private final Map<ThingUID, PhonebookProvider> phonebookProviders = new ConcurrentHashMap<>();
56
57     private final Map<LocalizedKey, ProfileType> localizedProfileTypeCache = new ConcurrentHashMap<>();
58
59     private final ProfileTypeI18nLocalizationService profileTypeI18nLocalizationService;
60     private final Bundle bundle;
61
62     @Activate
63     public PhonebookProfileFactory(
64             final @Reference ProfileTypeI18nLocalizationService profileTypeI18nLocalizationService,
65             final @Reference BundleResolver bundleResolver) {
66         this.profileTypeI18nLocalizationService = profileTypeI18nLocalizationService;
67         this.bundle = bundleResolver.resolveBundle(PhonebookProfileFactory.class);
68     }
69
70     @Override
71     public @Nullable Profile createProfile(ProfileTypeUID profileTypeUID, ProfileCallback callback,
72             ProfileContext profileContext) {
73         return new PhonebookProfile(callback, profileContext, phonebookProviders);
74     }
75
76     @Override
77     public Collection<ProfileTypeUID> getSupportedProfileTypeUIDs() {
78         return Set.of(PhonebookProfile.PHONEBOOK_PROFILE_TYPE_UID);
79     }
80
81     @Override
82     public Collection<ProfileType> getProfileTypes(@Nullable Locale locale) {
83         return Set.of(createLocalizedProfileType(PhonebookProfile.PHONEBOOK_PROFILE_TYPE, locale));
84     }
85
86     private ProfileType createLocalizedProfileType(ProfileType profileType, @Nullable Locale locale) {
87         final LocalizedKey localizedKey = new LocalizedKey(profileType.getUID(),
88                 locale != null ? locale.toLanguageTag() : null);
89
90         return Objects.requireNonNull(localizedProfileTypeCache.computeIfAbsent(localizedKey,
91                 key -> profileTypeI18nLocalizationService.createLocalizedProfileType(bundle, profileType, locale)));
92     }
93
94     /**
95      * register a phonebook provider
96      *
97      * @param phonebookProvider the provider that shall be added
98      */
99     public void registerPhonebookProvider(PhonebookProvider phonebookProvider) {
100         if (phonebookProviders.put(phonebookProvider.getUID(), phonebookProvider) != null) {
101             logger.warn("Tried to register a phonebook provider with UID '{}' for the second time.",
102                     phonebookProvider.getUID());
103         }
104     }
105
106     /**
107      * unregister a phonebook provider
108      *
109      * @param phonebookProvider the provider that shall be removed
110      */
111     public void unregisterPhonebookProvider(PhonebookProvider phonebookProvider) {
112         if (phonebookProviders.remove(phonebookProvider.getUID()) == null) {
113             logger.warn("Tried to unregister a phonebook provider with UID '{}' but it was not found.",
114                     phonebookProvider.getUID());
115         }
116     }
117
118     private List<ParameterOption> createPhonebookList(Map.Entry<ThingUID, PhonebookProvider> entry) {
119         String thingUid = UIDUtils.encode(entry.getKey().toString());
120         String thingName = entry.getValue().getFriendlyName();
121
122         List<ParameterOption> parameterOptions = entry.getValue().getPhonebooks().stream()
123                 .map(phonebook -> new ParameterOption(thingUid + ":" + UIDUtils.encode(phonebook.getName()),
124                         thingName + " - " + phonebook.getName()))
125                 .collect(Collectors.toList());
126
127         if (parameterOptions.size() > 0) {
128             parameterOptions.add(new ParameterOption(thingUid, thingName));
129         }
130
131         return parameterOptions;
132     }
133
134     @Override
135     public @Nullable Collection<ParameterOption> getParameterOptions(URI uri, String s, @Nullable String s1,
136             @Nullable Locale locale) {
137         if (uri.getSchemeSpecificPart().equals(PhonebookProfile.PHONEBOOK_PROFILE_TYPE_UID.toString())
138                 && s.equals(PhonebookProfile.PHONEBOOK_PARAM)) {
139             List<ParameterOption> parameterOptions = new ArrayList<>();
140             for (Map.Entry<ThingUID, PhonebookProvider> entry : phonebookProviders.entrySet()) {
141                 parameterOptions.addAll(createPhonebookList(entry));
142             }
143             parameterOptions.sort(comparing(o -> o.getLabel()));
144             return parameterOptions;
145         }
146         return null;
147     }
148 }