2018-09-23 17:54:20 +02:00
|
|
|
#ifndef BASECONN_H
|
|
|
|
#define BASECONN_H
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
#include <QTcpSocket>
|
|
|
|
#include <QDataStream>
|
|
|
|
#include <QDateTime>
|
|
|
|
#include <QTimer>
|
|
|
|
#include <QEventLoop>
|
2018-10-04 18:35:29 +02:00
|
|
|
#include <QSemaphore>
|
2018-10-14 18:39:39 +02:00
|
|
|
#include <QThread>
|
|
|
|
#include <QFuture>
|
|
|
|
#include <QtConcurrent/QtConcurrent>
|
|
|
|
#include <string.h>
|
2019-11-02 15:22:50 +01:00
|
|
|
#include <QByteArray>
|
2018-10-04 18:35:29 +02:00
|
|
|
|
|
|
|
#include "headers/appsettings.h"
|
|
|
|
#include "headers/speedtimer.h"
|
2018-09-23 17:54:20 +02:00
|
|
|
|
|
|
|
class BaseConn : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit BaseConn(QObject *parent = nullptr);
|
|
|
|
|
|
|
|
// values for the socket connection
|
|
|
|
int connection_progress;
|
|
|
|
QString ip;
|
2019-03-24 21:16:16 +01:00
|
|
|
ushort port = 3563;
|
2018-09-23 17:54:20 +02:00
|
|
|
int errors;
|
|
|
|
int errors_until_disconnect = 4;
|
|
|
|
|
2019-10-18 16:10:41 +02:00
|
|
|
QVariant connections;
|
|
|
|
|
|
|
|
QString latestReadReply;
|
|
|
|
|
|
|
|
//---general status values---//
|
|
|
|
|
|
|
|
// some meta data of the base
|
2019-11-02 15:22:50 +01:00
|
|
|
QString firmwareVersion;
|
|
|
|
bool firmwareUpToDate;
|
2019-10-18 16:10:41 +02:00
|
|
|
double timeOffset;
|
|
|
|
|
2019-11-02 15:22:50 +01:00
|
|
|
|
2018-09-23 17:54:20 +02:00
|
|
|
// the current state
|
|
|
|
QString state;
|
|
|
|
// can be:
|
|
|
|
// - 'disconnected'
|
|
|
|
// - 'connecting'
|
|
|
|
// - 'connected'
|
|
|
|
|
2018-10-04 18:35:29 +02:00
|
|
|
|
2018-09-23 17:54:20 +02:00
|
|
|
private:
|
|
|
|
QDateTime *date;
|
|
|
|
//to get the current time
|
|
|
|
|
|
|
|
QTcpSocket *socket;
|
|
|
|
//socket for communication with the extention
|
|
|
|
|
2019-08-20 22:55:37 +02:00
|
|
|
QTimer *timeoutTimer;
|
|
|
|
|
2019-05-19 14:06:05 +02:00
|
|
|
QString readBuffer;
|
|
|
|
|
2018-10-14 18:39:39 +02:00
|
|
|
int nextConnectionId;
|
|
|
|
|
|
|
|
struct waitingRequest {
|
|
|
|
int id;
|
|
|
|
QEventLoop * loop;
|
2019-03-02 20:20:13 +01:00
|
|
|
QJsonObject reply;
|
2018-10-14 18:39:39 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
QList<waitingRequest> waitingRequests;
|
|
|
|
|
2018-09-23 17:54:20 +02:00
|
|
|
signals:
|
|
|
|
void stateChanged();
|
|
|
|
//is emitted, when the connection state changes
|
|
|
|
|
|
|
|
void progressChanged();
|
|
|
|
//is emmited during the connection process when the progress changes
|
|
|
|
|
2018-10-14 18:39:39 +02:00
|
|
|
void gotUnexpectedReply(QString reply);
|
2018-09-23 17:54:20 +02:00
|
|
|
|
2019-10-06 19:02:47 +02:00
|
|
|
void gotUpdate(QVariant data);
|
|
|
|
|
2018-10-04 18:35:29 +02:00
|
|
|
void connectionsChanged();
|
|
|
|
|
2018-10-14 18:39:39 +02:00
|
|
|
void connectionSlotReleased();
|
|
|
|
|
|
|
|
void nextRemoteActionChanged();
|
|
|
|
|
|
|
|
void nextRemoteActionDelayProgChanged();
|
|
|
|
|
2019-03-02 20:20:13 +01:00
|
|
|
void gotError(QString error);
|
|
|
|
|
2019-11-02 15:22:50 +01:00
|
|
|
void propertiesChanged();
|
|
|
|
|
2018-09-23 17:54:20 +02:00
|
|
|
public slots:
|
|
|
|
|
2019-11-02 15:22:50 +01:00
|
|
|
void connectToHost();
|
2018-09-23 17:54:20 +02:00
|
|
|
//function to connect to the base station
|
|
|
|
|
2019-08-20 22:55:37 +02:00
|
|
|
void connectionTimeout();
|
|
|
|
|
2019-11-02 15:22:50 +01:00
|
|
|
bool init();
|
|
|
|
void deInit();
|
2019-04-27 22:50:22 +02:00
|
|
|
|
2019-11-02 15:22:50 +01:00
|
|
|
void closeConnection();
|
2019-03-02 20:20:13 +01:00
|
|
|
|
|
|
|
void gotError(QAbstractSocket::SocketError err);
|
|
|
|
|
2019-05-21 18:40:45 +02:00
|
|
|
// --- socket communication handling ---
|
|
|
|
|
2019-11-02 15:22:50 +01:00
|
|
|
QVariantMap sendCommand(int header, QJsonValue data = "", bool useTerminationKeys = true, int timeout = 3000);
|
|
|
|
|
|
|
|
// --- updater functions ---
|
|
|
|
|
|
|
|
bool updateTime();
|
|
|
|
bool updateFirmware();
|
|
|
|
bool isFirmwareUpToDate();
|
2018-09-23 17:54:20 +02:00
|
|
|
|
2019-05-21 18:40:45 +02:00
|
|
|
// --- helper functions ---
|
|
|
|
|
2019-11-02 15:22:50 +01:00
|
|
|
int writeRemoteSetting(QString key, QString value);
|
2019-03-03 17:31:24 +01:00
|
|
|
|
2019-11-02 15:22:50 +01:00
|
|
|
bool refreshConnections();
|
2018-10-04 18:35:29 +02:00
|
|
|
|
2019-10-07 19:52:39 +02:00
|
|
|
void setConnections(QVariantList connections);
|
|
|
|
|
2019-11-02 15:22:50 +01:00
|
|
|
|
2018-09-23 17:54:20 +02:00
|
|
|
// functions for the qml adapter
|
|
|
|
QString getIP() const;
|
|
|
|
void setIP(const QString &ipAdress);
|
|
|
|
|
|
|
|
QString getState() const;
|
|
|
|
void setState(QString newState);
|
|
|
|
|
|
|
|
int getProgress() const;
|
|
|
|
|
2019-03-07 17:18:24 +01:00
|
|
|
QVariant getConnections();
|
2018-10-14 18:39:39 +02:00
|
|
|
|
2018-09-23 17:54:20 +02:00
|
|
|
private slots:
|
|
|
|
void readyRead();
|
2019-03-29 23:42:56 +01:00
|
|
|
|
2019-05-21 18:40:45 +02:00
|
|
|
void processSocketMessage(QString message);
|
|
|
|
|
|
|
|
void socketReplyRecieved(QString reply);
|
|
|
|
|
2019-03-29 23:42:56 +01:00
|
|
|
void socketStateChanged(QAbstractSocket::SocketState socketState);
|
2018-09-23 17:54:20 +02:00
|
|
|
};
|
2019-03-07 17:18:24 +01:00
|
|
|
extern BaseConn * pGlobalBaseConn;
|
2018-09-23 17:54:20 +02:00
|
|
|
|
|
|
|
#endif // BASECONN_H
|