From c2071a805911d5ccb66a956cd34dc95cf2b713c7 Mon Sep 17 00:00:00 2001 From: Dorian Zedler Date: Thu, 26 Jul 2018 14:54:11 +0200 Subject: [PATCH] added buzzer connectivity --- SettingsDialog.qml | 29 +++------------ buzzerconn.cpp | 91 +++++++++++++++++++++++++++++++++++++++------- buzzerconn.h | 16 ++++++-- main.qml | 35 ++++++++++-------- 4 files changed, 113 insertions(+), 58 deletions(-) diff --git a/SettingsDialog.qml b/SettingsDialog.qml index 8a60145..630dae7 100644 --- a/SettingsDialog.qml +++ b/SettingsDialog.qml @@ -59,31 +59,12 @@ Dialog { anchors.fill: parent text: "connect" onClicked: { - var i; - if(i === 100){ - i = 0 - running = false - } +// connect_del.enabled = false +// connect_del.text = _cppBuzzerConn.get("offset") +// _cppBuzzerConn.connect() +// connect_del.enabled = true + connect_del.text = _cppBuzzerConn.test() - var theUrl = "http://192.168.4.1"; - var xmlHttp = null; - - xmlHttp = new XMLHttpRequest(); - xmlHttp.open( "GET", theUrl, false ); - xmlHttp.setRequestHeader("Content-type", "text/plain;charset=UTF-8"); - xmlHttp.send( null ); - var response = xmlHttp.responseText - var response_status = xmlHttp.status - connect_del.text = response - var res = response.split("
"); - var time_now = new Date().getTime() - console.log(time_now) - console.log(res[1]) - console.log(time_now-res[0]) - root.buzzer_offset = time_now-res[0] - root.last_button_pressed = res[1] - console.log("buzzer_offset: "+root.buzzer_offset) - connect_del.text = response } } } diff --git a/buzzerconn.cpp b/buzzerconn.cpp index e95acf2..66d7383 100644 --- a/buzzerconn.cpp +++ b/buzzerconn.cpp @@ -3,7 +3,9 @@ BuzzerConn::BuzzerConn(QObject *parent) : QObject(parent) { this->networkManager = new QNetworkAccessManager(); - connect(); + this->date = new QDateTime; + this->latest_button_pressed = 0; + this->connected = false; } bool BuzzerConn::connect() @@ -12,9 +14,10 @@ bool BuzzerConn::connect() if(times[0] == 200.0){ this->connected = true; - calcoffset(); - calcoffset(); - calcoffset(); + this->latest_button_pressed = times[2]; + for(int i=0;i<100;i++){ + calcoffset(); + } return(true); } else{ @@ -25,15 +28,24 @@ bool BuzzerConn::connect() void BuzzerConn::calcoffset() { QList times = gettimes(); - QDateTime * date = new QDateTime; - double offset = date->currentMSecsSinceEpoch() - times[2]; - this->latest_offsets.append(offset); - double mem = 0; - for(int i=0;icurrentMSecsSinceEpoch() - times[2]; + if(this->latest_offsets.length()>=100){ + this->latest_offsets.removeFirst(); + } + this->latest_offsets.append(offset); + + double mem = 0; + for(int i=0;ioffset = mem / double(latest_offsets.length()); + this->offset = latest_offsets[latest_offsets.length()-1]; + qDebug() << latest_offsets.length(); + qDebug() << this->latest_offsets; + qDebug() << latest_offsets[latest_offsets.length()-1]; + qDebug() << mem / double(latest_offsets.length()); } - this->offset = mem / double(latest_offsets.length()); } QList BuzzerConn::gettimes() @@ -43,7 +55,6 @@ QList BuzzerConn::gettimes() times.append(double(ret.status_code)); if(ret.status_code == 200){ - qDebug()<"); @@ -57,6 +68,59 @@ QList BuzzerConn::gettimes() } } +bool BuzzerConn::buzzer_triggered() +{ + QList times = this->gettimes(); + qDebug() << int(this->get("currtime") - this->starttime); + if(times[0] == 200.0 && this->connected){ + if(times[2] > this->latest_button_pressed){ + this->latest_button_pressed = times[2]; + + return(true); + } + else { + return(false); + } + } + else{ + return(false); + } +} + +bool BuzzerConn::start() +{ + if(!this->connected){ + return(false); + } + QList times = this->gettimes(); + if(times[0] == 200.0 && this->connected){ + this->latest_button_pressed = times[2]; + return(true); + } + else{ + return(false); + } +} + +double BuzzerConn::get(QString key) +{ + if(key == "offset"){ + return(this->offset); + } + else if (key == "lastpressed") { + return(this->latest_button_pressed); + } + else if( key == "currtime") { + return(this->date->currentMSecsSinceEpoch()); + } +} + +QString BuzzerConn::test() +{ + ReturnData_t ret = this->senddata(QUrl("http://www.google.de")); + return(ret.text); +} + ReturnData_t BuzzerConn::senddata(QUrl serviceUrl) { @@ -67,7 +131,6 @@ ReturnData_t BuzzerConn::senddata(QUrl serviceUrl) request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded"); - //set ssl configuration //send a POST request with the given url and data to the server QUrlQuery pdata; QNetworkReply* reply; diff --git a/buzzerconn.h b/buzzerconn.h index 1bb22e0..356bb90 100644 --- a/buzzerconn.h +++ b/buzzerconn.h @@ -20,20 +20,28 @@ class BuzzerConn : public QObject Q_OBJECT public: explicit BuzzerConn(QObject *parent = nullptr); - double offset; + Q_INVOKABLE double offset; QList latest_offsets; + double latest_button_pressed; + double starttime; bool connected; + private: QNetworkAccessManager *networkManager; + QDateTime *date; signals: public slots: ReturnData_t senddata(QUrl serviceUrl); - QList gettimes(); - bool connect(); - void calcoffset(); + Q_INVOKABLE QList gettimes(); + Q_INVOKABLE bool connect(); + Q_INVOKABLE void calcoffset(); + Q_INVOKABLE bool buzzer_triggered(); + Q_INVOKABLE bool start(); + Q_INVOKABLE double get(QString key); + Q_INVOKABLE QString test(); }; #endif // BUZZERCONN_H diff --git a/main.qml b/main.qml index 11cf8cc..9eb2201 100644 --- a/main.qml +++ b/main.qml @@ -21,7 +21,7 @@ Window { property double startTime: 0 property double stoppedTime: 0 - property double currTime: new Date().getTime() + property double currTime property double buzzer_offset property double last_button_pressed @@ -43,26 +43,28 @@ Window { Timer { id: running_refresh_timer running: root.state === "RUNNING" - repeat: true + repeat: false interval: 1 onTriggered: { - var theUrl = "http://192.168.4.1"; - var xmlHttp = null; + //root.stoppedTime = (root.last_button_pressed + root.buzzer_offset) - root.startTime + //console.log("STOPPED: "+root.stoppedTime) + //time.text = ( root.stoppedTime / 1000 ).toFixed(3) + " sec" + if(_cppBuzzerConn.buzzer_triggered()){ - xmlHttp = new XMLHttpRequest(); - xmlHttp.open( "GET", theUrl, false ); - xmlHttp.send( null ); - var response = xmlHttp.responseText - var res = response.split("
"); - startButt.text = res[0] - if(res[1]>root.last_button_pressed){ - root.last_button_pressed = res[1] - root.stoppedTime = (root.last_button_pressed + root.buzzer_offset) - root.startTime - console.log("STOPPED: "+root.stoppedTime) + root.buzzer_offset = _cppBuzzerConn.get("offset") + root.last_button_pressed = _cppBuzzerConn.get("lastpressed") + console.log(root.startTime); + console.log(root.buzzer_offset); + //root.stoppedTime = ( ( (root.last_button_pressed + root.buzzer_offset) - root.startTime ) / 2 ) / 10 + root.stoppedTime = new Date().getTime() - root.startTime time.text = ( root.stoppedTime / 1000 ).toFixed(3) + " sec" + console.log("STOPPED: "+root.stoppedTime) root.state = "STOPPED" } + if(root.state === "RUNNING"){ + running_refresh_timer.start() + } } } @@ -73,8 +75,9 @@ Window { onPlayingChanged: { if(!playing){ - root.startTime = new Date().getTime() - root.currTime = new Date().getTime() + root.startTime = _cppBuzzerConn.get("currtime") + _cppBuzzerConn.start() + root.currTime = _cppBuzzerConn.get("currtime") time.text = ( ( root.currTime - root.startTime ) / 1000 ).toFixed(3) + " sec" root.state = "RUNNING" }