]> git.basschouten.com Git - openhab-addons.git/blob
c7b48ae4812b6a494e84b99c8f32e8cbd29d20ea
[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.verisure.internal.handler;
14
15 import static org.openhab.binding.verisure.internal.VerisureBindingConstants.*;
16
17 import java.util.Collections;
18 import java.util.List;
19 import java.util.Set;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.openhab.binding.verisure.internal.dto.VerisureUserPresencesDTO;
23 import org.openhab.binding.verisure.internal.dto.VerisureUserPresencesDTO.UserTracking;
24 import org.openhab.core.library.types.StringType;
25 import org.openhab.core.thing.Channel;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.thing.ThingStatus;
28 import org.openhab.core.thing.ThingTypeUID;
29 import org.openhab.core.types.State;
30 import org.openhab.core.types.UnDefType;
31
32 /**
33  * Handler for the User Presence Device thing type that Verisure provides.
34  *
35  * @author Jan Gustafsson - Initial contribution
36  *
37  */
38 @NonNullByDefault
39 public class VerisureUserPresenceThingHandler extends VerisureThingHandler<VerisureUserPresencesDTO> {
40
41     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_USERPRESENCE);
42
43     public VerisureUserPresenceThingHandler(Thing thing) {
44         super(thing);
45     }
46
47     @Override
48     public Class<VerisureUserPresencesDTO> getVerisureThingClass() {
49         return VerisureUserPresencesDTO.class;
50     }
51
52     @Override
53     public synchronized void update(VerisureUserPresencesDTO thing) {
54         updateUserPresenceState(thing);
55         updateStatus(ThingStatus.ONLINE);
56     }
57
58     private void updateUserPresenceState(VerisureUserPresencesDTO userPresenceJSON) {
59         List<UserTracking> userTrackingList = userPresenceJSON.getData().getInstallation().getUserTrackings();
60         if (!userTrackingList.isEmpty()) {
61             UserTracking userTracking = userTrackingList.get(0);
62             getThing().getChannels().stream().map(Channel::getUID)
63                     .filter(channelUID -> isLinked(channelUID) && !channelUID.getId().equals("timestamp"))
64                     .forEach(channelUID -> {
65                         State state = getValue(channelUID.getId(), userTracking);
66                         updateState(channelUID, state);
67                     });
68             updateTimeStamp(userTracking.getCurrentLocationTimestamp());
69             updateInstallationChannels(userPresenceJSON);
70         } else {
71             logger.debug("UserTrackingList is empty!");
72         }
73     }
74
75     public State getValue(String channelId, UserTracking userTracking) {
76         switch (channelId) {
77             case CHANNEL_USER_NAME:
78                 String name = userTracking.getName();
79                 return name != null ? new StringType(name) : UnDefType.NULL;
80             case CHANNEL_USER_LOCATION_STATUS:
81                 String currentLocation = userTracking.getCurrentLocationName();
82                 return currentLocation != null ? new StringType(currentLocation)
83                         : new StringType(userTracking.getCurrentLocationId());
84             case CHANNEL_STATUS:
85                 String status = userTracking.getStatus();
86                 return status != null ? new StringType(status) : UnDefType.NULL;
87             case CHANNEL_WEBACCOUNT:
88                 String webAccount = userTracking.getWebAccount();
89                 return webAccount != null ? new StringType(webAccount) : UnDefType.NULL;
90             case CHANNEL_USER_DEVICE_NAME:
91                 String deviceName = userTracking.getDeviceName();
92                 return deviceName != null ? new StringType(deviceName) : UnDefType.NULL;
93
94         }
95         return UnDefType.UNDEF;
96     }
97
98     @Override
99     public void updateTriggerChannel(String event) {
100     }
101 }