]> git.basschouten.com Git - openhab-addons.git/blob
0f2e52c53469efda40eb46342cf2937721dc3040
[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.omnilink.internal.handler;
14
15 import static org.openhab.binding.omnilink.internal.OmnilinkBindingConstants.*;
16
17 import java.util.Optional;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.omnilink.internal.discovery.ObjectPropertyRequest;
22 import org.openhab.binding.omnilink.internal.discovery.ObjectPropertyRequests;
23 import org.openhab.binding.omnilink.internal.exceptions.BridgeOfflineException;
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             updateProperty(THING_PROPERTIES_NAME, lockProperties.getName());
78         }
79     }
80
81     @Override
82     public void handleCommand(ChannelUID channelUID, Command command) {
83         logger.debug("handleCommand called for channel: {}, command: {}", channelUID, command);
84
85         if (command instanceof RefreshType) {
86             retrieveStatus().ifPresentOrElse(this::updateChannels, () -> updateStatus(ThingStatus.OFFLINE,
87                     ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, "Received null status update!"));
88             return;
89         }
90
91         switch (channelUID.getId()) {
92             case CHANNEL_LOCK_SWITCH:
93                 if (command instanceof OnOffType) {
94                     sendOmnilinkCommand(OnOffType.OFF.equals(command) ? CommandMessage.CMD_UNLOCK_DOOR
95                             : CommandMessage.CMD_LOCK_DOOR, 0, thingID);
96                 } else {
97                     logger.debug("Invalid command {}, must be OnOffType", command);
98                 }
99                 break;
100             default:
101                 logger.warn("Unknown channel for Lock thing: {}", channelUID);
102         }
103     }
104
105     @Override
106     public void updateChannels(ExtendedAccessControlReaderLockStatus status) {
107         logger.debug("updateChannels called for Lock status: {}", status);
108         updateState(CHANNEL_LOCK_SWITCH, OnOffType.from(status.isLocked()));
109     }
110
111     @Override
112     protected Optional<ExtendedAccessControlReaderLockStatus> retrieveStatus() {
113         try {
114             final OmnilinkBridgeHandler bridgeHandler = getOmnilinkBridgeHandler();
115             if (bridgeHandler != null) {
116                 ObjectStatus objStatus = bridgeHandler.requestObjectStatus(Message.OBJ_TYPE_CONTROL_LOCK, thingID,
117                         thingID, true);
118                 return Optional.of((ExtendedAccessControlReaderLockStatus) objStatus.getStatuses()[0]);
119             } else {
120                 logger.debug("Received null bridge while updating Lock status!");
121                 return Optional.empty();
122             }
123         } catch (OmniInvalidResponseException | OmniUnknownMessageTypeException | BridgeOfflineException e) {
124             logger.debug("Received exception while refreshing Lock status: {}", e.getMessage());
125             return Optional.empty();
126         }
127     }
128 }