increased stability of connection and added basic error handling
This commit is contained in:
parent
ab51f501ca
commit
4dc9ff53f3
3 changed files with 61 additions and 17 deletions
|
@ -29,6 +29,8 @@ public:
|
||||||
int connection_progress;
|
int connection_progress;
|
||||||
QString ip;
|
QString ip;
|
||||||
int port;
|
int port;
|
||||||
|
int errors;
|
||||||
|
int errors_until_disconnect = 4;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -43,7 +45,16 @@ signals:
|
||||||
public slots:
|
public slots:
|
||||||
ReturnData_t senddata(QNetworkAccessManager * NetMan, QUrl serviceUrl, int timeout);
|
ReturnData_t senddata(QNetworkAccessManager * NetMan, QUrl serviceUrl, int timeout);
|
||||||
//function to communicate with the buzzer
|
//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);
|
Q_INVOKABLE QList<double> gettimes(int timeout);
|
||||||
//function to get the times from the buzzer as a list with the normal network manager
|
//function to get the times from the buzzer as a list with the normal network manager
|
||||||
Q_INVOKABLE bool connect();
|
Q_INVOKABLE bool connect();
|
||||||
|
|
|
@ -50,6 +50,11 @@ bool BuzzerConn::connect()
|
||||||
loop.exec();
|
loop.exec();
|
||||||
timer.stop();
|
timer.stop();
|
||||||
|
|
||||||
|
if(timer.remainingTime() == 0){
|
||||||
|
//the time has been triggered -> timeout
|
||||||
|
return(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
QList<double> times = gettimes(1000);
|
QList<double> times = gettimes(1000);
|
||||||
qDebug() << times[0];
|
qDebug() << times[0];
|
||||||
|
@ -119,19 +124,25 @@ QList<double> BuzzerConn::gettimes(int timeout)
|
||||||
// }
|
// }
|
||||||
|
|
||||||
QList<double> times;
|
QList<double> times;
|
||||||
unsigned long ret;
|
signed long ret;
|
||||||
ret = this->sendCommand("GET_TIMESTAMP");
|
ret = this->sendCommand("GET_TIMESTAMP", timeout);
|
||||||
if(ret){
|
if(ret >= 0){
|
||||||
times.append(double(200));
|
times.append(double(200));
|
||||||
times.append(double(ret));
|
times.append(double(ret));
|
||||||
ret = this->sendCommand("GET_LASTPRESSED");
|
ret = this->sendCommand("GET_LASTPRESSED", timeout);
|
||||||
times.append(double(ret));
|
if(ret >= 0){
|
||||||
return(times);
|
times.append(double(ret));
|
||||||
|
return(times);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
times[0] = ret;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
times.append(444);
|
times.append(ret);
|
||||||
}
|
}
|
||||||
|
return(times);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -224,8 +235,20 @@ bool BuzzerConn::refresh()
|
||||||
// //this->connected = false;
|
// //this->connected = false;
|
||||||
// return(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)
|
ReturnData_t BuzzerConn::senddata(QNetworkAccessManager * NetMan, QUrl serviceUrl, int timeout)
|
||||||
|
@ -269,7 +292,13 @@ ReturnData_t BuzzerConn::senddata(QNetworkAccessManager * NetMan, QUrl serviceUr
|
||||||
return(ret);
|
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
|
//send request to the socket server
|
||||||
QByteArray arrBlock;
|
QByteArray arrBlock;
|
||||||
QDataStream out(&arrBlock, QIODevice::WriteOnly);
|
QDataStream out(&arrBlock, QIODevice::WriteOnly);
|
||||||
|
@ -288,22 +317,26 @@ unsigned long BuzzerConn::sendCommand(QString command){
|
||||||
timer.setSingleShot(true);
|
timer.setSingleShot(true);
|
||||||
loop.connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
|
loop.connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
|
||||||
loop.connect(socket, SIGNAL(readyRead()), &loop, SLOT(quit()));
|
loop.connect(socket, SIGNAL(readyRead()), &loop, SLOT(quit()));
|
||||||
timer.start(3000);
|
timer.start(timeout);
|
||||||
loop.exec();
|
loop.exec();
|
||||||
|
|
||||||
//loop finished
|
//loop finished
|
||||||
timer.stop();
|
timer.stop();
|
||||||
|
if(timer.remainingTime() == 0){
|
||||||
|
//the time has been triggered -> timeout
|
||||||
|
return(-1);
|
||||||
|
}
|
||||||
|
|
||||||
qDebug() << "ready read";
|
//if the data is not 4 bites long if is invalid -> clear and terminate
|
||||||
qDebug() << this->socket->bytesAvailable();
|
|
||||||
if(this->socket->bytesAvailable() != 4){
|
if(this->socket->bytesAvailable() != 4){
|
||||||
this->socket->readAll();
|
this->socket->readAll();
|
||||||
|
return(-2);
|
||||||
}
|
}
|
||||||
unsigned long data = 0;
|
long data = 0;
|
||||||
this->socket->read((char*)&data,4);
|
this->socket->read((char*)&data,4);
|
||||||
|
|
||||||
qDebug() << data;
|
qDebug() << data;
|
||||||
qDebug() << this->socket->bytesAvailable();
|
qDebug() << this->socket->bytesAvailable();
|
||||||
qDebug() << "end read";
|
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
|
@ -106,7 +106,7 @@ int main(int argc, char *argv[])
|
||||||
|
|
||||||
connectToDatabase();
|
connectToDatabase();
|
||||||
BuzzerConn * pBuzzerConn = new BuzzerConn(nullptr, "192.168.4.1", 80);
|
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();
|
AppSettings * pAppSettings = new AppSettings();
|
||||||
|
|
||||||
//setup the sql storage model as a qml model
|
//setup the sql storage model as a qml model
|
||||||
|
|
Reference in a new issue