updated entire communication to tcp socets

This commit is contained in:
Dorian Zedler 2018-08-30 16:44:04 +02:00
parent 6d44da2a25
commit 9de6dc3cc5
3 changed files with 117 additions and 35 deletions

View file

@ -20,25 +20,30 @@ class BuzzerConn : public QObject
{ {
Q_OBJECT Q_OBJECT
public: 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; double offset;
QList<double> latest_offsets; QList<double> latest_offsets;
double latest_button_pressed; double latest_button_pressed;
double starttime; double starttime;
bool connected; bool connected;
int connection_progress; int connection_progress;
QUrl buzz_url; QString ip;
int port;
private: private:
QNetworkAccessManager *networkManager; QNetworkAccessManager *networkManager;
QNetworkAccessManager *reloadNetworkManager; QNetworkAccessManager *reloadNetworkManager;
QDateTime *date; QDateTime *date;
QTcpSocket *socket;
signals: 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);
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();
@ -54,6 +59,8 @@ public slots:
Q_INVOKABLE QString test(); Q_INVOKABLE QString test();
Q_INVOKABLE bool refresh(); Q_INVOKABLE bool refresh();
//refreshed the connection to the buzzer //refreshed the connection to the buzzer
}; };
#endif // BUZZERCONN_H #endif // BUZZERCONN_H

View file

@ -17,22 +17,40 @@
#include "headers/buzzerconn.h" #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->networkManager = new QNetworkAccessManager();
this->reloadNetworkManager = new QNetworkAccessManager(); this->reloadNetworkManager = new QNetworkAccessManager();
this->socket = new QTcpSocket();
this->date = new QDateTime; this->date = new QDateTime;
this->latest_button_pressed = 0; this->latest_button_pressed = 0;
this->connected = false; this->connected = false;
this->buzz_url = ip; this->ip = ip;
this->port = port;
// "http://192.168.4.1" // "http://192.168.4.1"
} }
bool BuzzerConn::connect() bool BuzzerConn::connect()
{ {
qDebug() << "connecting..."; 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<double> times = gettimes(1000); QList<double> times = gettimes(1000);
qDebug() << times[0]; qDebug() << times[0];
if(times[0] == 200.0){ if(times[0] == 200.0){
@ -83,22 +101,38 @@ bool BuzzerConn::calcoffset(QList<double> times)
QList<double> BuzzerConn::gettimes(int timeout) QList<double> BuzzerConn::gettimes(int timeout)
{ {
// QList<double> 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("<br>");
// times.append(times_cache[0].toDouble());
// times.append(times_cache[1].toDouble());
// return(times);
// }
// else{
// return(times);
// }
QList<double> times; QList<double> times;
ReturnData_t ret = senddata(this->networkManager, QUrl(this->buzz_url), timeout); unsigned long ret;
times.append(double(ret.status_code)); ret = this->sendCommand("GET_TIMESTAMP");
if(ret){
if(ret.status_code == 200){ times.append(double(200));
ret.text.replace("\n",""); times.append(double(ret));
ret.text.replace("\r",""); ret = this->sendCommand("GET_LASTPRESSED");
QStringList times_cache = ret.text.split("<br>"); times.append(double(ret));
times.append(times_cache[0].toDouble());
times.append(times_cache[1].toDouble());
return(times); return(times);
} }
else{ else {
return(times); times.append(444);
} }
} }
bool BuzzerConn::buzzer_triggered() bool BuzzerConn::buzzer_triggered()
@ -173,23 +207,25 @@ bool BuzzerConn::refresh()
if(!this->connected){ if(!this->connected){
return(false); return(false);
} }
QList<double> times; // QList<double> times;
ReturnData_t ret = senddata(this->reloadNetworkManager, QUrl(this->buzz_url), 1000); // ReturnData_t ret = senddata(this->reloadNetworkManager, QUrl(this->ip), 1000);
times.append(double(ret.status_code)); // times.append(double(ret.status_code));
if(ret.status_code == 200){ // if(ret.status_code == 200){
ret.text.replace("\n",""); // ret.text.replace("\n","");
ret.text.replace("\r",""); // ret.text.replace("\r","");
QStringList times_cache = ret.text.split("<br>"); // QStringList times_cache = ret.text.split("<br>");
times.append(times_cache[0].toDouble()); // times.append(times_cache[0].toDouble());
times.append(times_cache[1].toDouble()); // times.append(times_cache[1].toDouble());
calcoffset(times); // calcoffset(times);
return(true); // return(true);
} // }
else{ // else{
this->connected = false; // //this->connected = false;
return(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)
@ -200,7 +236,7 @@ ReturnData_t BuzzerConn::senddata(QNetworkAccessManager * NetMan, QUrl serviceUr
QNetworkRequest request(serviceUrl); QNetworkRequest request(serviceUrl);
request.setHeader(QNetworkRequest::ContentTypeHeader, 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 //send a POST request with the given url and data to the server
QUrlQuery pdata; QUrlQuery pdata;
@ -232,3 +268,42 @@ ReturnData_t BuzzerConn::senddata(QNetworkAccessManager * NetMan, QUrl serviceUr
//return the data //return the data
return(ret); 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;
}

View file

@ -105,8 +105,8 @@ int main(int argc, char *argv[])
#endif #endif
connectToDatabase(); connectToDatabase();
BuzzerConn * pBuzzerConn = new BuzzerConn(nullptr, QUrl("http://192.168.4.1")); BuzzerConn * pBuzzerConn = new BuzzerConn(nullptr, "192.168.4.1", 80);
BuzzerConn * pStartpadConn = new BuzzerConn(nullptr, QUrl("http://192.168.4.2")); BuzzerConn * pStartpadConn = new BuzzerConn(nullptr, "192.168.4.2", 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