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