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.Collections;
18 import java.util.Optional;
20 import java.util.concurrent.ScheduledFuture;
21 import java.util.concurrent.TimeUnit;
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;
41 * The {@link Atd100Handler} is responsible for handling commands, which are
42 * sent to one of the channels of an ATD-100 device.
44 * @author Krzysztof Goworek - Initial contribution
47 public class Atd100Handler extends WirelessChannelsHandler {
49 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_ATD100);
51 private final Logger logger = LoggerFactory.getLogger(getClass());
53 private @Nullable ScheduledFuture<?> pollingJob;
55 public Atd100Handler(Thing thing) {
60 public void initialize() {
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);
72 this.pollingJob = scheduler.scheduleWithFixedDelay(pollingCommand, 0, config.getRefresh(),
79 public void dispose() {
80 logger.debug("Disposing thing handler.");
82 final ScheduledFuture<?> pollingJob = this.pollingJob;
83 if (pollingJob != null && !pollingJob.isCancelled()) {
84 pollingJob.cancel(true);
86 this.pollingJob = null;
90 public void handleCommand(ChannelUID channelUID, Command command) {
91 if (CHANNEL_TEMPERATURE.equals(channelUID.getId())) {
92 logger.debug("New command for {}: {}", channelUID, command);
94 if (command == RefreshType.REFRESH) {
95 withBridgeHandlerPresent(bridgeHandler -> {
96 bridgeHandler.sendCommand(new ReadZoneTemperature(getThingConfig().getId()), true);
100 super.handleCommand(channelUID, command);
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));
113 protected boolean isWirelessDevice() {
118 protected Optional<SatelCommand> convertCommand(@Nullable ChannelUID channel, @Nullable Command command) {
119 // no commands supported
120 return Optional.empty();