fixed device clean up on a failed device setup

This commit is contained in:
nymea 2019-09-11 20:14:00 +02:00
parent 7123a92a01
commit be32972ff0
4 changed files with 35 additions and 12 deletions

View File

@ -60,11 +60,6 @@ Device::DeviceError DevicePluginSenic::discoverDevices(const DeviceClassId &devi
Device::DeviceSetupStatus DevicePluginSenic::setupDevice(Device *device) Device::DeviceSetupStatus DevicePluginSenic::setupDevice(Device *device)
{ {
if (!m_reconnectTimer) {
m_reconnectTimer = hardwareManager()->pluginTimerManager()->registerTimer(10);
connect(m_reconnectTimer, &PluginTimer::timeout, this, &DevicePluginSenic::onReconnectTimeout);
}
qCDebug(dcSenic()) << "Setup device" << device->name() << device->params(); qCDebug(dcSenic()) << "Setup device" << device->name() << device->params();
QBluetoothAddress address = QBluetoothAddress(device->paramValue(nuimoDeviceMacParamTypeId).toString()); QBluetoothAddress address = QBluetoothAddress(device->paramValue(nuimoDeviceMacParamTypeId).toString());
@ -89,6 +84,16 @@ Device::DeviceSetupStatus DevicePluginSenic::setupDevice(Device *device)
return Device::DeviceSetupStatusAsync; return Device::DeviceSetupStatusAsync;
} }
void DevicePluginSenic::postSetupDevice(Device *device)
{
Q_UNUSED(device)
if (!m_reconnectTimer) {
m_reconnectTimer = hardwareManager()->pluginTimerManager()->registerTimer(10);
connect(m_reconnectTimer, &PluginTimer::timeout, this, &DevicePluginSenic::onReconnectTimeout);
}
}
Device::DeviceError DevicePluginSenic::executeAction(Device *device, const Action &action) Device::DeviceError DevicePluginSenic::executeAction(Device *device, const Action &action)
{ {
@ -195,12 +200,21 @@ void DevicePluginSenic::onBluetoothDiscoveryFinished()
emit devicesDiscovered(nuimoDeviceClassId, deviceDescriptors); emit devicesDiscovered(nuimoDeviceClassId, deviceDescriptors);
} }
void DevicePluginSenic::onDeviceInitializationFinished() void DevicePluginSenic::onDeviceInitializationFinished(bool success)
{ {
Nuimo *nuimo = static_cast<Nuimo *>(sender()); Nuimo *nuimo = static_cast<Nuimo *>(sender());
Device *device = m_nuimos.value(nuimo); Device *device = m_nuimos.value(nuimo);
if (!device->setupComplete()) { if (!device->setupComplete()) {
emit deviceSetupFinished(device, Device::DeviceSetupStatusSuccess); if (success) {
emit deviceSetupFinished(device, Device::DeviceSetupStatusSuccess);
} else {
m_nuimos.take(nuimo);
hardwareManager()->bluetoothLowEnergyManager()->unregisterDevice(nuimo->bluetoothDevice());
nuimo->deleteLater();
emit deviceSetupFinished(device, Device::DeviceSetupStatusFailure);
}
} }
} }

View File

@ -42,6 +42,7 @@ public:
void init() override; void init() override;
Device::DeviceError discoverDevices(const DeviceClassId &deviceClassId, const ParamList &params) override; Device::DeviceError discoverDevices(const DeviceClassId &deviceClassId, const ParamList &params) override;
Device::DeviceSetupStatus setupDevice(Device *device) override; Device::DeviceSetupStatus setupDevice(Device *device) override;
void postSetupDevice(Device *device) override;
Device::DeviceError executeAction(Device *device, const Action &action) override; Device::DeviceError executeAction(Device *device, const Action &action) override;
void deviceRemoved(Device *device) override; void deviceRemoved(Device *device) override;
@ -56,7 +57,7 @@ private slots:
void onReconnectTimeout(); void onReconnectTimeout();
void onBluetoothDiscoveryFinished(); void onBluetoothDiscoveryFinished();
void onDeviceInitializationFinished(); void onDeviceInitializationFinished(bool success);
void onConnectedChanged(bool connected); void onConnectedChanged(bool connected);
void onBatteryValueChanged(const uint &percentage); void onBatteryValueChanged(const uint &percentage);
void onButtonPressed(); void onButtonPressed();

View File

@ -295,7 +295,7 @@ void Nuimo::onLongPressTimer()
void Nuimo::onConnectedChanged(const bool &connected) void Nuimo::onConnectedChanged(bool connected)
{ {
qCDebug(dcSenic()) << m_bluetoothDevice->name() << m_bluetoothDevice->address().toString() << (connected ? "connected" : "disconnected"); qCDebug(dcSenic()) << m_bluetoothDevice->name() << m_bluetoothDevice->address().toString() << (connected ? "connected" : "disconnected");
@ -323,21 +323,25 @@ void Nuimo::onServiceDiscoveryFinished()
if (!m_bluetoothDevice->serviceUuids().contains(QBluetoothUuid::DeviceInformation)) { if (!m_bluetoothDevice->serviceUuids().contains(QBluetoothUuid::DeviceInformation)) {
qCWarning(dcSenic()) << "Device Information service not found for device" << bluetoothDevice()->name() << bluetoothDevice()->address().toString(); qCWarning(dcSenic()) << "Device Information service not found for device" << bluetoothDevice()->name() << bluetoothDevice()->address().toString();
emit deviceInitializationFinished(false);
return; return;
} }
if (!m_bluetoothDevice->serviceUuids().contains(QBluetoothUuid::BatteryService)) { if (!m_bluetoothDevice->serviceUuids().contains(QBluetoothUuid::BatteryService)) {
qCWarning(dcSenic()) << "Battery service not found for device" << bluetoothDevice()->name() << bluetoothDevice()->address().toString(); qCWarning(dcSenic()) << "Battery service not found for device" << bluetoothDevice()->name() << bluetoothDevice()->address().toString();
emit deviceInitializationFinished(false);
return; return;
} }
if (!m_bluetoothDevice->serviceUuids().contains(ledMatrinxServiceUuid)) { if (!m_bluetoothDevice->serviceUuids().contains(ledMatrinxServiceUuid)) {
qCWarning(dcSenic()) << "Led matrix service not found for device" << bluetoothDevice()->name() << bluetoothDevice()->address().toString(); qCWarning(dcSenic()) << "Led matrix service not found for device" << bluetoothDevice()->name() << bluetoothDevice()->address().toString();
emit deviceInitializationFinished(false);
return; return;
} }
if (!m_bluetoothDevice->serviceUuids().contains(inputServiceUuid)) { if (!m_bluetoothDevice->serviceUuids().contains(inputServiceUuid)) {
qCWarning(dcSenic()) << "Input service not found for device" << bluetoothDevice()->name() << bluetoothDevice()->address().toString(); qCWarning(dcSenic()) << "Input service not found for device" << bluetoothDevice()->name() << bluetoothDevice()->address().toString();
emit deviceInitializationFinished(false);
return; return;
} }
@ -346,6 +350,7 @@ void Nuimo::onServiceDiscoveryFinished()
m_deviceInfoService = m_bluetoothDevice->controller()->createServiceObject(QBluetoothUuid::DeviceInformation, this); m_deviceInfoService = m_bluetoothDevice->controller()->createServiceObject(QBluetoothUuid::DeviceInformation, this);
if (!m_deviceInfoService) { if (!m_deviceInfoService) {
qCWarning(dcSenic()) << "Could not create device info service."; qCWarning(dcSenic()) << "Could not create device info service.";
emit deviceInitializationFinished(false);
return; return;
} }
@ -361,6 +366,7 @@ void Nuimo::onServiceDiscoveryFinished()
m_batteryService = m_bluetoothDevice->controller()->createServiceObject(QBluetoothUuid::BatteryService, this); m_batteryService = m_bluetoothDevice->controller()->createServiceObject(QBluetoothUuid::BatteryService, this);
if (!m_batteryService) { if (!m_batteryService) {
qCWarning(dcSenic()) << "Could not create battery service."; qCWarning(dcSenic()) << "Could not create battery service.";
emit deviceInitializationFinished(false);
return; return;
} }
@ -377,6 +383,7 @@ void Nuimo::onServiceDiscoveryFinished()
m_inputService = m_bluetoothDevice->controller()->createServiceObject(inputServiceUuid, this); m_inputService = m_bluetoothDevice->controller()->createServiceObject(inputServiceUuid, this);
if (!m_inputService) { if (!m_inputService) {
qCWarning(dcSenic()) << "Could not create input service."; qCWarning(dcSenic()) << "Could not create input service.";
emit deviceInitializationFinished(false);
return; return;
} }
@ -393,6 +400,7 @@ void Nuimo::onServiceDiscoveryFinished()
m_ledMatrixService = m_bluetoothDevice->controller()->createServiceObject(ledMatrinxServiceUuid, this); m_ledMatrixService = m_bluetoothDevice->controller()->createServiceObject(ledMatrinxServiceUuid, this);
if (!m_ledMatrixService) { if (!m_ledMatrixService) {
qCWarning(dcSenic()) << "Could not create led matrix service."; qCWarning(dcSenic()) << "Could not create led matrix service.";
emit deviceInitializationFinished(false);
return; return;
} }
@ -402,7 +410,7 @@ void Nuimo::onServiceDiscoveryFinished()
m_ledMatrixService->discoverDetails(); m_ledMatrixService->discoverDetails();
} }
} }
emit deviceInitializationFinished(); emit deviceInitializationFinished(true);
} }
void Nuimo::onDeviceInfoServiceStateChanged(const QLowEnergyService::ServiceState &state) void Nuimo::onDeviceInfoServiceStateChanged(const QLowEnergyService::ServiceState &state)

View File

@ -95,10 +95,10 @@ signals:
void swipeDetected(const SwipeDirection &direction); void swipeDetected(const SwipeDirection &direction);
void rotationValueChanged(const uint &value); void rotationValueChanged(const uint &value);
void deviceInformationChanged(const QString &firmwareRevision, const QString &hardwareRevision, const QString &softwareRevision); void deviceInformationChanged(const QString &firmwareRevision, const QString &hardwareRevision, const QString &softwareRevision);
void deviceInitializationFinished(); void deviceInitializationFinished(bool success);
private slots: private slots:
void onConnectedChanged(const bool &connected); void onConnectedChanged(bool connected);
void onServiceDiscoveryFinished(); void onServiceDiscoveryFinished();
void onDeviceInfoServiceStateChanged(const QLowEnergyService::ServiceState &state); void onDeviceInfoServiceStateChanged(const QLowEnergyService::ServiceState &state);