]> git.basschouten.com Git - openhab-addons.git/blob
389c1f4352f0b1850d630772d8e98dc4dc5435d9
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.VerisureDoorWindowsDTO;
23 import org.openhab.binding.verisure.internal.dto.VerisureDoorWindowsDTO.DoorWindow;
24 import org.openhab.core.library.types.OpenClosedType;
25 import org.openhab.core.library.types.StringType;
26 import org.openhab.core.thing.Channel;
27 import org.openhab.core.thing.Thing;
28 import org.openhab.core.thing.ThingStatus;
29 import org.openhab.core.thing.ThingTypeUID;
30 import org.openhab.core.types.State;
31 import org.openhab.core.types.UnDefType;
32
33 /**
34  * Handler for the Smart Lock Device thing type that Verisure provides.
35  *
36  * @author Jan Gustafsson - Initial contribution
37  *
38  */
39 @NonNullByDefault
40 public class VerisureDoorWindowThingHandler extends VerisureThingHandler<VerisureDoorWindowsDTO> {
41
42     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_DOORWINDOW);
43
44     public VerisureDoorWindowThingHandler(Thing thing) {
45         super(thing);
46     }
47
48     @Override
49     public Class<VerisureDoorWindowsDTO> getVerisureThingClass() {
50         return VerisureDoorWindowsDTO.class;
51     }
52
53     @Override
54     public synchronized void update(VerisureDoorWindowsDTO thing) {
55         updateDoorWindowState(thing);
56         updateStatus(ThingStatus.ONLINE);
57     }
58
59     private void updateDoorWindowState(VerisureDoorWindowsDTO doorWindowJSON) {
60         List<DoorWindow> doorWindowList = doorWindowJSON.getData().getInstallation().getDoorWindows();
61         if (!doorWindowList.isEmpty()) {
62             DoorWindow doorWindow = doorWindowList.get(0);
63
64             getThing().getChannels().stream().map(Channel::getUID)
65                     .filter(channelUID -> isLinked(channelUID) && !channelUID.getId().equals("timestamp"))
66                     .forEach(channelUID -> {
67                         State state = getValue(channelUID.getId(), doorWindow);
68                         updateState(channelUID, state);
69
70                     });
71             updateTimeStamp(doorWindow.getReportTime());
72             updateInstallationChannels(doorWindowJSON);
73         } else {
74             logger.debug("DoorWindow list is empty!");
75         }
76     }
77
78     public State getValue(String channelId, DoorWindow doorWindow) {
79         switch (channelId) {
80             case CHANNEL_STATE:
81                 return "OPEN".equals(doorWindow.getState()) ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
82             case CHANNEL_LOCATION:
83                 String location = doorWindow.getDevice().getArea();
84                 return location != null ? new StringType(location) : UnDefType.UNDEF;
85         }
86         return UnDefType.UNDEF;
87     }
88
89     @Override
90     public void updateTriggerChannel(String event) {
91         logger.debug("DoorWindowThingHandler trigger event {}", event);
92         triggerChannel(CHANNEL_DOOR_WINDOW_TRIGGER_CHANNEL, event);
93     }
94 }