From 1f055931f7de7e1f0a95d506ed22adba389806dd Mon Sep 17 00:00:00 2001 From: dorian Date: Tue, 20 Aug 2019 22:55:37 +0200 Subject: [PATCH] minor bugfxes and optimizations --- headers/baseconn.h | 6 ++++- headers/climbingrace.h | 2 +- qml/ProfilesDialog/ProfilesDialog.qml | 2 +- qml/SettingsDialog.qml | 2 +- qml/components/ConnectionDelegate.qml | 27 +++++++++---------- qml/main.qml | 4 +++ sources/baseconn.cpp | 39 +++++++++++++-------------- sources/climbingrace.cpp | 4 +-- 8 files changed, 45 insertions(+), 41 deletions(-) diff --git a/headers/baseconn.h b/headers/baseconn.h index 8c96b9e..5be052e 100644 --- a/headers/baseconn.h +++ b/headers/baseconn.h @@ -51,6 +51,8 @@ private: QTcpSocket *socket; //socket for communication with the extention + QTimer *timeoutTimer; + QString readBuffer; QSemaphore remoteSessions; @@ -86,9 +88,11 @@ signals: public slots: - Q_INVOKABLE bool connectToHost(); + Q_INVOKABLE void connectToHost(); //function to connect to the base station + void connectionTimeout(); + Q_INVOKABLE bool init(); Q_INVOKABLE void deInit(); diff --git a/headers/climbingrace.h b/headers/climbingrace.h index 23f7419..09b5b55 100644 --- a/headers/climbingrace.h +++ b/headers/climbingrace.h @@ -91,7 +91,7 @@ public slots: Q_INVOKABLE void writeSetting(QString key, QVariant value); Q_INVOKABLE QString readSetting(QString key); - Q_INVOKABLE bool connectBaseStation(); + Q_INVOKABLE void connectBaseStation(); Q_INVOKABLE void disconnectBaseStation(); Q_INVOKABLE QString getBaseStationState(); Q_INVOKABLE QVariant getBaseStationConnections(); diff --git a/qml/ProfilesDialog/ProfilesDialog.qml b/qml/ProfilesDialog/ProfilesDialog.qml index 71fea9e..3885185 100644 --- a/qml/ProfilesDialog/ProfilesDialog.qml +++ b/qml/ProfilesDialog/ProfilesDialog.qml @@ -118,7 +118,7 @@ Popup { id: headlineUnderline height: 1 width: parent.width - color: "grey" + color: "transparent" anchors { bottom: parent.bottom left: parent.left diff --git a/qml/SettingsDialog.qml b/qml/SettingsDialog.qml index 2aa11e8..1670bc8 100644 --- a/qml/SettingsDialog.qml +++ b/qml/SettingsDialog.qml @@ -357,7 +357,7 @@ Popup { id: connectToBaseDel text: status.status === "connected" ? qsTr("disconnect"): status.status === "disconnected" ? qsTr("connect"):qsTr("connecting...") - status: { "status": speedBackend.baseStationState } + status: { "status": speedBackend.baseStationState, "progress": 100 } connect: speedBackend.connectBaseStation disconnect: speedBackend.disconnectBaseStation type: "baseStation" diff --git a/qml/components/ConnectionDelegate.qml b/qml/components/ConnectionDelegate.qml index c071f39..98c56c5 100644 --- a/qml/components/ConnectionDelegate.qml +++ b/qml/components/ConnectionDelegate.qml @@ -5,6 +5,7 @@ SmoothItemDelegate { id: control property var status + property string oldState: "" property var connect property var disconnect @@ -16,16 +17,22 @@ SmoothItemDelegate { onClicked: { if(status.status === "disconnected"){ connect() - if(status.status !== "connected"){ - statusIndicator.color_override = "red" - shortDelay.start() - } } else { disconnect() } } + onStatusChanged: { + if(oldState !== status.status) { + if(status.status === "disconnected" && oldState === "connecting") { + statusIndicator.color_override = "red" + shortDelay.start() + } + oldState = status.status + } + } + Timer { id: shortDelay running: false @@ -77,19 +84,11 @@ SmoothItemDelegate { lineWidth: height * 0.1 arcBegin: 0 - arcEnd: 0 + arcEnd: 360 * ( status.progress / 100 ) colorCircle: "grey" onColorCircleChanged: prog.repaint() + onArcEndChanged: prog.repaint() - Timer { - id: prog_refresh - running: status.status === "connecting" - interval: 1 - repeat: true - onTriggered: { - prog.arcEnd = 360 * ( status.progress / 100 ) - } - } Behavior on opacity { NumberAnimation { duration: 200 diff --git a/qml/main.qml b/qml/main.qml index de8c01f..efc96e9 100644 --- a/qml/main.qml +++ b/qml/main.qml @@ -67,18 +67,22 @@ Window { case 1: stateString = "STARTING" settingsDialog.close() + profilesDialog.close() break; case 2: stateString = "WAITING" settingsDialog.close() + profilesDialog.close() break; case 3: stateString = "RUNNING" settingsDialog.close() + profilesDialog.close() break; case 4: stateString = "STOPPED" settingsDialog.close() + profilesDialog.close() } root.state = stateString } diff --git a/sources/baseconn.cpp b/sources/baseconn.cpp index a1c2129..b948530 100644 --- a/sources/baseconn.cpp +++ b/sources/baseconn.cpp @@ -5,8 +5,13 @@ BaseConn * pGlobalBaseConn = nullptr; BaseConn::BaseConn(QObject *parent) : QObject(parent) { pGlobalBaseConn = this; + socket = new QTcpSocket(this); - this->setState("disconnected"); + + this->timeoutTimer = new QTimer(this); + this->timeoutTimer->setSingleShot(true); + + this->state = "disconnected"; connect(this->socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(gotError(QAbstractSocket::SocketError))); @@ -17,30 +22,28 @@ BaseConn::BaseConn(QObject *parent) : QObject(parent) this->connections = QVariantList({}); } -bool BaseConn::connectToHost() { +void BaseConn::connectToHost() { qDebug() << "connecting"; setState("connecting"); this->connection_progress = 0; - QEventLoop loop; - - // quit loop when state changed (successfull connection is handled elswhere - loop.connect(this, SIGNAL(stateChanged()), &loop, SLOT(quit())); + connect(this->timeoutTimer, SIGNAL(timeout()), this, SLOT(connectionTimeout())); //connect this->socket->connectToHost(this->ip, this->port); - while(this->state != "connected" && this->state != "disconnected"){ - loop.exec(); - } + timeoutTimer->start(3000); +} - if(this->state == "connected") { - return true; - } - return false; +void BaseConn::connectionTimeout() { + this->socket->abort(); + disconnect(this->timeoutTimer, SIGNAL(timeout()), this, SLOT(connectionTimeout())); } bool BaseConn::init() { + disconnect(this->timeoutTimer, SIGNAL(timeout()), this, SLOT(connectionTimeout())); + this->timeoutTimer->stop(); + connect(this->socket, &QTcpSocket::readyRead, this, &BaseConn::readyRead); this->connection_progress = 50; @@ -69,13 +72,7 @@ void BaseConn::closeConnection() socket->abort(); } - this->deInit(); 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) @@ -113,7 +110,6 @@ void BaseConn::socketStateChanged(QAbstractSocket::SocketState socketState) { switch (socketState) { case QAbstractSocket::UnconnectedState: { - this->deInit(); this->setState("disconnected"); break; } @@ -143,7 +139,7 @@ QVariantMap BaseConn::sendCommand(int header, QJsonValue data){ // generate id and witing requests entry int thisId = nextConnectionId; - qDebug() << "sending command: " << header << " with data: " << data << " and id: " << thisId; + //qDebug() << "sending command: " << header << " with data: " << data << " and id: " << thisId; nextConnectionId ++; QEventLoop *loop = new QEventLoop(this); @@ -325,6 +321,7 @@ QString BaseConn::getState() const void BaseConn::setState(QString newState){ if(this->state != newState) { + qDebug() << "+--- BaseConn state changed: " << newState; this->state = newState; emit stateChanged(); if(this->state == "disconnected") { diff --git a/sources/climbingrace.cpp b/sources/climbingrace.cpp index 4613f3e..07f61fc 100644 --- a/sources/climbingrace.cpp +++ b/sources/climbingrace.cpp @@ -611,9 +611,9 @@ QString ClimbingRace::readSetting(QString key) { } } -bool ClimbingRace::connectBaseStation() { +void ClimbingRace::connectBaseStation() { this->reloadBaseStationIpAdress(); - return this->baseConn->connectToHost(); + this->baseConn->connectToHost(); } void ClimbingRace::disconnectBaseStation() {