154 lines
3.9 KiB
C
154 lines
3.9 KiB
C
|
#ifndef BASECONN_H
|
||
|
#define BASECONN_H
|
||
|
|
||
|
#include <QObject>
|
||
|
#include <QTcpSocket>
|
||
|
#include <QDataStream>
|
||
|
#include <QDateTime>
|
||
|
#include <QTimer>
|
||
|
#include <QEventLoop>
|
||
|
|
||
|
#include <QJsonDocument>
|
||
|
#include <QJsonArray>
|
||
|
#include <QJsonObject>
|
||
|
#include <QJsonValue>
|
||
|
|
||
|
class BaseConn : public QObject
|
||
|
{
|
||
|
Q_OBJECT
|
||
|
Q_PROPERTY(QString ipAddress READ getIP WRITE setIP)
|
||
|
Q_PROPERTY(QString state READ getState NOTIFY stateChanged)
|
||
|
Q_PROPERTY(QVariant timers READ getTimerTextList NOTIFY timerTextChanged)
|
||
|
Q_PROPERTY(int raceState READ getRaceState NOTIFY raceStateChanged)
|
||
|
|
||
|
Q_PROPERTY(double nextStartActionDelayProgress READ getNextStartActionDelayProgress NOTIFY nextStartActionDelayProgressChanged)
|
||
|
Q_PROPERTY(int nextStartAction READ getNextStartAction NOTIFY nextStartActionChanged)
|
||
|
|
||
|
public:
|
||
|
explicit BaseConn(QObject *parent = nullptr);
|
||
|
|
||
|
// values for the socket connection
|
||
|
QString ip;
|
||
|
ushort port = 3563;
|
||
|
int errors;
|
||
|
int errors_until_disconnect = 4;
|
||
|
|
||
|
// the current state
|
||
|
QString state;
|
||
|
// can be:
|
||
|
// - 'disconnected'
|
||
|
// - 'connecting'
|
||
|
// - 'connected'
|
||
|
|
||
|
QString latestReadReply;
|
||
|
|
||
|
//---general status values---//
|
||
|
|
||
|
// stuff for storing the timers
|
||
|
enum timerState { IDLE, STARTING, WAITING, RUNNING, WON, LOST, FAILED, CANCELLED, DISABLED };
|
||
|
|
||
|
QVariant remoteTimers;
|
||
|
QVariant timerTextList;
|
||
|
int remoteRaceState;
|
||
|
|
||
|
// for next start action
|
||
|
enum NextStartAction { AtYourMarks, Ready, Start, None };
|
||
|
NextStartAction nextStartAction;
|
||
|
double nextStartActionDelayProgress;
|
||
|
// only used in remote mode:
|
||
|
double nextStartActionDelayStartedAt;
|
||
|
double nextStartActionTotalDelay;
|
||
|
|
||
|
private:
|
||
|
QDateTime *date;
|
||
|
//to get the current time
|
||
|
|
||
|
QTcpSocket *socket;
|
||
|
//socket for communication with the extention
|
||
|
|
||
|
QTimer *autoConnectRetryTimer; // timer to frequently trigger a connection attempt to the base station
|
||
|
QTimer *timeoutTimer; // timer to trigger connection timeout
|
||
|
QTimer *timerTextRefreshTimer; // timer to refresh the text of the timers on the frontend
|
||
|
|
||
|
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 gotUnexpectedReply(QString reply);
|
||
|
|
||
|
void gotUpdate(QVariantMap data);
|
||
|
|
||
|
void gotError(QString error);
|
||
|
|
||
|
// for qml
|
||
|
void timerTextChanged();
|
||
|
void raceStateChanged();
|
||
|
|
||
|
void nextStartActionChanged();
|
||
|
void nextStartActionDelayProgressChanged();
|
||
|
|
||
|
public slots:
|
||
|
|
||
|
Q_INVOKABLE void connectToHost();
|
||
|
//function to connect to the base station
|
||
|
|
||
|
void connectionTimeout();
|
||
|
|
||
|
Q_INVOKABLE bool init();
|
||
|
Q_INVOKABLE void deInit();
|
||
|
|
||
|
Q_INVOKABLE void closeConnection();
|
||
|
|
||
|
void gotError(QAbstractSocket::SocketError err);
|
||
|
|
||
|
// --- socket communication handling ---
|
||
|
|
||
|
Q_INVOKABLE QVariantMap sendCommand(int header, QJsonValue data = "");
|
||
|
|
||
|
// helper functions
|
||
|
void doConnectionAttempt();
|
||
|
void setState(QString newState);
|
||
|
int writeRemoteSetting(QString key, QString value);
|
||
|
QString readRemoteSetting(QString key);
|
||
|
|
||
|
// for timer sync
|
||
|
void handleUpdate(QVariantMap data);
|
||
|
void refreshRemoteTimers(QVariantList timers);
|
||
|
void refreshTimerTextList();
|
||
|
|
||
|
// for qml
|
||
|
QString getIP() const;
|
||
|
void setIP(const QString &ipAdress);
|
||
|
|
||
|
QString getState() const;
|
||
|
|
||
|
QVariant getTimerTextList();
|
||
|
int getRaceState();
|
||
|
|
||
|
Q_INVOKABLE double getNextStartActionDelayProgress();
|
||
|
Q_INVOKABLE int getNextStartAction();
|
||
|
|
||
|
private slots:
|
||
|
void readyRead();
|
||
|
|
||
|
void processSocketMessage(QString message);
|
||
|
|
||
|
void socketReplyRecieved(QString reply);
|
||
|
|
||
|
void socketStateChanged(QAbstractSocket::SocketState socketState);
|
||
|
};
|
||
|
|
||
|
#endif // BASECONN_H
|