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.teleinfo.internal.serial;
15 import java.io.IOException;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.teleinfo.internal.data.Frame;
20 import org.openhab.binding.teleinfo.internal.reader.io.TeleinfoInputStream;
21 import org.openhab.binding.teleinfo.internal.reader.io.serialport.InvalidFrameException;
22 import org.openhab.core.io.transport.serial.SerialPort;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
27 * The {@link TeleinfoReceiveThread} class defines a thread to decode and fire Teleinfo frames for Serial controller.
29 * @author Nicolas SIBERIL - Initial contribution
32 public class TeleinfoReceiveThread extends Thread {
34 private final Logger logger = LoggerFactory.getLogger(TeleinfoReceiveThread.class);
36 private SerialPort serialPort;
37 private @Nullable TeleinfoReceiveThreadListener listener;
38 private boolean autoRepairInvalidADPSgroupLine;
39 private final TeleinfoTicMode ticMode;
40 private final boolean verifyChecksum;
42 public TeleinfoReceiveThread(SerialPort serialPort, final TeleinfoSerialControllerHandler listener,
43 boolean autoRepairInvalidADPSgroupLine, TeleinfoTicMode ticMode, boolean verifyChecksum) {
44 super("OH-binding-TeleinfoReceiveThread-" + listener.getThing().getUID().getId());
46 this.serialPort = serialPort;
47 this.listener = listener;
48 this.autoRepairInvalidADPSgroupLine = autoRepairInvalidADPSgroupLine;
49 this.ticMode = ticMode;
50 this.verifyChecksum = verifyChecksum;
55 try (TeleinfoInputStream teleinfoStream = new TeleinfoInputStream(serialPort.getInputStream(),
56 autoRepairInvalidADPSgroupLine, ticMode, verifyChecksum)) {
57 while (!interrupted()) {
58 TeleinfoReceiveThreadListener listener = this.listener;
59 if (listener != null) {
61 Frame nextFrame = teleinfoStream.readNextFrame();
62 if (nextFrame != null) {
63 listener.onFrameReceived(nextFrame);
65 } catch (InvalidFrameException e) {
66 logger.warn("Got invalid frame. Detail: \"{}\"", e.getLocalizedMessage());
67 listener.onInvalidFrameReceived(this, e);
68 } catch (IOException e) {
69 logger.warn("Got I/O exception. Detail: \"{}\"", e.getLocalizedMessage(), e);
70 listener.onSerialPortInputStreamIOException(this, e);
72 } catch (IllegalStateException e) {
73 logger.warn("Got illegal state exception", e);
77 } catch (IOException e) {
78 logger.warn("An error occurred during serial port input stream opening", e);
81 serialPort.removeEventListener();
84 public @Nullable TeleinfoReceiveThreadListener getListener() {
88 public void setListener(@Nullable TeleinfoReceiveThreadListener listener) {
89 this.listener = listener;