]> git.basschouten.com Git - openhab-addons.git/blob
83cfdbd28dd61fd50a8cd26e4be66609a83d95fd
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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 import java.util.concurrent.ScheduledFuture;
17 import java.util.concurrent.TimeUnit;
18
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;
28
29 import com.github.mob41.blapi.BLDevice;
30
31 /**
32  * The {@link BroadlinkThermostatHandler} is the device handler class for a broadlinkthermostat device.
33  *
34  * @author Florian Mueller - Initial contribution
35  */
36 @NonNullByDefault
37 public abstract class BroadlinkThermostatHandler extends BaseThingHandler {
38
39     private final Logger logger = LoggerFactory.getLogger(BroadlinkThermostatHandler.class);
40
41     @Nullable
42     BLDevice blDevice;
43     private @Nullable ScheduledFuture<?> scanJob;
44     @Nullable
45     String host;
46     @Nullable
47     String macAddress;
48
49     /**
50      * Creates a new instance of this class for the {@link Thing}.
51      *
52      * @param thing the thing that should be handled, not null
53      */
54     BroadlinkThermostatHandler(Thing thing) {
55         super(thing);
56     }
57
58     void authenticate(boolean reauth) {
59         logger.debug("Authenticating with broadlinkthermostat device {}...", thing.getLabel());
60         try {
61             BLDevice blDevice = this.blDevice;
62             if (blDevice != null && blDevice.auth(reauth)) {
63                 updateStatus(ThingStatus.ONLINE);
64             }
65         } catch (IOException e) {
66             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
67                     "Error while authenticating broadlinkthermostat device " + thing.getLabel() + ":" + e.getMessage());
68         }
69     }
70
71     @Override
72     public void initialize() {
73         BroadlinkThermostatConfig config = getConfigAs(BroadlinkThermostatConfig.class);
74         host = config.getHost();
75         macAddress = config.getMacAddress();
76
77         // schedule a new scan every minute
78         scanJob = scheduler.scheduleWithFixedDelay(this::refreshData, 0, 1, TimeUnit.MINUTES);
79     }
80
81     protected abstract void refreshData();
82
83     @Override
84     public void dispose() {
85         ScheduledFuture<?> currentScanJob = scanJob;
86         if (currentScanJob != null) {
87             currentScanJob.cancel(true);
88         }
89     }
90 }