]> git.basschouten.com Git - openhab-addons.git/blob
92a20ec8e6d51303b31c3cf35ae51f2a3c6c99d6
[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.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.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;
40
41 /**
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.
45  *
46  * @author Brian O'Connell - Initial contribution
47  * @author Ethan Dye - openHAB3 rewrite
48  */
49 @NonNullByDefault
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;
54
55     public LockHandler(Thing thing) {
56         super(thing);
57     }
58
59     @Override
60     public void initialize() {
61         super.initialize();
62         final OmnilinkBridgeHandler bridgeHandler = getOmnilinkBridgeHandler();
63         if (bridgeHandler != null) {
64             updateLockProperties(bridgeHandler);
65         } else {
66             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
67                     "Received null bridge while initializing Lock!");
68         }
69     }
70
71     private void updateLockProperties(OmnilinkBridgeHandler bridgeHandler) {
72         ObjectPropertyRequest<AccessControlReaderProperties> objectPropertyRequest = ObjectPropertyRequest
73                 .builder(bridgeHandler, ObjectPropertyRequests.LOCK, thingID, 0).selectNamed().build();
74
75         for (AccessControlReaderProperties lockProperties : objectPropertyRequest) {
76             Map<String, String> properties = editProperties();
77             properties.put(THING_PROPERTIES_NAME, lockProperties.getName());
78             updateProperties(properties);
79         }
80     }
81
82     @Override
83     public void handleCommand(ChannelUID channelUID, Command command) {
84         logger.debug("handleCommand called for channel: {}, command: {}", channelUID, command);
85
86         if (command instanceof RefreshType) {
87             retrieveStatus().ifPresentOrElse(this::updateChannels, () -> updateStatus(ThingStatus.OFFLINE,
88                     ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, "Received null staus update!"));
89             return;
90         }
91
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);
97                 } else {
98                     logger.debug("Invalid command {}, must be OnOffType", command);
99                 }
100                 break;
101             default:
102                 logger.warn("Unknown channel for Lock thing: {}", channelUID);
103         }
104     }
105
106     @Override
107     public void updateChannels(ExtendedAccessControlReaderLockStatus status) {
108         logger.debug("updateChannels called for Lock status: {}", status);
109         updateState(CHANNEL_LOCK_SWITCH, OnOffType.from(status.isLocked()));
110     }
111
112     @Override
113     protected Optional<ExtendedAccessControlReaderLockStatus> retrieveStatus() {
114         try {
115             final OmnilinkBridgeHandler bridgeHandler = getOmnilinkBridgeHandler();
116             if (bridgeHandler != null) {
117                 ObjectStatus objStatus = bridgeHandler.requestObjectStatus(Message.OBJ_TYPE_CONTROL_LOCK, thingID,
118                         thingID, true);
119                 return Optional.of((ExtendedAccessControlReaderLockStatus) objStatus.getStatuses()[0]);
120             } else {
121                 logger.debug("Received null bridge while updating Lock status!");
122                 return Optional.empty();
123             }
124         } catch (OmniInvalidResponseException | OmniUnknownMessageTypeException | BridgeOfflineException e) {
125             logger.debug("Received exception while refreshing Lock status: {}", e.getMessage());
126             return Optional.empty();
127         }
128     }
129 }