]> git.basschouten.com Git - openhab-addons.git/blob
cea7ad225d914835a1d2929111408f9d86b80213
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.teleinfo.internal.serial;
14
15 import java.io.IOException;
16 import java.util.concurrent.ExecutorService;
17 import java.util.concurrent.TimeoutException;
18
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;
27
28 /**
29  * The {@link TeleinfoReceiveThread} class defines a thread to decode and fire Teleinfo frames for Serial controller.
30  *
31  * @author Nicolas SIBERIL - Initial contribution
32  */
33 @NonNullByDefault
34 public class TeleinfoReceiveThread extends Thread {
35
36     private static final int SERIAL_PORT_DELAY_RETRY_IN_SECONDS = 60;
37
38     private final Logger logger = LoggerFactory.getLogger(TeleinfoReceiveThread.class);
39
40     private SerialPort serialPort;
41     private @Nullable TeleinfoReceiveThreadListener listener;
42     private boolean autoRepairInvalidADPSgroupLine;
43     private ExecutorService executorService;
44
45     public TeleinfoReceiveThread(SerialPort serialPort, final TeleinfoSerialControllerHandler listener,
46             boolean autoRepairInvalidADPSgroupLine, ExecutorService scheduler) {
47         super("OH-binding-TeleinfoReceiveThread-" + listener.getThing().getUID().getId());
48         setDaemon(true);
49         this.serialPort = serialPort;
50         this.listener = listener;
51         this.autoRepairInvalidADPSgroupLine = autoRepairInvalidADPSgroupLine;
52         this.executorService = scheduler;
53     }
54
55     @Override
56     public void run() {
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,
60                 executorService)) {
61             while (!interrupted()) {
62                 TeleinfoReceiveThreadListener listener = this.listener;
63                 if (listener != null) {
64                     try {
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();
76                         try {
77                             Thread.sleep(SERIAL_PORT_DELAY_RETRY_IN_SECONDS * 1000);
78                         } catch (InterruptedException e1) {
79                             break;
80                         }
81
82                     } catch (IOException e) {
83                         logger.warn("Got I/O exception. Detail: \"{}\"", e.getLocalizedMessage(), e);
84                         listener.onSerialPortInputStreamIOException(this, e);
85                         break;
86                     } catch (IllegalStateException e) {
87                         logger.warn("Got illegal state exception", e);
88                     }
89                 }
90             }
91         } catch (IOException e) {
92             logger.warn("An error occurred during serial port input stream opening", e);
93         }
94
95         serialPort.removeEventListener();
96     }
97
98     public @Nullable TeleinfoReceiveThreadListener getListener() {
99         return listener;
100     }
101
102     public void setListener(@Nullable TeleinfoReceiveThreadListener listener) {
103         this.listener = listener;
104     }
105 }