mirror of
https://github.com/kavishdevar/librepods.git
synced 2026-04-09 21:02:53 +00:00
Remove old devices
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
#include "blemanager.h"
|
#include "blemanager.h"
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
|
#include <QTimer>
|
||||||
|
|
||||||
BleManager::BleManager(QObject *parent) : QObject(parent)
|
BleManager::BleManager(QObject *parent) : QObject(parent)
|
||||||
{
|
{
|
||||||
@@ -12,11 +13,17 @@ BleManager::BleManager(QObject *parent) : QObject(parent)
|
|||||||
this, &BleManager::onScanFinished);
|
this, &BleManager::onScanFinished);
|
||||||
connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::errorOccurred,
|
connect(discoveryAgent, &QBluetoothDeviceDiscoveryAgent::errorOccurred,
|
||||||
this, &BleManager::onErrorOccurred);
|
this, &BleManager::onErrorOccurred);
|
||||||
|
|
||||||
|
// Set up pruning timer
|
||||||
|
pruneTimer = new QTimer(this);
|
||||||
|
connect(pruneTimer, &QTimer::timeout, this, &BleManager::pruneOldDevices);
|
||||||
|
pruneTimer->start(PRUNE_INTERVAL_MS); // Start timer (runs every 5 seconds)
|
||||||
}
|
}
|
||||||
|
|
||||||
BleManager::~BleManager()
|
BleManager::~BleManager()
|
||||||
{
|
{
|
||||||
delete discoveryAgent;
|
delete discoveryAgent;
|
||||||
|
delete pruneTimer;
|
||||||
}
|
}
|
||||||
|
|
||||||
void BleManager::startScan()
|
void BleManager::startScan()
|
||||||
@@ -24,6 +31,7 @@ void BleManager::startScan()
|
|||||||
qDebug() << "Starting BLE scan...";
|
qDebug() << "Starting BLE scan...";
|
||||||
devices.clear();
|
devices.clear();
|
||||||
discoveryAgent->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
|
discoveryAgent->start(QBluetoothDeviceDiscoveryAgent::LowEnergyMethod);
|
||||||
|
pruneTimer->start(PRUNE_INTERVAL_MS); // Ensure timer is running
|
||||||
}
|
}
|
||||||
|
|
||||||
void BleManager::stopScan()
|
void BleManager::stopScan()
|
||||||
@@ -111,6 +119,9 @@ void BleManager::onDeviceDiscovered(const QBluetoothDeviceInfo &info)
|
|||||||
else
|
else
|
||||||
deviceInfo.lidState = DeviceInfo::LidState::UNKNOWN;
|
deviceInfo.lidState = DeviceInfo::LidState::UNKNOWN;
|
||||||
|
|
||||||
|
// Update timestamp
|
||||||
|
deviceInfo.lastSeen = QDateTime::currentDateTime();
|
||||||
|
|
||||||
// Store device info in the map
|
// Store device info in the map
|
||||||
devices[address] = deviceInfo;
|
devices[address] = deviceInfo;
|
||||||
|
|
||||||
@@ -136,4 +147,22 @@ void BleManager::onErrorOccurred(QBluetoothDeviceDiscoveryAgent::Error error)
|
|||||||
{
|
{
|
||||||
qDebug() << "Error occurred:" << error;
|
qDebug() << "Error occurred:" << error;
|
||||||
stopScan();
|
stopScan();
|
||||||
|
}
|
||||||
|
|
||||||
|
void BleManager::pruneOldDevices()
|
||||||
|
{
|
||||||
|
QDateTime now = QDateTime::currentDateTime();
|
||||||
|
auto it = devices.begin();
|
||||||
|
while (it != devices.end())
|
||||||
|
{
|
||||||
|
if (it.value().lastSeen.msecsTo(now) > DEVICE_TIMEOUT_MS)
|
||||||
|
{
|
||||||
|
qDebug() << "Removing old device:" << it.value().name << "at" << it.key();
|
||||||
|
it = devices.erase(it); // Remove device if not seen recently
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -5,6 +5,9 @@
|
|||||||
#include <QBluetoothDeviceDiscoveryAgent>
|
#include <QBluetoothDeviceDiscoveryAgent>
|
||||||
#include <QMap>
|
#include <QMap>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
|
#include <QDateTime>
|
||||||
|
|
||||||
|
class QTimer;
|
||||||
|
|
||||||
class DeviceInfo
|
class DeviceInfo
|
||||||
{
|
{
|
||||||
@@ -41,6 +44,8 @@ public:
|
|||||||
UNKNOWN
|
UNKNOWN
|
||||||
};
|
};
|
||||||
LidState lidState = LidState::UNKNOWN;
|
LidState lidState = LidState::UNKNOWN;
|
||||||
|
|
||||||
|
QDateTime lastSeen; // Timestamp of last detection
|
||||||
};
|
};
|
||||||
|
|
||||||
class BleManager : public QObject
|
class BleManager : public QObject
|
||||||
@@ -58,10 +63,15 @@ private slots:
|
|||||||
void onDeviceDiscovered(const QBluetoothDeviceInfo &info);
|
void onDeviceDiscovered(const QBluetoothDeviceInfo &info);
|
||||||
void onScanFinished();
|
void onScanFinished();
|
||||||
void onErrorOccurred(QBluetoothDeviceDiscoveryAgent::Error error);
|
void onErrorOccurred(QBluetoothDeviceDiscoveryAgent::Error error);
|
||||||
|
void pruneOldDevices();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QBluetoothDeviceDiscoveryAgent *discoveryAgent;
|
QBluetoothDeviceDiscoveryAgent *discoveryAgent;
|
||||||
QMap<QString, DeviceInfo> devices;
|
QMap<QString, DeviceInfo> devices;
|
||||||
|
|
||||||
|
QTimer *pruneTimer; // Timer for periodic pruning
|
||||||
|
static const int PRUNE_INTERVAL_MS = 5000; // Check every 5 seconds
|
||||||
|
static const int DEVICE_TIMEOUT_MS = 10000; // Remove after 10 seconds
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // BLEMANAGER_H
|
#endif // BLEMANAGER_H
|
||||||
@@ -131,15 +131,6 @@ BleScanner::BleScanner(QWidget *parent) : QMainWindow(parent)
|
|||||||
mainLayout->addWidget(detailsGroup);
|
mainLayout->addWidget(detailsGroup);
|
||||||
detailsGroup->setVisible(false);
|
detailsGroup->setVisible(false);
|
||||||
|
|
||||||
trayIcon = new QSystemTrayIcon(QIcon::fromTheme("bluetooth"), this);
|
|
||||||
QMenu *trayMenu = new QMenu(this);
|
|
||||||
QAction *showAction = trayMenu->addAction("Show");
|
|
||||||
trayMenu->addSeparator();
|
|
||||||
QAction *exitAction = trayMenu->addAction("Exit");
|
|
||||||
trayIcon->setContextMenu(trayMenu);
|
|
||||||
trayIcon->setToolTip("AirPods Battery Monitor");
|
|
||||||
trayIcon->show();
|
|
||||||
|
|
||||||
bleManager = new BleManager(this);
|
bleManager = new BleManager(this);
|
||||||
refreshTimer = new QTimer(this);
|
refreshTimer = new QTimer(this);
|
||||||
|
|
||||||
@@ -147,8 +138,6 @@ BleScanner::BleScanner(QWidget *parent) : QMainWindow(parent)
|
|||||||
connect(stopButton, &QPushButton::clicked, this, &BleScanner::stopScan);
|
connect(stopButton, &QPushButton::clicked, this, &BleScanner::stopScan);
|
||||||
connect(deviceTable, &QTableWidget::itemSelectionChanged, this, &BleScanner::onDeviceSelected);
|
connect(deviceTable, &QTableWidget::itemSelectionChanged, this, &BleScanner::onDeviceSelected);
|
||||||
connect(refreshTimer, &QTimer::timeout, this, &BleScanner::updateDeviceList);
|
connect(refreshTimer, &QTimer::timeout, this, &BleScanner::updateDeviceList);
|
||||||
connect(showAction, &QAction::triggered, this, &BleScanner::show);
|
|
||||||
connect(exitAction, &QAction::triggered, qApp, &QApplication::quit);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void BleScanner::startScan()
|
void BleScanner::startScan()
|
||||||
@@ -158,7 +147,7 @@ void BleScanner::startScan()
|
|||||||
deviceTable->setRowCount(0);
|
deviceTable->setRowCount(0);
|
||||||
detailsGroup->setVisible(false);
|
detailsGroup->setVisible(false);
|
||||||
bleManager->startScan();
|
bleManager->startScan();
|
||||||
refreshTimer->start(2000);
|
refreshTimer->start(500);
|
||||||
}
|
}
|
||||||
|
|
||||||
void BleScanner::stopScan()
|
void BleScanner::stopScan()
|
||||||
@@ -202,18 +191,8 @@ void BleScanner::updateDeviceList()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!devices.isEmpty())
|
if (deviceTable->selectedItems().isEmpty()) {
|
||||||
{
|
deviceTable->selectRow(0);
|
||||||
auto it = devices.begin();
|
|
||||||
QString leftBat = (it.value().leftPodBattery >= 0 ? QString::number(it.value().leftPodBattery) + "%" : "N/A");
|
|
||||||
QString rightBat = (it.value().rightPodBattery >= 0 ? QString::number(it.value().rightPodBattery) + "%" : "N/A");
|
|
||||||
QString caseBat = (it.value().caseBattery >= 0 ? QString::number(it.value().caseBattery) + "%" : "N/A");
|
|
||||||
QString tooltip = QString("%1\nLeft: %2 | Right: %3 | Case: %4")
|
|
||||||
.arg(it.value().name)
|
|
||||||
.arg(leftBat)
|
|
||||||
.arg(rightBat)
|
|
||||||
.arg(caseBat);
|
|
||||||
trayIcon->setToolTip(tooltip);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -45,7 +45,6 @@ private:
|
|||||||
QLabel *lidStateLabel; // Renamed from lidOpenLabel
|
QLabel *lidStateLabel; // Renamed from lidOpenLabel
|
||||||
QLabel *colorLabel;
|
QLabel *colorLabel;
|
||||||
QLabel *rawDataLabel;
|
QLabel *rawDataLabel;
|
||||||
QSystemTrayIcon *trayIcon;
|
|
||||||
|
|
||||||
// New labels for additional DeviceInfo fields
|
// New labels for additional DeviceInfo fields
|
||||||
QLabel *leftInEarLabel;
|
QLabel *leftInEarLabel;
|
||||||
|
|||||||
Reference in New Issue
Block a user