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