#include "buzzerconn.h" BuzzerConn::BuzzerConn(QObject *parent) : QObject(parent) { this->networkManager = new QNetworkAccessManager(); this->date = new QDateTime; this->latest_button_pressed = 0; this->connected = false; } bool BuzzerConn::connect() { QList times = gettimes(); if(times[0] == 200.0){ this->connected = true; this->latest_button_pressed = times[2]; for(int i=0;i<100;i++){ calcoffset(); } return(true); } else{ return(false); } } void BuzzerConn::calcoffset() { QList times = gettimes(); if(times[0] == 200.0){ double offset = date->currentMSecsSinceEpoch() - 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()); } } QList BuzzerConn::gettimes() { QList times; ReturnData_t ret = senddata(QUrl("http://192.168.4.1")); 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("
"); times.append(times_cache[0].toDouble()); times.append(times_cache[1].toDouble()); return(times); } else{ return(times); } } 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) { 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; reply = this->networkManager->post(request, pdata.toString(QUrl::FullyEncoded).toUtf8()); //wait until the request has finished QEventLoop loop; loop.connect(this->networkManager, SIGNAL(finished(QNetworkReply*)), SLOT(quit())); loop.exec(); //get the status code QVariant status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute); ret.status_code = status_code.toInt(); if(ret.status_code == 0){ //if tehstatus code 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); }