Skip to content

Commit 47ea3c2

Browse files
authored
QZ silently loses connection with Taurus FX9.9 elliptical (Issue #3933) (#3935)
1 parent c83c272 commit 47ea3c2

2 files changed

Lines changed: 61 additions & 7 deletions

File tree

src/devices/trxappgateusbelliptical/trxappgateusbelliptical.cpp

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,29 @@ void trxappgateusbelliptical::update() {
8484
QSettings settings;
8585
update_metrics(true, watts());
8686

87+
// Restore resistance after reconnection and init
88+
if (needsResistanceRestore && lastResistanceBeforeDisconnection > 0) {
89+
qDebug() << QStringLiteral("Restoring resistance after reconnection:") << lastResistanceBeforeDisconnection;
90+
forceResistance(lastResistanceBeforeDisconnection);
91+
needsResistanceRestore = false;
92+
lastResistanceBeforeDisconnection = -1;
93+
}
94+
95+
// Calculate time since last valid packet
96+
qint64 msSinceLastValidPacket = lastValidPacketTime.msecsTo(QDateTime::currentDateTime());
97+
98+
// If we haven't received a valid packet for more than 5 seconds, reinitialize
99+
if (msSinceLastValidPacket > 5000) {
100+
qDebug() << QStringLiteral("NO VALID PACKETS for") << (msSinceLastValidPacket / 1000.0)
101+
<< QStringLiteral("seconds. Reinitializing connection...");
102+
103+
// Reset timer
104+
lastValidPacketTime = QDateTime::currentDateTime();
105+
106+
m_control->disconnectFromDevice();
107+
}
108+
109+
87110
{
88111
if (requestResistance != -1) {
89112
if (requestResistance < 1)
@@ -191,10 +214,24 @@ void trxappgateusbelliptical::characteristicChanged(const QLowEnergyCharacterist
191214

192215
lastPacket = newValue;
193216

194-
if(newValue.length() != 21) {
217+
lastValidPacketTime = QDateTime::currentDateTime();
218+
219+
// Check for invalid packet length first
220+
bool isValidPacket = (newValue.length() == 21);
221+
222+
if (!isValidPacket) {
223+
// Invalid packet length - log and return
224+
qDebug() << QStringLiteral("Invalid packet length:") << newValue.length();
195225
return;
196226
}
197227

228+
// Log controller errors but don't block processing of valid packets
229+
bool hasError = (m_control->error() != QLowEnergyController::NoError);
230+
if (hasError) {
231+
qDebug() << QStringLiteral("QLowEnergyController ERROR!!") << m_control->errorString();
232+
// Continue processing - the packet is still valid
233+
}
234+
198235
Resistance = newValue.at(18) - 1;
199236
Speed = GetSpeedFromPacket(newValue);
200237
Cadence = (GetCadenceFromPacket(newValue) * cadence_gain) + cadence_offset;
@@ -227,10 +264,6 @@ void trxappgateusbelliptical::characteristicChanged(const QLowEnergyCharacterist
227264
emit debug(QStringLiteral("Current Calculate Distance: ") + QString::number(Distance.value()));
228265
// debug("Current Distance: " + QString::number(distance));
229266
emit debug(QStringLiteral("Current Watt: ") + QString::number(watts()));
230-
231-
if (m_control->error() != QLowEnergyController::NoError) {
232-
qDebug() << QStringLiteral("QLowEnergyController ERROR!!") << m_control->errorString();
233-
}
234267
}
235268

236269
void trxappgateusbelliptical::btinit() {
@@ -497,9 +530,26 @@ bool trxappgateusbelliptical::connected() {
497530
void trxappgateusbelliptical::controllerStateChanged(QLowEnergyController::ControllerState state) {
498531
qDebug() << QStringLiteral("controllerStateChanged") << state;
499532
if (state == QLowEnergyController::UnconnectedState && m_control) {
500-
qDebug() << QStringLiteral("trying to connect back again...");
533+
qDebug() << QStringLiteral("trying to connect back again in 3 seconds...");
534+
535+
// Save current resistance before disconnection
536+
if (Resistance.value() > 0) {
537+
lastResistanceBeforeDisconnection = Resistance.value();
538+
needsResistanceRestore = true;
539+
qDebug() << QStringLiteral("Saved resistance before disconnection:") << lastResistanceBeforeDisconnection;
540+
}
541+
501542
initDone = false;
502-
m_control->connectToDevice();
543+
544+
// Schedule reconnection after 3 seconds
545+
QTimer::singleShot(3000, this, [this]() {
546+
if (m_control && m_control->state() == QLowEnergyController::UnconnectedState) {
547+
qDebug() << QStringLiteral("Reconnection timer fired, attempting to reconnect...");
548+
// Reset the last valid packet timer
549+
lastValidPacketTime = QDateTime::currentDateTime();
550+
m_control->connectToDevice();
551+
}
552+
});
503553
}
504554
}
505555

src/devices/trxappgateusbelliptical/trxappgateusbelliptical.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class trxappgateusbelliptical : public elliptical {
5959

6060
uint8_t sec1Update = 0;
6161
QByteArray lastPacket;
62+
QDateTime lastValidPacketTime = QDateTime::currentDateTime();
6263
QDateTime lastRefreshCharacteristicChanged = QDateTime::currentDateTime();
6364
uint8_t firstStateChanged = 0;
6465
int8_t bikeResistanceOffset = 4;
@@ -69,6 +70,9 @@ class trxappgateusbelliptical : public elliptical {
6970
bool initDone = false;
7071
bool initRequest = false;
7172

73+
resistance_t lastResistanceBeforeDisconnection = -1;
74+
bool needsResistanceRestore = false;
75+
7276
bool noWriteResistance = false;
7377
bool noHeartService = false;
7478

0 commit comments

Comments
 (0)