Commit 2fd70162 authored by Gus Grubba's avatar Gus Grubba

Prevent Microhard handler from locking main thread

parent ec7f7406
...@@ -42,21 +42,27 @@ MicrohardHandler::close() ...@@ -42,21 +42,27 @@ MicrohardHandler::close()
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
bool void
MicrohardHandler::_start(uint16_t port, QHostAddress addr) MicrohardHandler::_start(uint16_t port, QHostAddress addr)
{ {
close(); close();
_tcpSocket = new QTcpSocket(); _tcpSocket = new QTcpSocket();
QObject::connect(_tcpSocket, &QIODevice::readyRead, this, &MicrohardHandler::_readBytes); QObject::connect(_tcpSocket, &QIODevice::readyRead, this, &MicrohardHandler::_readBytes);
qCDebug(MicrohardLog) << "Connecting to" << addr; qCDebug(MicrohardLog) << "Connecting to" << addr;
_tcpSocket->connectToHost(addr, port); _tcpSocket->connectToHost(addr, port);
//-- TODO: This has to be removed. It's blocking the main thread. QTimer::singleShot(1000, this, &MicrohardHandler::_testConnection);
if (!_tcpSocket->waitForConnected(1000)) { }
//-----------------------------------------------------------------------------
void
MicrohardHandler::_testConnection()
{
if(_tcpSocket) {
if(_tcpSocket->state() == QAbstractSocket::ConnectedState) {
qCDebug(MicrohardLog) << "Connected";
return;
}
emit connected(0); emit connected(0);
close(); close();
return false;
} }
return true;
} }
...@@ -29,10 +29,11 @@ public: ...@@ -29,10 +29,11 @@ public:
virtual bool close (); virtual bool close ();
protected: protected:
virtual bool _start (uint16_t port, QHostAddress addr = QHostAddress::AnyIPv4); virtual void _start (uint16_t port, QHostAddress addr = QHostAddress::AnyIPv4);
protected slots: protected slots:
virtual void _readBytes () = 0; virtual void _readBytes () = 0;
virtual void _testConnection ();
signals: signals:
void connected (int status); void connected (int status);
......
...@@ -205,7 +205,13 @@ MicrohardManager::_setEnabled() ...@@ -205,7 +205,13 @@ MicrohardManager::_setEnabled()
void void
MicrohardManager::_connectedLoc(int status) MicrohardManager::_connectedLoc(int status)
{ {
qCDebug(MicrohardLog) << "GND Microhard Settings Connected"; static const char* msg = "GND Microhard Settings: ";
if(status > 0)
qCDebug(MicrohardLog) << msg << "Connected";
else if(status < 0)
qCDebug(MicrohardLog) << msg << "Error";
else
qCDebug(MicrohardLog) << msg << "Not Connected";
_connectedStatus = status; _connectedStatus = status;
_locTimer.start(LONG_TIMEOUT); _locTimer.start(LONG_TIMEOUT);
emit connectedChanged(); emit connectedChanged();
...@@ -215,7 +221,13 @@ MicrohardManager::_connectedLoc(int status) ...@@ -215,7 +221,13 @@ MicrohardManager::_connectedLoc(int status)
void void
MicrohardManager::_connectedRem(int status) MicrohardManager::_connectedRem(int status)
{ {
qCDebug(MicrohardLog) << "AIR Microhard Settings Connected"; static const char* msg = "AIR Microhard Settings: ";
if(status > 0)
qCDebug(MicrohardLog) << msg << "Connected";
else if(status < 0)
qCDebug(MicrohardLog) << msg << "Error";
else
qCDebug(MicrohardLog) << msg << "Not Connected";
_linkConnectedStatus = status; _linkConnectedStatus = status;
_remTimer.start(LONG_TIMEOUT); _remTimer.start(LONG_TIMEOUT);
emit linkConnectedChanged(); emit linkConnectedChanged();
......
...@@ -27,7 +27,8 @@ MicrohardSettings::start() ...@@ -27,7 +27,8 @@ MicrohardSettings::start()
{ {
qCDebug(MicrohardLog) << "Start Microhard Settings"; qCDebug(MicrohardLog) << "Start Microhard Settings";
_loggedIn = false; _loggedIn = false;
return _start(MICROHARD_SETTINGS_PORT, QHostAddress(_address)); _start(MICROHARD_SETTINGS_PORT, QHostAddress(_address));
return true;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment