]> git.basschouten.com Git - openhab-addons.git/blob
8e1959fadb3ab25b2a5ba38655ecff9b6cf5b410
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.smartmeter.internal.sml;
14
15 import java.io.BufferedInputStream;
16 import java.io.BufferedOutputStream;
17 import java.io.DataInputStream;
18 import java.io.DataOutputStream;
19 import java.io.IOException;
20 import java.text.MessageFormat;
21 import java.util.Stack;
22 import java.util.function.Supplier;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.binding.smartmeter.connectors.ConnectorBase;
27 import org.openhab.binding.smartmeter.internal.helper.Baudrate;
28 import org.openhab.binding.smartmeter.internal.helper.SerialParameter;
29 import org.openhab.core.io.transport.serial.PortInUseException;
30 import org.openhab.core.io.transport.serial.SerialPort;
31 import org.openhab.core.io.transport.serial.SerialPortIdentifier;
32 import org.openhab.core.io.transport.serial.SerialPortManager;
33 import org.openhab.core.io.transport.serial.UnsupportedCommOperationException;
34 import org.openhab.core.util.HexUtils;
35 import org.openmuc.jsml.structures.SmlFile;
36 import org.openmuc.jsml.transport.Transport;
37
38 /**
39  * Represents a serial SML device connector.
40  *
41  * @author Matthias Steigenberger - Initial contribution
42  * @author Mathias Gilhuber - Also-By
43  */
44 @NonNullByDefault
45 public final class SmlSerialConnector extends ConnectorBase<SmlFile> {
46
47     private static final Transport TRANSPORT = new Transport();
48
49     private Supplier<SerialPortManager> serialManagerSupplier;
50     @NonNullByDefault({})
51     private SerialPort serialPort;
52     @Nullable
53     private DataInputStream is;
54     @Nullable
55     private DataOutputStream os;
56     private int baudrate;
57
58     /**
59      * Constructor to create a serial connector instance.
60      *
61      * @param portName the port where the device is connected as defined in openHAB configuration.
62      */
63     public SmlSerialConnector(Supplier<SerialPortManager> serialPortManagerSupplier, String portName) {
64         super(portName);
65         this.serialManagerSupplier = serialPortManagerSupplier;
66     }
67
68     /**
69      * Constructor to create a serial connector instance with a specific serial parameter.
70      *
71      * @param portName the port where the device is connected as defined in openHAB configuration.
72      * @param baudrate
73      */
74     public SmlSerialConnector(Supplier<SerialPortManager> serialPortManagerSupplier, String portName, int baudrate,
75             int baudrateChangeDelay) {
76         this(serialPortManagerSupplier, portName);
77         this.baudrate = baudrate;
78     }
79
80     @Override
81     protected SmlFile readNext(byte @Nullable [] initMessage) throws IOException {
82         if (initMessage != null) {
83             logger.debug("Writing init message: {}", HexUtils.bytesToHex(initMessage, " "));
84             if (os != null) {
85                 os.write(initMessage);
86                 os.flush();
87             }
88         }
89
90         // read out the whole buffer. We are only interested in the most recent SML file.
91         Stack<SmlFile> smlFiles = new Stack<>();
92         do {
93             logger.trace("Reading {}. SML message", smlFiles.size() + 1);
94             smlFiles.push(TRANSPORT.getSMLFile(is));
95         } while (is != null && is.available() > 0);
96         if (smlFiles.isEmpty()) {
97             throw new IOException(getPortName() + " : There is no SML file in buffer. Try to increase Refresh rate.");
98         }
99         logger.debug("{} : Read {} SML files from Buffer", this.getPortName(), smlFiles.size());
100         return smlFiles.pop();
101     }
102
103     @Override
104     public void openConnection() throws IOException {
105         closeConnection();
106         SerialPortIdentifier id = serialManagerSupplier.get().getIdentifier(getPortName());
107         if (id != null) {
108             try {
109                 serialPort = id.open("meterreaderbinding", 0);
110             } catch (PortInUseException e) {
111                 throw new IOException(MessageFormat
112                         .format("Error at SerialConnector.openConnection: unable to open port {0}.", getPortName()), e);
113             }
114             SerialParameter serialParameter = SerialParameter._8N1;
115             int baudrateToUse = this.baudrate == Baudrate.AUTO.getBaudrate() ? Baudrate._9600.getBaudrate()
116                     : this.baudrate;
117             try {
118                 serialPort.setSerialPortParams(baudrateToUse, serialParameter.getDatabits(),
119                         serialParameter.getStopbits(), serialParameter.getParity());
120                 serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN | SerialPort.FLOWCONTROL_RTSCTS_OUT);
121                 try {
122                     serialPort.enableReceiveTimeout(100);
123                 } catch (UnsupportedCommOperationException e) {
124                     // doesn't matter (rfc2217 is not supporting this)
125                 }
126             } catch (UnsupportedCommOperationException e) {
127                 throw new IOException(MessageFormat.format(
128                         "Error at SerialConnector.openConnection: unable to set serial port parameters for port {0}.",
129                         getPortName()), e);
130             }
131             // serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN | SerialPort.FLOWCONTROL_RTSCTS_OUT);
132             serialPort.notifyOnDataAvailable(true);
133             is = new DataInputStream(new BufferedInputStream(serialPort.getInputStream()));
134             os = new DataOutputStream(new BufferedOutputStream(serialPort.getOutputStream()));
135         } else {
136             throw new IllegalStateException(MessageFormat.format("No provider for port {0} found", getPortName()));
137         }
138     }
139
140     /**
141      * {@inheritDoc}
142      */
143     @Override
144     public void closeConnection() {
145         try {
146             if (is != null) {
147                 is.close();
148                 is = null;
149             }
150         } catch (IOException e) {
151             logger.error("Failed to close serial input stream", e);
152         }
153         try {
154             if (os != null) {
155                 os.close();
156                 os = null;
157             }
158         } catch (IOException e) {
159             logger.error("Failed to close serial output stream", e);
160         }
161         if (serialPort != null) {
162             serialPort.close();
163             serialPort = null;
164         }
165     }
166
167     @Override
168     protected boolean applyPeriod() {
169         return true;
170     }
171 }