2 * Copyright (c) 2010-2023 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.satel.internal.handler;
15 import static org.openhab.binding.satel.internal.SatelBindingConstants.*;
17 import java.util.Optional;
19 import java.util.concurrent.ScheduledFuture;
20 import java.util.concurrent.TimeUnit;
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;
40 * The {@link Atd100Handler} is responsible for handling commands, which are
41 * sent to one of the channels of an ATD-100 device.
43 * @author Krzysztof Goworek - Initial contribution
46 public class Atd100Handler extends WirelessChannelsHandler {
48 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_ATD100);
50 private final Logger logger = LoggerFactory.getLogger(getClass());
52 private @Nullable ScheduledFuture<?> pollingJob;
54 public Atd100Handler(Thing thing) {
59 public void initialize() {
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);
71 this.pollingJob = scheduler.scheduleWithFixedDelay(pollingCommand, 0, config.getRefresh(),
78 public void dispose() {
79 logger.debug("Disposing thing handler.");
81 final ScheduledFuture<?> pollingJob = this.pollingJob;
82 if (pollingJob != null && !pollingJob.isCancelled()) {
83 pollingJob.cancel(true);
85 this.pollingJob = null;
89 public void handleCommand(ChannelUID channelUID, Command command) {
90 if (CHANNEL_TEMPERATURE.equals(channelUID.getId())) {
91 logger.debug("New command for {}: {}", channelUID, command);
93 if (command == RefreshType.REFRESH) {
94 withBridgeHandlerPresent(bridgeHandler -> {
95 bridgeHandler.sendCommand(new ReadZoneTemperature(getThingConfig().getId()), true);
99 super.handleCommand(channelUID, command);
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));
112 protected boolean isWirelessDevice() {
117 protected Optional<SatelCommand> convertCommand(@Nullable ChannelUID channel, @Nullable Command command) {
118 // no commands supported
119 return Optional.empty();