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