]> git.basschouten.com Git - openhab-addons.git/blob
e3eefc6fd36ba4cbe464525e7146ccd61f76a9a5
[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.regoheatpump.internal.handler;
14
15 import java.io.IOException;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.regoheatpump.internal.RegoHeatPumpBindingConstants;
20 import org.openhab.binding.regoheatpump.internal.protocol.RegoConnection;
21 import org.openhab.binding.regoheatpump.internal.protocol.SerialRegoConnection;
22 import org.openhab.core.io.transport.serial.SerialPortIdentifier;
23 import org.openhab.core.io.transport.serial.SerialPortManager;
24 import org.openhab.core.thing.Thing;
25 import org.openhab.core.thing.ThingStatus;
26 import org.openhab.core.thing.ThingStatusDetail;
27
28 /**
29  * The {@link SerialHusdataHandler} is responsible for handling commands, which are
30  * sent to one of the channels.
31  *
32  * @author Boris Krivonog - Initial contribution
33  */
34 @NonNullByDefault
35 public class SerialHusdataHandler extends HusdataHandler {
36     private final SerialPortManager serialPortManager;
37     private @Nullable SerialPortIdentifier serialPortIdentifier;
38
39     public SerialHusdataHandler(Thing thing, SerialPortManager serialPortManager) {
40         super(thing);
41         this.serialPortManager = serialPortManager;
42     }
43
44     @Override
45     public void initialize() {
46         String portName = (String) getConfig().get(RegoHeatPumpBindingConstants.PORT_NAME);
47         serialPortIdentifier = serialPortManager.getIdentifier(portName);
48         if (serialPortIdentifier == null) {
49             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
50                     "Serial port does not exist: " + portName);
51         } else {
52             super.initialize();
53         }
54     }
55
56     @Override
57     protected RegoConnection createConnection() throws IOException {
58         SerialPortIdentifier serialPortIdentifier = this.serialPortIdentifier;
59         if (serialPortIdentifier == null) {
60             throw new IOException("Serial port does not exist");
61         }
62         return new SerialRegoConnection(serialPortIdentifier, 19200);
63     }
64 }