]> git.basschouten.com Git - openhab-addons.git/blob
90b4a3318f7ec5fd71cf7a9d422caba5ba09e696
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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
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;
25
26 /**
27  * The {@link TeleinfoReceiveThread} class defines a thread to decode and fire Teleinfo frames for Serial controller.
28  *
29  * @author Nicolas SIBERIL - Initial contribution
30  */
31 @NonNullByDefault
32 public class TeleinfoReceiveThread extends Thread {
33
34     private final Logger logger = LoggerFactory.getLogger(TeleinfoReceiveThread.class);
35
36     private SerialPort serialPort;
37     private @Nullable TeleinfoReceiveThreadListener listener;
38     private boolean autoRepairInvalidADPSgroupLine;
39
40     public TeleinfoReceiveThread(SerialPort serialPort, final TeleinfoSerialControllerHandler listener,
41             boolean autoRepairInvalidADPSgroupLine) {
42         super("OH-binding-TeleinfoReceiveThread-" + listener.getThing().getUID().getId());
43         setDaemon(true);
44         this.serialPort = serialPort;
45         this.listener = listener;
46         this.autoRepairInvalidADPSgroupLine = autoRepairInvalidADPSgroupLine;
47     }
48
49     @Override
50     public void run() {
51         try (TeleinfoInputStream teleinfoStream = new TeleinfoInputStream(serialPort.getInputStream(),
52                 autoRepairInvalidADPSgroupLine)) {
53             while (!interrupted()) {
54                 TeleinfoReceiveThreadListener listener = this.listener;
55                 if (listener != null) {
56                     try {
57                         Frame nextFrame = teleinfoStream.readNextFrame();
58                         if (nextFrame != null)
59                             listener.onFrameReceived(nextFrame);
60                     } catch (InvalidFrameException e) {
61                         logger.warn("Got invalid frame. Detail: \"{}\"", e.getLocalizedMessage());
62                         listener.onInvalidFrameReceived(this, e);
63                     } catch (IOException e) {
64                         logger.warn("Got I/O exception. Detail: \"{}\"", e.getLocalizedMessage(), e);
65                         listener.onSerialPortInputStreamIOException(this, e);
66                         break;
67                     } catch (IllegalStateException e) {
68                         logger.warn("Got illegal state exception", e);
69                     }
70                 }
71             }
72         } catch (IOException e) {
73             logger.warn("An error occurred during serial port input stream opening", e);
74         }
75
76         serialPort.removeEventListener();
77     }
78
79     public @Nullable TeleinfoReceiveThreadListener getListener() {
80         return listener;
81     }
82
83     public void setListener(@Nullable TeleinfoReceiveThreadListener listener) {
84         this.listener = listener;
85     }
86 }