]> git.basschouten.com Git - openhab-addons.git/blob
67b4485401d3c8dc05e9cd8f8345752a1dfb02c7
[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 org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.binding.gpstracker.internal.config.ConfigHelper;
17 import org.openhab.core.library.types.OnOffType;
18 import org.openhab.core.thing.profiles.ProfileCallback;
19 import org.openhab.core.thing.profiles.ProfileContext;
20 import org.openhab.core.thing.profiles.ProfileTypeUID;
21 import org.openhab.core.thing.profiles.TriggerProfile;
22 import org.openhab.core.types.State;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * The {@link GPSTrackerTriggerSwitchProfile} class implements the behavior when being linked to a Switch item.
28  *
29  * @author Gabor Bicskei - Initial contribution
30  */
31 @NonNullByDefault
32 public class GPSTrackerTriggerSwitchProfile implements TriggerProfile {
33     /**
34      * Class logger
35      */
36     private final Logger logger = LoggerFactory.getLogger(GPSTrackerTriggerSwitchProfile.class);
37
38     /**
39      * Callback
40      */
41     private ProfileCallback callback;
42
43     /**
44      * Link region name
45      */
46     private String regionName;
47
48     /**
49      * Constructor.
50      *
51      * @param callback Callback
52      * @param context Context
53      */
54     GPSTrackerTriggerSwitchProfile(ProfileCallback callback, ProfileContext context) {
55         this.callback = callback;
56         this.regionName = ConfigHelper.getRegionName(context.getConfiguration());
57         logger.debug("Trigger switch profile created for region {}", regionName);
58     }
59
60     @Override
61     public ProfileTypeUID getProfileTypeUID() {
62         return GPSTrackerProfileFactory.UID_TRIGGER_SWITCH;
63     }
64
65     @Override
66     public void onStateUpdateFromItem(State state) {
67     }
68
69     @Override
70     public void onTriggerFromHandler(String payload) {
71         if (payload.startsWith(regionName)) {
72             OnOffType state = payload.endsWith("enter") ? OnOffType.ON : OnOffType.OFF;
73             callback.sendCommand(state);
74             logger.debug("Transition trigger {} handled for region {} by profile: {}", payload, regionName, state);
75         }
76     }
77 }