updated lukeroberts

This commit is contained in:
Boernsman 2021-01-29 15:23:46 +01:00 committed by Boernsman
parent 05b9b9dfaa
commit a0fd7c6fdb
4 changed files with 79 additions and 66 deletions

View File

@ -91,18 +91,22 @@ void IntegrationPluginLukeRoberts::setupThing(ThingSetupInfo *info)
qCDebug(dcLukeRoberts()) << "Setup device" << thing->name() << thing->params(); qCDebug(dcLukeRoberts()) << "Setup device" << thing->name() << thing->params();
if (thing->thingClassId() == modelFThingClassId) {
QBluetoothAddress address = QBluetoothAddress(thing->paramValue(modelFThingMacParamTypeId).toString()); QBluetoothAddress address = QBluetoothAddress(thing->paramValue(modelFThingMacParamTypeId).toString());
QBluetoothDeviceInfo deviceInfo = QBluetoothDeviceInfo(address, thing->name(), 0); 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); LukeRoberts *lamp = new LukeRoberts(bluetoothDevice);
connect(lamp, &LukeRoberts::connectedChanged, this, &IntegrationPluginLukeRoberts::onConnectedChanged); connect(lamp, &LukeRoberts::connectedChanged, this, &IntegrationPluginLukeRoberts::onConnectedChanged);
connect(lamp, &LukeRoberts::deviceInformationChanged, this, &IntegrationPluginLukeRoberts::onDeviceInformationChanged); 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){ connect(lamp, &LukeRoberts::deviceInitializationFinished, info, [this, info, lamp](bool success){
qCDebug(dcLukeRoberts()) << "Lamp device initialization finished";
Thing *thing = info->thing(); Thing *thing = info->thing();
if (!thing->setupComplete()) { if (!thing->setupComplete()) {
@ -121,12 +125,14 @@ void IntegrationPluginLukeRoberts::setupThing(ThingSetupInfo *info)
}); });
lamp->bluetoothDevice()->connectDevice(); lamp->bluetoothDevice()->connectDevice();
} }
}
void IntegrationPluginLukeRoberts::postSetupThing(Thing *thing) void IntegrationPluginLukeRoberts::postSetupThing(Thing *thing)
{ {
Q_UNUSED(thing) qCDebug(dcLukeRoberts()) << "Post setup device" << thing->name();
if (!m_reconnectTimer) { if (!m_reconnectTimer) {
qCDebug(dcLukeRoberts()) << "Initializing reconnect timer";
m_reconnectTimer = hardwareManager()->pluginTimerManager()->registerTimer(10); m_reconnectTimer = hardwareManager()->pluginTimerManager()->registerTimer(10);
connect(m_reconnectTimer, &PluginTimer::timeout, this, &IntegrationPluginLukeRoberts::onReconnectTimeout); connect(m_reconnectTimer, &PluginTimer::timeout, this, &IntegrationPluginLukeRoberts::onReconnectTimeout);
} }
@ -138,6 +144,7 @@ void IntegrationPluginLukeRoberts::executeAction(ThingActionInfo *info)
Thing *thing = info->thing(); Thing *thing = info->thing();
Action action = info->action(); Action action = info->action();
if (thing->thingClassId() == modelFThingClassId) {
QPointer<LukeRoberts> lamp = m_lamps.key(thing); QPointer<LukeRoberts> lamp = m_lamps.key(thing);
if (lamp.isNull()) if (lamp.isNull())
return info->finish(Thing::ThingErrorHardwareFailure); return info->finish(Thing::ThingErrorHardwareFailure);
@ -166,21 +173,29 @@ void IntegrationPluginLukeRoberts::executeAction(ThingActionInfo *info)
int kelvin = action.param(modelFColorTemperatureActionColorTemperatureParamTypeId).value().toInt(); int kelvin = action.param(modelFColorTemperatureActionColorTemperatureParamTypeId).value().toInt();
lamp->modifyColorTemperature(kelvin); lamp->modifyColorTemperature(kelvin);
} }
} else {
}
} }
void IntegrationPluginLukeRoberts::thingRemoved(Thing *thing) void IntegrationPluginLukeRoberts::thingRemoved(Thing *thing)
{ {
qCDebug(dcLukeRoberts()) << "Delete thing" << thing->name();
if (thing->thingClassId() == modelFThingClassId) {
if (!m_lamps.values().contains(thing)) if (!m_lamps.values().contains(thing))
return; return;
LukeRoberts *lamp = m_lamps.key(thing); LukeRoberts *lamp = m_lamps.key(thing);
m_lamps.take(lamp); m_lamps.remove(lamp);
hardwareManager()->bluetoothLowEnergyManager()->unregisterDevice(lamp->bluetoothDevice()); hardwareManager()->bluetoothLowEnergyManager()->unregisterDevice(lamp->bluetoothDevice());
lamp->deleteLater(); lamp->deleteLater();
}
if (myThings().isEmpty()) { if (myThings().isEmpty()) {
qCDebug(dcLukeRoberts()) << "Unregistering reconnect timer";
hardwareManager()->pluginTimerManager()->unregisterTimer(m_reconnectTimer); hardwareManager()->pluginTimerManager()->unregisterTimer(m_reconnectTimer);
m_reconnectTimer = nullptr; m_reconnectTimer = nullptr;
} }
@ -206,7 +221,6 @@ void IntegrationPluginLukeRoberts::browserItem(BrowserItemResult *result)
return; return;
} }
qDebug(dcLukeRoberts()) << "Browse item called"; qDebug(dcLukeRoberts()) << "Browse item called";
} }
void IntegrationPluginLukeRoberts::executeBrowserItem(BrowserActionInfo *info) void IntegrationPluginLukeRoberts::executeBrowserItem(BrowserActionInfo *info)
@ -229,13 +243,6 @@ void IntegrationPluginLukeRoberts::executeBrowserItemAction(BrowserItemActionInf
} }
} }
void IntegrationPluginLukeRoberts::onPluginConfigurationChanged(const ParamTypeId &paramTypeId, const QVariant &value)
{
Q_UNUSED(paramTypeId)
Q_UNUSED(value)
}
void IntegrationPluginLukeRoberts::onReconnectTimeout() void IntegrationPluginLukeRoberts::onReconnectTimeout()
{ {
foreach (LukeRoberts *lamp, m_lamps.keys()) { foreach (LukeRoberts *lamp, m_lamps.keys()) {

View File

@ -69,7 +69,6 @@ private:
QHash<int, BrowserItemActionInfo*> m_pendingBrowserItemActions; QHash<int, BrowserItemActionInfo*> m_pendingBrowserItemActions;
private slots: private slots:
void onPluginConfigurationChanged(const ParamTypeId &paramTypeId, const QVariant &value);
void onReconnectTimeout(); void onReconnectTimeout();
void onConnectedChanged(bool connected); void onConnectedChanged(bool connected);

View File

@ -36,8 +36,8 @@
LukeRoberts::LukeRoberts(BluetoothLowEnergyDevice *bluetoothDevice, QObject *parent) : LukeRoberts::LukeRoberts(BluetoothLowEnergyDevice *bluetoothDevice) :
QObject(parent), QObject(bluetoothDevice),
m_bluetoothDevice(bluetoothDevice) m_bluetoothDevice(bluetoothDevice)
{ {
connect(m_bluetoothDevice, &BluetoothLowEnergyDevice::connectedChanged, this, &LukeRoberts::onConnectedChanged); connect(m_bluetoothDevice, &BluetoothLowEnergyDevice::connectedChanged, this, &LukeRoberts::onConnectedChanged);
@ -225,7 +225,14 @@ void LukeRoberts::onServiceDiscoveryFinished()
return; 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::stateChanged, this, &LukeRoberts::onDeviceInfoServiceStateChanged);
//connect(m_deviceInfoService, &QLowEnergyService::characteristicChanged, this, &LukeRoberts::onDeviceInfoServiceCharacteristicChanged);
if (m_deviceInfoService->state() == QLowEnergyService::DiscoveryRequired) { if (m_deviceInfoService->state() == QLowEnergyService::DiscoveryRequired) {
m_deviceInfoService->discoverDetails(); m_deviceInfoService->discoverDetails();
@ -242,6 +249,7 @@ void LukeRoberts::onServiceDiscoveryFinished()
} }
connect(m_controlService, &QLowEnergyService::stateChanged, this, &LukeRoberts::onControlServiceChanged); connect(m_controlService, &QLowEnergyService::stateChanged, this, &LukeRoberts::onControlServiceChanged);
connect(m_deviceInfoService, &QLowEnergyService::characteristicChanged, this, &LukeRoberts::onControlServiceCharacteristicChanged);
if (m_controlService->state() == QLowEnergyService::DiscoveryRequired) { if (m_controlService->state() == QLowEnergyService::DiscoveryRequired) {
m_controlService->discoverDetails(); m_controlService->discoverDetails();
@ -279,7 +287,7 @@ void LukeRoberts::onControlServiceChanged(const QLowEnergyService::ServiceState
// Custom API endpoint // Custom API endpoint
m_externalApiEndpoint = m_controlService->characteristic(externalApiEndpointCharacteristicUuid); m_externalApiEndpoint = m_controlService->characteristic(externalApiEndpointCharacteristicUuid);
if (!m_externalApiEndpoint.isValid()) { 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; return;
} }
// Enable notifications // Enable notifications
@ -287,7 +295,7 @@ void LukeRoberts::onControlServiceChanged(const QLowEnergyService::ServiceState
m_controlService->writeDescriptor(notificationDescriptor, QByteArray::fromHex("0100")); 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()) { if (characteristic.uuid() == m_externalApiEndpoint.uuid()) {
qCDebug(dcLukeRoberts()) << "Data received" << value; qCDebug(dcLukeRoberts()) << "Data received" << value;
@ -303,6 +311,5 @@ void LukeRoberts::onExternalApiEndpointCharacteristicChanged(const QLowEnergyCha
} }
} }
qCDebug(dcLukeRoberts()) << "Service characteristic changed" << characteristic.name() << value.toHex(); qCDebug(dcLukeRoberts()) << "Service characteristic changed" << characteristic.name() << value.toHex();
} }

View File

@ -89,7 +89,7 @@ struct Scene {
int8_t id; int8_t id;
QString name; QString name;
}; };
explicit LukeRoberts(BluetoothLowEnergyDevice *bluetoothDevice, QObject *parent = nullptr); explicit LukeRoberts(BluetoothLowEnergyDevice *bluetoothDevice);
BluetoothLowEnergyDevice *bluetoothDevice(); BluetoothLowEnergyDevice *bluetoothDevice();
void ping(); void ping();
@ -133,7 +133,7 @@ private slots:
void onDeviceInfoServiceStateChanged(const QLowEnergyService::ServiceState &state); void onDeviceInfoServiceStateChanged(const QLowEnergyService::ServiceState &state);
void onControlServiceChanged(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 #endif // LUKEROBERTS_H