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.
shared-libraries/ScStwLibraries/scstwclient.h

174 lines
3.3 KiB
C
Raw Normal View History

2020-04-05 14:11:52 +02:00
#ifndef SCSTWCLIENT_H
#define SCSTWCLIENT_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>
/**
* This class is used to connect and talk to the ScStw basestation.
*
* @brief The ScStwClient class
* @author Dorian Zedler
*/
class ScStwClient : public QObject
{
Q_OBJECT
public:
/**
* The constructor
*
* @brief ScStwClient
*/
explicit ScStwClient();
private:
// values for the socket connection
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'
QDateTime *date;
//to get the current time
QTcpSocket *socket;
//socket for communication with the extention
QTimer *timeoutTimer;
QString readBuffer;
int nextConnectionId;
struct waitingRequest {
int id;
QEventLoop * loop;
QJsonObject reply;
};
QList<waitingRequest> waitingRequests;
signals:
/**
* Is emitted, when the connection state changes
*
* @brief stateChanged
*/
void stateChanged();
/**
* Is emitted, whenever a reply is recieved which does not match any requests
*
* @brief gotUnexpectedReply
* @param reply contains the reply
*/
void gotUnexpectedReply(QString reply);
/**
* Is emitted, when an update signal from the basestation is recieved
*
* @brief gotUpdate
* @param data
*/
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
void connectionTimeout();
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();
// functions for the qml adapter
QString getIP();
void setIP(QString ipAdress);
QString getState();
void setState(QString newState);
int getProgress();
QVariant getConnections();
private slots:
bool init();
void deInit();
void readyRead();
void processSocketMessage(QString message);
void socketReplyRecieved(QString reply);
void socketStateChanged(QAbstractSocket::SocketState socketState);
void setConnections(QVariantList connections);
};
extern ScStwClient * pGlobalScStwClient;
#endif // SCSTWCLIENT_H