2 * Copyright (c) 2010-2021 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.*;
18 import java.util.Optional;
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;
34 import com.digitaldan.jomnilinkII.Message;
35 import com.digitaldan.jomnilinkII.MessageTypes.ObjectStatus;
36 import com.digitaldan.jomnilinkII.MessageTypes.properties.AccessControlReaderProperties;
37 import com.digitaldan.jomnilinkII.MessageTypes.statuses.ExtendedAccessControlReaderLockStatus;
38 import com.digitaldan.jomnilinkII.OmniInvalidResponseException;
39 import com.digitaldan.jomnilinkII.OmniUnknownMessageTypeException;
42 * The {@link LockHandler} defines some methods that are used to
43 * interface with an OmniLink Lock. This by extension also defines the
44 * Lock thing that openHAB will be able to pick up and interface with.
46 * @author Brian O'Connell - Initial contribution
47 * @author Ethan Dye - openHAB3 rewrite
50 public class LockHandler extends AbstractOmnilinkStatusHandler<ExtendedAccessControlReaderLockStatus> {
51 private final Logger logger = LoggerFactory.getLogger(LockHandler.class);
52 private final int thingID = getThingNumber();
53 public @Nullable String number;
55 public LockHandler(Thing thing) {
60 public void initialize() {
62 final OmnilinkBridgeHandler bridgeHandler = getOmnilinkBridgeHandler();
63 if (bridgeHandler != null) {
64 updateLockProperties(bridgeHandler);
66 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
67 "Received null bridge while initializing Lock!");
71 private void updateLockProperties(OmnilinkBridgeHandler bridgeHandler) {
72 ObjectPropertyRequest<AccessControlReaderProperties> objectPropertyRequest = ObjectPropertyRequest
73 .builder(bridgeHandler, ObjectPropertyRequests.LOCK, thingID, 0).selectNamed().build();
75 for (AccessControlReaderProperties lockProperties : objectPropertyRequest) {
76 Map<String, String> properties = editProperties();
77 properties.put(THING_PROPERTIES_NAME, lockProperties.getName());
78 updateProperties(properties);
83 public void handleCommand(ChannelUID channelUID, Command command) {
84 logger.debug("handleCommand called for channel: {}, command: {}", channelUID, command);
86 if (command instanceof RefreshType) {
87 retrieveStatus().ifPresentOrElse(this::updateChannels, () -> updateStatus(ThingStatus.OFFLINE,
88 ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, "Received null staus update!"));
92 switch (channelUID.getId()) {
93 case CHANNEL_LOCK_SWITCH:
94 if (command instanceof OnOffType) {
95 sendOmnilinkCommand(OnOffType.OFF.equals(command) ? OmniLinkCmd.CMD_UNLOCK_DOOR.getNumber()
96 : OmniLinkCmd.CMD_LOCK_DOOR.getNumber(), 0, thingID);
98 logger.debug("Invalid command {}, must be OnOffType", command);
102 logger.warn("Unknown channel for Lock thing: {}", channelUID);
107 public void updateChannels(ExtendedAccessControlReaderLockStatus status) {
108 logger.debug("updateChannels called for Lock status: {}", status);
109 updateState(CHANNEL_LOCK_SWITCH, OnOffType.from(status.isLocked()));
113 protected Optional<ExtendedAccessControlReaderLockStatus> retrieveStatus() {
115 final OmnilinkBridgeHandler bridgeHandler = getOmnilinkBridgeHandler();
116 if (bridgeHandler != null) {
117 ObjectStatus objStatus = bridgeHandler.requestObjectStatus(Message.OBJ_TYPE_CONTROL_LOCK, thingID,
119 return Optional.of((ExtendedAccessControlReaderLockStatus) objStatus.getStatuses()[0]);
121 logger.debug("Received null bridge while updating Lock status!");
122 return Optional.empty();
124 } catch (OmniInvalidResponseException | OmniUnknownMessageTypeException | BridgeOfflineException e) {
125 logger.debug("Received exception while refreshing Lock status: {}", e.getMessage());
126 return Optional.empty();