increased stability of connection and added basic error handling

This commit is contained in:
Dorian Zedler 2018-08-30 17:49:24 +02:00
parent 9de6dc3cc5
commit 759c9f10ce
3 changed files with 61 additions and 17 deletions

View file

@ -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<double> gettimes(int timeout);
//function to get the times from the buzzer as a list with the normal network manager
Q_INVOKABLE bool connect();

View file

@ -50,6 +50,11 @@ bool BuzzerConn::connect()
loop.exec();
timer.stop();
if(timer.remainingTime() == 0){
//the time has been triggered -> timeout
return(false);
}
QList<double> times = gettimes(1000);
qDebug() << times[0];
@ -119,19 +124,25 @@ QList<double> BuzzerConn::gettimes(int timeout)
// }
QList<double> 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");
ret = this->sendCommand("GET_LASTPRESSED", timeout);
if(ret >= 0){
times.append(double(ret));
return(times);
}
else {
times.append(444);
times[0] = ret;
}
}
else {
times.append(ret);
}
return(times);
}
@ -224,8 +235,20 @@ bool BuzzerConn::refresh()
// //this->connected = false;
// return(false);
// }
QList<double> 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;
}

View file

@ -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