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,42 +91,48 @@ void IntegrationPluginLukeRoberts::setupThing(ThingSetupInfo *info)
qCDebug(dcLukeRoberts()) << "Setup device" << thing->name() << thing->params(); qCDebug(dcLukeRoberts()) << "Setup device" << thing->name() << thing->params();
QBluetoothAddress address = QBluetoothAddress(thing->paramValue(modelFThingMacParamTypeId).toString()); if (thing->thingClassId() == modelFThingClassId) {
QBluetoothDeviceInfo deviceInfo = QBluetoothDeviceInfo(address, thing->name(), 0); 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); 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){
qCDebug(dcLukeRoberts()) << "Lamp device initialization finished";
Thing *thing = info->thing();
connect(lamp, &LukeRoberts::deviceInitializationFinished, info, [this, info, lamp](bool success){ if (!thing->setupComplete()) {
Thing *thing = info->thing(); if (success) {
info->finish(Thing::ThingErrorNoError);
} else {
m_lamps.take(lamp);
if (!thing->setupComplete()) { hardwareManager()->bluetoothLowEnergyManager()->unregisterDevice(lamp->bluetoothDevice());
if (success) { lamp->deleteLater();
info->finish(Thing::ThingErrorNoError);
} else {
m_lamps.take(lamp);
hardwareManager()->bluetoothLowEnergyManager()->unregisterDevice(lamp->bluetoothDevice()); info->finish(Thing::ThingErrorHardwareFailure, QT_TR_NOOP("Error connecting to lamp."));
lamp->deleteLater(); }
info->finish(Thing::ThingErrorHardwareFailure, QT_TR_NOOP("Error connecting to lamp."));
} }
}
}); });
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,49 +144,58 @@ void IntegrationPluginLukeRoberts::executeAction(ThingActionInfo *info)
Thing *thing = info->thing(); Thing *thing = info->thing();
Action action = info->action(); Action action = info->action();
QPointer<LukeRoberts> lamp = m_lamps.key(thing); if (thing->thingClassId() == modelFThingClassId) {
if (lamp.isNull()) QPointer<LukeRoberts> lamp = m_lamps.key(thing);
return info->finish(Thing::ThingErrorHardwareFailure); if (lamp.isNull())
return info->finish(Thing::ThingErrorHardwareFailure);
if (!lamp->bluetoothDevice()->connected()) { if (!lamp->bluetoothDevice()->connected()) {
return info->finish(Thing::ThingErrorHardwareNotAvailable); 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
} }
} else if (action.actionTypeId() == modelFBrightnessActionTypeId) { if (action.actionTypeId() == modelFPowerActionTypeId) {
int brightness = action.param(modelFBrightnessActionBrightnessParamTypeId).value().toInt(); bool power = action.param(modelFPowerActionPowerParamTypeId).value().toBool();
lamp->modifyBrightness(brightness); 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) { } else if (action.actionTypeId() == modelFBrightnessActionTypeId) {
QColor rgb = QColor::fromRgb(QRgb(action.param(modelFColorActionColorParamTypeId).value().toInt())); int brightness = action.param(modelFBrightnessActionBrightnessParamTypeId).value().toInt();
lamp->setImmediateLight(0, rgb.saturation(), rgb.hue(), rgb.lightness()); 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) void IntegrationPluginLukeRoberts::thingRemoved(Thing *thing)
{ {
if (!m_lamps.values().contains(thing)) qCDebug(dcLukeRoberts()) << "Delete thing" << thing->name();
return;
LukeRoberts *lamp = m_lamps.key(thing); if (thing->thingClassId() == modelFThingClassId) {
m_lamps.take(lamp); if (!m_lamps.values().contains(thing))
return;
hardwareManager()->bluetoothLowEnergyManager()->unregisterDevice(lamp->bluetoothDevice()); LukeRoberts *lamp = m_lamps.key(thing);
lamp->deleteLater(); m_lamps.remove(lamp);
hardwareManager()->bluetoothLowEnergyManager()->unregisterDevice(lamp->bluetoothDevice());
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