2018-08-12 20:51:57 +02:00
|
|
|
/*
|
|
|
|
Speed Climbing Stopwatch - Simple Stopwatch for Climbers
|
|
|
|
Copyright (C) 2018 Itsblue Development - Dorian Zeder
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published
|
|
|
|
by the Free Software Foundation, version 3 of the License.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2018-07-28 23:05:26 +02:00
|
|
|
#include "headers/buzzerconn.h"
|
2018-07-25 00:32:20 +02:00
|
|
|
|
2018-08-30 16:44:04 +02:00
|
|
|
BuzzerConn::BuzzerConn(QObject *parent, QString ip, int port) : QObject(parent)
|
2018-07-25 00:32:20 +02:00
|
|
|
{
|
|
|
|
this->networkManager = new QNetworkAccessManager();
|
2018-08-02 18:10:08 +02:00
|
|
|
this->reloadNetworkManager = new QNetworkAccessManager();
|
2018-07-26 20:51:46 +02:00
|
|
|
|
2018-08-30 16:44:04 +02:00
|
|
|
this->socket = new QTcpSocket();
|
|
|
|
|
2018-07-26 14:54:11 +02:00
|
|
|
this->date = new QDateTime;
|
|
|
|
this->latest_button_pressed = 0;
|
|
|
|
this->connected = false;
|
2018-08-14 17:07:42 +02:00
|
|
|
|
2018-08-30 16:44:04 +02:00
|
|
|
this->ip = ip;
|
|
|
|
this->port = port;
|
2018-08-14 17:07:42 +02:00
|
|
|
// "http://192.168.4.1"
|
2018-07-25 00:32:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool BuzzerConn::connect()
|
|
|
|
{
|
2018-08-28 23:03:35 +02:00
|
|
|
qDebug() << "connecting...";
|
2018-08-30 16:44:04 +02:00
|
|
|
|
|
|
|
//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();
|
|
|
|
|
2018-08-30 17:49:24 +02:00
|
|
|
if(timer.remainingTime() == 0){
|
|
|
|
//the time has been triggered -> timeout
|
|
|
|
return(false);
|
|
|
|
}
|
|
|
|
|
2018-08-30 16:44:04 +02:00
|
|
|
|
2018-09-09 20:18:00 +02:00
|
|
|
QList<double> times = gettimes(2000);
|
2018-07-26 20:51:46 +02:00
|
|
|
qDebug() << times[0];
|
2018-07-25 00:32:20 +02:00
|
|
|
if(times[0] == 200.0){
|
2018-07-26 14:54:11 +02:00
|
|
|
this->latest_button_pressed = times[2];
|
2018-07-26 22:13:43 +02:00
|
|
|
for(int i=0;i<=100;i++){
|
2018-07-26 20:51:46 +02:00
|
|
|
this->connection_progress = i;
|
2018-08-02 18:10:08 +02:00
|
|
|
if(!calcoffset(this->gettimes(1000))){
|
2018-07-26 22:13:43 +02:00
|
|
|
this->connection_progress = 100;
|
2018-08-02 18:10:08 +02:00
|
|
|
this->connected = false;
|
2018-07-26 20:51:46 +02:00
|
|
|
return(false);
|
|
|
|
}
|
2018-07-26 14:54:11 +02:00
|
|
|
}
|
2018-08-02 18:10:08 +02:00
|
|
|
this->connected = true;
|
2018-07-25 00:32:20 +02:00
|
|
|
return(true);
|
|
|
|
}
|
|
|
|
else{
|
2018-08-02 18:10:08 +02:00
|
|
|
this->connected = false;
|
2018-07-25 00:32:20 +02:00
|
|
|
return(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-02 18:10:08 +02:00
|
|
|
bool BuzzerConn::calcoffset(QList<double> times)
|
2018-07-25 00:32:20 +02:00
|
|
|
{
|
2018-07-26 22:13:43 +02:00
|
|
|
if(times.length() != 3){
|
|
|
|
return(false);
|
|
|
|
}
|
2018-08-02 18:10:08 +02:00
|
|
|
if(times[0] == 200.0){
|
|
|
|
this->latest_button_pressed = times[2];
|
2018-07-26 20:51:46 +02:00
|
|
|
double offset = date->currentMSecsSinceEpoch() - times[1];
|
2018-07-26 14:54:11 +02:00
|
|
|
if(this->latest_offsets.length()>=100){
|
|
|
|
this->latest_offsets.removeFirst();
|
|
|
|
}
|
|
|
|
this->latest_offsets.append(offset);
|
|
|
|
|
|
|
|
double mem = 0;
|
|
|
|
for(int i=0;i<latest_offsets.length();i++){
|
|
|
|
mem += latest_offsets[i];
|
|
|
|
}
|
2018-07-26 22:13:43 +02:00
|
|
|
this->offset = mem / double(latest_offsets.length());
|
2018-07-26 20:51:46 +02:00
|
|
|
qDebug("%20f", this->offset);
|
|
|
|
return(true);
|
|
|
|
}
|
|
|
|
else {
|
2018-08-02 18:10:08 +02:00
|
|
|
//this->connected = false;
|
2018-07-26 20:51:46 +02:00
|
|
|
return(false);
|
2018-07-25 00:32:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-02 12:50:55 +02:00
|
|
|
QList<double> BuzzerConn::gettimes(int timeout)
|
2018-07-25 00:32:20 +02:00
|
|
|
{
|
2018-08-30 16:44:04 +02:00
|
|
|
// 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());
|
2018-07-25 00:32:20 +02:00
|
|
|
|
2018-08-30 16:44:04 +02:00
|
|
|
// return(times);
|
|
|
|
// }
|
|
|
|
// else{
|
|
|
|
// return(times);
|
|
|
|
// }
|
2018-07-25 00:32:20 +02:00
|
|
|
|
2018-08-30 16:44:04 +02:00
|
|
|
QList<double> times;
|
2018-08-30 17:49:24 +02:00
|
|
|
signed long ret;
|
|
|
|
ret = this->sendCommand("GET_TIMESTAMP", timeout);
|
|
|
|
if(ret >= 0){
|
2018-08-30 16:44:04 +02:00
|
|
|
times.append(double(200));
|
|
|
|
times.append(double(ret));
|
2018-08-30 17:49:24 +02:00
|
|
|
ret = this->sendCommand("GET_LASTPRESSED", timeout);
|
|
|
|
if(ret >= 0){
|
|
|
|
times.append(double(ret));
|
|
|
|
return(times);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
times[0] = ret;
|
|
|
|
}
|
|
|
|
|
2018-07-25 00:32:20 +02:00
|
|
|
}
|
2018-08-30 16:44:04 +02:00
|
|
|
else {
|
2018-08-30 17:49:24 +02:00
|
|
|
times.append(ret);
|
2018-07-25 00:32:20 +02:00
|
|
|
}
|
2018-08-30 17:49:24 +02:00
|
|
|
return(times);
|
2018-08-30 16:44:04 +02:00
|
|
|
|
2018-07-25 00:32:20 +02:00
|
|
|
}
|
|
|
|
|
2018-07-26 14:54:11 +02:00
|
|
|
bool BuzzerConn::buzzer_triggered()
|
|
|
|
{
|
2018-07-26 20:51:46 +02:00
|
|
|
|
|
|
|
if(!this->connected){
|
|
|
|
return(false);
|
|
|
|
}
|
2018-09-09 20:18:00 +02:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-02 12:50:55 +02:00
|
|
|
QList<double> times = this->gettimes(1000);
|
2018-07-26 20:51:46 +02:00
|
|
|
if(times[0] == 200.0){
|
2018-07-26 14:54:11 +02:00
|
|
|
if(times[2] > this->latest_button_pressed){
|
|
|
|
this->latest_button_pressed = times[2];
|
|
|
|
|
|
|
|
return(true);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
2018-08-02 12:50:55 +02:00
|
|
|
//this->connected = false;
|
2018-07-26 14:54:11 +02:00
|
|
|
return(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BuzzerConn::start()
|
|
|
|
{
|
|
|
|
if(!this->connected){
|
|
|
|
return(false);
|
|
|
|
}
|
2018-08-02 12:50:55 +02:00
|
|
|
QList<double> times = this->gettimes(1000);
|
2018-07-26 14:54:11 +02:00
|
|
|
if(times[0] == 200.0 && this->connected){
|
|
|
|
this->latest_button_pressed = times[2];
|
|
|
|
return(true);
|
|
|
|
}
|
|
|
|
else{
|
2018-07-26 20:51:46 +02:00
|
|
|
this->connected = false;
|
2018-07-26 14:54:11 +02:00
|
|
|
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());
|
|
|
|
}
|
2018-07-26 20:51:46 +02:00
|
|
|
else if( key == "connection_progress") {
|
|
|
|
return(this->connection_progress);
|
|
|
|
}
|
|
|
|
else if( key == "connected") {
|
|
|
|
if(this->connected){
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
return(0);
|
|
|
|
}
|
2018-07-26 14:54:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QString BuzzerConn::test()
|
|
|
|
{
|
2018-08-02 18:10:08 +02:00
|
|
|
ReturnData_t ret = this->senddata(this->networkManager, QUrl("http://www.google.de"), 500);
|
2018-07-26 14:54:11 +02:00
|
|
|
return(ret.text);
|
|
|
|
}
|
|
|
|
|
2018-08-02 18:10:08 +02:00
|
|
|
bool BuzzerConn::refresh()
|
|
|
|
{
|
|
|
|
if(!this->connected){
|
|
|
|
return(false);
|
|
|
|
}
|
2018-08-30 16:44:04 +02:00
|
|
|
// 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);
|
|
|
|
// }
|
2018-09-09 20:18:00 +02:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//refresh the times
|
2018-08-30 17:49:24 +02:00
|
|
|
QList<double> ret = this->gettimes(800);
|
|
|
|
if(ret[0] >= 0){
|
|
|
|
this->errors = 0;
|
|
|
|
return(this->calcoffset(ret));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this->errors ++;
|
|
|
|
if(this->errors > errors_until_disconnect){
|
|
|
|
this->socket->disconnectFromHost();
|
|
|
|
this->connected = false;
|
|
|
|
}
|
|
|
|
return(false);
|
|
|
|
}
|
2018-08-30 16:44:04 +02:00
|
|
|
|
2018-08-02 18:10:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ReturnData_t BuzzerConn::senddata(QNetworkAccessManager * NetMan, QUrl serviceUrl, int timeout)
|
2018-07-25 00:32:20 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
ReturnData_t ret; //this is a custom type to store the returned data
|
|
|
|
// Call the webservice
|
|
|
|
|
|
|
|
QNetworkRequest request(serviceUrl);
|
|
|
|
request.setHeader(QNetworkRequest::ContentTypeHeader,
|
2018-08-30 16:44:04 +02:00
|
|
|
"application/x-www-form-urlencoded");
|
2018-07-25 00:32:20 +02:00
|
|
|
|
|
|
|
//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;
|
2018-07-26 20:51:46 +02:00
|
|
|
QTimer timer;
|
|
|
|
|
|
|
|
timer.setSingleShot(true);
|
|
|
|
loop.connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
|
2018-08-02 18:10:08 +02:00
|
|
|
loop.connect(NetMan, SIGNAL(finished(QNetworkReply*)), SLOT(quit()));
|
2018-07-26 20:51:46 +02:00
|
|
|
|
2018-08-02 12:50:55 +02:00
|
|
|
timer.start(timeout);
|
2018-08-02 18:10:08 +02:00
|
|
|
reply = NetMan->post(request, pdata.toString(QUrl::FullyEncoded).toUtf8());
|
2018-07-25 00:32:20 +02:00
|
|
|
loop.exec();
|
2018-07-26 20:51:46 +02:00
|
|
|
timer.stop();
|
2018-07-25 00:32:20 +02:00
|
|
|
|
|
|
|
//get the status code
|
|
|
|
QVariant status_code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
|
|
|
|
|
|
|
|
ret.status_code = status_code.toInt();
|
2018-07-26 20:51:46 +02:00
|
|
|
if(ret.status_code == 0){ //if the statuscode is zero, the connecion to the server was not possible
|
2018-07-25 00:32:20 +02:00
|
|
|
ret.status_code = 444;
|
|
|
|
}
|
|
|
|
//get the full text response
|
|
|
|
ret.text = QString::fromUtf8(reply->readAll());
|
|
|
|
|
|
|
|
//return the data
|
|
|
|
return(ret);
|
|
|
|
}
|
2018-08-30 16:44:04 +02:00
|
|
|
|
2018-08-30 17:49:24 +02:00
|
|
|
signed long BuzzerConn::sendCommand(QString command, int timeout){
|
|
|
|
|
|
|
|
//if there is any data in the storage, clear it
|
|
|
|
if(this->socket->bytesAvailable() > 0){
|
|
|
|
this->socket->readAll();
|
|
|
|
}
|
|
|
|
|
2018-08-30 16:44:04 +02:00
|
|
|
//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()));
|
2018-08-30 17:49:24 +02:00
|
|
|
timer.start(timeout);
|
2018-08-30 16:44:04 +02:00
|
|
|
loop.exec();
|
|
|
|
|
|
|
|
//loop finished
|
|
|
|
timer.stop();
|
2018-08-30 17:49:24 +02:00
|
|
|
if(timer.remainingTime() == 0){
|
|
|
|
//the time has been triggered -> timeout
|
|
|
|
return(-1);
|
|
|
|
}
|
2018-08-30 16:44:04 +02:00
|
|
|
|
2018-08-30 17:49:24 +02:00
|
|
|
//if the data is not 4 bites long if is invalid -> clear and terminate
|
2018-08-30 16:44:04 +02:00
|
|
|
if(this->socket->bytesAvailable() != 4){
|
|
|
|
this->socket->readAll();
|
2018-08-30 17:49:24 +02:00
|
|
|
return(-2);
|
2018-08-30 16:44:04 +02:00
|
|
|
}
|
2018-08-30 17:49:24 +02:00
|
|
|
long data = 0;
|
2018-08-30 16:44:04 +02:00
|
|
|
this->socket->read((char*)&data,4);
|
|
|
|
|
|
|
|
qDebug() << data;
|
|
|
|
qDebug() << this->socket->bytesAvailable();
|
2018-08-30 17:49:24 +02:00
|
|
|
|
2018-08-30 16:44:04 +02:00
|
|
|
return data;
|
|
|
|
}
|
2018-09-09 20:18:00 +02:00
|
|
|
|
|
|
|
void BuzzerConn::appendCommand(QString command){
|
|
|
|
this->pending_commands.append(command);
|
|
|
|
}
|