updated entire communication to tcp socets

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

View file

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

View file

@ -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<double> times = gettimes(1000);
qDebug() << times[0];
if(times[0] == 200.0){
@ -83,22 +101,38 @@ bool BuzzerConn::calcoffset(QList<double> times)
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;
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("<br>");
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);
times.append(444);
}
}
bool BuzzerConn::buzzer_triggered()
@ -173,23 +207,25 @@ bool BuzzerConn::refresh()
if(!this->connected){
return(false);
}
QList<double> times;
ReturnData_t ret = senddata(this->reloadNetworkManager, QUrl(this->buzz_url), 1000);
times.append(double(ret.status_code));
// QList<double> 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("<br>");
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("<br>");
// 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)
@ -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;
}

View file

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