From ab51f501ca3d982a707bc7ab7badb87be5e87163 Mon Sep 17 00:00:00 2001 From: Dorian Zedler Date: Thu, 30 Aug 2018 16:44:04 +0200 Subject: [PATCH] updated entire communication to tcp socets --- headers/buzzerconn.h | 11 +++- sources/buzzerconn.cpp | 137 +++++++++++++++++++++++++++++++---------- sources/main.cpp | 4 +- 3 files changed, 117 insertions(+), 35 deletions(-) diff --git a/headers/buzzerconn.h b/headers/buzzerconn.h index 7cd2ff7..bfbdd1f 100644 --- a/headers/buzzerconn.h +++ b/headers/buzzerconn.h @@ -20,25 +20,30 @@ class BuzzerConn : public QObject { Q_OBJECT public: - explicit BuzzerConn(QObject *parent = nullptr, QUrl ip = QUrl("http://192.168.4.1")); + explicit BuzzerConn(QObject *parent = nullptr, QString ip = "http://192.168.4.1", int port = 80); double offset; QList latest_offsets; double latest_button_pressed; double starttime; bool connected; int connection_progress; - QUrl buzz_url; + QString ip; + int port; + + private: QNetworkAccessManager *networkManager; QNetworkAccessManager *reloadNetworkManager; QDateTime *date; + QTcpSocket *socket; signals: public slots: ReturnData_t senddata(QNetworkAccessManager * NetMan, QUrl serviceUrl, int timeout); //function to communicate with the buzzer + unsigned long sendCommand(QString command); 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(); @@ -54,6 +59,8 @@ public slots: Q_INVOKABLE QString test(); Q_INVOKABLE bool refresh(); //refreshed the connection to the buzzer + + }; #endif // BUZZERCONN_H diff --git a/sources/buzzerconn.cpp b/sources/buzzerconn.cpp index 1734b57..1036387 100644 --- a/sources/buzzerconn.cpp +++ b/sources/buzzerconn.cpp @@ -17,22 +17,40 @@ #include "headers/buzzerconn.h" -BuzzerConn::BuzzerConn(QObject *parent, QUrl ip) : QObject(parent) +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->buzz_url = ip; + this->ip = ip; + this->port = port; // "http://192.168.4.1" } bool BuzzerConn::connect() { qDebug() << "connecting..."; + + //wait until the request has finished + QEventLoop loop; + QTimer timer; + + timer.setSingleShot(true); + loop.connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit())); + loop.connect(this->socket, SIGNAL(connected()), &loop, SLOT(quit())); + + timer.start(3000); + this->socket->connectToHost(this->ip, this->port); + loop.exec(); + timer.stop(); + + QList times = gettimes(1000); qDebug() << times[0]; if(times[0] == 200.0){ @@ -83,22 +101,38 @@ bool BuzzerConn::calcoffset(QList times) QList BuzzerConn::gettimes(int timeout) { + // 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; - ReturnData_t ret = senddata(this->networkManager, QUrl(this->buzz_url), 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()); - + unsigned long ret; + ret = this->sendCommand("GET_TIMESTAMP"); + if(ret){ + times.append(double(200)); + times.append(double(ret)); + ret = this->sendCommand("GET_LASTPRESSED"); + times.append(double(ret)); return(times); } - else{ - return(times); + else { + times.append(444); } + + } bool BuzzerConn::buzzer_triggered() @@ -173,23 +207,25 @@ bool BuzzerConn::refresh() if(!this->connected){ return(false); } - QList times; - ReturnData_t ret = senddata(this->reloadNetworkManager, QUrl(this->buzz_url), 1000); - times.append(double(ret.status_code)); +// QList times; +// ReturnData_t ret = senddata(this->reloadNetworkManager, QUrl(this->ip), 1000); +// 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()); - calcoffset(times); - return(true); - } - else{ - this->connected = false; - return(false); - } +// 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()); +// calcoffset(times); +// return(true); +// } +// else{ +// //this->connected = false; +// return(false); +// } + + return(this->calcoffset(this->gettimes(1000))); } ReturnData_t BuzzerConn::senddata(QNetworkAccessManager * NetMan, QUrl serviceUrl, int timeout) @@ -200,7 +236,7 @@ ReturnData_t BuzzerConn::senddata(QNetworkAccessManager * NetMan, QUrl serviceUr QNetworkRequest request(serviceUrl); request.setHeader(QNetworkRequest::ContentTypeHeader, - "application/x-www-form-urlencoded"); + "application/x-www-form-urlencoded"); //send a POST request with the given url and data to the server QUrlQuery pdata; @@ -232,3 +268,42 @@ ReturnData_t BuzzerConn::senddata(QNetworkAccessManager * NetMan, QUrl serviceUr //return the data return(ret); } + +unsigned long BuzzerConn::sendCommand(QString command){ + //send request to the socket server + QByteArray arrBlock; + QDataStream out(&arrBlock, QIODevice::WriteOnly); + //out.setVersion(QDataStream::Qt_5_10); + out << quint16(0) << command; + + out.device()->seek(0); + out << quint16(arrBlock.size() - sizeof(quint16)); + + this->socket->write(arrBlock); + + //now wait for the server of the sensor to answer + QEventLoop loop; + QTimer timer; + + timer.setSingleShot(true); + loop.connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit())); + loop.connect(socket, SIGNAL(readyRead()), &loop, SLOT(quit())); + timer.start(3000); + loop.exec(); + + //loop finished + timer.stop(); + + qDebug() << "ready read"; + qDebug() << this->socket->bytesAvailable(); + if(this->socket->bytesAvailable() != 4){ + this->socket->readAll(); + } + unsigned 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 b6e07bd..00859a8 100644 --- a/sources/main.cpp +++ b/sources/main.cpp @@ -105,8 +105,8 @@ int main(int argc, char *argv[]) #endif connectToDatabase(); - BuzzerConn * pBuzzerConn = new BuzzerConn(nullptr, QUrl("http://192.168.4.1")); - BuzzerConn * pStartpadConn = new BuzzerConn(nullptr, QUrl("http://192.168.4.2")); + BuzzerConn * pBuzzerConn = new BuzzerConn(nullptr, "192.168.4.1", 80); + BuzzerConn * pStartpadConn = new BuzzerConn(nullptr, "192.168.4.2", 80); AppSettings * pAppSettings = new AppSettings(); //setup the sql storage model as a qml model