]> git.basschouten.com Git - openhab-addons.git/blob
b2e554c525e4b0c33aef1e37ab74b0fb48bc6d28
[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.omnilink.internal.handler;
14
15 import static org.openhab.binding.omnilink.internal.OmnilinkBindingConstants.*;
16
17 import java.util.Map;
18 import java.util.Optional;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.omnilink.internal.discovery.ObjectPropertyRequest;
23 import org.openhab.binding.omnilink.internal.discovery.ObjectPropertyRequests;
24 import org.openhab.core.library.types.OnOffType;
25 import org.openhab.core.thing.ChannelUID;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.thing.ThingStatus;
28 import org.openhab.core.thing.ThingStatusDetail;
29 import org.openhab.core.types.Command;
30 import org.openhab.core.types.RefreshType;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 import com.digitaldan.jomnilinkII.Message;
35 import com.digitaldan.jomnilinkII.MessageTypes.CommandMessage;
36 import com.digitaldan.jomnilinkII.MessageTypes.ObjectStatus;
37 import com.digitaldan.jomnilinkII.MessageTypes.properties.AccessControlReaderProperties;
38 import com.digitaldan.jomnilinkII.MessageTypes.statuses.ExtendedAccessControlReaderLockStatus;
39 import com.digitaldan.jomnilinkII.OmniInvalidResponseException;
40 import com.digitaldan.jomnilinkII.OmniUnknownMessageTypeException;
41
42 /**
43  * The {@link LockHandler} defines some methods that are used to
44  * interface with an OmniLink Lock. This by extension also defines the
45  * Lock thing that openHAB will be able to pick up and interface with.
46  *
47  * @author Brian O'Connell - Initial contribution
48  * @author Ethan Dye - openHAB3 rewrite
49  */
50 @NonNullByDefault
51 public class LockHandler extends AbstractOmnilinkStatusHandler<ExtendedAccessControlReaderLockStatus> {
52     private final Logger logger = LoggerFactory.getLogger(LockHandler.class);
53     private final int thingID = getThingNumber();
54     public @Nullable String number;
55
56     public LockHandler(Thing thing) {
57         super(thing);
58     }
59
60     @Override
61     public void initialize() {
62         super.initialize();
63         final OmnilinkBridgeHandler bridgeHandler = getOmnilinkBridgeHandler();
64         if (bridgeHandler != null) {
65             updateLockProperties(bridgeHandler);
66         } else {
67             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
68                     "Received null bridge while initializing Lock!");
69         }
70     }
71
72     private void updateLockProperties(OmnilinkBridgeHandler bridgeHandler) {
73         ObjectPropertyRequest<AccessControlReaderProperties> objectPropertyRequest = ObjectPropertyRequest
74                 .builder(bridgeHandler, ObjectPropertyRequests.LOCK, thingID, 0).selectNamed().build();
75
76         for (AccessControlReaderProperties lockProperties : objectPropertyRequest) {
77             Map<String, String> properties = editProperties();
78             properties.put(THING_PROPERTIES_NAME, lockProperties.getName());
79             updateProperties(properties);
80         }
81     }
82
83     @Override
84     public void handleCommand(ChannelUID channelUID, Command command) {
85         logger.debug("handleCommand called for channel: {}, command: {}", channelUID, command);
86
87         if (command instanceof RefreshType) {
88             retrieveStatus().ifPresentOrElse(this::updateChannels, () -> updateStatus(ThingStatus.OFFLINE,
89                     ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, "Received null status update!"));
90             return;
91         }
92
93         switch (channelUID.getId()) {
94             case CHANNEL_LOCK_SWITCH:
95                 if (command instanceof OnOffType) {
96                     sendOmnilinkCommand(OnOffType.OFF.equals(command) ? CommandMessage.CMD_UNLOCK_DOOR
97                             : CommandMessage.CMD_LOCK_DOOR, 0, thingID);
98                 } else {
99                     logger.debug("Invalid command {}, must be OnOffType", command);
100                 }
101                 break;
102             default:
103                 logger.warn("Unknown channel for Lock thing: {}", channelUID);
104         }
105     }
106
107     @Override
108     public void updateChannels(ExtendedAccessControlReaderLockStatus status) {
109         logger.debug("updateChannels called for Lock status: {}", status);
110         updateState(CHANNEL_LOCK_SWITCH, OnOffType.from(status.isLocked()));
111     }
112
113     @Override
114     protected Optional<ExtendedAccessControlReaderLockStatus> retrieveStatus() {
115         try {
116             final OmnilinkBridgeHandler bridgeHandler = getOmnilinkBridgeHandler();
117             if (bridgeHandler != null) {
118                 ObjectStatus objStatus = bridgeHandler.requestObjectStatus(Message.OBJ_TYPE_CONTROL_LOCK, thingID,
119                         thingID, true);
120                 return Optional.of((ExtendedAccessControlReaderLockStatus) objStatus.getStatuses()[0]);
121             } else {
122                 logger.debug("Received null bridge while updating Lock status!");
123                 return Optional.empty();
124             }
125         } catch (OmniInvalidResponseException | OmniUnknownMessageTypeException | BridgeOfflineException e) {
126             logger.debug("Received exception while refreshing Lock status: {}", e.getMessage());
127             return Optional.empty();
128         }
129     }
130 }