File indexing completed on 2024-05-12 03:47:43
0001 /* 0002 File : LiveDataSource.h 0003 Project : LabPlot 0004 Description : File data source 0005 -------------------------------------------------------------------- 0006 SPDX-FileCopyrightText: 2017 Fabian Kristof <fkristofszabolcs@gmail.com> 0007 SPDX-FileCopyrightText: 2017-2018 Alexander Semke <alexander.semke@web.de> 0008 SPDX-FileCopyrightText: 2018 Stefan Gerlach <stefan.gerlach@uni.kn> 0009 SPDX-License-Identifier: GPL-2.0-or-later 0010 */ 0011 0012 #ifndef LIVEDATASOURCE_H 0013 #define LIVEDATASOURCE_H 0014 0015 #include "backend/spreadsheet/Spreadsheet.h" 0016 0017 #include <QLocalSocket> 0018 #include <QMap> 0019 #include <QTimer> 0020 #include <QVector> 0021 #ifdef HAVE_QTSERIALPORT 0022 #include <QSerialPort> 0023 #endif 0024 0025 class QString; 0026 class AbstractFileFilter; 0027 class QFileSystemWatcher; 0028 class QAction; 0029 class QTcpSocket; 0030 class QUdpSocket; 0031 0032 class LiveDataSource : public Spreadsheet { 0033 Q_OBJECT 0034 Q_ENUMS(SourceType) 0035 Q_ENUMS(UpdateType) 0036 Q_ENUMS(ReadingType) 0037 0038 public: 0039 enum class SourceType { 0040 FileOrPipe = 0, // regular file or pipe 0041 NetworkTCPSocket, // TCP socket 0042 NetworkUDPSocket, // UDP socket 0043 LocalSocket, // local socket 0044 SerialPort, // serial port 0045 MQTT 0046 }; 0047 0048 enum class UpdateType { 0049 TimeInterval = 0, // update periodically using given interval 0050 NewData // update when new data is available 0051 }; 0052 0053 enum class ReadingType { 0054 ContinuousFixed = 0, // read continuously sampleSize number of samples (lines) 0055 FromEnd, // read sampleSize number of samples (lines) from end 0056 TillEnd, // read until the end 0057 WholeFile // reread whole file 0058 }; 0059 0060 explicit LiveDataSource(const QString& name, bool loading = false); 0061 ~LiveDataSource() override; 0062 0063 static QStringList supportedBaudRates(); 0064 static QStringList availablePorts(); 0065 0066 void setFileType(const AbstractFileFilter::FileType); 0067 AbstractFileFilter::FileType fileType() const; 0068 0069 UpdateType updateType() const; 0070 void setUpdateType(UpdateType); 0071 0072 SourceType sourceType() const; 0073 void setSourceType(SourceType); 0074 0075 ReadingType readingType() const; 0076 void setReadingType(ReadingType); 0077 0078 int sampleSize() const; 0079 void setSampleSize(int); 0080 0081 void setBytesRead(qint64 bytes); 0082 int bytesRead() const; 0083 0084 int port() const; 0085 void setPort(quint16); 0086 0087 bool isPaused() const; 0088 0089 void setSerialPort(const QString&); 0090 QString serialPortName() const; 0091 0092 QString host() const; 0093 void setHost(const QString&); 0094 0095 int baudRate() const; 0096 void setBaudRate(int); 0097 0098 void setUpdateInterval(int); 0099 int updateInterval() const; 0100 0101 void setKeepNValues(int); 0102 int keepNValues() const; 0103 0104 void setKeepLastValues(bool); 0105 bool keepLastValues() const; 0106 0107 void setFileLinked(bool); 0108 bool isFileLinked() const; 0109 0110 void setUseRelativePath(bool); 0111 bool useRelativePath() const; 0112 0113 void setFileName(const QString&); 0114 QString fileName() const; 0115 0116 void setLocalSocketName(const QString&); 0117 QString localSocketName() const; 0118 0119 void updateNow(); 0120 void pauseReading(); 0121 void continueReading(); 0122 0123 void setFilter(AbstractFileFilter*); 0124 AbstractFileFilter* filter() const; 0125 0126 QIcon icon() const override; 0127 QWidget* view() const override; 0128 0129 void save(QXmlStreamWriter*) const override; 0130 bool load(XmlStreamReader*, bool preview) override; 0131 void finalizeLoad(); 0132 0133 private: 0134 QString m_fileName; 0135 QString m_dirName; 0136 QString m_serialPortName; 0137 QString m_localSocketName; 0138 QString m_host; 0139 0140 AbstractFileFilter::FileType m_fileType{AbstractFileFilter::FileType::Ascii}; 0141 UpdateType m_updateType; 0142 SourceType m_sourceType; 0143 ReadingType m_readingType; 0144 0145 bool m_fileWatched{false}; 0146 bool m_fileLinked{false}; 0147 bool m_relativePath{false}; 0148 bool m_paused{false}; 0149 bool m_prepared{false}; 0150 bool m_reading{false}; 0151 bool m_pending{false}; 0152 0153 int m_sampleSize{1}; 0154 int m_keepNValues{0}; // number of values to keep (0 - all) 0155 int m_updateInterval{1000}; 0156 quint16 m_port{1027}; 0157 int m_baudRate{9600}; 0158 0159 qint64 m_bytesRead{0}; 0160 0161 AbstractFileFilter* m_filter{nullptr}; 0162 0163 QTimer* m_updateTimer; 0164 QTimer* m_watchTimer; 0165 QFileSystemWatcher* m_fileSystemWatcher{nullptr}; 0166 0167 QLocalSocket* m_localSocket{nullptr}; 0168 QTcpSocket* m_tcpSocket{nullptr}; 0169 QUdpSocket* m_udpSocket{nullptr}; 0170 #ifdef HAVE_QTSERIALPORT 0171 QSerialPort* m_serialPort{nullptr}; 0172 #endif 0173 QIODevice* m_device{nullptr}; 0174 QAction* m_plotDataAction{nullptr}; 0175 0176 public Q_SLOTS: 0177 void read(); 0178 void readOnUpdate(); 0179 0180 private Q_SLOTS: 0181 void plotData(); 0182 void readyRead(); 0183 0184 void localSocketError(QLocalSocket::LocalSocketError); 0185 void tcpSocketError(QAbstractSocket::SocketError); 0186 #ifdef HAVE_QTSERIALPORT 0187 void serialPortError(QSerialPort::SerialPortError); 0188 #endif 0189 }; 0190 0191 #endif