Update nuimo plugin

master
Michael Zanetti 2019-09-19 20:11:44 +02:00
parent ae5ebcbc58
commit 6ce95f7bda
2 changed files with 63 additions and 76 deletions

View File

@ -38,28 +38,50 @@ void DevicePluginSenic::init()
} }
Device::DeviceError DevicePluginSenic::discoverDevices(const DeviceClassId &deviceClassId, const ParamList &params) void DevicePluginSenic::discoverDevices(DeviceDiscoveryInfo *info)
{ {
Q_UNUSED(params)
if (deviceClassId != nuimoDeviceClassId)
return Device::DeviceErrorDeviceClassNotFound;
if (!hardwareManager()->bluetoothLowEnergyManager()->available()) if (!hardwareManager()->bluetoothLowEnergyManager()->available())
return Device::DeviceErrorHardwareNotAvailable; return info->finish(Device::DeviceErrorHardwareNotAvailable, QT_TR_NOOP("Bluetooth is not available on this system."));
if (!hardwareManager()->bluetoothLowEnergyManager()->enabled()) if (!hardwareManager()->bluetoothLowEnergyManager()->enabled())
return Device::DeviceErrorHardwareNotAvailable; return info->finish(Device::DeviceErrorHardwareNotAvailable, QT_TR_NOOP("Bluetooth is disabled. Please enable Bluetooth and try again."));
BluetoothDiscoveryReply *reply = hardwareManager()->bluetoothLowEnergyManager()->discoverDevices(); BluetoothDiscoveryReply *reply = hardwareManager()->bluetoothLowEnergyManager()->discoverDevices();
connect(reply, &BluetoothDiscoveryReply::finished, this, &DevicePluginSenic::onBluetoothDiscoveryFinished); connect(reply, &BluetoothDiscoveryReply::finished, reply, &BluetoothDiscoveryReply::deleteLater);
return Device::DeviceErrorAsync; connect(reply, &BluetoothDiscoveryReply::finished, info, [this, info, reply](){
if (reply->error() != BluetoothDiscoveryReply::BluetoothDiscoveryReplyErrorNoError) {
qCWarning(dcSenic()) << "Bluetooth discovery error:" << reply->error();
info->finish(Device::DeviceErrorHardwareFailure, QT_TR_NOOP("An error happened during Bluetooth discovery."));
return;
}
foreach (const QBluetoothDeviceInfo &deviceInfo, reply->discoveredDevices()) {
if (deviceInfo.name().contains("Nuimo")) {
DeviceDescriptor descriptor(nuimoDeviceClassId, "Nuimo", deviceInfo.name() + " (" + deviceInfo.address().toString() + ")");
ParamList params;
foreach (Device *existingDevice, myDevices()) {
if (existingDevice->paramValue(nuimoDeviceMacParamTypeId).toString() == deviceInfo.address().toString()) {
descriptor.setDeviceId(existingDevice->id());
break;
}
}
params.append(Param(nuimoDeviceMacParamTypeId, deviceInfo.address().toString()));
descriptor.setParams(params);
info->addDeviceDescriptor(descriptor);
}
}
info->finish(Device::DeviceErrorNoError);
});
} }
Device::DeviceSetupStatus DevicePluginSenic::setupDevice(Device *device) void DevicePluginSenic::setupDevice(DeviceSetupInfo *info)
{ {
Device *device = info->device();
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());
@ -69,7 +91,6 @@ Device::DeviceSetupStatus DevicePluginSenic::setupDevice(Device *device)
Nuimo *nuimo = new Nuimo(bluetoothDevice, this); Nuimo *nuimo = new Nuimo(bluetoothDevice, this);
nuimo->setLongPressTime(configValue(senicPluginLongPressTimeParamTypeId).toInt()); nuimo->setLongPressTime(configValue(senicPluginLongPressTimeParamTypeId).toInt());
connect(nuimo, &Nuimo::deviceInitializationFinished, this, &DevicePluginSenic::onDeviceInitializationFinished);
connect(nuimo, &Nuimo::buttonPressed, this, &DevicePluginSenic::onButtonPressed); connect(nuimo, &Nuimo::buttonPressed, this, &DevicePluginSenic::onButtonPressed);
connect(nuimo, &Nuimo::buttonLongPressed, this, &DevicePluginSenic::onButtonLongPressed); connect(nuimo, &Nuimo::buttonLongPressed, this, &DevicePluginSenic::onButtonLongPressed);
connect(nuimo, &Nuimo::swipeDetected, this, &DevicePluginSenic::onSwipeDetected); connect(nuimo, &Nuimo::swipeDetected, this, &DevicePluginSenic::onSwipeDetected);
@ -79,9 +100,27 @@ Device::DeviceSetupStatus DevicePluginSenic::setupDevice(Device *device)
connect(nuimo, &Nuimo::batteryValueChanged, this, &DevicePluginSenic::onBatteryValueChanged); connect(nuimo, &Nuimo::batteryValueChanged, this, &DevicePluginSenic::onBatteryValueChanged);
m_nuimos.insert(nuimo, device); m_nuimos.insert(nuimo, device);
nuimo->bluetoothDevice()->connectDevice();
return Device::DeviceSetupStatusAsync; connect(nuimo, &Nuimo::deviceInitializationFinished, info, [this, info, nuimo](bool success){
Device *device = info->device();
if (!device->setupComplete()) {
if (success) {
info->finish(Device::DeviceErrorNoError);
} else {
m_nuimos.take(nuimo);
hardwareManager()->bluetoothLowEnergyManager()->unregisterDevice(nuimo->bluetoothDevice());
nuimo->deleteLater();
info->finish(Device::DeviceErrorHardwareFailure, QT_TR_NOOP("Error connecting to nuimo."));
}
}
});
nuimo->bluetoothDevice()->connectDevice();
} }
void DevicePluginSenic::postSetupDevice(Device *device) void DevicePluginSenic::postSetupDevice(Device *device)
@ -95,14 +134,17 @@ void DevicePluginSenic::postSetupDevice(Device *device)
} }
Device::DeviceError DevicePluginSenic::executeAction(Device *device, const Action &action) void DevicePluginSenic::executeAction(DeviceActionInfo *info)
{ {
Device *device = info->device();
Action action = info->action();
QPointer<Nuimo> nuimo = m_nuimos.key(device); QPointer<Nuimo> nuimo = m_nuimos.key(device);
if (nuimo.isNull()) if (nuimo.isNull())
return Device::DeviceErrorHardwareFailure; return info->finish(Device::DeviceErrorHardwareFailure);
if (!nuimo->bluetoothDevice()->connected()) { if (!nuimo->bluetoothDevice()->connected()) {
return Device::DeviceErrorHardwareNotAvailable; return info->finish(Device::DeviceErrorHardwareNotAvailable);
} }
if (action.actionTypeId() == nuimoShowLogoActionTypeId) { if (action.actionTypeId() == nuimoShowLogoActionTypeId) {
@ -134,10 +176,8 @@ Device::DeviceError DevicePluginSenic::executeAction(Device *device, const Actio
if (action.param(nuimoShowLogoActionLogoParamTypeId).value().toString() == "Light") if (action.param(nuimoShowLogoActionLogoParamTypeId).value().toString() == "Light")
nuimo->showImage(Nuimo::MatrixTypeLight); nuimo->showImage(Nuimo::MatrixTypeLight);
return Device::DeviceErrorNoError; return info->finish(Device::DeviceErrorNoError);
} }
return Device::DeviceErrorActionTypeNotFound;
} }
@ -168,57 +208,6 @@ void DevicePluginSenic::onReconnectTimeout()
} }
} }
void DevicePluginSenic::onBluetoothDiscoveryFinished()
{
BluetoothDiscoveryReply *reply = static_cast<BluetoothDiscoveryReply *>(sender());
reply->deleteLater();
if (reply->error() != BluetoothDiscoveryReply::BluetoothDiscoveryReplyErrorNoError) {
qCWarning(dcSenic()) << "Bluetooth discovery error:" << reply->error();
emit devicesDiscovered(nuimoDeviceClassId, QList<DeviceDescriptor>());
return;
}
QList<DeviceDescriptor> deviceDescriptors;
foreach (const QBluetoothDeviceInfo &deviceInfo, reply->discoveredDevices()) {
if (deviceInfo.name().contains("Nuimo")) {
DeviceDescriptor descriptor(nuimoDeviceClassId, "Nuimo", deviceInfo.name() + " (" + deviceInfo.address().toString() + ")");
ParamList params;
foreach (Device *existingDevice, myDevices()) {
if (existingDevice->paramValue(nuimoDeviceMacParamTypeId).toString() == deviceInfo.address().toString()) {
descriptor.setDeviceId(existingDevice->id());
break;
}
}
params.append(Param(nuimoDeviceMacParamTypeId, deviceInfo.address().toString()));
descriptor.setParams(params);
deviceDescriptors.append(descriptor);
}
}
emit devicesDiscovered(nuimoDeviceClassId, deviceDescriptors);
}
void DevicePluginSenic::onDeviceInitializationFinished(bool success)
{
Nuimo *nuimo = static_cast<Nuimo *>(sender());
Device *device = m_nuimos.value(nuimo);
if (!device->setupComplete()) {
if (success) {
emit deviceSetupFinished(device, Device::DeviceSetupStatusSuccess);
} else {
m_nuimos.take(nuimo);
hardwareManager()->bluetoothLowEnergyManager()->unregisterDevice(nuimo->bluetoothDevice());
nuimo->deleteLater();
emit deviceSetupFinished(device, Device::DeviceSetupStatusFailure);
}
}
}
void DevicePluginSenic::onConnectedChanged(bool connected) void DevicePluginSenic::onConnectedChanged(bool connected)
{ {
Nuimo *nuimo = static_cast<Nuimo *>(sender()); Nuimo *nuimo = static_cast<Nuimo *>(sender());

View File

@ -40,10 +40,10 @@ public:
explicit DevicePluginSenic(); explicit DevicePluginSenic();
void init() override; void init() override;
Device::DeviceError discoverDevices(const DeviceClassId &deviceClassId, const ParamList &params) override; void discoverDevices(DeviceDiscoveryInfo *info) override;
Device::DeviceSetupStatus setupDevice(Device *device) override; void setupDevice(DeviceSetupInfo *info) override;
void postSetupDevice(Device *device) override; void postSetupDevice(Device *device) override;
Device::DeviceError executeAction(Device *device, const Action &action) override; void executeAction(DeviceActionInfo *info) override;
void deviceRemoved(Device *device) override; void deviceRemoved(Device *device) override;
@ -55,9 +55,7 @@ private:
private slots: private slots:
void onPluginConfigurationChanged(const ParamTypeId &paramTypeId, const QVariant &value); void onPluginConfigurationChanged(const ParamTypeId &paramTypeId, const QVariant &value);
void onReconnectTimeout(); void onReconnectTimeout();
void onBluetoothDiscoveryFinished();
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();