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
|
|
|
{
|
2018-09-23 17:54:20 +02:00
|
|
|
// initialize TcpSocket for communication with the extentions
|
2018-08-30 16:44:04 +02:00
|
|
|
this->socket = new QTcpSocket();
|
|
|
|
|
2018-09-23 17:54:20 +02:00
|
|
|
// initialize Qdate
|
2018-07-26 14:54:11 +02:00
|
|
|
this->date = new QDateTime;
|
|
|
|
this->latest_button_pressed = 0;
|
2018-08-14 17:07:42 +02:00
|
|
|
|
2018-09-23 17:54:20 +02:00
|
|
|
// set ip and port
|
2018-08-30 16:44:04 +02:00
|
|
|
this->ip = ip;
|
|
|
|
this->port = port;
|
2018-09-17 23:06:34 +02:00
|
|
|
|
2018-09-23 17:54:20 +02:00
|
|
|
//standard state: disconnected
|
2018-09-17 23:06:34 +02:00
|
|
|
this->setState("disconnected");
|
2018-07-25 00:32:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool BuzzerConn::connect()
|
|
|
|
{
|
2018-09-23 17:54:20 +02:00
|
|
|
// function to connect to buzzer
|
|
|
|
|
2018-08-28 23:03:35 +02:00
|
|
|
qDebug() << "connecting...";
|
2018-08-30 16:44:04 +02:00
|
|
|
|
2018-09-17 23:06:34 +02:00
|
|
|
setState("connecting");
|
|
|
|
|
2018-09-18 17:11:47 +02:00
|
|
|
//setup loop to wait until the connection has finished
|
2018-08-30 16:44:04 +02:00
|
|
|
QEventLoop loop;
|
|
|
|
QTimer timer;
|
|
|
|
|
|
|
|
timer.setSingleShot(true);
|
2018-09-23 17:54:20 +02:00
|
|
|
// quit the loop when the timer times out
|
2018-08-30 16:44:04 +02:00
|
|
|
loop.connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
|
2018-09-23 17:54:20 +02:00
|
|
|
//quit the loop when the connection was established
|
2018-08-30 16:44:04 +02:00
|
|
|
loop.connect(this->socket, SIGNAL(connected()), &loop, SLOT(quit()));
|
2018-09-23 17:54:20 +02:00
|
|
|
// start the timer before starting to connect
|
2018-08-30 16:44:04 +02:00
|
|
|
timer.start(3000);
|
2018-09-18 17:11:47 +02:00
|
|
|
//connect
|
2018-08-30 16:44:04 +02:00
|
|
|
this->socket->connectToHost(this->ip, this->port);
|
2018-09-18 17:11:47 +02:00
|
|
|
|
2018-09-23 17:54:20 +02:00
|
|
|
//wait for the connection to finish (programm gets stuck in here)
|
2018-08-30 16:44:04 +02:00
|
|
|
loop.exec();
|
|
|
|
|
2018-09-18 17:36:11 +02:00
|
|
|
//loop finished
|
|
|
|
if(timer.remainingTime() == -1){
|
2018-08-30 17:49:24 +02:00
|
|
|
//the time has been triggered -> timeout
|
2018-09-18 17:11:47 +02:00
|
|
|
setState("disconnected");
|
2018-08-30 17:49:24 +02:00
|
|
|
return(false);
|
|
|
|
}
|
|
|
|
|
2018-09-23 17:54:20 +02:00
|
|
|
// stop the timer as the connection has been established
|
|
|
|
timer.stop();
|
2018-08-30 16:44:04 +02:00
|
|
|
|
2018-09-23 17:54:20 +02:00
|
|
|
// get the timestamps ( last_pressed and current_timestamp ) from the extention
|
2018-09-18 17:11:47 +02:00
|
|
|
QList<double> times = gettimes(2000, true);
|
2018-09-23 17:54:20 +02:00
|
|
|
|
2018-07-25 00:32:20 +02:00
|
|
|
if(times[0] == 200.0){
|
2018-09-23 17:54:20 +02:00
|
|
|
//if the request was successfull, set last_triggered and
|
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-09-23 17:54:20 +02:00
|
|
|
// middle the offset 100 times
|
2018-07-26 20:51:46 +02:00
|
|
|
this->connection_progress = i;
|
2018-09-17 23:06:34 +02:00
|
|
|
emit this->progressChanged();
|
2018-09-18 17:11:47 +02:00
|
|
|
if(!calcoffset(this->gettimes(1000, false))){
|
2018-09-23 17:54:20 +02:00
|
|
|
// if a request fails, cancle the connection process
|
2018-07-26 22:13:43 +02:00
|
|
|
this->connection_progress = 100;
|
2018-09-17 23:06:34 +02:00
|
|
|
setState("disconnected");
|
2018-07-26 20:51:46 +02:00
|
|
|
return(false);
|
|
|
|
}
|
2018-07-26 14:54:11 +02:00
|
|
|
}
|
2018-09-23 17:54:20 +02:00
|
|
|
// after middeling the offset for 100 times set the state to connected and quit
|
2018-09-17 23:06:34 +02:00
|
|
|
setState("connected");
|
2018-07-25 00:32:20 +02:00
|
|
|
return(true);
|
|
|
|
}
|
|
|
|
else{
|
2018-09-23 17:54:20 +02:00
|
|
|
//if not, cancel
|
2018-09-17 23:06:34 +02:00
|
|
|
setState("disconnected");
|
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-09-23 17:54:20 +02:00
|
|
|
//function that recieves the current time of the extention,
|
|
|
|
//puts it into the latest offsets list and
|
|
|
|
//calculates the avarage offset
|
|
|
|
|
|
|
|
//if there are not enoug items in the list (0=status of the request (200 = success ...); 1 = timestamp that has to be added to the list)
|
2018-09-18 17:11:47 +02:00
|
|
|
if(times.length() != 2){
|
2018-07-26 22:13:43 +02:00
|
|
|
return(false);
|
|
|
|
}
|
2018-09-18 17:11:47 +02:00
|
|
|
|
2018-09-23 17:54:20 +02:00
|
|
|
if(times[0] == 200.0){
|
|
|
|
// if the given request was a successfull request, calculate the offset
|
2018-07-26 20:51:46 +02:00
|
|
|
double offset = date->currentMSecsSinceEpoch() - times[1];
|
2018-09-23 17:54:20 +02:00
|
|
|
//check if the list contains more than or 100 values
|
2018-07-26 14:54:11 +02:00
|
|
|
if(this->latest_offsets.length()>=100){
|
2018-09-23 17:54:20 +02:00
|
|
|
//if so, delete the first (oldest) one
|
2018-07-26 14:54:11 +02:00
|
|
|
this->latest_offsets.removeFirst();
|
|
|
|
}
|
2018-09-23 17:54:20 +02:00
|
|
|
//append the new offset to the list
|
2018-07-26 14:54:11 +02:00
|
|
|
this->latest_offsets.append(offset);
|
2018-09-23 17:54:20 +02:00
|
|
|
//variable to store the avarage
|
2018-07-26 14:54:11 +02:00
|
|
|
double mem = 0;
|
|
|
|
for(int i=0;i<latest_offsets.length();i++){
|
2018-09-23 17:54:20 +02:00
|
|
|
//go through the list and add all offsets
|
2018-07-26 14:54:11 +02:00
|
|
|
mem += latest_offsets[i];
|
|
|
|
}
|
2018-09-23 17:54:20 +02:00
|
|
|
//calculate the avarage
|
2018-07-26 22:13:43 +02:00
|
|
|
this->offset = mem / double(latest_offsets.length());
|
2018-09-23 17:54:20 +02:00
|
|
|
|
|
|
|
//emit that the offset has changed
|
|
|
|
emit offsetChanged();
|
2018-09-17 23:06:34 +02:00
|
|
|
//qDebug("%20f", this->offset);
|
2018-07-26 20:51:46 +02:00
|
|
|
return(true);
|
|
|
|
}
|
|
|
|
else {
|
2018-09-23 17:54:20 +02:00
|
|
|
//if the given request was not valid, return false
|
2018-07-26 20:51:46 +02:00
|
|
|
return(false);
|
2018-07-25 00:32:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-18 17:11:47 +02:00
|
|
|
QList<double> BuzzerConn::gettimes(int timeout, bool bothTimes)
|
2018-07-25 00:32:20 +02:00
|
|
|
{
|
2018-09-23 17:54:20 +02:00
|
|
|
// function to recieve the timestamps (last_triggered, current_timestamp) from the extentions
|
|
|
|
|
|
|
|
//list to store the return code of the request and the timestamps
|
2018-08-30 16:44:04 +02:00
|
|
|
QList<double> times;
|
2018-09-23 17:54:20 +02:00
|
|
|
//variable to store answer of the extention
|
2018-08-30 17:49:24 +02:00
|
|
|
signed long ret;
|
2018-09-23 17:54:20 +02:00
|
|
|
//send request to extention
|
2018-08-30 17:49:24 +02:00
|
|
|
ret = this->sendCommand("GET_TIMESTAMP", timeout);
|
|
|
|
if(ret >= 0){
|
2018-09-23 17:54:20 +02:00
|
|
|
// if it is higer than 0 (=success) append the return code
|
2018-08-30 16:44:04 +02:00
|
|
|
times.append(double(200));
|
2018-09-23 17:54:20 +02:00
|
|
|
//and the timestamp
|
2018-08-30 16:44:04 +02:00
|
|
|
times.append(double(ret));
|
2018-08-30 17:49:24 +02:00
|
|
|
|
2018-09-18 17:11:47 +02:00
|
|
|
if(bothTimes){
|
2018-09-23 17:54:20 +02:00
|
|
|
//if both timstamps were requested do the same thing again for the other one (last_triggered)
|
2018-09-18 17:11:47 +02:00
|
|
|
ret = this->sendCommand("GET_LASTPRESSED", timeout);
|
|
|
|
|
|
|
|
if(ret >= 0){
|
2018-09-23 17:54:20 +02:00
|
|
|
//if the reuest was successfull, append the value to the list
|
2018-09-18 17:11:47 +02:00
|
|
|
times.append(double(ret));
|
|
|
|
}
|
|
|
|
else {
|
2018-09-23 17:54:20 +02:00
|
|
|
// if not, change the return code
|
2018-09-18 17:11:47 +02:00
|
|
|
times[0] = ret;
|
|
|
|
}
|
|
|
|
}
|
2018-07-25 00:32:20 +02:00
|
|
|
}
|
2018-08-30 16:44:04 +02:00
|
|
|
else {
|
2018-09-23 17:54:20 +02:00
|
|
|
// if not, append the return code
|
2018-08-30 17:49:24 +02:00
|
|
|
times.append(ret);
|
2018-07-25 00:32:20 +02:00
|
|
|
}
|
2018-09-23 17:54:20 +02:00
|
|
|
|
|
|
|
//return the list
|
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
|
|
|
double BuzzerConn::get(QString key)
|
|
|
|
{
|
2018-09-23 17:54:20 +02:00
|
|
|
// function to get all kinds of data
|
|
|
|
|
2018-07-26 14:54:11 +02:00
|
|
|
if(key == "offset"){
|
2018-09-23 17:54:20 +02:00
|
|
|
// get the offset of the extention
|
2018-07-26 14:54:11 +02:00
|
|
|
return(this->offset);
|
|
|
|
}
|
|
|
|
else if (key == "lastpressed") {
|
2018-09-23 17:54:20 +02:00
|
|
|
// get the last_triggered timestamp of the extention
|
2018-07-26 14:54:11 +02:00
|
|
|
return(this->latest_button_pressed);
|
|
|
|
}
|
|
|
|
else if( key == "currtime") {
|
2018-09-23 17:54:20 +02:00
|
|
|
// get the current time
|
2018-07-26 14:54:11 +02:00
|
|
|
return(this->date->currentMSecsSinceEpoch());
|
|
|
|
}
|
2018-07-26 20:51:46 +02:00
|
|
|
else if( key == "connection_progress") {
|
2018-09-23 17:54:20 +02:00
|
|
|
//get the connection progress
|
2018-07-26 20:51:46 +02:00
|
|
|
return(this->connection_progress);
|
|
|
|
}
|
|
|
|
else if( key == "connected") {
|
2018-09-23 17:54:20 +02:00
|
|
|
// get the state of the connection
|
2018-09-18 17:11:47 +02:00
|
|
|
if(this->state == "connected"){
|
2018-07-26 20:51:46 +02:00
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
return(0);
|
|
|
|
}
|
2018-09-17 23:06:34 +02:00
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
2018-08-02 18:10:08 +02:00
|
|
|
bool BuzzerConn::refresh()
|
|
|
|
{
|
2018-09-23 17:54:20 +02:00
|
|
|
// function that has to be called frequently to refresh the connection
|
|
|
|
// check if the extention has been triggered
|
|
|
|
// sends one command the pending command list
|
|
|
|
// and calculates the offset once
|
|
|
|
|
2018-09-18 17:11:47 +02:00
|
|
|
if(this->state != "connected"){
|
2018-09-23 17:54:20 +02:00
|
|
|
// if the extention is not connected return
|
2018-08-02 18:10:08 +02:00
|
|
|
return(false);
|
|
|
|
}
|
2018-09-09 20:18:00 +02:00
|
|
|
|
2018-09-18 17:11:47 +02:00
|
|
|
//send one of the pending commands
|
2018-09-09 20:18:00 +02:00
|
|
|
if(pending_commands.length() > 0){
|
2018-09-23 17:54:20 +02:00
|
|
|
//get the irst command in the list
|
2018-09-09 20:18:00 +02:00
|
|
|
QString command = this->pending_commands.first();
|
2018-09-23 17:54:20 +02:00
|
|
|
//send the command
|
2018-09-17 23:06:34 +02:00
|
|
|
signed long retval = this->sendCommand(command, 2000);
|
2018-09-23 17:54:20 +02:00
|
|
|
|
2018-09-09 20:18:00 +02:00
|
|
|
if(retval > 0){
|
2018-09-23 17:54:20 +02:00
|
|
|
//if the request has been successfull, remove the command from the list
|
2018-09-09 20:18:00 +02:00
|
|
|
this->pending_commands.removeFirst();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-23 17:54:20 +02:00
|
|
|
// get the timestamps from the extention
|
2018-09-17 23:06:34 +02:00
|
|
|
QList<double> ret = this->gettimes(2000);
|
2018-09-23 17:54:20 +02:00
|
|
|
|
2018-08-30 17:49:24 +02:00
|
|
|
if(ret[0] >= 0){
|
2018-09-23 17:54:20 +02:00
|
|
|
//if the request has been successfull check if the last_triggered timestamp has changed
|
2018-09-17 23:06:34 +02:00
|
|
|
if(ret[2] > this->latest_button_pressed){
|
2018-09-23 17:54:20 +02:00
|
|
|
// if it has, set it
|
2018-09-17 23:06:34 +02:00
|
|
|
this->latest_button_pressed = ret[2];
|
2018-09-23 17:54:20 +02:00
|
|
|
//and emit the trggered signal
|
2018-09-17 23:06:34 +02:00
|
|
|
emit triggered();
|
|
|
|
}
|
2018-09-23 17:54:20 +02:00
|
|
|
// as the requst as been sucessfull, reset the error counter
|
2018-08-30 17:49:24 +02:00
|
|
|
this->errors = 0;
|
2018-09-23 17:54:20 +02:00
|
|
|
// calculate the offset on time and return
|
2018-08-30 17:49:24 +02:00
|
|
|
return(this->calcoffset(ret));
|
|
|
|
}
|
|
|
|
else {
|
2018-09-23 17:54:20 +02:00
|
|
|
// if not add one to the error conter
|
2018-08-30 17:49:24 +02:00
|
|
|
this->errors ++;
|
2018-09-23 17:54:20 +02:00
|
|
|
|
2018-08-30 17:49:24 +02:00
|
|
|
if(this->errors > errors_until_disconnect){
|
2018-09-23 17:54:20 +02:00
|
|
|
// if the error counter is too high, disconnect
|
2018-08-30 17:49:24 +02:00
|
|
|
this->socket->disconnectFromHost();
|
2018-09-18 17:11:47 +02:00
|
|
|
this->setState("disconnected");
|
2018-08-30 17:49:24 +02:00
|
|
|
}
|
2018-09-23 17:54:20 +02:00
|
|
|
|
|
|
|
// return false
|
2018-08-30 17:49:24 +02:00
|
|
|
return(false);
|
|
|
|
}
|
2018-08-30 16:44:04 +02:00
|
|
|
|
2018-08-02 18:10:08 +02:00
|
|
|
}
|
|
|
|
|
2018-08-30 17:49:24 +02:00
|
|
|
signed long BuzzerConn::sendCommand(QString command, int timeout){
|
2018-09-23 17:54:20 +02:00
|
|
|
//function to send a commnd to the extention
|
2018-08-30 17:49:24 +02:00
|
|
|
|
|
|
|
//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);
|
2018-09-23 17:54:20 +02:00
|
|
|
|
2018-08-30 16:44:04 +02:00
|
|
|
out << quint16(0) << command;
|
|
|
|
|
|
|
|
out.device()->seek(0);
|
|
|
|
out << quint16(arrBlock.size() - sizeof(quint16));
|
|
|
|
|
|
|
|
this->socket->write(arrBlock);
|
|
|
|
|
2018-09-23 17:54:20 +02:00
|
|
|
//now wait for the extention to answer
|
2018-08-30 16:44:04 +02:00
|
|
|
QEventLoop loop;
|
|
|
|
QTimer timer;
|
|
|
|
|
|
|
|
timer.setSingleShot(true);
|
2018-09-23 17:54:20 +02:00
|
|
|
//quit the loop if the timer times out
|
2018-08-30 16:44:04 +02:00
|
|
|
loop.connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
|
2018-09-23 17:54:20 +02:00
|
|
|
//or if the request is anwered
|
2018-08-30 16:44:04 +02:00
|
|
|
loop.connect(socket, SIGNAL(readyRead()), &loop, SLOT(quit()));
|
2018-09-23 17:54:20 +02:00
|
|
|
|
|
|
|
//start the timer
|
2018-08-30 17:49:24 +02:00
|
|
|
timer.start(timeout);
|
2018-09-23 17:54:20 +02:00
|
|
|
//start the loop
|
2018-08-30 16:44:04 +02:00
|
|
|
loop.exec();
|
|
|
|
|
|
|
|
//loop finished
|
2018-09-18 17:36:11 +02:00
|
|
|
if(timer.remainingTime() == -1){
|
2018-08-30 17:49:24 +02:00
|
|
|
//the time has been triggered -> timeout
|
|
|
|
return(-1);
|
|
|
|
}
|
2018-09-23 17:54:20 +02:00
|
|
|
// stop the timer
|
2018-09-18 17:36:11 +02:00
|
|
|
timer.stop();
|
|
|
|
|
|
|
|
|
2018-09-18 17:11:47 +02:00
|
|
|
//if the data is not 4 bytes long it is invalid -> clear and terminate
|
2018-08-30 16:44:04 +02:00
|
|
|
if(this->socket->bytesAvailable() != 4){
|
2018-09-18 17:36:11 +02:00
|
|
|
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-09-23 17:54:20 +02:00
|
|
|
// read four bytes and cast them as a double
|
2018-08-30 16:44:04 +02:00
|
|
|
this->socket->read((char*)&data,4);
|
|
|
|
|
2018-09-23 17:54:20 +02:00
|
|
|
//return the data
|
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);
|
|
|
|
}
|
2018-09-23 17:54:20 +02:00
|
|
|
|
|
|
|
void BuzzerConn::setIP(const QString &ipAdress){
|
|
|
|
this->ip = ipAdress;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString BuzzerConn::getIP() const
|
|
|
|
{
|
|
|
|
return(this->ip);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString BuzzerConn::getState() const
|
|
|
|
{
|
|
|
|
return(this->state);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BuzzerConn::setState(QString newState){
|
|
|
|
this->state = newState;
|
|
|
|
emit stateChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
int BuzzerConn::getProgress() const
|
|
|
|
{
|
|
|
|
return(connection_progress);
|
|
|
|
}
|
|
|
|
|
|
|
|
double BuzzerConn::getOffset() const
|
|
|
|
{
|
|
|
|
return(this->offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
double BuzzerConn::getLastTriggered() const
|
|
|
|
{
|
|
|
|
return(this->latest_button_pressed);
|
|
|
|
}
|