]> git.basschouten.com Git - openhab-addons.git/blob
98b26ee9c85d28901788c3f16ad352d99d4543d6
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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     private final TeleinfoTicMode ticMode;
40     private final boolean verifyChecksum;
41
42     public TeleinfoReceiveThread(SerialPort serialPort, final TeleinfoSerialControllerHandler listener,
43             boolean autoRepairInvalidADPSgroupLine, TeleinfoTicMode ticMode, boolean verifyChecksum) {
44         super("OH-binding-TeleinfoReceiveThread-" + listener.getThing().getUID().getId());
45         setDaemon(true);
46         this.serialPort = serialPort;
47         this.listener = listener;
48         this.autoRepairInvalidADPSgroupLine = autoRepairInvalidADPSgroupLine;
49         this.ticMode = ticMode;
50         this.verifyChecksum = verifyChecksum;
51     }
52
53     @Override
54     public void run() {
55         try (TeleinfoInputStream teleinfoStream = new TeleinfoInputStream(serialPort.getInputStream(),
56                 autoRepairInvalidADPSgroupLine, ticMode, verifyChecksum)) {
57             while (!interrupted()) {
58                 TeleinfoReceiveThreadListener listener = this.listener;
59                 if (listener != null) {
60                     try {
61                         Frame nextFrame = teleinfoStream.readNextFrame();
62                         if (nextFrame != null) {
63                             listener.onFrameReceived(nextFrame);
64                         }
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);
71                         break;
72                     } catch (IllegalStateException e) {
73                         logger.warn("Got illegal state exception", e);
74                     }
75                 }
76             }
77         } catch (IOException e) {
78             logger.warn("An error occurred during serial port input stream opening", e);
79         }
80
81         serialPort.removeEventListener();
82     }
83
84     public @Nullable TeleinfoReceiveThreadListener getListener() {
85         return listener;
86     }
87
88     public void setListener(@Nullable TeleinfoReceiveThreadListener listener) {
89         this.listener = listener;
90     }
91 }