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.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;
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;
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.
48 * @author Brian O'Connell - Initial contribution
49 * @author Ethan Dye - openHAB3 rewrite
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;
57 public LockHandler(Thing thing) {
62 public void initialize() {
64 final OmnilinkBridgeHandler bridgeHandler = getOmnilinkBridgeHandler();
65 if (bridgeHandler != null) {
66 updateLockProperties(bridgeHandler);
68 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
69 "Received null bridge while initializing Lock!");
73 private void updateLockProperties(OmnilinkBridgeHandler bridgeHandler) {
74 ObjectPropertyRequest<AccessControlReaderProperties> objectPropertyRequest = ObjectPropertyRequest
75 .builder(bridgeHandler, ObjectPropertyRequests.LOCK, thingID, 0).selectNamed().build();
77 for (AccessControlReaderProperties lockProperties : objectPropertyRequest) {
78 Map<String, String> properties = editProperties();
79 properties.put(THING_PROPERTIES_NAME, lockProperties.getName());
80 updateProperties(properties);
85 public void handleCommand(ChannelUID channelUID, Command command) {
86 logger.debug("handleCommand called for channel: {}, command: {}", channelUID, command);
88 if (command instanceof RefreshType) {
89 retrieveStatus().ifPresentOrElse(this::updateChannels, () -> updateStatus(ThingStatus.OFFLINE,
90 ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, "Received null status update!"));
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);
100 logger.debug("Invalid command {}, must be OnOffType", command);
104 logger.warn("Unknown channel for Lock thing: {}", channelUID);
109 public void updateChannels(ExtendedAccessControlReaderLockStatus status) {
110 logger.debug("updateChannels called for Lock status: {}", status);
111 updateState(CHANNEL_LOCK_SWITCH, OnOffType.from(status.isLocked()));
115 protected Optional<ExtendedAccessControlReaderLockStatus> retrieveStatus() {
117 final OmnilinkBridgeHandler bridgeHandler = getOmnilinkBridgeHandler();
118 if (bridgeHandler != null) {
119 ObjectStatus objStatus = bridgeHandler.requestObjectStatus(Message.OBJ_TYPE_CONTROL_LOCK, thingID,
121 return Optional.of((ExtendedAccessControlReaderLockStatus) objStatus.getStatuses()[0]);
123 logger.debug("Received null bridge while updating Lock status!");
124 return Optional.empty();
126 } catch (OmniInvalidResponseException | OmniUnknownMessageTypeException | BridgeOfflineException e) {
127 logger.debug("Received exception while refreshing Lock status: {}", e.getMessage());
128 return Optional.empty();