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-14 18:39:39 +02:00
|
|
|
remoteSessions.release(1);
|
|
|
|
|
|
|
|
this->nextConnectionId = 1;
|
2018-10-04 18:35:29 +02:00
|
|
|
|
|
|
|
this->speedTimers.append(pGlobalSpeedTimer);
|
|
|
|
|
|
|
|
this->refreshTimer = new QTimer();
|
|
|
|
refreshTimer->setInterval(1);
|
|
|
|
refreshTimer->setSingleShot(true);
|
|
|
|
refreshTimer->connect(this->refreshTimer, &QTimer::timeout, this, &BaseConn::refreshTimers);
|
2018-10-14 18:39:39 +02:00
|
|
|
refreshTimer->start();
|
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");
|
2018-10-14 18:39:39 +02:00
|
|
|
this->speedTimers[0]->remoteControlled = true;
|
2018-09-23 17:54:20 +02:00
|
|
|
return(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString BaseConn::sendCommand(QString command){
|
2018-10-04 18:35:29 +02:00
|
|
|
if(this->state != "connected"){
|
|
|
|
return "ERR_NOT_CONNECTED";
|
|
|
|
}
|
2018-09-23 17:54:20 +02:00
|
|
|
|
2018-10-14 18:39:39 +02:00
|
|
|
// generate id and witing requests entry
|
|
|
|
int thisId = nextConnectionId;
|
|
|
|
nextConnectionId ++;
|
|
|
|
QEventLoop loop;
|
|
|
|
this->waitingRequests.append({thisId, &loop, ""});
|
|
|
|
|
|
|
|
command = "ID:" + QString::number(thisId) + "_"+command;
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
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
|
2018-09-23 17:54:20 +02:00
|
|
|
|
2018-10-14 18:39:39 +02:00
|
|
|
socket->write(arrBlock);
|
2018-09-23 17:54:20 +02:00
|
|
|
|
2018-10-14 18:39:39 +02:00
|
|
|
//wait for an answer to finish (programm gets stuck in here)
|
|
|
|
loop.exec();
|
2018-09-23 17:54:20 +02:00
|
|
|
|
2018-10-14 18:39:39 +02:00
|
|
|
//loop finished
|
|
|
|
if(timer.remainingTime() == -1){
|
|
|
|
//the time has been triggered -> timeout
|
2018-09-23 17:54:20 +02:00
|
|
|
|
2018-10-14 18:39:39 +02:00
|
|
|
return("ERR_TIMEOUT");
|
|
|
|
}
|
2018-09-23 17:54:20 +02:00
|
|
|
|
2018-10-14 18:39:39 +02:00
|
|
|
QString reply;
|
2018-09-23 17:54:20 +02:00
|
|
|
|
2018-10-14 18:39:39 +02:00
|
|
|
for(int i = 0; i<this->waitingRequests.length(); i++){
|
|
|
|
if(this->waitingRequests[i].id == thisId){
|
|
|
|
reply = this->waitingRequests[i].reply;
|
2018-09-23 17:54:20 +02:00
|
|
|
}
|
2018-10-14 18:39:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// stop the timer as the connection has been established
|
|
|
|
timer.stop();
|
2018-09-23 17:54:20 +02:00
|
|
|
|
2018-10-14 18:39:39 +02:00
|
|
|
timer.deleteLater();
|
2018-09-23 17:54:20 +02:00
|
|
|
|
2018-10-14 18:39:39 +02:00
|
|
|
//remoteSessions.release(1);
|
|
|
|
return(reply);
|
2018-09-23 17:54:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2018-10-14 18:39:39 +02:00
|
|
|
// if (str == "0")
|
|
|
|
// {
|
|
|
|
// str = "Connection closed";
|
|
|
|
// closeConnection();
|
|
|
|
// }
|
|
|
|
|
2018-09-23 17:54:20 +02:00
|
|
|
nextBlockSize = 0;
|
2018-10-14 18:39:39 +02:00
|
|
|
QString reply = str;
|
|
|
|
int id = 0;
|
|
|
|
if(reply.startsWith("ID:")){
|
|
|
|
reply.replace("ID:", "");
|
|
|
|
QStringList temp = reply.split("_");
|
|
|
|
reply.replace(temp[0]+"_", "");
|
|
|
|
id = temp[0].toInt();
|
|
|
|
|
|
|
|
for(int i = 0; i<this->waitingRequests.length(); i++){
|
|
|
|
if(this->waitingRequests[i].id == id){
|
|
|
|
this->waitingRequests[i].reply = reply;
|
|
|
|
this->waitingRequests[i].loop->quit();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-23 17:54:20 +02:00
|
|
|
latestReadReply = str;
|
2018-10-14 18:39:39 +02:00
|
|
|
emit gotUnexpectedReply(str);
|
2018-09-23 17:54:20 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-04 18:35:29 +02:00
|
|
|
/*-----Functions to control the local stopwatch-----*/
|
|
|
|
|
|
|
|
void BaseConn::refreshTimers(){
|
|
|
|
if(this->state != "connected"){
|
2018-10-14 18:39:39 +02:00
|
|
|
this->refreshTimer->start();
|
2018-10-04 18:35:29 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString remoteState;
|
2018-10-14 18:39:39 +02:00
|
|
|
QString remoteTime;
|
|
|
|
QString tmpNextRemoteAction;
|
|
|
|
QString tmpNextRemoteActionDelayProg;
|
|
|
|
|
|
|
|
remoteState = sendCommand("GET_TIMER_STATE");
|
2018-10-04 18:35:29 +02:00
|
|
|
|
|
|
|
switch (speedTimers[0]->state) {
|
|
|
|
case SpeedTimer::IDLE:
|
|
|
|
break;
|
|
|
|
case SpeedTimer::STARTING:
|
|
|
|
if(remoteState == "RUNNING"){
|
|
|
|
speedTimers[0]->start();
|
|
|
|
}
|
|
|
|
else if (remoteState == "STOPPED") {
|
|
|
|
speedTimers[0]->stop("manual");
|
|
|
|
}
|
2018-10-14 18:39:39 +02:00
|
|
|
|
|
|
|
tmpNextRemoteAction = sendCommand("GET_NEXT_ACTION");
|
|
|
|
if(tmpNextRemoteAction.startsWith("ERR")){
|
|
|
|
//handle Error!!
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if(this->nextRemoteAction != tmpNextRemoteAction){
|
|
|
|
this->nextRemoteAction = tmpNextRemoteAction;
|
|
|
|
this->nextRemoteActionChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpNextRemoteActionDelayProg = sendCommand("GET_NEXT_ACTION_DELAY_PROG");
|
|
|
|
if(tmpNextRemoteActionDelayProg.startsWith("ERR")){
|
|
|
|
//handle error!!
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if(this->nextRemoteActionDelayProg != tmpNextRemoteActionDelayProg.toFloat()){
|
|
|
|
this->nextRemoteActionDelayProg = tmpNextRemoteActionDelayProg.toFloat();
|
|
|
|
this->nextRemoteActionDelayProgChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-04 18:35:29 +02:00
|
|
|
break;
|
|
|
|
case SpeedTimer::RUNNING:
|
2018-10-14 18:39:39 +02:00
|
|
|
|
2018-10-04 18:35:29 +02:00
|
|
|
if(remoteState == "STOPPED"){
|
|
|
|
speedTimers[0]->stop("manual");
|
|
|
|
}
|
2018-10-14 18:39:39 +02:00
|
|
|
remoteTime = sendCommand("GET_CURRTIME");
|
|
|
|
if(remoteTime.startsWith("ERR")){
|
|
|
|
//handle error!!
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
speedTimers[0]->stoppedTime = remoteTime.toInt();
|
|
|
|
}
|
2018-10-04 18:35:29 +02:00
|
|
|
break;
|
|
|
|
case SpeedTimer::STOPPED:
|
2018-10-14 18:39:39 +02:00
|
|
|
remoteTime = sendCommand("GET_STOPPED_TIME");
|
|
|
|
if(remoteTime.startsWith("ERR")){
|
|
|
|
//handle error!!
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
speedTimers[0]->stoppedTime = remoteTime.toInt();
|
|
|
|
}
|
2018-10-04 18:35:29 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-10-14 18:39:39 +02:00
|
|
|
if(speedTimers[0]->state != speedTimers[0]->stateFromString(remoteState)){
|
|
|
|
// speedTimers[0]->setState(speedTimers[0]->stateFromString(remoteState));
|
|
|
|
qWarning() << "WARNING: Remote State not matching!!" << " remote state: " << remoteState << " local state: " << this->speedTimers[0]->getState();
|
|
|
|
}
|
|
|
|
|
2018-10-04 18:35:29 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-10-14 18:39:39 +02:00
|
|
|
bool BaseConn::stopTimers(QString type){
|
2018-10-04 18:35:29 +02:00
|
|
|
qDebug() << "stopping timers";
|
|
|
|
|
|
|
|
QString ret = this->sendCommand("CMD_STOP_TIMER");
|
|
|
|
|
|
|
|
if(ret.startsWith("ERR")){
|
|
|
|
//handle Error!
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-14 18:39:39 +02:00
|
|
|
this->speedTimers[0]->stop(type);
|
|
|
|
qDebug() << "stopped timers";
|
2018-10-04 18:35:29 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-14 18:39:39 +02:00
|
|
|
bool BaseConn::resetTimers(){
|
|
|
|
qDebug() << "resetting timers";
|
|
|
|
|
|
|
|
QString ret = this->sendCommand("CMD_RESET_TIMER");
|
|
|
|
|
|
|
|
if(ret.startsWith("ERR")){
|
|
|
|
//handle Error!
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->speedTimers[0]->reset();
|
|
|
|
return true;
|
|
|
|
}
|
2018-10-04 18:35:29 +02:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-10-14 18:39:39 +02:00
|
|
|
QString BaseConn::getNextRemoteAction() {
|
|
|
|
return this->nextRemoteAction;
|
|
|
|
}
|
|
|
|
|
|
|
|
float BaseConn::getNextRemoteActionDelayProg(){
|
|
|
|
return this->nextRemoteActionDelayProg;
|
|
|
|
}
|
|
|
|
|