2 * Copyright (c) 2010-2022 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.broadlinkthermostat.internal.handler;
15 import java.io.IOException;
16 import java.util.concurrent.ScheduledFuture;
17 import java.util.concurrent.TimeUnit;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.broadlinkthermostat.internal.BroadlinkThermostatConfig;
22 import org.openhab.core.thing.Thing;
23 import org.openhab.core.thing.ThingStatus;
24 import org.openhab.core.thing.ThingStatusDetail;
25 import org.openhab.core.thing.binding.BaseThingHandler;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 import com.github.mob41.blapi.BLDevice;
32 * The {@link BroadlinkThermostatHandler} is the device handler class for a broadlinkthermostat device.
34 * @author Florian Mueller - Initial contribution
37 public abstract class BroadlinkThermostatHandler extends BaseThingHandler {
39 private final Logger logger = LoggerFactory.getLogger(BroadlinkThermostatHandler.class);
43 private @Nullable ScheduledFuture<?> scanJob;
50 * Creates a new instance of this class for the {@link Thing}.
52 * @param thing the thing that should be handled, not null
54 BroadlinkThermostatHandler(Thing thing) {
58 void authenticate(boolean reauth) {
59 logger.debug("Authenticating with broadlinkthermostat device {}...", thing.getLabel());
61 BLDevice blDevice = this.blDevice;
62 if (blDevice != null && blDevice.auth(reauth)) {
63 updateStatus(ThingStatus.ONLINE);
65 } catch (IOException e) {
66 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
67 "Error while authenticating broadlinkthermostat device " + thing.getLabel() + ":" + e.getMessage());
72 public void initialize() {
73 BroadlinkThermostatConfig config = getConfigAs(BroadlinkThermostatConfig.class);
74 host = config.getHost();
75 macAddress = config.getMacAddress();
77 // schedule a new scan every minute
78 scanJob = scheduler.scheduleWithFixedDelay(this::refreshData, 0, 1, TimeUnit.MINUTES);
81 protected abstract void refreshData();
84 public void dispose() {
85 ScheduledFuture<?> currentScanJob = scanJob;
86 if (currentScanJob != null) {
87 currentScanJob.cancel(true);