2 * Copyright (c) 2010-2021 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;
16 import java.util.concurrent.ExecutorService;
17 import java.util.concurrent.TimeoutException;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.teleinfo.internal.dto.Frame;
22 import org.openhab.binding.teleinfo.internal.reader.io.TeleinfoInputStream;
23 import org.openhab.binding.teleinfo.internal.reader.io.serialport.InvalidFrameException;
24 import org.openhab.core.io.transport.serial.SerialPort;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * The {@link TeleinfoReceiveThread} class defines a thread to decode and fire Teleinfo frames for Serial controller.
31 * @author Nicolas SIBERIL - Initial contribution
34 public class TeleinfoReceiveThread extends Thread {
36 private static final int SERIAL_PORT_DELAY_RETRY_IN_SECONDS = 60;
38 private final Logger logger = LoggerFactory.getLogger(TeleinfoReceiveThread.class);
40 private SerialPort serialPort;
41 private @Nullable TeleinfoReceiveThreadListener listener;
42 private boolean autoRepairInvalidADPSgroupLine;
43 private ExecutorService executorService;
45 public TeleinfoReceiveThread(SerialPort serialPort, final TeleinfoSerialControllerHandler listener,
46 boolean autoRepairInvalidADPSgroupLine, ExecutorService scheduler) {
47 super("OH-binding-TeleinfoReceiveThread-" + listener.getThing().getUID().getId());
49 this.serialPort = serialPort;
50 this.listener = listener;
51 this.autoRepairInvalidADPSgroupLine = autoRepairInvalidADPSgroupLine;
52 this.executorService = scheduler;
57 try (TeleinfoInputStream teleinfoStream = new TeleinfoInputStream(serialPort.getInputStream(),
58 TeleinfoInputStream.DEFAULT_TIMEOUT_NEXT_HEADER_FRAME_US * 100,
59 TeleinfoInputStream.DEFAULT_TIMEOUT_READING_FRAME_US * 100, autoRepairInvalidADPSgroupLine,
61 while (!interrupted()) {
62 TeleinfoReceiveThreadListener listener = this.listener;
63 if (listener != null) {
65 Frame nextFrame = teleinfoStream.readNextFrame();
66 if (nextFrame != null)
67 listener.onFrameReceived(this, nextFrame);
68 } catch (InvalidFrameException e) {
69 logger.warn("Got invalid frame. Detail: \"{}\"", e.getLocalizedMessage());
70 listener.onInvalidFrameReceived(this, e);
71 } catch (TimeoutException e) {
72 logger.warn("Got timeout during frame reading", e);
73 logger.warn("Retry in progress. Next retry in {} seconds...",
74 SERIAL_PORT_DELAY_RETRY_IN_SECONDS);
75 listener.continueOnReadNextFrameTimeoutException();
77 Thread.sleep(SERIAL_PORT_DELAY_RETRY_IN_SECONDS * 1000);
78 } catch (InterruptedException e1) {
82 } catch (IOException e) {
83 logger.warn("Got I/O exception. Detail: \"{}\"", e.getLocalizedMessage(), e);
84 listener.onSerialPortInputStreamIOException(this, e);
86 } catch (IllegalStateException e) {
87 logger.warn("Got illegal state exception", e);
91 } catch (IOException e) {
92 logger.warn("An error occurred during serial port input stream opening", e);
95 serialPort.removeEventListener();
98 public @Nullable TeleinfoReceiveThreadListener getListener() {
102 public void setListener(@Nullable TeleinfoReceiveThreadListener listener) {
103 this.listener = listener;