]> git.basschouten.com Git - openhab-addons.git/blob
f1de2cde08eed67456d84976c21ebe2f1feaff43
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.enocean.internal.profiles;
14
15 import static org.openhab.binding.enocean.internal.EnOceanBindingConstants.*;
16 import static org.openhab.binding.enocean.internal.profiles.EnOceanProfiles.*;
17
18 import java.util.Collection;
19 import java.util.Collections;
20 import java.util.Locale;
21 import java.util.Set;
22 import java.util.stream.Collectors;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.core.library.CoreItemFactory;
27 import org.openhab.core.thing.Channel;
28 import org.openhab.core.thing.profiles.Profile;
29 import org.openhab.core.thing.profiles.ProfileAdvisor;
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.thing.type.ChannelType;
37 import org.openhab.core.thing.type.ChannelTypeRegistry;
38 import org.osgi.service.component.annotations.Activate;
39 import org.osgi.service.component.annotations.Component;
40 import org.osgi.service.component.annotations.Reference;
41
42 /**
43  * The {@link EnOceanProfileFactory} class creates EnOceanProfiles
44  *
45  * @author Daniel Weber - Initial contribution
46  */
47 @Component
48 @NonNullByDefault
49 public class EnOceanProfileFactory implements ProfileFactory, ProfileAdvisor, ProfileTypeProvider {
50
51     private static final Set<ProfileType> SUPPORTED_PROFILE_TYPES = Set.of(ROCKERSWITCHACTION_TOGGLE_SWITCH_TYPE,
52             ROCKERSWITCHACTION_TOGGLE_PLAYER_TYPE);
53
54     private static final Set<ProfileTypeUID> SUPPORTED_PROFILE_TYPE_UIDS = Set.of(ROCKERSWITCHACTION_TOGGLE_SWITCH,
55             ROCKERSWITCHACTION_TOGGLE_PLAYER);
56
57     private final ChannelTypeRegistry channelTypeRegistry;
58
59     @Activate
60     public EnOceanProfileFactory(final @Reference ChannelTypeRegistry channelTypeRegistry) {
61         this.channelTypeRegistry = channelTypeRegistry;
62     }
63
64     @Override
65     public Collection<ProfileType> getProfileTypes(@Nullable Locale locale) {
66         return Collections.unmodifiableList(SUPPORTED_PROFILE_TYPES.stream().collect(Collectors.toList()));
67     }
68
69     @Override
70     public @Nullable ProfileTypeUID getSuggestedProfileTypeUID(Channel channel, @Nullable String itemType) {
71         ChannelType channelType = channelTypeRegistry.getChannelType(channel.getChannelTypeUID());
72         if (channelType == null) {
73             return null;
74         }
75
76         return getSuggestedProfileTypeUID(channelType, itemType);
77     }
78
79     @Override
80     public @Nullable ProfileTypeUID getSuggestedProfileTypeUID(ChannelType channelType, @Nullable String itemType) {
81
82         if (CHANNELTYPE_ROCKERSWITCH_ACTION_UID.equals(channelType.getUID())) {
83             if (CoreItemFactory.PLAYER.equalsIgnoreCase(itemType)) {
84                 return ROCKERSWITCHACTION_TOGGLE_PLAYER;
85             } else if (CoreItemFactory.SWITCH.equalsIgnoreCase(itemType)) {
86                 return ROCKERSWITCHACTION_TOGGLE_SWITCH;
87             }
88         }
89
90         return null;
91     }
92
93     @Override
94     public @Nullable Profile createProfile(ProfileTypeUID profileTypeUID, ProfileCallback callback,
95             ProfileContext profileContext) {
96         if (ROCKERSWITCHACTION_TOGGLE_PLAYER.equals(profileTypeUID)) {
97             return new RockerSwitchActionTogglePlayerProfile(callback, profileContext);
98         } else if (ROCKERSWITCHACTION_TOGGLE_SWITCH.equals(profileTypeUID)) {
99             return new RockerSwitchActionToggleSwitchProfile(callback, profileContext);
100         } else {
101             return null;
102         }
103     }
104
105     @Override
106     public Collection<ProfileTypeUID> getSupportedProfileTypeUIDs() {
107         return Collections.unmodifiableList(SUPPORTED_PROFILE_TYPE_UIDS.stream().collect(Collectors.toList()));
108     }
109 }