]> git.basschouten.com Git - openhab-addons.git/blob
885e18ac024851de946296567ce3054358c7f216
[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.leapmotion.internal;
14
15 import java.util.Collection;
16 import java.util.Locale;
17 import java.util.stream.Collectors;
18 import java.util.stream.Stream;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.core.library.CoreItemFactory;
23 import org.openhab.core.thing.Channel;
24 import org.openhab.core.thing.profiles.Profile;
25 import org.openhab.core.thing.profiles.ProfileAdvisor;
26 import org.openhab.core.thing.profiles.ProfileCallback;
27 import org.openhab.core.thing.profiles.ProfileContext;
28 import org.openhab.core.thing.profiles.ProfileFactory;
29 import org.openhab.core.thing.profiles.ProfileType;
30 import org.openhab.core.thing.profiles.ProfileTypeBuilder;
31 import org.openhab.core.thing.profiles.ProfileTypeProvider;
32 import org.openhab.core.thing.profiles.ProfileTypeUID;
33 import org.openhab.core.thing.profiles.TriggerProfileType;
34 import org.openhab.core.thing.type.ChannelType;
35 import org.openhab.core.thing.type.ChannelTypeUID;
36 import org.osgi.service.component.annotations.Component;
37
38 /**
39  * This class defines and provides all profiles and their types of this binding.
40  *
41  * @author Kai Kreuzer - Initial contribution
42  *
43  */
44 @NonNullByDefault
45 @Component
46 public class LeapMotionProfileFactory implements ProfileFactory, ProfileAdvisor, ProfileTypeProvider {
47
48     static final ProfileTypeUID UID_SWITCH = new ProfileTypeUID(LeapMotionBindingConstants.BINDING_ID, "switch");
49     static final ProfileTypeUID UID_DIMMER = new ProfileTypeUID(LeapMotionBindingConstants.BINDING_ID, "dimmer");
50     static final ProfileTypeUID UID_COLOR = new ProfileTypeUID(LeapMotionBindingConstants.BINDING_ID, "color");
51
52     private static final TriggerProfileType SWITCH_TYPE = ProfileTypeBuilder.newTrigger(UID_SWITCH, "Toggle Switch")
53             .withSupportedItemTypes(CoreItemFactory.SWITCH, CoreItemFactory.DIMMER, CoreItemFactory.COLOR)
54             .withSupportedChannelTypeUIDs(LeapMotionBindingConstants.CHANNEL_GESTURE_UID).build();
55
56     private static final TriggerProfileType DIMMER_TYPE = ProfileTypeBuilder.newTrigger(UID_DIMMER, "Dimmer Control")
57             .withSupportedItemTypes(CoreItemFactory.DIMMER, CoreItemFactory.COLOR)
58             .withSupportedChannelTypeUIDs(LeapMotionBindingConstants.CHANNEL_GESTURE_UID).build();
59
60     private static final TriggerProfileType COLOR_TYPE = ProfileTypeBuilder.newTrigger(UID_COLOR, "Color Control")
61             .withSupportedItemTypes(CoreItemFactory.COLOR)
62             .withSupportedChannelTypeUIDs(LeapMotionBindingConstants.CHANNEL_GESTURE_UID).build();
63
64     @Override
65     public Collection<ProfileTypeUID> getSupportedProfileTypeUIDs() {
66         return Stream.of(UID_SWITCH, UID_DIMMER, UID_COLOR).collect(Collectors.toSet());
67     }
68
69     @Override
70     public Collection<ProfileType> getProfileTypes(@Nullable Locale locale) {
71         return Stream.of(SWITCH_TYPE, DIMMER_TYPE, COLOR_TYPE).collect(Collectors.toSet());
72     }
73
74     @Override
75     public @Nullable ProfileTypeUID getSuggestedProfileTypeUID(Channel channel, @Nullable String itemType) {
76         return getSuggestedProfileTypeUID(channel.getChannelTypeUID(), itemType);
77     }
78
79     @Override
80     public @Nullable ProfileTypeUID getSuggestedProfileTypeUID(ChannelType channelType, @Nullable String itemType) {
81         return getSuggestedProfileTypeUID(channelType.getUID(), itemType);
82     }
83
84     private @Nullable ProfileTypeUID getSuggestedProfileTypeUID(@Nullable ChannelTypeUID channelTypeUID,
85             @Nullable String itemType) {
86         if (LeapMotionBindingConstants.CHANNEL_GESTURE_UID.equals(channelTypeUID) && itemType != null) {
87             switch (itemType) {
88                 case CoreItemFactory.SWITCH:
89                     return UID_SWITCH;
90                 case CoreItemFactory.DIMMER:
91                     return UID_DIMMER;
92                 case CoreItemFactory.COLOR:
93                     return UID_COLOR;
94                 default:
95                     return null;
96             }
97         }
98         return null;
99     }
100
101     @Override
102     public @Nullable Profile createProfile(ProfileTypeUID profileTypeUID, ProfileCallback callback,
103             ProfileContext profileContext) {
104         if (UID_SWITCH.equals(profileTypeUID)) {
105             return new LeapMotionSwitchProfile(callback);
106         } else if (UID_DIMMER.equals(profileTypeUID)) {
107             return new LeapMotionDimmerProfile(callback, profileContext);
108         } else if (UID_COLOR.equals(profileTypeUID)) {
109             return new LeapMotionColorProfile(callback);
110         } else {
111             return null;
112         }
113     }
114 }