This repository has been archived on 2024-06-03. You can view files and clone it, but cannot push or open issues or pull requests.
app/headers/baseconn.h

154 lines
3 KiB
C
Raw Normal View History

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