This repository has been archived on 2024-06-03. You can view files and clone it, but cannot push or open issues or pull requests.
app/sources/buzzerconn.cpp

288 lines
6.9 KiB
C++
Raw Normal View History

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"
BuzzerConn::BuzzerConn(QObject *parent, QString ip, int port) : QObject(parent)
{
this->socket = new QTcpSocket();
2018-07-26 14:54:11 +02:00
this->date = new QDateTime;
this->latest_button_pressed = 0;
this->ip = ip;
this->port = port;
this->setState("disconnected");
// "http://192.168.4.1"
}
bool BuzzerConn::connect()
{
qDebug() << "connecting...";
setState("connecting");
//setup loop to wait until the connection 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);
//connect
this->socket->connectToHost(this->ip, this->port);
//wait for the connection to finish
loop.exec();
timer.stop();
if(timer.remainingTime() == 0){
//the time has been triggered -> timeout
setState("disconnected");
return(false);
}
QList<double> times = gettimes(2000, true);
qDebug() << times;
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++){
this->connection_progress = i;
emit this->progressChanged();
if(!calcoffset(this->gettimes(1000, false))){
2018-07-26 22:13:43 +02:00
this->connection_progress = 100;
setState("disconnected");
return(false);
}
2018-07-26 14:54:11 +02:00
}
setState("connected");
return(true);
}
else{
setState("disconnected");
return(false);
}
}
bool BuzzerConn::calcoffset(QList<double> times)
{
if(times.length() != 2){
2018-07-26 22:13:43 +02:00
return(false);
}
if(times[0] == 200.0){
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());
offsetChanged();
//qDebug("%20f", this->offset);
return(true);
}
else {
//this->connected = false;
return(false);
}
}
QList<double> BuzzerConn::gettimes(int timeout, bool bothTimes)
{
QList<double> times;
signed long ret;
ret = this->sendCommand("GET_TIMESTAMP", timeout);
qDebug() << timeout << bothTimes;
if(ret >= 0){
times.append(double(200));
times.append(double(ret));
if(bothTimes){
ret = this->sendCommand("GET_LASTPRESSED", timeout);
if(ret >= 0){
times.append(double(ret));
}
else {
times[0] = ret;
}
}
}
else {
times.append(ret);
}
return(times);
}
2018-07-26 14:54:11 +02:00
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());
}
else if( key == "connection_progress") {
return(this->connection_progress);
}
else if( key == "connected") {
if(this->state == "connected"){
return(1);
}
return(0);
}
return(0);
}
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);
2018-07-26 14:54:11 +02:00
}
bool BuzzerConn::refresh()
{
if(this->state != "connected"){
return(false);
}
2018-09-09 20:18:00 +02:00
//send one of the pending commands
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, 2000);
2018-09-09 20:18:00 +02:00
if(retval > 0){
this->pending_commands.removeFirst();
}
}
//refresh the times
QList<double> ret = this->gettimes(2000);
if(ret[0] >= 0){
if(ret[2] > this->latest_button_pressed){
this->latest_button_pressed = ret[2];
emit triggered();
}
this->errors = 0;
return(this->calcoffset(ret));
}
else {
this->errors ++;
if(this->errors > errors_until_disconnect){
this->socket->disconnectFromHost();
this->setState("disconnected");
}
return(false);
}
}
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();
}
//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()));
timer.start(timeout);
loop.exec();
//loop finished
timer.stop();
if(timer.remainingTime() == 0){
//the time has been triggered -> timeout
return(-1);
}
//if the data is not 4 bytes long it is invalid -> clear and terminate
if(this->socket->bytesAvailable() != 4){
qDebug() << this->socket->readAll();
return(-2);
}
long data = 0;
this->socket->read((char*)&data,4);
//qDebug() << data;
//qDebug() << this->socket->bytesAvailable();
return data;
}
2018-09-09 20:18:00 +02:00
void BuzzerConn::appendCommand(QString command){
this->pending_commands.append(command);
}