]> git.basschouten.com Git - openhab-addons.git/blob
6e70ee9350d1b9ffd8d89e7aa3d19adfd6546921
[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.doorbird.internal.handler;
14
15 import static org.openhab.binding.doorbird.internal.DoorbirdBindingConstants.*;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.doorbird.internal.api.DoorbirdAPI;
20 import org.openhab.binding.doorbird.internal.api.DoorbirdInfo;
21 import org.openhab.binding.doorbird.internal.config.ControllerConfiguration;
22 import org.openhab.core.library.types.OnOffType;
23 import org.openhab.core.thing.ChannelUID;
24 import org.openhab.core.thing.Thing;
25 import org.openhab.core.thing.ThingStatus;
26 import org.openhab.core.thing.ThingStatusDetail;
27 import org.openhab.core.thing.binding.BaseThingHandler;
28 import org.openhab.core.types.Command;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * The {@link ControllerHandler} is responsible for handling commands
34  * to the A1081 Controller.
35  *
36  * @author Mark Hilbush - Initial contribution
37  */
38 @NonNullByDefault
39 public class ControllerHandler extends BaseThingHandler {
40     private final Logger logger = LoggerFactory.getLogger(ControllerHandler.class);
41
42     private @Nullable String controllerId;
43
44     private DoorbirdAPI api = new DoorbirdAPI();
45
46     public ControllerHandler(Thing thing) {
47         super(thing);
48     }
49
50     @Override
51     public void initialize() {
52         ControllerConfiguration config = getConfigAs(ControllerConfiguration.class);
53         String host = config.doorbirdHost;
54         if (host == null) {
55             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Doorbird host not provided");
56             return;
57         }
58         String user = config.userId;
59         if (user == null) {
60             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "User ID not provided");
61             return;
62         }
63         String password = config.userPassword;
64         if (password == null) {
65             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "User password not provided");
66             return;
67         }
68         api.setAuthorization(host, user, password);
69
70         // Get the Id of the controller for use in the open door API
71         controllerId = getControllerId(config.controllerId);
72         if (controllerId != null) {
73             updateStatus(ThingStatus.ONLINE);
74         } else {
75             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
76                     "Doorbird not configured with a Controller");
77         }
78     }
79
80     @Override
81     public void handleCommand(ChannelUID channelUID, Command command) {
82         logger.debug("Got command {} for channel {} of thing {}", command, channelUID, getThing().getUID());
83
84         switch (channelUID.getId()) {
85             case CHANNEL_OPENDOOR1:
86                 handleOpenDoor(command, "1");
87                 break;
88             case CHANNEL_OPENDOOR2:
89                 handleOpenDoor(command, "2");
90                 break;
91             case CHANNEL_OPENDOOR3:
92                 handleOpenDoor(command, "3");
93                 break;
94         }
95     }
96
97     private void handleOpenDoor(Command command, String doorNumber) {
98         String id = controllerId;
99         if (id == null) {
100             logger.debug("Unable to handle open door command because controller ID is not set");
101             return;
102         }
103         if (command.equals(OnOffType.ON)) {
104             api.openDoorController(id, doorNumber);
105         }
106     }
107
108     private @Nullable String getControllerId(@Nullable String configId) {
109         DoorbirdInfo info = api.getDoorbirdInfo();
110         return info == null ? null : info.getControllerId(configId);
111     }
112 }