2018-09-23 17:54:20 +02:00
|
|
|
#include "headers/baseconn.h"
|
|
|
|
|
|
|
|
BaseConn::BaseConn(QObject *parent) : QObject(parent)
|
|
|
|
{
|
|
|
|
socket = new QTcpSocket();
|
|
|
|
this->setState("disconnected");
|
2018-10-04 18:35:29 +02:00
|
|
|
remoteSessions.release(2);
|
|
|
|
|
|
|
|
this->speedTimers.append(pGlobalSpeedTimer);
|
|
|
|
|
|
|
|
this->refreshTimer = new QTimer();
|
|
|
|
refreshTimer->setInterval(1);
|
|
|
|
refreshTimer->setSingleShot(true);
|
|
|
|
refreshTimer->connect(this->refreshTimer, &QTimer::timeout, this, &BaseConn::refreshTimers);
|
2018-09-23 17:54:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool BaseConn::connectToHost() {
|
2018-10-04 18:35:29 +02:00
|
|
|
qDebug() << "connecting";
|
2018-09-23 17:54:20 +02:00
|
|
|
setState("connecting");
|
|
|
|
this->connection_progress = 0;
|
|
|
|
QEventLoop loop;
|
|
|
|
QTimer timer;
|
|
|
|
|
|
|
|
timer.setSingleShot(true);
|
|
|
|
// quit the loop when the timer times out
|
|
|
|
loop.connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
|
|
|
|
//quit the loop when the connection was established
|
|
|
|
loop.connect(this->socket, SIGNAL(connected()), &loop, SLOT(quit()));
|
|
|
|
// start the timer before starting to connect
|
|
|
|
timer.start(3000);
|
|
|
|
//connect
|
|
|
|
this->socket->connectToHost(this->ip, this->port);
|
|
|
|
|
|
|
|
//wait for the connection to finish (programm gets stuck in here)
|
|
|
|
loop.exec();
|
|
|
|
|
2018-10-04 18:35:29 +02:00
|
|
|
//loop finish
|
2018-09-23 17:54:20 +02:00
|
|
|
|
|
|
|
if(timer.remainingTime() == -1){
|
|
|
|
//the time has been triggered -> timeout
|
|
|
|
this->socket->abort();
|
|
|
|
setState("disconnected");
|
|
|
|
return(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// stop the timer as the connection has been established
|
|
|
|
timer.stop();
|
|
|
|
connect(this->socket, &QTcpSocket::readyRead, this, &BaseConn::readyRead);
|
|
|
|
this->connection_progress = 100;
|
|
|
|
setState("connected");
|
|
|
|
return(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString BaseConn::sendCommand(QString command){
|
2018-10-04 18:35:29 +02:00
|
|
|
if(this->state != "connected"){
|
|
|
|
return "ERR_NOT_CONNECTED";
|
|
|
|
}
|
|
|
|
remoteSessions.acquire(1);
|
2018-09-23 17:54:20 +02:00
|
|
|
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));
|
|
|
|
|
|
|
|
QEventLoop loop;
|
|
|
|
QTimer timer;
|
|
|
|
|
|
|
|
timer.setSingleShot(true);
|
|
|
|
// quit the loop when the timer times out
|
|
|
|
loop.connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
|
|
|
|
//quit the loop when the connection was established
|
|
|
|
loop.connect(this, &BaseConn::gotReply, &loop, &QEventLoop::quit);
|
|
|
|
// start the timer before starting to connect
|
|
|
|
timer.start(3000);
|
|
|
|
|
|
|
|
//write data
|
|
|
|
|
|
|
|
socket->write(arrBlock);
|
|
|
|
|
|
|
|
//wait for an answer to finish (programm gets stuck in here)
|
|
|
|
loop.exec();
|
|
|
|
|
|
|
|
//loop finished
|
|
|
|
if(timer.remainingTime() == -1){
|
|
|
|
//the time has been triggered -> timeout
|
|
|
|
return("timeout");
|
|
|
|
}
|
|
|
|
|
|
|
|
// stop the timer as the connection has been established
|
|
|
|
timer.stop();
|
|
|
|
|
2018-10-04 18:35:29 +02:00
|
|
|
remoteSessions.release(1);
|
2018-09-23 17:54:20 +02:00
|
|
|
return(this->latestReadReply);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BaseConn::readyRead() {
|
|
|
|
QDataStream in(socket);
|
|
|
|
//in.setVersion(QDataStream::Qt_5_10);
|
|
|
|
qint16 nextBlockSize = 0;
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
if (!nextBlockSize)
|
|
|
|
{
|
|
|
|
if (socket->bytesAvailable() < sizeof(quint16)) { break; }
|
|
|
|
in >> nextBlockSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (socket->bytesAvailable() < nextBlockSize) { break; }
|
|
|
|
|
|
|
|
QString str; in >> str;
|
|
|
|
|
|
|
|
// if (str == "0")
|
|
|
|
// {
|
|
|
|
// str = "Connection closed";
|
|
|
|
// closeConnection();
|
|
|
|
// }
|
|
|
|
nextBlockSize = 0;
|
|
|
|
latestReadReply = str;
|
|
|
|
emit gotReply();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-04 18:35:29 +02:00
|
|
|
/*-----Functions to control the local stopwatch-----*/
|
|
|
|
|
|
|
|
void BaseConn::refreshTimers(){
|
|
|
|
if(this->state != "connected"){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString remoteState;
|
|
|
|
|
|
|
|
switch (speedTimers[0]->state) {
|
|
|
|
case SpeedTimer::IDLE:
|
|
|
|
break;
|
|
|
|
case SpeedTimer::STARTING:
|
|
|
|
remoteState = sendCommand("GET_TIMER_STATE");
|
|
|
|
if(remoteState == "RUNNING"){
|
|
|
|
speedTimers[0]->start();
|
|
|
|
}
|
|
|
|
else if (remoteState == "STOPPED") {
|
|
|
|
speedTimers[0]->stop("manual");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SpeedTimer::RUNNING:
|
|
|
|
remoteState = sendCommand("GET_TIMER_STATE");
|
|
|
|
if(remoteState == "STOPPED"){
|
|
|
|
speedTimers[0]->stop("manual");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SpeedTimer::STOPPED:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->refreshTimer->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BaseConn::startTimers(){
|
|
|
|
qDebug() << "starting timers";
|
|
|
|
|
|
|
|
QString ret = this->sendCommand("CMD_START_TIMER");
|
|
|
|
|
|
|
|
if(ret.startsWith("ERR")){
|
|
|
|
//handle Error!!
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->speedTimers[0]->setState(SpeedTimer::STARTING);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BaseConn::stopTimers(){
|
|
|
|
qDebug() << "stopping timers";
|
|
|
|
|
|
|
|
QString ret = this->sendCommand("CMD_STOP_TIMER");
|
|
|
|
|
|
|
|
if(ret.startsWith("ERR")){
|
|
|
|
//handle Error!
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->speedTimers[0]->stop("manual");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-23 17:54:20 +02:00
|
|
|
void BaseConn::setIP(const QString &ipAdress){
|
|
|
|
this->ip = ipAdress;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString BaseConn::getIP() const
|
|
|
|
{
|
|
|
|
return(this->ip);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString BaseConn::getState() const
|
|
|
|
{
|
|
|
|
return(this->state);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BaseConn::setState(QString newState){
|
|
|
|
this->state = newState;
|
|
|
|
emit stateChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
int BaseConn::getProgress() const
|
|
|
|
{
|
|
|
|
return(connection_progress);
|
|
|
|
}
|
|
|
|
|
2018-10-04 18:35:29 +02:00
|
|
|
bool BaseConn::refreshConnections() {
|
|
|
|
QString ret = this->sendCommand("GET_CONNECTIONS");
|
|
|
|
if(ret.startsWith("ERR")){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
connections = ret.split("|||");
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList BaseConn::getConnections() {
|
|
|
|
return(connections);
|
|
|
|
}
|
|
|
|
|