]> git.basschouten.com Git - openhab-addons.git/blob
dfa99e086adcd02053b6d7d585250e08767bc56a
[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.lutron.internal.radiora.handler;
14
15 import java.util.concurrent.ScheduledFuture;
16 import java.util.concurrent.TimeUnit;
17
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;
39
40 /**
41  * The {@link RS232Handler} is responsible for handling commands, which are
42  * sent to one of the channels.
43  *
44  * @author Jeff Lauterbach - Initial contribution
45  */
46 @NonNullByDefault
47 public class RS232Handler extends BaseBridgeHandler implements RadioRAFeedbackListener {
48
49     private final Logger logger = LoggerFactory.getLogger(RS232Handler.class);
50
51     private RadioRAConnection connection;
52
53     private @Nullable ScheduledFuture<?> zoneMapScheduledTask;
54
55     public RS232Handler(Bridge bridge, SerialPortManager serialPortManager) {
56         super(bridge);
57
58         this.connection = new RS232Connection(serialPortManager);
59         this.connection.setListener(this);
60     }
61
62     @Override
63     public void dispose() {
64         ScheduledFuture<?> zoneMapScheduledTask = this.zoneMapScheduledTask;
65         if (zoneMapScheduledTask != null) {
66             zoneMapScheduledTask.cancel(true);
67         }
68
69         if (connection != null) {
70             connection.disconnect();
71         }
72     }
73
74     @Override
75     public void initialize() {
76         connectToRS232();
77
78         scheduleZoneMapQuery();
79     }
80
81     protected void connectToRS232() {
82         RS232Config config = getConfigAs(RS232Config.class);
83         String portName = config.getPortName();
84         int baud = config.getBaud();
85
86         logger.debug("Attempting to connect to RS232 on port {}", portName);
87
88         try {
89             connection.open(portName, baud);
90         } catch (RadioRAConnectionException e) {
91             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.HANDLER_INITIALIZING_ERROR, e.getMessage());
92             return;
93         }
94
95         logger.debug("Connected successfully");
96
97         updateStatus(ThingStatus.ONLINE);
98     }
99
100     protected void scheduleZoneMapQuery() {
101         RS232Config config = getConfigAs(RS232Config.class);
102         logger.debug("Scheduling zone map query at {} second inverval", config.getZoneMapQueryInterval());
103
104         Runnable task = () -> sendCommand(new ZoneMapInquiryCommand());
105
106         zoneMapScheduledTask = this.scheduler.scheduleWithFixedDelay(task, 3, config.getZoneMapQueryInterval(),
107                 TimeUnit.SECONDS);
108     }
109
110     public void sendCommand(RadioRACommand command) {
111         connection.write(command.toString());
112     }
113
114     @Override
115     public void handleCommand(ChannelUID channelUID, Command command) {
116     }
117
118     @Override
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);
124             } else {
125                 logger.debug("Unexpected - Thing {} is not a LutronHandler", thing.getClass());
126             }
127         }
128     }
129 }