]> git.basschouten.com Git - openhab-addons.git/blob
105271b15ad679f599650d1a737214ceb99b4fdb
[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.gpstracker.internal.profile;
14
15 import static org.openhab.binding.gpstracker.internal.GPSTrackerBindingConstants.CHANNEL_TYPE_REGION;
16
17 import java.util.Collection;
18 import java.util.Locale;
19 import java.util.stream.Collectors;
20 import java.util.stream.Stream;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.gpstracker.internal.GPSTrackerBindingConstants;
25 import org.openhab.core.library.CoreItemFactory;
26 import org.openhab.core.thing.Channel;
27 import org.openhab.core.thing.profiles.*;
28 import org.openhab.core.thing.type.ChannelType;
29 import org.openhab.core.thing.type.ChannelTypeUID;
30 import org.osgi.service.component.annotations.Component;
31
32 /**
33  * The {@link GPSTrackerProfileFactory} class defines and provides switch profile and its type of this binding.
34  *
35  * @author Gabor Bicskei - Initial contribution
36  */
37 @NonNullByDefault
38 @Component
39 public class GPSTrackerProfileFactory implements ProfileFactory, ProfileAdvisor, ProfileTypeProvider {
40     /**
41      * Profile UID for trigger events
42      */
43     static final ProfileTypeUID UID_TRIGGER_SWITCH = new ProfileTypeUID(GPSTrackerBindingConstants.BINDING_ID,
44             "trigger-geofence");
45
46     /**
47      * Profile type for trigger events
48      */
49     private static final TriggerProfileType TRIGGER_SWITCH_TYPE = ProfileTypeBuilder
50             .newTrigger(UID_TRIGGER_SWITCH, "Geofence").withSupportedItemTypes(CoreItemFactory.SWITCH)
51             .withSupportedChannelTypeUIDs(CHANNEL_TYPE_REGION).build();
52
53     @Override
54     public Collection<ProfileTypeUID> getSupportedProfileTypeUIDs() {
55         return Stream.of(UID_TRIGGER_SWITCH).collect(Collectors.toSet());
56     }
57
58     @Override
59     public Collection<ProfileType> getProfileTypes(@Nullable Locale locale) {
60         return Stream.of(TRIGGER_SWITCH_TYPE).collect(Collectors.toSet());
61     }
62
63     @Override
64     public @Nullable ProfileTypeUID getSuggestedProfileTypeUID(Channel channel, @Nullable String itemType) {
65         return getSuggestedProfileTypeUID(channel.getChannelTypeUID(), itemType);
66     }
67
68     @Override
69     public @Nullable ProfileTypeUID getSuggestedProfileTypeUID(ChannelType channelType, @Nullable String itemType) {
70         return getSuggestedProfileTypeUID(channelType.getUID(), itemType);
71     }
72
73     private @Nullable ProfileTypeUID getSuggestedProfileTypeUID(@Nullable ChannelTypeUID channelTypeUID,
74             @Nullable String itemType) {
75         if (CoreItemFactory.SWITCH.equals(itemType) && CHANNEL_TYPE_REGION.equals(channelTypeUID)) {
76             return UID_TRIGGER_SWITCH;
77         }
78         return null;
79     }
80
81     @Override
82     public @Nullable Profile createProfile(ProfileTypeUID profileTypeUID, ProfileCallback callback,
83             ProfileContext profileContext) {
84         if (UID_TRIGGER_SWITCH.equals(profileTypeUID)) {
85             return new GPSTrackerTriggerSwitchProfile(callback, profileContext);
86         }
87         return null;
88     }
89 }