removed some unnecessary functions
when connecting only the current timestamp is is requested -> connection speed doubled
This commit is contained in:
parent
24f9dcbc6f
commit
2849dc0e3b
2 changed files with 74 additions and 162 deletions
|
@ -11,10 +11,10 @@
|
||||||
#include <QDateTime>
|
#include <QDateTime>
|
||||||
#include <QtDebug>
|
#include <QtDebug>
|
||||||
|
|
||||||
typedef struct strReturnData{
|
//typedef struct strReturnData{
|
||||||
int status_code;
|
// int status_code;
|
||||||
QString text;
|
// QString text;
|
||||||
}ReturnData_t;
|
//}ReturnData_t;
|
||||||
|
|
||||||
class BuzzerConn : public QObject
|
class BuzzerConn : public QObject
|
||||||
{
|
{
|
||||||
|
@ -27,39 +27,54 @@ class BuzzerConn : public QObject
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit BuzzerConn(QObject *parent = nullptr, QString ip = "http://192.168.4.1", int port = 80);
|
explicit BuzzerConn(QObject *parent = nullptr, QString ip = "http://192.168.4.1", int port = 80);
|
||||||
|
|
||||||
|
// values for the time calculation
|
||||||
double offset;
|
double offset;
|
||||||
QList<double> latest_offsets;
|
QList<double> latest_offsets;
|
||||||
double latest_button_pressed;
|
double latest_button_pressed;
|
||||||
double starttime;
|
|
||||||
bool connected;
|
// double starttime;
|
||||||
|
// bool connected;
|
||||||
|
|
||||||
|
// values for the socket connection
|
||||||
int connection_progress;
|
int connection_progress;
|
||||||
QString ip;
|
QString ip;
|
||||||
int port;
|
int port;
|
||||||
int errors;
|
int errors;
|
||||||
int errors_until_disconnect = 4;
|
int errors_until_disconnect = 4;
|
||||||
|
|
||||||
|
// the current state
|
||||||
QString state;
|
QString state;
|
||||||
|
// can be:
|
||||||
|
// - 'disconnected'
|
||||||
|
// - 'connecting'
|
||||||
|
// - 'connected'
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QNetworkAccessManager *networkManager;
|
|
||||||
QNetworkAccessManager *reloadNetworkManager;
|
|
||||||
QDateTime *date;
|
QDateTime *date;
|
||||||
|
//to get the current time
|
||||||
|
|
||||||
QTcpSocket *socket;
|
QTcpSocket *socket;
|
||||||
|
//socket for communication with the extention
|
||||||
|
|
||||||
QStringList pending_commands;
|
QStringList pending_commands;
|
||||||
//QSemaphore dataPipe(1);
|
//commands to send to the extention
|
||||||
|
//one command is being sent whenever refresh() is called
|
||||||
signals:
|
signals:
|
||||||
void triggered();
|
void triggered();
|
||||||
|
//is emitted when the device is triggered
|
||||||
|
|
||||||
void stateChanged();
|
void stateChanged();
|
||||||
|
//is emitted, when the connection state changes
|
||||||
|
|
||||||
void progressChanged();
|
void progressChanged();
|
||||||
|
|
||||||
void offsetChanged();
|
void offsetChanged();
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
ReturnData_t senddata(QNetworkAccessManager * NetMan, QUrl serviceUrl, int timeout);
|
|
||||||
//function to communicate with the buzzer
|
|
||||||
Q_INVOKABLE signed long sendCommand(QString command, int timeout);
|
Q_INVOKABLE signed long sendCommand(QString command, int timeout);
|
||||||
//function to send commands to the sensor
|
//function to send commands to the sensor
|
||||||
//Can be:
|
//Can be:
|
||||||
|
@ -70,25 +85,33 @@ public slots:
|
||||||
//error codes:
|
//error codes:
|
||||||
//(-1) : timeout
|
//(-1) : timeout
|
||||||
//(-2) : invalid data was recieved
|
//(-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 QList<double> gettimes(int timeout, bool bothTimes = true);
|
||||||
|
//function to get the times from the buzzer as a list
|
||||||
|
//if bothTimes is true the current and the last-pressed timestamp will be returned
|
||||||
|
//else only the current timestamp will be returned
|
||||||
|
|
||||||
Q_INVOKABLE bool connect();
|
Q_INVOKABLE bool connect();
|
||||||
//function to connect to buzzer
|
//function to connect to buzzer
|
||||||
|
|
||||||
Q_INVOKABLE bool calcoffset(QList<double> times);
|
Q_INVOKABLE bool calcoffset(QList<double> times);
|
||||||
//function that calculates the average time offset between the buzzer and the device
|
//function that calculates the average time offset between the buzzer and the device
|
||||||
Q_INVOKABLE bool buzzer_triggered();
|
|
||||||
//function that checks ih the buzzer has been pushed since the last call of this function
|
|
||||||
Q_INVOKABLE bool start();
|
|
||||||
//syncs the buzzer and the base to make a start possible
|
|
||||||
Q_INVOKABLE double get(QString key);
|
Q_INVOKABLE double get(QString key);
|
||||||
//can return some things (offset, lastpressed, currtime, connection_progress, connected)
|
//can return some things (offset, lastpressed, currtime, connection_progress, connected)
|
||||||
Q_INVOKABLE QString test();
|
|
||||||
Q_INVOKABLE bool refresh();
|
|
||||||
//refreshed the connection to the buzzer
|
|
||||||
Q_INVOKABLE void appendCommand(QString command);
|
|
||||||
|
|
||||||
void setIP(const QString &ipAdress);
|
Q_INVOKABLE bool refresh();
|
||||||
|
//- refreshes the connection to the buzzer
|
||||||
|
//- checks if it as triggered if it was 'triggered()' is emitted
|
||||||
|
//- sends one command from the pending commans list
|
||||||
|
|
||||||
|
Q_INVOKABLE void appendCommand(QString command);
|
||||||
|
//appends a command to the pending commnds list
|
||||||
|
|
||||||
|
|
||||||
|
// functions for the qml adapter
|
||||||
QString getIP() const;
|
QString getIP() const;
|
||||||
|
void setIP(const QString &ipAdress);
|
||||||
|
|
||||||
QString getState() const;
|
QString getState() const;
|
||||||
void setState(QString newState);
|
void setState(QString newState);
|
||||||
|
|
|
@ -19,14 +19,10 @@
|
||||||
|
|
||||||
BuzzerConn::BuzzerConn(QObject *parent, QString ip, int port) : 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->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->ip = ip;
|
this->ip = ip;
|
||||||
this->port = port;
|
this->port = port;
|
||||||
|
@ -41,7 +37,7 @@ bool BuzzerConn::connect()
|
||||||
|
|
||||||
setState("connecting");
|
setState("connecting");
|
||||||
|
|
||||||
//wait until the request has finished
|
//setup loop to wait until the connection has finished
|
||||||
QEventLoop loop;
|
QEventLoop loop;
|
||||||
QTimer timer;
|
QTimer timer;
|
||||||
|
|
||||||
|
@ -50,36 +46,37 @@ bool BuzzerConn::connect()
|
||||||
loop.connect(this->socket, SIGNAL(connected()), &loop, SLOT(quit()));
|
loop.connect(this->socket, SIGNAL(connected()), &loop, SLOT(quit()));
|
||||||
|
|
||||||
timer.start(3000);
|
timer.start(3000);
|
||||||
|
//connect
|
||||||
this->socket->connectToHost(this->ip, this->port);
|
this->socket->connectToHost(this->ip, this->port);
|
||||||
|
|
||||||
|
//wait for the connection to finish
|
||||||
loop.exec();
|
loop.exec();
|
||||||
timer.stop();
|
timer.stop();
|
||||||
|
|
||||||
if(timer.remainingTime() == 0){
|
if(timer.remainingTime() == 0){
|
||||||
//the time has been triggered -> timeout
|
//the time has been triggered -> timeout
|
||||||
|
setState("disconnected");
|
||||||
return(false);
|
return(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
QList<double> times = gettimes(2000);
|
QList<double> times = gettimes(2000, true);
|
||||||
qDebug() << times[0];
|
qDebug() << times;
|
||||||
if(times[0] == 200.0){
|
if(times[0] == 200.0){
|
||||||
this->latest_button_pressed = times[2];
|
this->latest_button_pressed = times[2];
|
||||||
for(int i=0;i<=100;i++){
|
for(int i=0;i<=100;i++){
|
||||||
this->connection_progress = i;
|
this->connection_progress = i;
|
||||||
emit this->progressChanged();
|
emit this->progressChanged();
|
||||||
if(!calcoffset(this->gettimes(1000))){
|
if(!calcoffset(this->gettimes(1000, false))){
|
||||||
this->connection_progress = 100;
|
this->connection_progress = 100;
|
||||||
this->connected = false;
|
|
||||||
setState("disconnected");
|
setState("disconnected");
|
||||||
return(false);
|
return(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this->connected = true;
|
|
||||||
setState("connected");
|
setState("connected");
|
||||||
return(true);
|
return(true);
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
this->connected = false;
|
|
||||||
setState("disconnected");
|
setState("disconnected");
|
||||||
return(false);
|
return(false);
|
||||||
}
|
}
|
||||||
|
@ -87,11 +84,11 @@ bool BuzzerConn::connect()
|
||||||
|
|
||||||
bool BuzzerConn::calcoffset(QList<double> times)
|
bool BuzzerConn::calcoffset(QList<double> times)
|
||||||
{
|
{
|
||||||
if(times.length() != 3){
|
if(times.length() != 2){
|
||||||
return(false);
|
return(false);
|
||||||
}
|
}
|
||||||
if(times[0] == 200.0){
|
if(times[0] == 200.0){
|
||||||
this->latest_button_pressed = times[2];
|
|
||||||
double offset = date->currentMSecsSinceEpoch() - times[1];
|
double offset = date->currentMSecsSinceEpoch() - times[1];
|
||||||
if(this->latest_offsets.length()>=100){
|
if(this->latest_offsets.length()>=100){
|
||||||
this->latest_offsets.removeFirst();
|
this->latest_offsets.removeFirst();
|
||||||
|
@ -113,40 +110,27 @@ bool BuzzerConn::calcoffset(QList<double> times)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<double> BuzzerConn::gettimes(int timeout)
|
QList<double> BuzzerConn::gettimes(int timeout, bool bothTimes)
|
||||||
{
|
{
|
||||||
// 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;
|
||||||
signed long ret;
|
signed long ret;
|
||||||
ret = this->sendCommand("GET_TIMESTAMP", timeout);
|
ret = this->sendCommand("GET_TIMESTAMP", timeout);
|
||||||
|
qDebug() << timeout << bothTimes;
|
||||||
if(ret >= 0){
|
if(ret >= 0){
|
||||||
|
|
||||||
times.append(double(200));
|
times.append(double(200));
|
||||||
times.append(double(ret));
|
times.append(double(ret));
|
||||||
|
|
||||||
|
if(bothTimes){
|
||||||
ret = this->sendCommand("GET_LASTPRESSED", timeout);
|
ret = this->sendCommand("GET_LASTPRESSED", timeout);
|
||||||
|
|
||||||
if(ret >= 0){
|
if(ret >= 0){
|
||||||
times.append(double(ret));
|
times.append(double(ret));
|
||||||
return(times);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
times[0] = ret;
|
times[0] = ret;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
times.append(ret);
|
times.append(ret);
|
||||||
|
@ -155,55 +139,6 @@ QList<double> BuzzerConn::gettimes(int timeout)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BuzzerConn::buzzer_triggered()
|
|
||||||
{
|
|
||||||
|
|
||||||
if(!this->connected){
|
|
||||||
return(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(pending_commands.length() > 0){
|
|
||||||
QString command = this->pending_commands.first();
|
|
||||||
|
|
||||||
signed long retval = this->sendCommand(command, 800);
|
|
||||||
if(retval > 0){
|
|
||||||
this->pending_commands.removeFirst();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
QList<double> times = this->gettimes(1000);
|
|
||||||
if(times[0] == 200.0){
|
|
||||||
if(times[2] > this->latest_button_pressed){
|
|
||||||
this->latest_button_pressed = times[2];
|
|
||||||
|
|
||||||
return(true);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
//this->connected = false;
|
|
||||||
return(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool BuzzerConn::start()
|
|
||||||
{
|
|
||||||
if(!this->connected){
|
|
||||||
return(false);
|
|
||||||
}
|
|
||||||
QList<double> times = this->gettimes(1000);
|
|
||||||
if(times[0] == 200.0 && this->connected){
|
|
||||||
this->latest_button_pressed = times[2];
|
|
||||||
return(true);
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
this->connected = false;
|
|
||||||
return(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
double BuzzerConn::get(QString key)
|
double BuzzerConn::get(QString key)
|
||||||
{
|
{
|
||||||
if(key == "offset"){
|
if(key == "offset"){
|
||||||
|
@ -219,7 +154,7 @@ double BuzzerConn::get(QString key)
|
||||||
return(this->connection_progress);
|
return(this->connection_progress);
|
||||||
}
|
}
|
||||||
else if( key == "connected") {
|
else if( key == "connected") {
|
||||||
if(this->connected){
|
if(this->state == "connected"){
|
||||||
return(1);
|
return(1);
|
||||||
}
|
}
|
||||||
return(0);
|
return(0);
|
||||||
|
@ -261,18 +196,13 @@ double BuzzerConn::getLastTriggered() const
|
||||||
return(this->latest_button_pressed);
|
return(this->latest_button_pressed);
|
||||||
}
|
}
|
||||||
|
|
||||||
QString BuzzerConn::test()
|
|
||||||
{
|
|
||||||
ReturnData_t ret = this->senddata(this->networkManager, QUrl("http://www.google.de"), 500);
|
|
||||||
return(ret.text);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool BuzzerConn::refresh()
|
bool BuzzerConn::refresh()
|
||||||
{
|
{
|
||||||
if(!this->connected){
|
if(this->state != "connected"){
|
||||||
return(false);
|
return(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//send one of the pending commands
|
||||||
if(pending_commands.length() > 0){
|
if(pending_commands.length() > 0){
|
||||||
QString command = this->pending_commands.first();
|
QString command = this->pending_commands.first();
|
||||||
|
|
||||||
|
@ -296,54 +226,13 @@ bool BuzzerConn::refresh()
|
||||||
this->errors ++;
|
this->errors ++;
|
||||||
if(this->errors > errors_until_disconnect){
|
if(this->errors > errors_until_disconnect){
|
||||||
this->socket->disconnectFromHost();
|
this->socket->disconnectFromHost();
|
||||||
this->connected = false;
|
this->setState("disconnected");
|
||||||
}
|
}
|
||||||
return(false);
|
return(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ReturnData_t BuzzerConn::senddata(QNetworkAccessManager * NetMan, QUrl serviceUrl, int timeout)
|
|
||||||
{
|
|
||||||
|
|
||||||
ReturnData_t ret; //this is a custom type to store the returned data
|
|
||||||
// Call the webservice
|
|
||||||
|
|
||||||
QNetworkRequest request(serviceUrl);
|
|
||||||
request.setHeader(QNetworkRequest::ContentTypeHeader,
|
|
||||||
"application/x-www-form-urlencoded");
|
|
||||||
|
|
||||||
//send a POST request with the given url and data to the server
|
|
||||||
QUrlQuery pdata;
|
|
||||||
QNetworkReply* reply;
|
|
||||||
|
|
||||||
//wait until the request has finished
|
|
||||||
QEventLoop loop;
|
|
||||||
QTimer timer;
|
|
||||||
|
|
||||||
timer.setSingleShot(true);
|
|
||||||
loop.connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
|
|
||||||
loop.connect(NetMan, SIGNAL(finished(QNetworkReply*)), SLOT(quit()));
|
|
||||||
|
|
||||||
timer.start(timeout);
|
|
||||||
reply = NetMan->post(request, pdata.toString(QUrl::FullyEncoded).toUtf8());
|
|
||||||
loop.exec();
|
|
||||||
timer.stop();
|
|
||||||
|
|
||||||
//get the status code
|
|
||||||
QVariant status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
|
|
||||||
|
|
||||||
ret.status_code = status_code.toInt();
|
|
||||||
if(ret.status_code == 0){ //if the statuscode is zero, the connecion to the server was not possible
|
|
||||||
ret.status_code = 444;
|
|
||||||
}
|
|
||||||
//get the full text response
|
|
||||||
ret.text = QString::fromUtf8(reply->readAll());
|
|
||||||
|
|
||||||
//return the data
|
|
||||||
return(ret);
|
|
||||||
}
|
|
||||||
|
|
||||||
signed long BuzzerConn::sendCommand(QString command, int timeout){
|
signed long BuzzerConn::sendCommand(QString command, int timeout){
|
||||||
|
|
||||||
//if there is any data in the storage, clear it
|
//if there is any data in the storage, clear it
|
||||||
|
@ -379,9 +268,9 @@ signed long BuzzerConn::sendCommand(QString command, int timeout){
|
||||||
return(-1);
|
return(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
//if the data is not 4 bites long if is invalid -> clear and terminate
|
//if the data is not 4 bytes long it is invalid -> clear and terminate
|
||||||
if(this->socket->bytesAvailable() != 4){
|
if(this->socket->bytesAvailable() != 4){
|
||||||
this->socket->readAll();
|
qDebug() << this->socket->readAll();
|
||||||
return(-2);
|
return(-2);
|
||||||
}
|
}
|
||||||
long data = 0;
|
long data = 0;
|
||||||
|
|
Reference in a new issue