From 759c9f10ce40dde78b086bd5664d54db560debba Mon Sep 17 00:00:00 2001 From: Dorian Zedler Date: Thu, 30 Aug 2018 17:49:24 +0200 Subject: [PATCH] increased stability of connection and added basic error handling --- headers/buzzerconn.h | 13 ++++++++- sources/buzzerconn.cpp | 63 ++++++++++++++++++++++++++++++++---------- sources/main.cpp | 2 +- 3 files changed, 61 insertions(+), 17 deletions(-) diff --git a/headers/buzzerconn.h b/headers/buzzerconn.h index bfbdd1f..2d411aa 100644 --- a/headers/buzzerconn.h +++ b/headers/buzzerconn.h @@ -29,6 +29,8 @@ public: int connection_progress; QString ip; int port; + int errors; + int errors_until_disconnect = 4; @@ -43,7 +45,16 @@ signals: public slots: ReturnData_t senddata(QNetworkAccessManager * NetMan, QUrl serviceUrl, int timeout); //function to communicate with the buzzer - unsigned long sendCommand(QString command); + signed long sendCommand(QString command, int timeout); + //function to send commands to the sensor + //Can be: + //command - return + //GET_TIMESTAMP - timestamp of the sensor + //GET_LASTPRESSED - timestamp of the sensor when it was triggered the last time + // + //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 bool connect(); diff --git a/sources/buzzerconn.cpp b/sources/buzzerconn.cpp index 1036387..4362ccc 100644 --- a/sources/buzzerconn.cpp +++ b/sources/buzzerconn.cpp @@ -50,6 +50,11 @@ bool BuzzerConn::connect() loop.exec(); timer.stop(); + if(timer.remainingTime() == 0){ + //the time has been triggered -> timeout + return(false); + } + QList times = gettimes(1000); qDebug() << times[0]; @@ -119,19 +124,25 @@ QList BuzzerConn::gettimes(int timeout) // } QList times; - unsigned long ret; - ret = this->sendCommand("GET_TIMESTAMP"); - if(ret){ + signed long ret; + ret = this->sendCommand("GET_TIMESTAMP", timeout); + if(ret >= 0){ times.append(double(200)); times.append(double(ret)); - ret = this->sendCommand("GET_LASTPRESSED"); - times.append(double(ret)); - return(times); + ret = this->sendCommand("GET_LASTPRESSED", timeout); + if(ret >= 0){ + times.append(double(ret)); + return(times); + } + else { + times[0] = ret; + } + } else { - times.append(444); + times.append(ret); } - + return(times); } @@ -224,8 +235,20 @@ bool BuzzerConn::refresh() // //this->connected = false; // return(false); // } + QList ret = this->gettimes(800); + if(ret[0] >= 0){ + this->errors = 0; + return(this->calcoffset(ret)); + } + else { + this->errors ++; + if(this->errors > errors_until_disconnect){ + this->socket->disconnectFromHost(); + this->connected = false; + } + return(false); + } - return(this->calcoffset(this->gettimes(1000))); } ReturnData_t BuzzerConn::senddata(QNetworkAccessManager * NetMan, QUrl serviceUrl, int timeout) @@ -269,7 +292,13 @@ ReturnData_t BuzzerConn::senddata(QNetworkAccessManager * NetMan, QUrl serviceUr return(ret); } -unsigned long BuzzerConn::sendCommand(QString command){ +signed long BuzzerConn::sendCommand(QString command, int timeout){ + + //if there is any data in the storage, clear it + if(this->socket->bytesAvailable() > 0){ + this->socket->readAll(); + } + //send request to the socket server QByteArray arrBlock; QDataStream out(&arrBlock, QIODevice::WriteOnly); @@ -288,22 +317,26 @@ unsigned long BuzzerConn::sendCommand(QString command){ timer.setSingleShot(true); loop.connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit())); loop.connect(socket, SIGNAL(readyRead()), &loop, SLOT(quit())); - timer.start(3000); + timer.start(timeout); loop.exec(); //loop finished timer.stop(); + if(timer.remainingTime() == 0){ + //the time has been triggered -> timeout + return(-1); + } - qDebug() << "ready read"; - qDebug() << this->socket->bytesAvailable(); + //if the data is not 4 bites long if is invalid -> clear and terminate if(this->socket->bytesAvailable() != 4){ this->socket->readAll(); + return(-2); } - unsigned long data = 0; + long data = 0; this->socket->read((char*)&data,4); qDebug() << data; qDebug() << this->socket->bytesAvailable(); - qDebug() << "end read"; + return data; } diff --git a/sources/main.cpp b/sources/main.cpp index 00859a8..6144ca9 100644 --- a/sources/main.cpp +++ b/sources/main.cpp @@ -106,7 +106,7 @@ int main(int argc, char *argv[]) connectToDatabase(); BuzzerConn * pBuzzerConn = new BuzzerConn(nullptr, "192.168.4.1", 80); - BuzzerConn * pStartpadConn = new BuzzerConn(nullptr, "192.168.4.2", 80); + BuzzerConn * pStartpadConn = new BuzzerConn(nullptr, "192.168.4.3", 80); AppSettings * pAppSettings = new AppSettings(); //setup the sql storage model as a qml model