File indexing completed on 2024-05-12 15:26:56

0001 /***************************************************************************
0002     File                 : LiveDataSource.h
0003     Project              : LabPlot
0004     Description          : File data source
0005     --------------------------------------------------------------------
0006     Copyright            : (C) 2017 Fabian Kristof (fkristofszabolcs@gmail.com)
0007     Copyright            : (C) 2017-2018 Alexander Semke (alexander.semke@web.de)
0008     Copyright            : (C) 2018 Stefan Gerlach (stefan.gerlach@uni.kn)
0009 
0010  ***************************************************************************/
0011 
0012 /***************************************************************************
0013  *                                                                         *
0014  *  This program is free software; you can redistribute it and/or modify   *
0015  *  it under the terms of the GNU General Public License as published by   *
0016  *  the Free Software Foundation; either version 2 of the License, or      *
0017  *  (at your option) any later version.                                    *
0018  *                                                                         *
0019  *  This program is distributed in the hope that it will be useful,        *
0020  *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
0021  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
0022  *  GNU General Public License for more details.                           *
0023  *                                                                         *
0024  *   You should have received a copy of the GNU General Public License     *
0025  *   along with this program; if not, write to the Free Software           *
0026  *   Foundation, Inc., 51 Franklin Street, Fifth Floor,                    *
0027  *   Boston, MA  02110-1301  USA                                           *
0028  *                                                                         *
0029  ***************************************************************************/
0030 #ifndef LIVEDATASOURCE_H
0031 #define LIVEDATASOURCE_H
0032 
0033 #include "backend/spreadsheet/Spreadsheet.h"
0034 #include "backend/matrix/Matrix.h"
0035 
0036 #include <QLocalSocket>
0037 #include <QTimer>
0038 #include <QVector>
0039 #include <QMap>
0040 #ifdef HAVE_QTSERIALPORT
0041 #include <QSerialPort>
0042 #endif
0043 
0044 class QString;
0045 class AbstractFileFilter;
0046 class QFileSystemWatcher;
0047 class QAction;
0048 class QTcpSocket;
0049 class QUdpSocket;
0050 
0051 class LiveDataSource : public Spreadsheet {
0052     Q_OBJECT
0053     Q_ENUMS(SourceType)
0054     Q_ENUMS(UpdateType)
0055     Q_ENUMS(ReadingType)
0056 
0057 public:
0058     enum class SourceType {
0059         FileOrPipe = 0,     // regular file or pipe
0060         NetworkTcpSocket,   // TCP socket
0061         NetworkUdpSocket,   // UDP socket
0062         LocalSocket,        // local socket
0063         SerialPort,     // serial port
0064         MQTT
0065     };
0066 
0067     enum class UpdateType {
0068         TimeInterval = 0,   // update periodically using given interval
0069         NewData         // update when new data is available
0070     };
0071 
0072     enum class ReadingType {
0073         ContinuousFixed = 0,    // read continuously sampleSize number of samples (lines)
0074         FromEnd,        // read sampleSize number of samples (lines) from end
0075         TillEnd,        // read until the end
0076         WholeFile       // reread whole file
0077     };
0078 
0079     explicit LiveDataSource(const QString& name, bool loading = false);
0080     ~LiveDataSource() override;
0081 
0082     static QStringList supportedBaudRates();
0083     static QStringList availablePorts();
0084 
0085     void setFileType(const AbstractFileFilter::FileType);
0086     AbstractFileFilter::FileType fileType() const;
0087 
0088     UpdateType updateType() const;
0089     void setUpdateType(UpdateType);
0090 
0091     SourceType sourceType() const;
0092     void setSourceType(SourceType);
0093 
0094     ReadingType readingType() const;
0095     void setReadingType(ReadingType);
0096 
0097     int sampleSize() const;
0098     void setSampleSize(int);
0099 
0100     void setBytesRead(qint64 bytes);
0101     int bytesRead() const;
0102 
0103     int port() const;
0104     void setPort(quint16);
0105 
0106     bool isPaused() const;
0107 
0108     void setSerialPort(const QString& name);
0109     QString serialPortName() const;
0110 
0111     QString host() const;
0112     void setHost(const QString&);
0113 
0114     int baudRate() const;
0115     void setBaudRate(int);
0116 
0117     void setUpdateInterval(int);
0118     int updateInterval() const;
0119 
0120     void setKeepNValues(int);
0121     int keepNValues() const;
0122 
0123     void setKeepLastValues(bool);
0124     bool keepLastValues() const;
0125 
0126     void setFileLinked(bool);
0127     bool isFileLinked() const;
0128 
0129     void setUseRelativePath(bool);
0130     bool useRelativePath() const;
0131 
0132     void setFileName(const QString&);
0133     QString fileName() const;
0134 
0135     void setLocalSocketName(const QString&);
0136     QString localSocketName() const;
0137 
0138     void updateNow();
0139     void pauseReading();
0140     void continueReading();
0141 
0142     void setFilter(AbstractFileFilter*);
0143     AbstractFileFilter* filter() const;
0144 
0145     QIcon icon() const override;
0146     QMenu* createContextMenu() override;
0147     QWidget* view() const override;
0148 
0149     void save(QXmlStreamWriter*) const override;
0150     bool load(XmlStreamReader*, bool preview) override;
0151     void finalizeLoad();
0152 
0153 private:
0154     void initActions();
0155 
0156     QString m_fileName;
0157     QString m_dirName;
0158     QString m_serialPortName;
0159     QString m_localSocketName;
0160     QString m_host;
0161 
0162     AbstractFileFilter::FileType m_fileType{AbstractFileFilter::FileType::Ascii};
0163     UpdateType m_updateType;
0164     SourceType m_sourceType;
0165     ReadingType m_readingType;
0166 
0167     bool m_fileWatched{false};
0168     bool m_fileLinked{false};
0169     bool m_relativePath{false};
0170     bool m_paused{false};
0171     bool m_prepared{false};
0172     bool m_reading{false};
0173     bool m_pending{false};
0174 
0175     int m_sampleSize{1};
0176     int m_keepNValues{0};   // number of values to keep (0 - all)
0177     int m_updateInterval{1000};
0178     quint16 m_port{1027};
0179     int m_baudRate{9600};
0180 
0181     qint64 m_bytesRead{0};
0182 
0183     AbstractFileFilter* m_filter{nullptr};
0184 
0185     QTimer* m_updateTimer;
0186     QTimer* m_watchTimer;
0187     QFileSystemWatcher* m_fileSystemWatcher{nullptr};
0188 
0189     QLocalSocket* m_localSocket{nullptr};
0190     QTcpSocket* m_tcpSocket{nullptr};
0191     QUdpSocket* m_udpSocket{nullptr};
0192 #ifdef HAVE_QTSERIALPORT
0193     QSerialPort* m_serialPort{nullptr};
0194 #endif
0195     QIODevice* m_device{nullptr};
0196     QAction* m_plotDataAction{nullptr};
0197 
0198 public slots:
0199     void read();
0200     void readOnUpdate();
0201 
0202 private slots:
0203     void plotData();
0204     void readyRead();
0205 
0206     void localSocketError(QLocalSocket::LocalSocketError);
0207     void tcpSocketError(QAbstractSocket::SocketError);
0208 #ifdef HAVE_QTSERIALPORT
0209     void serialPortError(QSerialPort::SerialPortError);
0210 #endif
0211 };
0212 
0213 #endif