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();
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<LukeRoberts> lamp = m_lamps.key(thing);
if (lamp.isNull())
return info->finish(Thing::ThingErrorHardwareFailure);
if (thing->thingClassId() == modelFThingClassId) {
QPointer<LukeRoberts> 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 &paramTypeId, const QVariant &value)
{
Q_UNUSED(paramTypeId)
Q_UNUSED(value)
}
void IntegrationPluginLukeRoberts::onReconnectTimeout()
{
foreach (LukeRoberts *lamp, m_lamps.keys()) {

View File

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

View File

@ -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();
}

View File

@ -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