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.lutron.internal.radiora.handler;
15 import java.util.concurrent.ScheduledFuture;
16 import java.util.concurrent.TimeUnit;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.lutron.internal.radiora.RS232Connection;
21 import org.openhab.binding.lutron.internal.radiora.RadioRAConnection;
22 import org.openhab.binding.lutron.internal.radiora.RadioRAConnectionException;
23 import org.openhab.binding.lutron.internal.radiora.RadioRAFeedbackListener;
24 import org.openhab.binding.lutron.internal.radiora.config.RS232Config;
25 import org.openhab.binding.lutron.internal.radiora.protocol.RadioRACommand;
26 import org.openhab.binding.lutron.internal.radiora.protocol.RadioRAFeedback;
27 import org.openhab.binding.lutron.internal.radiora.protocol.ZoneMapInquiryCommand;
28 import org.openhab.core.io.transport.serial.SerialPortManager;
29 import org.openhab.core.thing.Bridge;
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.ThingStatusDetail;
34 import org.openhab.core.thing.binding.BaseBridgeHandler;
35 import org.openhab.core.thing.binding.ThingHandler;
36 import org.openhab.core.types.Command;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * The {@link RS232Handler} is responsible for handling commands, which are
42 * sent to one of the channels.
44 * @author Jeff Lauterbach - Initial contribution
47 public class RS232Handler extends BaseBridgeHandler implements RadioRAFeedbackListener {
49 private final Logger logger = LoggerFactory.getLogger(RS232Handler.class);
51 private RadioRAConnection connection;
53 private @Nullable ScheduledFuture<?> zoneMapScheduledTask;
55 public RS232Handler(Bridge bridge, SerialPortManager serialPortManager) {
58 this.connection = new RS232Connection(serialPortManager);
59 this.connection.setListener(this);
63 public void dispose() {
64 ScheduledFuture<?> zoneMapScheduledTask = this.zoneMapScheduledTask;
65 if (zoneMapScheduledTask != null) {
66 zoneMapScheduledTask.cancel(true);
69 if (connection != null) {
70 connection.disconnect();
75 public void initialize() {
78 scheduleZoneMapQuery();
81 protected void connectToRS232() {
82 RS232Config config = getConfigAs(RS232Config.class);
83 String portName = config.getPortName();
84 int baud = config.getBaud();
86 logger.debug("Attempting to connect to RS232 on port {}", portName);
89 connection.open(portName, baud);
90 } catch (RadioRAConnectionException e) {
91 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.HANDLER_INITIALIZING_ERROR, e.getMessage());
95 logger.debug("Connected successfully");
97 updateStatus(ThingStatus.ONLINE);
100 protected void scheduleZoneMapQuery() {
101 RS232Config config = getConfigAs(RS232Config.class);
102 logger.debug("Scheduling zone map query at {} second inverval", config.getZoneMapQueryInterval());
104 Runnable task = () -> sendCommand(new ZoneMapInquiryCommand());
106 zoneMapScheduledTask = this.scheduler.scheduleWithFixedDelay(task, 3, config.getZoneMapQueryInterval(),
110 public void sendCommand(RadioRACommand command) {
111 connection.write(command.toString());
115 public void handleCommand(ChannelUID channelUID, Command command) {
119 public void handleRadioRAFeedback(RadioRAFeedback feedback) {
120 for (Thing thing : getThing().getThings()) {
121 ThingHandler handler = thing.getHandler();
122 if (handler instanceof LutronHandler) {
123 ((LutronHandler) handler).handleFeedback(feedback);
125 logger.debug("Unexpected - Thing {} is not a LutronHandler", thing.getClass());