added buzzer connectivity

This commit is contained in:
Dorian Zedler 2018-07-26 14:54:11 +02:00
parent 9593dd9a4c
commit c2071a8059
4 changed files with 113 additions and 58 deletions

View file

@ -59,31 +59,12 @@ Dialog {
anchors.fill: parent anchors.fill: parent
text: "connect" text: "connect"
onClicked: { onClicked: {
var i; // connect_del.enabled = false
if(i === 100){ // connect_del.text = _cppBuzzerConn.get("offset")
i = 0 // _cppBuzzerConn.connect()
running = false // 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("<br>");
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
} }
} }
} }

View file

@ -3,7 +3,9 @@
BuzzerConn::BuzzerConn(QObject *parent) : QObject(parent) BuzzerConn::BuzzerConn(QObject *parent) : QObject(parent)
{ {
this->networkManager = new QNetworkAccessManager(); this->networkManager = new QNetworkAccessManager();
connect(); this->date = new QDateTime;
this->latest_button_pressed = 0;
this->connected = false;
} }
bool BuzzerConn::connect() bool BuzzerConn::connect()
@ -12,9 +14,10 @@ bool BuzzerConn::connect()
if(times[0] == 200.0){ if(times[0] == 200.0){
this->connected = true; this->connected = true;
this->latest_button_pressed = times[2];
for(int i=0;i<100;i++){
calcoffset(); calcoffset();
calcoffset(); }
calcoffset();
return(true); return(true);
} }
else{ else{
@ -25,15 +28,24 @@ bool BuzzerConn::connect()
void BuzzerConn::calcoffset() void BuzzerConn::calcoffset()
{ {
QList<double> times = gettimes(); QList<double> times = gettimes();
QDateTime * date = new QDateTime; if(times[0] == 200.0){
double offset = date->currentMSecsSinceEpoch() - times[2]; double offset = date->currentMSecsSinceEpoch() - times[2];
if(this->latest_offsets.length()>=100){
this->latest_offsets.removeFirst();
}
this->latest_offsets.append(offset); this->latest_offsets.append(offset);
double mem = 0; double mem = 0;
for(int i=0;i<latest_offsets.length();i++){ for(int i=0;i<latest_offsets.length();i++){
qDebug()<<latest_offsets[i];
mem += latest_offsets[i]; mem += latest_offsets[i];
} }
this->offset = mem / double(latest_offsets.length()); //this->offset = 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());
}
} }
QList<double> BuzzerConn::gettimes() QList<double> BuzzerConn::gettimes()
@ -43,7 +55,6 @@ QList<double> BuzzerConn::gettimes()
times.append(double(ret.status_code)); times.append(double(ret.status_code));
if(ret.status_code == 200){ if(ret.status_code == 200){
qDebug()<<ret.text;
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>");
@ -57,6 +68,59 @@ QList<double> BuzzerConn::gettimes()
} }
} }
bool BuzzerConn::buzzer_triggered()
{
QList<double> 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<double> 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) ReturnData_t BuzzerConn::senddata(QUrl serviceUrl)
{ {
@ -67,7 +131,6 @@ ReturnData_t BuzzerConn::senddata(QUrl serviceUrl)
request.setHeader(QNetworkRequest::ContentTypeHeader, request.setHeader(QNetworkRequest::ContentTypeHeader,
"application/x-www-form-urlencoded"); "application/x-www-form-urlencoded");
//set ssl configuration
//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;
QNetworkReply* reply; QNetworkReply* reply;

View file

@ -20,20 +20,28 @@ class BuzzerConn : public QObject
Q_OBJECT Q_OBJECT
public: public:
explicit BuzzerConn(QObject *parent = nullptr); explicit BuzzerConn(QObject *parent = nullptr);
double offset; Q_INVOKABLE double offset;
QList<double> latest_offsets; QList<double> latest_offsets;
double latest_button_pressed;
double starttime;
bool connected; bool connected;
private: private:
QNetworkAccessManager *networkManager; QNetworkAccessManager *networkManager;
QDateTime *date;
signals: signals:
public slots: public slots:
ReturnData_t senddata(QUrl serviceUrl); ReturnData_t senddata(QUrl serviceUrl);
QList<double> gettimes(); Q_INVOKABLE QList<double> gettimes();
bool connect(); Q_INVOKABLE bool connect();
void calcoffset(); 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 #endif // BUZZERCONN_H

View file

@ -21,7 +21,7 @@ Window {
property double startTime: 0 property double startTime: 0
property double stoppedTime: 0 property double stoppedTime: 0
property double currTime: new Date().getTime() property double currTime
property double buzzer_offset property double buzzer_offset
property double last_button_pressed property double last_button_pressed
@ -43,26 +43,28 @@ Window {
Timer { Timer {
id: running_refresh_timer id: running_refresh_timer
running: root.state === "RUNNING" running: root.state === "RUNNING"
repeat: true repeat: false
interval: 1 interval: 1
onTriggered: { onTriggered: {
var theUrl = "http://192.168.4.1"; //root.stoppedTime = (root.last_button_pressed + root.buzzer_offset) - root.startTime
var xmlHttp = null; //console.log("STOPPED: "+root.stoppedTime)
//time.text = ( root.stoppedTime / 1000 ).toFixed(3) + " sec"
if(_cppBuzzerConn.buzzer_triggered()){
xmlHttp = new XMLHttpRequest(); root.buzzer_offset = _cppBuzzerConn.get("offset")
xmlHttp.open( "GET", theUrl, false ); root.last_button_pressed = _cppBuzzerConn.get("lastpressed")
xmlHttp.send( null ); console.log(root.startTime);
var response = xmlHttp.responseText console.log(root.buzzer_offset);
var res = response.split("<br>"); //root.stoppedTime = ( ( (root.last_button_pressed + root.buzzer_offset) - root.startTime ) / 2 ) / 10
startButt.text = res[0] root.stoppedTime = new Date().getTime() - root.startTime
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)
time.text = ( root.stoppedTime / 1000 ).toFixed(3) + " sec" time.text = ( root.stoppedTime / 1000 ).toFixed(3) + " sec"
console.log("STOPPED: "+root.stoppedTime)
root.state = "STOPPED" root.state = "STOPPED"
} }
if(root.state === "RUNNING"){
running_refresh_timer.start()
}
} }
} }
@ -73,8 +75,9 @@ Window {
onPlayingChanged: { onPlayingChanged: {
if(!playing){ if(!playing){
root.startTime = new Date().getTime() root.startTime = _cppBuzzerConn.get("currtime")
root.currTime = new Date().getTime() _cppBuzzerConn.start()
root.currTime = _cppBuzzerConn.get("currtime")
time.text = ( ( root.currTime - root.startTime ) / 1000 ).toFixed(3) + " sec" time.text = ( ( root.currTime - root.startTime ) / 1000 ).toFixed(3) + " sec"
root.state = "RUNNING" root.state = "RUNNING"
} }