]> git.basschouten.com Git - openhab-addons.git/blob
b50e8b1a7a16856438bd487b62266a51a98d2728
[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.satel.internal.handler;
14
15 import static org.openhab.binding.satel.internal.SatelBindingConstants.*;
16
17 import java.util.Optional;
18 import java.util.Set;
19 import java.util.concurrent.ScheduledFuture;
20 import java.util.concurrent.TimeUnit;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.satel.internal.command.ReadZoneTemperature;
25 import org.openhab.binding.satel.internal.command.SatelCommand;
26 import org.openhab.binding.satel.internal.config.Atd100Config;
27 import org.openhab.binding.satel.internal.event.ZoneTemperatureEvent;
28 import org.openhab.core.library.types.QuantityType;
29 import org.openhab.core.library.unit.SIUnits;
30 import org.openhab.core.thing.ChannelUID;
31 import org.openhab.core.thing.Thing;
32 import org.openhab.core.thing.ThingStatus;
33 import org.openhab.core.thing.ThingTypeUID;
34 import org.openhab.core.types.Command;
35 import org.openhab.core.types.RefreshType;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * The {@link Atd100Handler} is responsible for handling commands, which are
41  * sent to one of the channels of an ATD-100 device.
42  *
43  * @author Krzysztof Goworek - Initial contribution
44  */
45 @NonNullByDefault
46 public class Atd100Handler extends WirelessChannelsHandler {
47
48     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_ATD100);
49
50     private final Logger logger = LoggerFactory.getLogger(getClass());
51
52     private @Nullable ScheduledFuture<?> pollingJob;
53
54     public Atd100Handler(Thing thing) {
55         super(thing);
56     }
57
58     @Override
59     public void initialize() {
60         super.initialize();
61
62         withBridgeHandlerPresent(bridgeHandler -> {
63             final ScheduledFuture<?> pollingJob = this.pollingJob;
64             if (pollingJob == null || pollingJob.isCancelled()) {
65                 Atd100Config config = getConfigAs(Atd100Config.class);
66                 Runnable pollingCommand = () -> {
67                     if (bridgeHandler.getThing().getStatus() == ThingStatus.ONLINE) {
68                         bridgeHandler.sendCommand(new ReadZoneTemperature(getThingConfig().getId()), true);
69                     }
70                 };
71                 this.pollingJob = scheduler.scheduleWithFixedDelay(pollingCommand, 0, config.getRefresh(),
72                         TimeUnit.MINUTES);
73             }
74         });
75     }
76
77     @Override
78     public void dispose() {
79         logger.debug("Disposing thing handler.");
80
81         final ScheduledFuture<?> pollingJob = this.pollingJob;
82         if (pollingJob != null && !pollingJob.isCancelled()) {
83             pollingJob.cancel(true);
84         }
85         this.pollingJob = null;
86     }
87
88     @Override
89     public void handleCommand(ChannelUID channelUID, Command command) {
90         if (CHANNEL_TEMPERATURE.equals(channelUID.getId())) {
91             logger.debug("New command for {}: {}", channelUID, command);
92
93             if (command == RefreshType.REFRESH) {
94                 withBridgeHandlerPresent(bridgeHandler -> {
95                     bridgeHandler.sendCommand(new ReadZoneTemperature(getThingConfig().getId()), true);
96                 });
97             }
98         } else {
99             super.handleCommand(channelUID, command);
100         }
101     }
102
103     @Override
104     public void incomingEvent(ZoneTemperatureEvent event) {
105         logger.trace("Handling incoming event: {}", event);
106         if (event.getZoneNbr() == getThingConfig().getId()) {
107             updateState(CHANNEL_TEMPERATURE, new QuantityType<>(event.getTemperature(), SIUnits.CELSIUS));
108         }
109     }
110
111     @Override
112     protected boolean isWirelessDevice() {
113         return true;
114     }
115
116     @Override
117     protected Optional<SatelCommand> convertCommand(@Nullable ChannelUID channel, @Nullable Command command) {
118         // no commands supported
119         return Optional.empty();
120     }
121 }