From 2849dc0e3b48815fd6d87800b8619759d935fcb6 Mon Sep 17 00:00:00 2001 From: Dorian Zedler Date: Tue, 18 Sep 2018 17:11:47 +0200 Subject: [PATCH] removed some unnecessary functions when connecting only the current timestamp is is requested -> connection speed doubled --- headers/buzzerconn.h | 67 ++++++++++------ sources/buzzerconn.cpp | 169 +++++++---------------------------------- 2 files changed, 74 insertions(+), 162 deletions(-) diff --git a/headers/buzzerconn.h b/headers/buzzerconn.h index 9918979..5446f51 100644 --- a/headers/buzzerconn.h +++ b/headers/buzzerconn.h @@ -11,10 +11,10 @@ #include #include -typedef struct strReturnData{ - int status_code; - QString text; -}ReturnData_t; +//typedef struct strReturnData{ +// int status_code; +// QString text; +//}ReturnData_t; class BuzzerConn : public QObject { @@ -27,39 +27,54 @@ class BuzzerConn : public QObject public: explicit BuzzerConn(QObject *parent = nullptr, QString ip = "http://192.168.4.1", int port = 80); + + // values for the time calculation double offset; QList latest_offsets; double latest_button_pressed; - double starttime; - bool connected; + + // double starttime; + // bool connected; + + // values for the socket connection int connection_progress; QString ip; int port; int errors; int errors_until_disconnect = 4; + + // the current state QString state; + // can be: + // - 'disconnected' + // - 'connecting' + // - 'connected' private: - QNetworkAccessManager *networkManager; - QNetworkAccessManager *reloadNetworkManager; QDateTime *date; + //to get the current time + QTcpSocket *socket; + //socket for communication with the extention + QStringList pending_commands; - //QSemaphore dataPipe(1); + //commands to send to the extention + //one command is being sent whenever refresh() is called signals: void triggered(); + //is emitted when the device is triggered void stateChanged(); + //is emitted, when the connection state changes void progressChanged(); void offsetChanged(); public slots: - ReturnData_t senddata(QNetworkAccessManager * NetMan, QUrl serviceUrl, int timeout); - //function to communicate with the buzzer + Q_INVOKABLE signed long sendCommand(QString command, int timeout); //function to send commands to the sensor //Can be: @@ -70,25 +85,33 @@ public slots: //error codes: //(-1) : timeout //(-2) : invalid data was recieved - Q_INVOKABLE QList gettimes(int timeout); - //function to get the times from the buzzer as a list with the normal network manager + + Q_INVOKABLE QList gettimes(int timeout, bool bothTimes = true); + //function to get the times from the buzzer as a list + //if bothTimes is true the current and the last-pressed timestamp will be returned + //else only the current timestamp will be returned + Q_INVOKABLE bool connect(); //function to connect to buzzer + Q_INVOKABLE bool calcoffset(QList times); //function that calculates the average time offset between the buzzer and the device - Q_INVOKABLE bool buzzer_triggered(); - //function that checks ih the buzzer has been pushed since the last call of this function - Q_INVOKABLE bool start(); - //syncs the buzzer and the base to make a start possible + Q_INVOKABLE double get(QString key); //can return some things (offset, lastpressed, currtime, connection_progress, connected) - Q_INVOKABLE QString test(); - Q_INVOKABLE bool refresh(); - //refreshed the connection to the buzzer - Q_INVOKABLE void appendCommand(QString command); - void setIP(const QString &ipAdress); + Q_INVOKABLE bool refresh(); + //- refreshes the connection to the buzzer + //- checks if it as triggered if it was 'triggered()' is emitted + //- sends one command from the pending commans list + + Q_INVOKABLE void appendCommand(QString command); + //appends a command to the pending commnds list + + + // functions for the qml adapter QString getIP() const; + void setIP(const QString &ipAdress); QString getState() const; void setState(QString newState); diff --git a/sources/buzzerconn.cpp b/sources/buzzerconn.cpp index 8e6a890..a8960d2 100644 --- a/sources/buzzerconn.cpp +++ b/sources/buzzerconn.cpp @@ -19,14 +19,10 @@ BuzzerConn::BuzzerConn(QObject *parent, QString ip, int port) : QObject(parent) { - this->networkManager = new QNetworkAccessManager(); - this->reloadNetworkManager = new QNetworkAccessManager(); - this->socket = new QTcpSocket(); this->date = new QDateTime; this->latest_button_pressed = 0; - this->connected = false; this->ip = ip; this->port = port; @@ -41,7 +37,7 @@ bool BuzzerConn::connect() setState("connecting"); - //wait until the request has finished + //setup loop to wait until the connection has finished QEventLoop loop; QTimer timer; @@ -50,36 +46,37 @@ bool BuzzerConn::connect() loop.connect(this->socket, SIGNAL(connected()), &loop, SLOT(quit())); timer.start(3000); + //connect this->socket->connectToHost(this->ip, this->port); + + //wait for the connection to finish loop.exec(); timer.stop(); if(timer.remainingTime() == 0){ //the time has been triggered -> timeout + setState("disconnected"); return(false); } - QList times = gettimes(2000); - qDebug() << times[0]; + QList times = gettimes(2000, true); + qDebug() << times; if(times[0] == 200.0){ this->latest_button_pressed = times[2]; for(int i=0;i<=100;i++){ this->connection_progress = i; emit this->progressChanged(); - if(!calcoffset(this->gettimes(1000))){ + if(!calcoffset(this->gettimes(1000, false))){ this->connection_progress = 100; - this->connected = false; setState("disconnected"); return(false); } } - this->connected = true; setState("connected"); return(true); } else{ - this->connected = false; setState("disconnected"); return(false); } @@ -87,11 +84,11 @@ bool BuzzerConn::connect() bool BuzzerConn::calcoffset(QList times) { - if(times.length() != 3){ + if(times.length() != 2){ return(false); } if(times[0] == 200.0){ - this->latest_button_pressed = times[2]; + double offset = date->currentMSecsSinceEpoch() - times[1]; if(this->latest_offsets.length()>=100){ this->latest_offsets.removeFirst(); @@ -113,40 +110,27 @@ bool BuzzerConn::calcoffset(QList times) } } -QList BuzzerConn::gettimes(int timeout) +QList BuzzerConn::gettimes(int timeout, bool bothTimes) { - // QList times; - // ReturnData_t ret = senddata(this->networkManager, QUrl(this->ip), timeout); - // times.append(double(ret.status_code)); - - // if(ret.status_code == 200){ - // ret.text.replace("\n",""); - // ret.text.replace("\r",""); - // QStringList times_cache = ret.text.split("
"); - // times.append(times_cache[0].toDouble()); - // times.append(times_cache[1].toDouble()); - - // return(times); - // } - // else{ - // return(times); - // } - QList times; signed long ret; ret = this->sendCommand("GET_TIMESTAMP", timeout); + qDebug() << timeout << bothTimes; if(ret >= 0){ + times.append(double(200)); times.append(double(ret)); - ret = this->sendCommand("GET_LASTPRESSED", timeout); - if(ret >= 0){ - times.append(double(ret)); - return(times); - } - else { - times[0] = ret; - } + if(bothTimes){ + ret = this->sendCommand("GET_LASTPRESSED", timeout); + + if(ret >= 0){ + times.append(double(ret)); + } + else { + times[0] = ret; + } + } } else { times.append(ret); @@ -155,55 +139,6 @@ QList BuzzerConn::gettimes(int timeout) } -bool BuzzerConn::buzzer_triggered() -{ - - if(!this->connected){ - return(false); - } - - if(pending_commands.length() > 0){ - QString command = this->pending_commands.first(); - - signed long retval = this->sendCommand(command, 800); - if(retval > 0){ - this->pending_commands.removeFirst(); - } - } - - QList times = this->gettimes(1000); - if(times[0] == 200.0){ - if(times[2] > this->latest_button_pressed){ - this->latest_button_pressed = times[2]; - - return(true); - } - else { - return(false); - } - } - else{ - //this->connected = false; - return(false); - } -} - -bool BuzzerConn::start() -{ - if(!this->connected){ - return(false); - } - QList times = this->gettimes(1000); - if(times[0] == 200.0 && this->connected){ - this->latest_button_pressed = times[2]; - return(true); - } - else{ - this->connected = false; - return(false); - } -} - double BuzzerConn::get(QString key) { if(key == "offset"){ @@ -219,7 +154,7 @@ double BuzzerConn::get(QString key) return(this->connection_progress); } else if( key == "connected") { - if(this->connected){ + if(this->state == "connected"){ return(1); } return(0); @@ -261,18 +196,13 @@ double BuzzerConn::getLastTriggered() const return(this->latest_button_pressed); } -QString BuzzerConn::test() -{ - ReturnData_t ret = this->senddata(this->networkManager, QUrl("http://www.google.de"), 500); - return(ret.text); -} - bool BuzzerConn::refresh() { - if(!this->connected){ + if(this->state != "connected"){ return(false); } + //send one of the pending commands if(pending_commands.length() > 0){ QString command = this->pending_commands.first(); @@ -296,54 +226,13 @@ bool BuzzerConn::refresh() this->errors ++; if(this->errors > errors_until_disconnect){ this->socket->disconnectFromHost(); - this->connected = false; + this->setState("disconnected"); } return(false); } } -ReturnData_t BuzzerConn::senddata(QNetworkAccessManager * NetMan, QUrl serviceUrl, int timeout) -{ - - ReturnData_t ret; //this is a custom type to store the returned data - // Call the webservice - - QNetworkRequest request(serviceUrl); - request.setHeader(QNetworkRequest::ContentTypeHeader, - "application/x-www-form-urlencoded"); - - //send a POST request with the given url and data to the server - QUrlQuery pdata; - QNetworkReply* reply; - - //wait until the request has finished - QEventLoop loop; - QTimer timer; - - timer.setSingleShot(true); - loop.connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit())); - loop.connect(NetMan, SIGNAL(finished(QNetworkReply*)), SLOT(quit())); - - timer.start(timeout); - reply = NetMan->post(request, pdata.toString(QUrl::FullyEncoded).toUtf8()); - loop.exec(); - timer.stop(); - - //get the status code - QVariant status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute); - - ret.status_code = status_code.toInt(); - if(ret.status_code == 0){ //if the statuscode is zero, the connecion to the server was not possible - ret.status_code = 444; - } - //get the full text response - ret.text = QString::fromUtf8(reply->readAll()); - - //return the data - return(ret); -} - signed long BuzzerConn::sendCommand(QString command, int timeout){ //if there is any data in the storage, clear it @@ -379,9 +268,9 @@ signed long BuzzerConn::sendCommand(QString command, int timeout){ return(-1); } - //if the data is not 4 bites long if is invalid -> clear and terminate + //if the data is not 4 bytes long it is invalid -> clear and terminate if(this->socket->bytesAvailable() != 4){ - this->socket->readAll(); + qDebug() << this->socket->readAll(); return(-2); } long data = 0;