2018-09-23 17:54:20 +02:00
|
|
|
#include "headers/baseconn.h"
|
|
|
|
|
|
|
|
BaseConn::BaseConn(QObject *parent) : QObject(parent)
|
|
|
|
{
|
|
|
|
socket = new QTcpSocket();
|
|
|
|
this->setState("disconnected");
|
2019-03-02 20:20:13 +01:00
|
|
|
|
|
|
|
connect(this->socket, SIGNAL(error(QAbstractSocket::SocketError)),
|
|
|
|
this, SLOT(gotError(QAbstractSocket::SocketError)));
|
2018-10-14 18:39:39 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-03-02 20:20:13 +01:00
|
|
|
void BaseConn::closeConnection()
|
|
|
|
{
|
|
|
|
qDebug() << "closing connection";
|
|
|
|
switch (socket->state())
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
socket->disconnectFromHost();
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
socket->abort();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
socket->abort();
|
|
|
|
}
|
|
|
|
setState("disconnected");
|
|
|
|
// for(int i = 0; i < this->waitingRequests.length(); i++){
|
|
|
|
// this->waitingRequests[i].reply = "ERR_NOT_CONNECTED";
|
|
|
|
// this->waitingRequests[i].loop->quit();
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
void BaseConn::gotError(QAbstractSocket::SocketError err)
|
|
|
|
{
|
|
|
|
//qDebug() << "got error";
|
|
|
|
QString strError = "unknown";
|
|
|
|
switch (err)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
strError = "Connection was refused";
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
strError = "Remote host closed the connection";
|
|
|
|
this->closeConnection();
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
strError = "Host address was not found";
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
strError = "Connection timed out";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
strError = "Unknown error";
|
|
|
|
}
|
|
|
|
|
|
|
|
emit gotError(strError);
|
|
|
|
qDebug() << "got socket error: " << strError;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariantMap BaseConn::sendCommand(int header, QJsonValue data = ""){
|
2018-10-04 18:35:29 +02:00
|
|
|
if(this->state != "connected"){
|
2019-03-02 20:20:13 +01:00
|
|
|
return {{"status", 910}, {"data", "not connected"}};
|
2018-10-04 18:35:29 +02:00
|
|
|
}
|
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 ++;
|
2019-03-02 20:20:13 +01:00
|
|
|
|
2018-10-14 18:39:39 +02:00
|
|
|
QEventLoop loop;
|
2019-03-02 20:20:13 +01:00
|
|
|
QJsonObject reply;
|
|
|
|
|
|
|
|
this->waitingRequests.append({thisId, &loop, reply});
|
|
|
|
|
|
|
|
QJsonObject requestObj;
|
|
|
|
requestObj.insert("id", thisId);
|
|
|
|
requestObj.insert("header", header);
|
|
|
|
requestObj.insert("data", data);
|
|
|
|
|
2018-10-14 18:39:39 +02:00
|
|
|
|
2019-03-02 20:20:13 +01:00
|
|
|
QString jsonRequest = QJsonDocument(requestObj).toJson();
|
2018-10-14 18:39:39 +02:00
|
|
|
|
|
|
|
QByteArray arrBlock;
|
|
|
|
QDataStream out(&arrBlock, QIODevice::WriteOnly);
|
|
|
|
//out.setVersion(QDataStream::Qt_5_10);
|
2019-03-02 20:20:13 +01:00
|
|
|
out << quint16(0) << jsonRequest;
|
2018-10-14 18:39:39 +02:00
|
|
|
|
|
|
|
out.device()->seek(0);
|
2019-03-02 20:20:13 +01:00
|
|
|
out << quint16(ulong(arrBlock.size()) - sizeof(quint16));
|
2018-10-14 18:39:39 +02:00
|
|
|
|
|
|
|
QTimer timer;
|
|
|
|
|
|
|
|
timer.setSingleShot(true);
|
|
|
|
// quit the loop when the timer times out
|
|
|
|
loop.connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
|
2019-03-02 20:20:13 +01:00
|
|
|
// quit the loop when the connection was established
|
2018-10-14 18:39:39 +02:00
|
|
|
// 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
|
|
|
|
2019-03-02 20:20:13 +01:00
|
|
|
return {{"status", 911}, {"data", ""}};
|
2018-10-14 18:39:39 +02:00
|
|
|
}
|
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);
|
2019-03-02 20:20:13 +01:00
|
|
|
return {{"status", reply.value("header").toInt()}, {"data", reply.value("data").toVariant()}};
|
|
|
|
|
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)
|
|
|
|
{
|
2019-03-02 20:20:13 +01:00
|
|
|
if (ulong(socket->bytesAvailable()) < sizeof(quint16)) { break; }
|
2018-09-23 17:54:20 +02:00
|
|
|
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;
|
|
|
|
|
2019-03-02 20:20:13 +01:00
|
|
|
QJsonDocument jsonReply = QJsonDocument::fromJson(reply.toUtf8());
|
|
|
|
QJsonObject replyObj = jsonReply.object();
|
|
|
|
|
|
|
|
if(!replyObj.isEmpty()){
|
|
|
|
id = replyObj.value("id").toInt();
|
|
|
|
|
|
|
|
for(int i = 0; i < this->waitingRequests.length(); i++){
|
2018-10-14 18:39:39 +02:00
|
|
|
if(this->waitingRequests[i].id == id){
|
2019-03-02 20:20:13 +01:00
|
|
|
this->waitingRequests[i].reply = replyObj;
|
2018-10-14 18:39:39 +02:00
|
|
|
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
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-03 17:31:24 +01:00
|
|
|
int BaseConn::writeRemoteSetting(QString key, QString value) {
|
|
|
|
QJsonArray requestData;
|
|
|
|
requestData.append(key);
|
|
|
|
requestData.append(value);
|
|
|
|
return this->sendCommand(3000, requestData)["status"].toInt();
|
|
|
|
}
|
2019-03-02 23:01:05 +01:00
|
|
|
|
2018-10-04 18:35:29 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-03-02 23:01:05 +01:00
|
|
|
QVariantMap tmpReply = this->sendCommand(2000);
|
|
|
|
|
|
|
|
if(tmpReply["status"] != 200){
|
|
|
|
return;
|
|
|
|
}
|
2018-10-14 18:39:39 +02:00
|
|
|
|
2019-03-02 23:01:05 +01:00
|
|
|
int remoteRaceState = tmpReply["data"].toInt();
|
2018-10-04 18:35:29 +02:00
|
|
|
|
2019-03-02 23:01:05 +01:00
|
|
|
switch (remoteRaceState) {
|
|
|
|
case 0:
|
|
|
|
{
|
|
|
|
// case IDLE
|
|
|
|
if(speedTimers[0]->state != 0){
|
|
|
|
speedTimers[0]->setState(SpeedTimer::IDLE);
|
2018-10-04 18:35:29 +02:00
|
|
|
}
|
2019-03-02 23:01:05 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 1:
|
|
|
|
{
|
|
|
|
// case STARTING
|
|
|
|
if(speedTimers[0]->state != 1){
|
|
|
|
speedTimers[0]->setState(SpeedTimer::STARTING);
|
2018-10-04 18:35:29 +02:00
|
|
|
}
|
2018-10-14 18:39:39 +02:00
|
|
|
|
2019-03-02 23:01:05 +01:00
|
|
|
tmpReply = sendCommand(2004);
|
|
|
|
if(tmpReply["status"] != 200){
|
2018-10-14 18:39:39 +02:00
|
|
|
//handle Error!!
|
2019-03-02 23:01:05 +01:00
|
|
|
qDebug() << "+--- getting next start action from basestation failed: " << tmpReply["status"];
|
|
|
|
this->refreshTimer->start();
|
|
|
|
return;
|
2018-10-14 18:39:39 +02:00
|
|
|
}
|
|
|
|
else {
|
2019-03-02 23:01:05 +01:00
|
|
|
if(this->nextRemoteAction != tmpReply["data"].toString()){
|
|
|
|
this->nextRemoteAction = tmpReply["data"].toString();
|
2018-10-14 18:39:39 +02:00
|
|
|
this->nextRemoteActionChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-02 23:01:05 +01:00
|
|
|
tmpReply = sendCommand(2005);
|
|
|
|
if(tmpReply["status"] != 200){
|
2018-10-14 18:39:39 +02:00
|
|
|
//handle error!!
|
2019-03-02 23:01:05 +01:00
|
|
|
qDebug() << "+--- getting next start action progress from basestation failed";
|
|
|
|
this->refreshTimer->start();
|
|
|
|
return;
|
2018-10-14 18:39:39 +02:00
|
|
|
}
|
|
|
|
else {
|
2019-03-02 23:01:05 +01:00
|
|
|
if(this->nextRemoteActionDelayProg != tmpReply["data"].toFloat()){
|
|
|
|
this->nextRemoteActionDelayProg = tmpReply["data"].toFloat();
|
2018-10-14 18:39:39 +02:00
|
|
|
this->nextRemoteActionDelayProgChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-04 18:35:29 +02:00
|
|
|
break;
|
2019-03-02 23:01:05 +01:00
|
|
|
}
|
|
|
|
case 2:
|
|
|
|
{
|
|
|
|
// case RUNNING
|
|
|
|
if(speedTimers[0]->state != 2){
|
|
|
|
speedTimers[0]->setState(SpeedTimer::RUNNING);
|
2018-10-04 18:35:29 +02:00
|
|
|
}
|
2019-03-02 23:01:05 +01:00
|
|
|
|
|
|
|
// get current time
|
|
|
|
tmpReply = sendCommand(2001, 0);
|
|
|
|
if(tmpReply["status"] != 200){
|
2018-10-14 18:39:39 +02:00
|
|
|
//handle error!!
|
2019-03-02 23:01:05 +01:00
|
|
|
qDebug() << "+--- getting current time (timer 0) from basestation failed";
|
|
|
|
this->refreshTimer->start();
|
|
|
|
return;
|
2018-10-14 18:39:39 +02:00
|
|
|
}
|
|
|
|
else {
|
2019-03-02 23:01:05 +01:00
|
|
|
speedTimers[0]->stoppedTime = tmpReply["data"].toInt();
|
2018-10-14 18:39:39 +02:00
|
|
|
}
|
2019-03-02 23:01:05 +01:00
|
|
|
|
2018-10-04 18:35:29 +02:00
|
|
|
break;
|
2019-03-02 23:01:05 +01:00
|
|
|
}
|
|
|
|
case 3:
|
|
|
|
{
|
|
|
|
// case STOPPED
|
|
|
|
if(speedTimers[0]->state != 3){
|
|
|
|
speedTimers[0]->setState(SpeedTimer::STOPPED);
|
|
|
|
}
|
2018-10-14 18:39:39 +02:00
|
|
|
|
2019-03-02 23:01:05 +01:00
|
|
|
// get current time
|
|
|
|
tmpReply = sendCommand(2001, 0);
|
|
|
|
if(tmpReply["status"] != 200){
|
|
|
|
//handle error!!
|
|
|
|
qDebug() << "+--- getting current time (timer 0) from basestation failed";
|
|
|
|
return;
|
2018-10-14 18:39:39 +02:00
|
|
|
}
|
|
|
|
else {
|
2019-03-02 23:01:05 +01:00
|
|
|
speedTimers[0]->stoppedTime = tmpReply["data"].toInt();
|
2018-10-14 18:39:39 +02:00
|
|
|
}
|
2019-03-02 23:01:05 +01:00
|
|
|
|
2018-10-04 18:35:29 +02:00
|
|
|
break;
|
|
|
|
}
|
2019-03-02 23:01:05 +01:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
// some error
|
2018-10-04 18:35:29 +02:00
|
|
|
|
2019-03-02 23:01:05 +01:00
|
|
|
break;
|
|
|
|
}
|
2018-10-14 18:39:39 +02:00
|
|
|
}
|
|
|
|
|
2018-10-04 18:35:29 +02:00
|
|
|
this->refreshTimer->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BaseConn::startTimers(){
|
|
|
|
qDebug() << "starting timers";
|
|
|
|
|
2019-03-02 23:01:05 +01:00
|
|
|
QVariantMap reply = this->sendCommand(1000);
|
2018-10-04 18:35:29 +02:00
|
|
|
|
2019-03-02 23:01:05 +01:00
|
|
|
if(reply["status"] != 200){
|
2018-10-04 18:35:29 +02:00
|
|
|
//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";
|
|
|
|
|
2019-03-02 23:01:05 +01:00
|
|
|
QVariantMap reply = this->sendCommand(1001);
|
2018-10-04 18:35:29 +02:00
|
|
|
|
2019-03-02 23:01:05 +01:00
|
|
|
if(reply["status"] != 200){
|
|
|
|
//handle Error!!
|
2018-10-04 18:35:29 +02:00
|
|
|
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";
|
|
|
|
|
2019-03-02 23:01:05 +01:00
|
|
|
QVariantMap reply = this->sendCommand(1002);
|
2018-10-14 18:39:39 +02:00
|
|
|
|
2019-03-02 23:01:05 +01:00
|
|
|
if(reply["status"] != 200){
|
|
|
|
//handle Error!!
|
2018-10-14 18:39:39 +02:00
|
|
|
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() {
|
2019-03-02 23:01:05 +01:00
|
|
|
QVariantMap reply = this->sendCommand(2006);
|
|
|
|
|
|
|
|
if(reply["status"] != 200){
|
|
|
|
//handle Error!!
|
2019-03-03 17:31:24 +01:00
|
|
|
qDebug() << "+--- error refreshing connections: " << reply["status"];
|
2018-10-04 18:35:29 +02:00
|
|
|
return false;
|
|
|
|
}
|
2019-03-02 23:01:05 +01:00
|
|
|
connections = reply["data"].toString().split("|||");
|
2018-10-04 18:35:29 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|