From a0fd7c6fdbd29c7905656599a0d614db5a4e89ea Mon Sep 17 00:00:00 2001 From: Boernsman Date: Fri, 29 Jan 2021 15:23:46 +0100 Subject: [PATCH] updated lukeroberts --- lukeroberts/integrationpluginlukeroberts.cpp | 123 ++++++++++--------- lukeroberts/integrationpluginlukeroberts.h | 1 - lukeroberts/lukeroberts.cpp | 17 ++- lukeroberts/lukeroberts.h | 4 +- 4 files changed, 79 insertions(+), 66 deletions(-) diff --git a/lukeroberts/integrationpluginlukeroberts.cpp b/lukeroberts/integrationpluginlukeroberts.cpp index 91f64596..b961b66c 100644 --- a/lukeroberts/integrationpluginlukeroberts.cpp +++ b/lukeroberts/integrationpluginlukeroberts.cpp @@ -91,42 +91,48 @@ void IntegrationPluginLukeRoberts::setupThing(ThingSetupInfo *info) qCDebug(dcLukeRoberts()) << "Setup device" << thing->name() << thing->params(); - QBluetoothAddress address = QBluetoothAddress(thing->paramValue(modelFThingMacParamTypeId).toString()); - QBluetoothDeviceInfo deviceInfo = QBluetoothDeviceInfo(address, thing->name(), 0); + if (thing->thingClassId() == modelFThingClassId) { + QBluetoothAddress address = QBluetoothAddress(thing->paramValue(modelFThingMacParamTypeId).toString()); + QBluetoothDeviceInfo deviceInfo = QBluetoothDeviceInfo(address, thing->name(), 0); - BluetoothLowEnergyDevice *bluetoothDevice = hardwareManager()->bluetoothLowEnergyManager()->registerDevice(deviceInfo, QLowEnergyController::RandomAddress); + BluetoothLowEnergyDevice *bluetoothDevice = hardwareManager()->bluetoothLowEnergyManager()->registerDevice(deviceInfo, QLowEnergyController::RandomAddress); - LukeRoberts *lamp = new LukeRoberts(bluetoothDevice, this); - connect(lamp, &LukeRoberts::connectedChanged, this, &IntegrationPluginLukeRoberts::onConnectedChanged); - connect(lamp, &LukeRoberts::deviceInformationChanged, this, &IntegrationPluginLukeRoberts::onDeviceInformationChanged); + LukeRoberts *lamp = new LukeRoberts(bluetoothDevice); + connect(lamp, &LukeRoberts::connectedChanged, this, &IntegrationPluginLukeRoberts::onConnectedChanged); + connect(lamp, &LukeRoberts::deviceInformationChanged, this, &IntegrationPluginLukeRoberts::onDeviceInformationChanged); + connect(lamp, &LukeRoberts::statusCodeReveiced, this, &IntegrationPluginLukeRoberts::onStatusCodeReceived); + connect(lamp, &LukeRoberts::sceneListReceived, this, &IntegrationPluginLukeRoberts::onSceneListReceived); + connect(lamp, &LukeRoberts::destroyed, this, [this, lamp] {m_lamps.remove(lamp);}); - m_lamps.insert(lamp, thing); + m_lamps.insert(lamp, thing); + connect(lamp, &LukeRoberts::deviceInitializationFinished, info, [this, info, lamp](bool success){ + qCDebug(dcLukeRoberts()) << "Lamp device initialization finished"; + Thing *thing = info->thing(); - connect(lamp, &LukeRoberts::deviceInitializationFinished, info, [this, info, lamp](bool success){ - Thing *thing = info->thing(); + if (!thing->setupComplete()) { + if (success) { + info->finish(Thing::ThingErrorNoError); + } else { + m_lamps.take(lamp); - if (!thing->setupComplete()) { - if (success) { - info->finish(Thing::ThingErrorNoError); - } else { - m_lamps.take(lamp); + hardwareManager()->bluetoothLowEnergyManager()->unregisterDevice(lamp->bluetoothDevice()); + lamp->deleteLater(); - hardwareManager()->bluetoothLowEnergyManager()->unregisterDevice(lamp->bluetoothDevice()); - lamp->deleteLater(); - - info->finish(Thing::ThingErrorHardwareFailure, QT_TR_NOOP("Error connecting to lamp.")); + info->finish(Thing::ThingErrorHardwareFailure, QT_TR_NOOP("Error connecting to lamp.")); + } } - } - }); - lamp->bluetoothDevice()->connectDevice(); + }); + lamp->bluetoothDevice()->connectDevice(); + } } void IntegrationPluginLukeRoberts::postSetupThing(Thing *thing) { - Q_UNUSED(thing) + qCDebug(dcLukeRoberts()) << "Post setup device" << thing->name(); if (!m_reconnectTimer) { + qCDebug(dcLukeRoberts()) << "Initializing reconnect timer"; m_reconnectTimer = hardwareManager()->pluginTimerManager()->registerTimer(10); connect(m_reconnectTimer, &PluginTimer::timeout, this, &IntegrationPluginLukeRoberts::onReconnectTimeout); } @@ -138,49 +144,58 @@ void IntegrationPluginLukeRoberts::executeAction(ThingActionInfo *info) Thing *thing = info->thing(); Action action = info->action(); - QPointer lamp = m_lamps.key(thing); - if (lamp.isNull()) - return info->finish(Thing::ThingErrorHardwareFailure); + if (thing->thingClassId() == modelFThingClassId) { + QPointer lamp = m_lamps.key(thing); + if (lamp.isNull()) + return info->finish(Thing::ThingErrorHardwareFailure); - if (!lamp->bluetoothDevice()->connected()) { - return info->finish(Thing::ThingErrorHardwareNotAvailable); - } - - if (action.actionTypeId() == modelFPowerActionTypeId) { - bool power = action.param(modelFPowerActionPowerParamTypeId).value().toBool(); - if (!power) { - lamp->selectScene(0); //Scene 0 is the off scene - } else { - lamp->selectScene(0xff); // Scene 0xff is the default scene + if (!lamp->bluetoothDevice()->connected()) { + return info->finish(Thing::ThingErrorHardwareNotAvailable); } - } else if (action.actionTypeId() == modelFBrightnessActionTypeId) { - int brightness = action.param(modelFBrightnessActionBrightnessParamTypeId).value().toInt(); - lamp->modifyBrightness(brightness); + if (action.actionTypeId() == modelFPowerActionTypeId) { + bool power = action.param(modelFPowerActionPowerParamTypeId).value().toBool(); + if (!power) { + lamp->selectScene(0); //Scene 0 is the off scene + } else { + lamp->selectScene(0xff); // Scene 0xff is the default scene + } - } else if (action.actionTypeId() == modelFColorActionTypeId) { - QColor rgb = QColor::fromRgb(QRgb(action.param(modelFColorActionColorParamTypeId).value().toInt())); - lamp->setImmediateLight(0, rgb.saturation(), rgb.hue(), rgb.lightness()); + } else if (action.actionTypeId() == modelFBrightnessActionTypeId) { + int brightness = action.param(modelFBrightnessActionBrightnessParamTypeId).value().toInt(); + lamp->modifyBrightness(brightness); + + } else if (action.actionTypeId() == modelFColorActionTypeId) { + QColor rgb = QColor::fromRgb(QRgb(action.param(modelFColorActionColorParamTypeId).value().toInt())); + lamp->setImmediateLight(0, rgb.saturation(), rgb.hue(), rgb.lightness()); + + } else if (action.actionTypeId() == modelFColorTemperatureActionTypeId) { + int kelvin = action.param(modelFColorTemperatureActionColorTemperatureParamTypeId).value().toInt(); + lamp->modifyColorTemperature(kelvin); + } + } else { - } else if (action.actionTypeId() == modelFColorTemperatureActionTypeId) { - int kelvin = action.param(modelFColorTemperatureActionColorTemperatureParamTypeId).value().toInt(); - lamp->modifyColorTemperature(kelvin); } } void IntegrationPluginLukeRoberts::thingRemoved(Thing *thing) { - if (!m_lamps.values().contains(thing)) - return; + qCDebug(dcLukeRoberts()) << "Delete thing" << thing->name(); - LukeRoberts *lamp = m_lamps.key(thing); - m_lamps.take(lamp); + if (thing->thingClassId() == modelFThingClassId) { + if (!m_lamps.values().contains(thing)) + return; - hardwareManager()->bluetoothLowEnergyManager()->unregisterDevice(lamp->bluetoothDevice()); - lamp->deleteLater(); + LukeRoberts *lamp = m_lamps.key(thing); + m_lamps.remove(lamp); + + hardwareManager()->bluetoothLowEnergyManager()->unregisterDevice(lamp->bluetoothDevice()); + lamp->deleteLater(); + } if (myThings().isEmpty()) { + qCDebug(dcLukeRoberts()) << "Unregistering reconnect timer"; hardwareManager()->pluginTimerManager()->unregisterTimer(m_reconnectTimer); m_reconnectTimer = nullptr; } @@ -206,7 +221,6 @@ void IntegrationPluginLukeRoberts::browserItem(BrowserItemResult *result) return; } qDebug(dcLukeRoberts()) << "Browse item called"; - } void IntegrationPluginLukeRoberts::executeBrowserItem(BrowserActionInfo *info) @@ -229,13 +243,6 @@ void IntegrationPluginLukeRoberts::executeBrowserItemAction(BrowserItemActionInf } } -void IntegrationPluginLukeRoberts::onPluginConfigurationChanged(const ParamTypeId ¶mTypeId, const QVariant &value) -{ - Q_UNUSED(paramTypeId) - Q_UNUSED(value) -} - - void IntegrationPluginLukeRoberts::onReconnectTimeout() { foreach (LukeRoberts *lamp, m_lamps.keys()) { diff --git a/lukeroberts/integrationpluginlukeroberts.h b/lukeroberts/integrationpluginlukeroberts.h index 39481721..c5ea3a19 100644 --- a/lukeroberts/integrationpluginlukeroberts.h +++ b/lukeroberts/integrationpluginlukeroberts.h @@ -69,7 +69,6 @@ private: QHash m_pendingBrowserItemActions; private slots: - void onPluginConfigurationChanged(const ParamTypeId ¶mTypeId, const QVariant &value); void onReconnectTimeout(); void onConnectedChanged(bool connected); diff --git a/lukeroberts/lukeroberts.cpp b/lukeroberts/lukeroberts.cpp index 19338892..8a9b54dc 100644 --- a/lukeroberts/lukeroberts.cpp +++ b/lukeroberts/lukeroberts.cpp @@ -36,8 +36,8 @@ -LukeRoberts::LukeRoberts(BluetoothLowEnergyDevice *bluetoothDevice, QObject *parent) : - QObject(parent), +LukeRoberts::LukeRoberts(BluetoothLowEnergyDevice *bluetoothDevice) : + QObject(bluetoothDevice), m_bluetoothDevice(bluetoothDevice) { connect(m_bluetoothDevice, &BluetoothLowEnergyDevice::connectedChanged, this, &LukeRoberts::onConnectedChanged); @@ -225,7 +225,14 @@ void LukeRoberts::onServiceDiscoveryFinished() return; } + // + //characteristicRead(const QLowEnergyCharacteristic &characteristic, const QByteArray &value) + //characteristicWritten(const QLowEnergyCharacteristic &characteristic, const QByteArray &newValue) + //escriptorRead(const QLowEnergyDescriptor &descriptor, const QByteArray &value) + //descriptorWritten(const QLowEnergyDescriptor &descriptor, const QByteArray &newValue) + //error(QLowEnergyService::ServiceError newError) connect(m_deviceInfoService, &QLowEnergyService::stateChanged, this, &LukeRoberts::onDeviceInfoServiceStateChanged); + //connect(m_deviceInfoService, &QLowEnergyService::characteristicChanged, this, &LukeRoberts::onDeviceInfoServiceCharacteristicChanged); if (m_deviceInfoService->state() == QLowEnergyService::DiscoveryRequired) { m_deviceInfoService->discoverDetails(); @@ -242,6 +249,7 @@ void LukeRoberts::onServiceDiscoveryFinished() } connect(m_controlService, &QLowEnergyService::stateChanged, this, &LukeRoberts::onControlServiceChanged); + connect(m_deviceInfoService, &QLowEnergyService::characteristicChanged, this, &LukeRoberts::onControlServiceCharacteristicChanged); if (m_controlService->state() == QLowEnergyService::DiscoveryRequired) { m_controlService->discoverDetails(); @@ -279,7 +287,7 @@ void LukeRoberts::onControlServiceChanged(const QLowEnergyService::ServiceState // Custom API endpoint m_externalApiEndpoint = m_controlService->characteristic(externalApiEndpointCharacteristicUuid); if (!m_externalApiEndpoint.isValid()) { - qCWarning(dcLukeRoberts()) << "Input button characteristc not found for device " << bluetoothDevice()->name() << bluetoothDevice()->address().toString(); + qCWarning(dcLukeRoberts()) << "External api endpoint characteristc not found for device " << bluetoothDevice()->name() << bluetoothDevice()->address().toString(); return; } // Enable notifications @@ -287,7 +295,7 @@ void LukeRoberts::onControlServiceChanged(const QLowEnergyService::ServiceState m_controlService->writeDescriptor(notificationDescriptor, QByteArray::fromHex("0100")); } -void LukeRoberts::onExternalApiEndpointCharacteristicChanged(const QLowEnergyCharacteristic &characteristic, const QByteArray &value) +void LukeRoberts::onControlServiceCharacteristicChanged(const QLowEnergyCharacteristic &characteristic, const QByteArray &value) { if (characteristic.uuid() == m_externalApiEndpoint.uuid()) { qCDebug(dcLukeRoberts()) << "Data received" << value; @@ -303,6 +311,5 @@ void LukeRoberts::onExternalApiEndpointCharacteristicChanged(const QLowEnergyCha } } - qCDebug(dcLukeRoberts()) << "Service characteristic changed" << characteristic.name() << value.toHex(); } diff --git a/lukeroberts/lukeroberts.h b/lukeroberts/lukeroberts.h index 470c9654..fb63c6f3 100644 --- a/lukeroberts/lukeroberts.h +++ b/lukeroberts/lukeroberts.h @@ -89,7 +89,7 @@ struct Scene { int8_t id; QString name; }; - explicit LukeRoberts(BluetoothLowEnergyDevice *bluetoothDevice, QObject *parent = nullptr); + explicit LukeRoberts(BluetoothLowEnergyDevice *bluetoothDevice); BluetoothLowEnergyDevice *bluetoothDevice(); void ping(); @@ -133,7 +133,7 @@ private slots: void onDeviceInfoServiceStateChanged(const QLowEnergyService::ServiceState &state); void onControlServiceChanged(const QLowEnergyService::ServiceState &state); - void onExternalApiEndpointCharacteristicChanged(const QLowEnergyCharacteristic &characteristic, const QByteArray &value); + void onControlServiceCharacteristicChanged(const QLowEnergyCharacteristic &characteristic, const QByteArray &value); }; #endif // LUKEROBERTS_H