2020-06-07 14:43:47 +02:00
|
|
|
/****************************************************************************
|
|
|
|
** ScStw Libraries
|
|
|
|
** Copyright (C) 2020 Itsblue development
|
|
|
|
**
|
|
|
|
** This program is free software: you can redistribute it and/or modify
|
|
|
|
** it under the terms of the GNU General Public License as published by
|
|
|
|
** the Free Software Foundation, either version 3 of the License, or
|
|
|
|
** (at your option) any later version.
|
|
|
|
**
|
|
|
|
** This program is distributed in the hope that it will be useful,
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
** GNU General Public License for more details.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
** along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
****************************************************************************/
|
|
|
|
|
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>
|
|
|
|
|
2020-04-11 23:41:34 +02:00
|
|
|
#include "ScStw.hpp"
|
2020-07-06 15:03:43 +02:00
|
|
|
#include "scstwsettings.h"
|
2020-04-06 17:51:20 +02:00
|
|
|
|
2020-04-15 19:21:32 +02:00
|
|
|
/*!
|
2020-04-05 14:11:52 +02:00
|
|
|
* This class is used to connect and talk to the ScStw basestation.
|
|
|
|
*
|
2020-04-15 19:21:32 +02:00
|
|
|
* \code{.cpp}
|
2020-04-08 18:56:37 +02:00
|
|
|
* ScStwClient * client = new ScStwClient();
|
|
|
|
* client->setIp("192.168.4.1");
|
|
|
|
* client->connectToHost();
|
2020-04-15 19:21:32 +02:00
|
|
|
* \endcode
|
2020-04-08 18:56:37 +02:00
|
|
|
*
|
2020-04-15 19:21:32 +02:00
|
|
|
* \brief The ScStwClient class
|
|
|
|
* \author Dorian Zedler
|
2020-04-05 14:11:52 +02:00
|
|
|
*/
|
|
|
|
class ScStwClient : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
2020-04-18 14:25:48 +02:00
|
|
|
Q_PROPERTY(State state READ getState NOTIFY stateChanged)
|
|
|
|
Q_PROPERTY(QVariantList extensions READ getExtensions NOTIFY extensionsChanged)
|
2020-05-22 19:36:04 +02:00
|
|
|
Q_PROPERTY(QString ipAddress READ getIP WRITE setIP)
|
2020-04-05 14:11:52 +02:00
|
|
|
|
|
|
|
public:
|
2020-04-15 19:21:32 +02:00
|
|
|
/*!
|
2020-04-05 14:11:52 +02:00
|
|
|
* The constructor
|
|
|
|
*
|
2020-04-15 19:21:32 +02:00
|
|
|
* \brief ScStwClient
|
2020-04-05 14:11:52 +02:00
|
|
|
*/
|
2020-05-16 12:38:56 +02:00
|
|
|
explicit ScStwClient(QObject *parent = nullptr);
|
2020-04-05 14:11:52 +02:00
|
|
|
|
2020-04-06 17:51:20 +02:00
|
|
|
enum State {DISCONNECTED, CONNECTING, INITIALISING, CONNECTED};
|
2020-04-18 14:25:48 +02:00
|
|
|
Q_ENUM(State);
|
2020-04-06 17:51:20 +02:00
|
|
|
|
2020-05-10 18:29:27 +02:00
|
|
|
const QString API_VERSION = "1.0.0";
|
|
|
|
|
2020-04-05 14:11:52 +02:00
|
|
|
private:
|
|
|
|
// values for the socket connection
|
|
|
|
QString ip;
|
|
|
|
ushort port = 3563;
|
|
|
|
int errors;
|
2020-04-06 17:51:20 +02:00
|
|
|
const static int ERRORS_UNTIL_DISCONNECT = 4;
|
2020-04-05 14:11:52 +02:00
|
|
|
|
2020-04-18 14:25:48 +02:00
|
|
|
QVariantList extensions;
|
2020-04-05 14:11:52 +02:00
|
|
|
|
|
|
|
//---general status values---//
|
|
|
|
|
2020-04-06 17:51:20 +02:00
|
|
|
// some meta data of the base station
|
2020-04-05 14:11:52 +02:00
|
|
|
QString firmwareVersion;
|
2020-05-10 18:29:27 +02:00
|
|
|
QString apiVersion;
|
2020-04-05 14:11:52 +02:00
|
|
|
double timeOffset;
|
|
|
|
|
|
|
|
// the current state
|
2020-04-06 17:51:20 +02:00
|
|
|
ScStwClient::State state;
|
2020-04-05 14:11:52 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
public slots:
|
|
|
|
|
2020-04-15 19:21:32 +02:00
|
|
|
/*!
|
|
|
|
* \brief Function to connect to the base station
|
2020-04-06 17:51:20 +02:00
|
|
|
*/
|
2020-04-05 14:11:52 +02:00
|
|
|
void connectToHost();
|
|
|
|
|
2020-04-15 19:21:32 +02:00
|
|
|
/*!
|
|
|
|
* \brief Function to disconnect from the basestation
|
2020-04-06 17:51:20 +02:00
|
|
|
*/
|
2020-04-05 14:11:52 +02:00
|
|
|
void closeConnection();
|
|
|
|
|
|
|
|
|
2020-04-15 19:21:32 +02:00
|
|
|
/*! socket communication handling */
|
2020-04-05 14:11:52 +02:00
|
|
|
|
2020-04-15 19:21:32 +02:00
|
|
|
/*!
|
|
|
|
* \brief Funtion to send a command to the server (for internal use)
|
|
|
|
* \param header the command to send
|
|
|
|
* \param data the data to send
|
|
|
|
* \param timeout the timeout
|
|
|
|
* \param useTerminationKeys wether to use the termination keys defined in
|
2020-04-06 17:51:20 +02:00
|
|
|
*
|
2020-04-15 19:21:32 +02:00
|
|
|
* \return a variant map containing the Keys "status" and "data"
|
2020-04-06 17:51:20 +02:00
|
|
|
*/
|
|
|
|
QVariantMap sendCommand(int header, QJsonValue data = "", int timeout = 3000);
|
2020-04-05 14:11:52 +02:00
|
|
|
|
2020-06-14 17:54:19 +02:00
|
|
|
/*! helper functions */
|
2020-04-05 14:11:52 +02:00
|
|
|
|
2020-04-15 19:21:32 +02:00
|
|
|
/*!
|
|
|
|
* \brief Function to write a setting on the base station
|
|
|
|
* \param key the key to write to
|
|
|
|
* \param value the value to write to
|
|
|
|
* \return the status code returned by the command
|
2020-04-06 17:51:20 +02:00
|
|
|
*/
|
2020-07-10 15:20:13 +02:00
|
|
|
ScStw::StatusCode writeRemoteSetting(ScStwSettings::BaseStationSetting key, QVariant value);
|
2020-04-05 14:11:52 +02:00
|
|
|
|
2020-04-15 19:21:32 +02:00
|
|
|
/*!
|
|
|
|
* \brief Function to read a setting on the base station
|
|
|
|
* \param key the key to read from
|
|
|
|
* \return the value of the key or "false" if the key is not found or an error occured
|
2020-04-06 17:51:20 +02:00
|
|
|
*/
|
2020-07-10 15:20:13 +02:00
|
|
|
QVariant readRemoteSetting(ScStwSettings::BaseStationSetting key);
|
2020-04-06 17:51:20 +02:00
|
|
|
|
2020-04-15 19:21:32 +02:00
|
|
|
/*! Getter fuctions */
|
2020-04-05 14:11:52 +02:00
|
|
|
|
2020-04-15 19:21:32 +02:00
|
|
|
/*!
|
|
|
|
* \brief Function to get the ip the client will try to connect to.
|
|
|
|
* \see setIP()
|
|
|
|
* \return the ip
|
2020-04-06 17:51:20 +02:00
|
|
|
*/
|
2020-04-05 14:11:52 +02:00
|
|
|
QString getIP();
|
|
|
|
|
2020-04-15 19:21:32 +02:00
|
|
|
/*!
|
|
|
|
* \brief Function to get the current state of the client
|
|
|
|
* \return the current state
|
2020-04-06 17:51:20 +02:00
|
|
|
*/
|
|
|
|
ScStwClient::State getState();
|
2020-04-05 14:11:52 +02:00
|
|
|
|
2020-04-15 19:21:32 +02:00
|
|
|
/*!
|
2020-04-18 14:25:48 +02:00
|
|
|
* \brief Function to get the extensions and their state from the base station
|
2020-04-15 19:21:32 +02:00
|
|
|
* \return a list with all configured extensions and their state
|
2020-04-06 17:51:20 +02:00
|
|
|
*/
|
2020-04-18 14:25:48 +02:00
|
|
|
QVariantList getExtensions();
|
2020-04-05 14:11:52 +02:00
|
|
|
|
2020-04-15 19:21:32 +02:00
|
|
|
/*!
|
|
|
|
* \brief Function to get the time offset of the base station relative to the clients time
|
|
|
|
* \see updateTime()
|
|
|
|
* \return the time offset in milliseconds
|
2020-04-06 17:51:20 +02:00
|
|
|
*/
|
|
|
|
int getTimeOffset();
|
|
|
|
|
2020-04-15 19:21:32 +02:00
|
|
|
/*!
|
|
|
|
* \brief Function to get the current firmware version of the base station
|
|
|
|
* \see updateFirmware()
|
|
|
|
* \see isFirmwareUpToDate()
|
|
|
|
* \return Firmware version as string encoded as <major>.<minor>.<patch>
|
2020-04-06 17:51:20 +02:00
|
|
|
*/
|
|
|
|
QString getFirmwareVersion();
|
|
|
|
|
2020-05-16 12:38:56 +02:00
|
|
|
/*!
|
|
|
|
* \brief Function to get the current API version of the base station
|
|
|
|
* \return
|
|
|
|
*/
|
|
|
|
QString getApiVersion();
|
|
|
|
|
2020-04-15 19:21:32 +02:00
|
|
|
/*! setter functions */
|
2020-04-06 17:51:20 +02:00
|
|
|
|
2020-04-15 19:21:32 +02:00
|
|
|
/*!
|
|
|
|
* \brief Function to set the ip to connect to
|
|
|
|
* \see getIP()
|
|
|
|
* \param ipAdress
|
2020-04-06 17:51:20 +02:00
|
|
|
*/
|
|
|
|
void setIP(QString ipAdress);
|
2020-04-05 14:11:52 +02:00
|
|
|
|
|
|
|
private slots:
|
2020-04-15 19:21:32 +02:00
|
|
|
/*!
|
|
|
|
* \brief called when timeout timer times out
|
2020-04-06 17:51:20 +02:00
|
|
|
*/
|
|
|
|
void connectionTimeout();
|
2020-04-05 14:11:52 +02:00
|
|
|
|
2020-04-15 19:21:32 +02:00
|
|
|
/*!
|
|
|
|
* \brief Function that is connected to the QAbstractSocket::error slot and used to handle upcoming errors
|
|
|
|
* \param err the error that occurred
|
2020-04-06 17:51:20 +02:00
|
|
|
*/
|
|
|
|
void handleError(QAbstractSocket::SocketError err);
|
|
|
|
|
2020-04-15 19:21:32 +02:00
|
|
|
/*!
|
|
|
|
* \brief Function to init a session at the base station
|
|
|
|
* \return true or false
|
2020-04-06 17:51:20 +02:00
|
|
|
*/
|
2020-04-05 14:11:52 +02:00
|
|
|
bool init();
|
|
|
|
|
2020-04-15 19:21:32 +02:00
|
|
|
/*!
|
|
|
|
* \brief Function to end a session on the base station
|
2020-04-06 17:51:20 +02:00
|
|
|
*/
|
2020-04-05 14:11:52 +02:00
|
|
|
void deInit();
|
|
|
|
|
2020-04-15 19:21:32 +02:00
|
|
|
/*!
|
|
|
|
* \brief Funtion to send a command to the server (for internal use)
|
|
|
|
* \param header the command to send
|
|
|
|
* \param data the data to send
|
|
|
|
* \param timeout the timeout
|
|
|
|
* \param useTerminationKeys wether to use the termination keys defined in
|
2020-04-06 17:51:20 +02:00
|
|
|
* ScStw::SOCKET_MESSAGE_START_KEY and ScStw::SOCKET_MESSAGE_END_KEY
|
2020-04-15 19:21:32 +02:00
|
|
|
* \return a variant map containing the Keys "status" and "data"
|
2020-04-06 17:51:20 +02:00
|
|
|
*/
|
|
|
|
QVariantMap sendCommand(int header, QJsonValue data, int timeout, bool useTerminationKeys);
|
2020-04-05 14:11:52 +02:00
|
|
|
|
2020-04-15 19:21:32 +02:00
|
|
|
/*!
|
|
|
|
* \brief Function connected to the QAbstractSocket::readyRead signal
|
2020-04-06 17:51:20 +02:00
|
|
|
*/
|
|
|
|
void handleReadyRead();
|
|
|
|
|
2020-04-15 19:21:32 +02:00
|
|
|
/*!
|
|
|
|
* \brief Function to process an incoming string and parse the messages contained in it.
|
|
|
|
* \param message the message sting to parse
|
2020-04-06 17:51:20 +02:00
|
|
|
*/
|
2020-04-05 14:11:52 +02:00
|
|
|
void processSocketMessage(QString message);
|
|
|
|
|
2020-04-15 19:21:32 +02:00
|
|
|
/*!
|
|
|
|
* \brief Function that handles a parsed message
|
|
|
|
* \details This fuction looks up the id of the incoming message and tries to find the according waiting request.
|
2020-04-06 17:51:20 +02:00
|
|
|
* If it is unable to find an accordin request, the signal ScStwClient::gotUnexpectedMessage(QString message) is called.
|
|
|
|
* If the recieved message is a signal, the ScStwClient::handleSignal() function is called.
|
2020-04-15 19:21:32 +02:00
|
|
|
* \see gotUnexpectedMessage()
|
|
|
|
* \see handleSignal()
|
|
|
|
* \param reply the massage that needs to be handles
|
2020-04-06 17:51:20 +02:00
|
|
|
*/
|
|
|
|
void handleSocketMessage(QString reply);
|
|
|
|
|
2020-04-15 19:21:32 +02:00
|
|
|
/*!
|
|
|
|
* \brief Function to handle a change of the state of the tcp socket
|
|
|
|
* \details it is connected to the QAbstractSocket::stateChanged signal
|
|
|
|
* \see stateChanged()
|
|
|
|
* \param socketState
|
2020-04-06 17:51:20 +02:00
|
|
|
*/
|
|
|
|
void handleSocketStateChange(QAbstractSocket::SocketState socketState);
|
|
|
|
|
2020-04-15 19:21:32 +02:00
|
|
|
/*!
|
|
|
|
* \brief Function to handle a signal from the base station.
|
|
|
|
* \details Called by the ScStwClient::handleSocketMessage function
|
|
|
|
* \see handleSocketMessage()
|
|
|
|
* \param data
|
2020-04-06 17:51:20 +02:00
|
|
|
*/
|
|
|
|
void handleSignal(QVariantMap data);
|
2020-04-05 14:11:52 +02:00
|
|
|
|
2020-04-15 19:21:32 +02:00
|
|
|
/*! Helper Functions */
|
2020-04-05 14:11:52 +02:00
|
|
|
|
2020-04-15 19:21:32 +02:00
|
|
|
/*!
|
|
|
|
* \brief Function used to set the local cache of the baseStation connections.
|
|
|
|
* \details emits ScStwClient::gotSignal() with a ScStw::ExtensionsChanged signal.
|
|
|
|
* \see gotSignal()
|
|
|
|
* \param connections the list to set the chache to
|
2020-04-06 17:51:20 +02:00
|
|
|
*/
|
2020-04-18 14:25:48 +02:00
|
|
|
void setExtensions(QVariantList extensions);
|
2020-04-06 17:51:20 +02:00
|
|
|
|
2020-04-15 19:21:32 +02:00
|
|
|
/*!
|
|
|
|
* \brief Function to set the local state.
|
|
|
|
* \details emits ScStwClient::stateChanged() when the new state does not match the old one.
|
|
|
|
* \see stateChanged()
|
|
|
|
* \param newState the state to change to
|
2020-04-06 17:51:20 +02:00
|
|
|
*/
|
|
|
|
void setState(ScStwClient::State newState);
|
2020-04-18 14:25:48 +02:00
|
|
|
|
|
|
|
signals:
|
|
|
|
/*!
|
|
|
|
* \brief Is emitted, when the connection state changes
|
|
|
|
*/
|
|
|
|
void stateChanged();
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Is emitted, whenever a reply is recieved which does not match any requests
|
|
|
|
*
|
|
|
|
* \param reply contains the reply
|
|
|
|
*/
|
|
|
|
void gotUnexpectedMessage(QString message);
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Is emitted, when an update signal from the basestation is recieved
|
|
|
|
*
|
|
|
|
* \param data
|
|
|
|
*/
|
|
|
|
void gotSignal(ScStw::SignalKey key, QVariant data);
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Is emitted, when there is any network error
|
|
|
|
* \param error
|
|
|
|
*/
|
|
|
|
void gotError(QAbstractSocket::SocketError error);
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Is emitted, when the extensions of the base station changed
|
|
|
|
*/
|
|
|
|
void extensionsChanged();
|
2020-04-05 14:11:52 +02:00
|
|
|
};
|
2020-04-06 17:51:20 +02:00
|
|
|
|
2020-04-05 14:11:52 +02:00
|
|
|
extern ScStwClient * pGlobalScStwClient;
|
|
|
|
|
|
|
|
#endif // SCSTWCLIENT_H
|