]> git.basschouten.com Git - openhab-addons.git/blob
d20e5d42dc8df140c8ed9cb58ec6d0a7f938ca6d
[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.broadlinkthermostat.internal.handler;
14
15 import java.io.IOException;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.broadlinkthermostat.internal.BroadlinkConfig;
20 import org.openhab.core.thing.Thing;
21 import org.openhab.core.thing.ThingStatus;
22 import org.openhab.core.thing.ThingStatusDetail;
23 import org.openhab.core.thing.binding.BaseThingHandler;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 import com.github.mob41.blapi.BLDevice;
28
29 /**
30  * The {@link BroadlinkBaseHandler} is the device handler class for a broadlink device.
31  *
32  * @author Florian Mueller - Initial contribution
33  */
34 @NonNullByDefault
35 public abstract class BroadlinkBaseHandler extends BaseThingHandler {
36
37     private final Logger logger = LoggerFactory.getLogger(BroadlinkBaseHandler.class);
38
39     @Nullable
40     BLDevice blDevice;
41     String host = "";
42     String macAddress = "";
43
44     /**
45      * Creates a new instance of this class for the {@link Thing}.
46      *
47      * @param thing the thing that should be handled, not null
48      */
49     BroadlinkBaseHandler(Thing thing) {
50         super(thing);
51     }
52
53     void authenticate(boolean reauth) {
54         logger.debug("Authenticating with broadlink device {}...", thing.getLabel());
55         try {
56             BLDevice blDevice = this.blDevice;
57             if (blDevice != null && blDevice.auth(reauth)) {
58                 updateStatus(ThingStatus.ONLINE);
59             }
60         } catch (IOException e) {
61             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
62                     "Error while authenticating broadlink device " + thing.getLabel() + ":" + e.getMessage());
63         }
64     }
65
66     @Override
67     public void initialize() {
68         BroadlinkConfig config = getConfigAs(BroadlinkConfig.class);
69         host = config.getHost();
70         macAddress = config.getMacAddress();
71     }
72 }