2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.omnilink.internal.handler;
15 import static org.openhab.binding.omnilink.internal.OmnilinkBindingConstants.*;
17 import java.util.Optional;
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;
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;
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.
47 * @author Brian O'Connell - Initial contribution
48 * @author Ethan Dye - openHAB3 rewrite
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;
56 public LockHandler(Thing thing) {
61 public void initialize() {
63 final OmnilinkBridgeHandler bridgeHandler = getOmnilinkBridgeHandler();
64 if (bridgeHandler != null) {
65 updateLockProperties(bridgeHandler);
67 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
68 "Received null bridge while initializing Lock!");
72 private void updateLockProperties(OmnilinkBridgeHandler bridgeHandler) {
73 ObjectPropertyRequest<AccessControlReaderProperties> objectPropertyRequest = ObjectPropertyRequest
74 .builder(bridgeHandler, ObjectPropertyRequests.LOCK, thingID, 0).selectNamed().build();
76 for (AccessControlReaderProperties lockProperties : objectPropertyRequest) {
77 updateProperty(THING_PROPERTIES_NAME, lockProperties.getName());
82 public void handleCommand(ChannelUID channelUID, Command command) {
83 logger.debug("handleCommand called for channel: {}, command: {}", channelUID, command);
85 if (command instanceof RefreshType) {
86 retrieveStatus().ifPresentOrElse(this::updateChannels, () -> updateStatus(ThingStatus.OFFLINE,
87 ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, "Received null status update!"));
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);
97 logger.debug("Invalid command {}, must be OnOffType", command);
101 logger.warn("Unknown channel for Lock thing: {}", channelUID);
106 public void updateChannels(ExtendedAccessControlReaderLockStatus status) {
107 logger.debug("updateChannels called for Lock status: {}", status);
108 updateState(CHANNEL_LOCK_SWITCH, OnOffType.from(status.isLocked()));
112 protected Optional<ExtendedAccessControlReaderLockStatus> retrieveStatus() {
114 final OmnilinkBridgeHandler bridgeHandler = getOmnilinkBridgeHandler();
115 if (bridgeHandler != null) {
116 ObjectStatus objStatus = bridgeHandler.requestObjectStatus(Message.OBJ_TYPE_CONTROL_LOCK, thingID,
118 return Optional.of((ExtendedAccessControlReaderLockStatus) objStatus.getStatuses()[0]);
120 logger.debug("Received null bridge while updating Lock status!");
121 return Optional.empty();
123 } catch (OmniInvalidResponseException | OmniUnknownMessageTypeException | BridgeOfflineException e) {
124 logger.debug("Received exception while refreshing Lock status: {}", e.getMessage());
125 return Optional.empty();