From 44256076a6905b0fb8dbc48f893c362d53d0bd80 Mon Sep 17 00:00:00 2001 From: nymea Date: Wed, 9 Oct 2019 14:37:21 +0200 Subject: [PATCH] fixed copy&paste typos --- lukeroberts/devicepluginlukeroberts.cpp | 246 +++++------------------ lukeroberts/devicepluginlukeroberts.h | 2 +- lukeroberts/devicepluginlukeroberts.json | 30 +-- lukeroberts/lukeroberts.cpp | 204 ++++--------------- lukeroberts/lukeroberts.h | 7 +- 5 files changed, 92 insertions(+), 397 deletions(-) diff --git a/lukeroberts/devicepluginlukeroberts.cpp b/lukeroberts/devicepluginlukeroberts.cpp index dcff7cfd..279ee1ff 100644 --- a/lukeroberts/devicepluginlukeroberts.cpp +++ b/lukeroberts/devicepluginlukeroberts.cpp @@ -1,6 +1,6 @@ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * - * Copyright (C) 2016-2018 Simon Stürz * + * Copyright (C) 2019 Bernhard Trinnes * * * * This file is part of nymea. * * * @@ -20,25 +20,22 @@ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ -#include "devicepluginsenic.h" +#include "devicepluginlukeroberts.h" #include "devices/device.h" #include "plugininfo.h" #include "hardware/bluetoothlowenergy/bluetoothlowenergymanager.h" -DevicePluginSenic::DevicePluginSenic() +DevicePluginLukeRoberts::DevicePluginLukeRoberts() { } -void DevicePluginSenic::init() +void DevicePluginLukeRoberts::init() { - // Initialize plugin configurations - m_autoSymbolMode = configValue(senicPluginAutoSymbolsParamTypeId).toBool(); - connect(this, &DevicePluginSenic::configValueChanged, this, &DevicePluginSenic::onPluginConfigurationChanged); } -void DevicePluginSenic::discoverDevices(DeviceDiscoveryInfo *info) +void DevicePluginLukeRoberts::discoverDevices(DeviceDiscoveryInfo *info) { if (!hardwareManager()->bluetoothLowEnergyManager()->available()) return info->finish(Device::DeviceErrorHardwareNotAvailable, QT_TR_NOOP("Bluetooth is not available on this system.")); @@ -52,23 +49,23 @@ void DevicePluginSenic::discoverDevices(DeviceDiscoveryInfo *info) connect(reply, &BluetoothDiscoveryReply::finished, info, [this, info, reply](){ if (reply->error() != BluetoothDiscoveryReply::BluetoothDiscoveryReplyErrorNoError) { - qCWarning(dcSenic()) << "Bluetooth discovery error:" << reply->error(); + qCWarning(dcLukeRoberts()) << "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() + ")"); + if (deviceInfo.name().contains("Luke")) { + DeviceDescriptor descriptor(modelFDeviceClassId, "Model F", deviceInfo.name() + " (" + deviceInfo.address().toString() + ")"); ParamList params; foreach (Device *existingDevice, myDevices()) { - if (existingDevice->paramValue(nuimoDeviceMacParamTypeId).toString() == deviceInfo.address().toString()) { + if (existingDevice->paramValue(modelFDeviceMacParamTypeId).toString() == deviceInfo.address().toString()) { descriptor.setDeviceId(existingDevice->id()); break; } } - params.append(Param(nuimoDeviceMacParamTypeId, deviceInfo.address().toString())); + params.append(Param(modelFDeviceMacParamTypeId, deviceInfo.address().toString())); descriptor.setParams(params); info->addDeviceDescriptor(descriptor); } @@ -78,119 +75,79 @@ void DevicePluginSenic::discoverDevices(DeviceDiscoveryInfo *info) } -void DevicePluginSenic::setupDevice(DeviceSetupInfo *info) +void DevicePluginLukeRoberts::setupDevice(DeviceSetupInfo *info) { Device *device = info->device(); - qCDebug(dcSenic()) << "Setup device" << device->name() << device->params(); + qCDebug(dcLukeRoberts()) << "Setup device" << device->name() << device->params(); - QBluetoothAddress address = QBluetoothAddress(device->paramValue(nuimoDeviceMacParamTypeId).toString()); + QBluetoothAddress address = QBluetoothAddress(device->paramValue(modelFDeviceMacParamTypeId).toString()); QBluetoothDeviceInfo deviceInfo = QBluetoothDeviceInfo(address, device->name(), 0); BluetoothLowEnergyDevice *bluetoothDevice = hardwareManager()->bluetoothLowEnergyManager()->registerDevice(deviceInfo, QLowEnergyController::RandomAddress); - Nuimo *nuimo = new Nuimo(bluetoothDevice, this); - nuimo->setLongPressTime(configValue(senicPluginLongPressTimeParamTypeId).toInt()); - connect(nuimo, &Nuimo::buttonPressed, this, &DevicePluginSenic::onButtonPressed); - connect(nuimo, &Nuimo::buttonLongPressed, this, &DevicePluginSenic::onButtonLongPressed); - connect(nuimo, &Nuimo::swipeDetected, this, &DevicePluginSenic::onSwipeDetected); - connect(nuimo, &Nuimo::rotationValueChanged, this, &DevicePluginSenic::onRotationValueChanged); - connect(nuimo, &Nuimo::connectedChanged, this, &DevicePluginSenic::onConnectedChanged); - connect(nuimo, &Nuimo::deviceInformationChanged, this, &DevicePluginSenic::onDeviceInformationChanged); - connect(nuimo, &Nuimo::batteryValueChanged, this, &DevicePluginSenic::onBatteryValueChanged); + LukeRoberts *lamp = new LukeRoberts(bluetoothDevice, this); + connect(lamp, &LukeRoberts::deviceInformationChanged, this, &DevicePluginLukeRoberts::onDeviceInformationChanged); - m_nuimos.insert(nuimo, device); + m_lamps.insert(lamp, device); - connect(nuimo, &Nuimo::deviceInitializationFinished, info, [this, info, nuimo](bool success){ + connect(lamp, &LukeRoberts::deviceInitializationFinished, info, [this, info, lamp](bool success){ Device *device = info->device(); if (!device->setupComplete()) { if (success) { info->finish(Device::DeviceErrorNoError); } else { - m_nuimos.take(nuimo); + m_lamps.take(lamp); - hardwareManager()->bluetoothLowEnergyManager()->unregisterDevice(nuimo->bluetoothDevice()); - nuimo->deleteLater(); + hardwareManager()->bluetoothLowEnergyManager()->unregisterDevice(lamp->bluetoothDevice()); + lamp->deleteLater(); - info->finish(Device::DeviceErrorHardwareFailure, QT_TR_NOOP("Error connecting to nuimo.")); + info->finish(Device::DeviceErrorHardwareFailure, QT_TR_NOOP("Error connecting to lamp.")); } } }); - - - nuimo->bluetoothDevice()->connectDevice(); + lamp->bluetoothDevice()->connectDevice(); } -void DevicePluginSenic::postSetupDevice(Device *device) +void DevicePluginLukeRoberts::postSetupDevice(Device *device) { Q_UNUSED(device) if (!m_reconnectTimer) { m_reconnectTimer = hardwareManager()->pluginTimerManager()->registerTimer(10); - connect(m_reconnectTimer, &PluginTimer::timeout, this, &DevicePluginSenic::onReconnectTimeout); + connect(m_reconnectTimer, &PluginTimer::timeout, this, &DevicePluginLukeRoberts::onReconnectTimeout); } } -void DevicePluginSenic::executeAction(DeviceActionInfo *info) +void DevicePluginLukeRoberts::executeAction(DeviceActionInfo *info) { Device *device = info->device(); Action action = info->action(); - QPointer nuimo = m_nuimos.key(device); - if (nuimo.isNull()) + QPointer lamp = m_lamps.key(device); + if (lamp.isNull()) return info->finish(Device::DeviceErrorHardwareFailure); - if (!nuimo->bluetoothDevice()->connected()) { + if (!lamp->bluetoothDevice()->connected()) { return info->finish(Device::DeviceErrorHardwareNotAvailable); } - if (action.actionTypeId() == nuimoShowLogoActionTypeId) { - - if (action.param(nuimoShowLogoActionLogoParamTypeId).value().toString() == "Up") - nuimo->showImage(Nuimo::MatrixTypeUp); - if (action.param(nuimoShowLogoActionLogoParamTypeId).value().toString() == "Down") - nuimo->showImage(Nuimo::MatrixTypeDown); - if (action.param(nuimoShowLogoActionLogoParamTypeId).value().toString() == "Left") - nuimo->showImage(Nuimo::MatrixTypeLeft); - if (action.param(nuimoShowLogoActionLogoParamTypeId).value().toString() == "Right") - nuimo->showImage(Nuimo::MatrixTypeRight); - if (action.param(nuimoShowLogoActionLogoParamTypeId).value().toString() == "Play") - nuimo->showImage(Nuimo::MatrixTypePlay); - if (action.param(nuimoShowLogoActionLogoParamTypeId).value().toString() == "Pause") - nuimo->showImage(Nuimo::MatrixTypePause); - if (action.param(nuimoShowLogoActionLogoParamTypeId).value().toString() == "Stop") - nuimo->showImage(Nuimo::MatrixTypeStop); - if (action.param(nuimoShowLogoActionLogoParamTypeId).value().toString() == "Music") - nuimo->showImage(Nuimo::MatrixTypeMusic); - if (action.param(nuimoShowLogoActionLogoParamTypeId).value().toString() == "Heart") - nuimo->showImage(Nuimo::MatrixTypeHeart); - if (action.param(nuimoShowLogoActionLogoParamTypeId).value().toString() == "Next") - nuimo->showImage(Nuimo::MatrixTypeNext); - if (action.param(nuimoShowLogoActionLogoParamTypeId).value().toString() == "Previous") - nuimo->showImage(Nuimo::MatrixTypePrevious); - if (action.param(nuimoShowLogoActionLogoParamTypeId).value().toString() == "Circle") - nuimo->showImage(Nuimo::MatrixTypeCircle); - if (action.param(nuimoShowLogoActionLogoParamTypeId).value().toString() == "Light") - nuimo->showImage(Nuimo::MatrixTypeLight); - - return info->finish(Device::DeviceErrorNoError); - } } -void DevicePluginSenic::deviceRemoved(Device *device) +void DevicePluginLukeRoberts::deviceRemoved(Device *device) { - if (!m_nuimos.values().contains(device)) + if (!m_lamps.values().contains(device)) return; - Nuimo *nuimo = m_nuimos.key(device); - m_nuimos.take(nuimo); + LukeRoberts *lamp = m_lamps.key(device); + m_lamps.take(lamp); - hardwareManager()->bluetoothLowEnergyManager()->unregisterDevice(nuimo->bluetoothDevice()); - nuimo->deleteLater(); + hardwareManager()->bluetoothLowEnergyManager()->unregisterDevice(lamp->bluetoothDevice()); + lamp->deleteLater(); if (myDevices().isEmpty()) { hardwareManager()->pluginTimerManager()->unregisterTimer(m_reconnectTimer); @@ -199,138 +156,35 @@ void DevicePluginSenic::deviceRemoved(Device *device) } -void DevicePluginSenic::onReconnectTimeout() +void DevicePluginLukeRoberts::onReconnectTimeout() { - foreach (Nuimo *nuimo, m_nuimos.keys()) { - if (!nuimo->bluetoothDevice()->connected()) { - nuimo->bluetoothDevice()->connectDevice(); + foreach (LukeRoberts *lamp, m_lamps.keys()) { + if (!lamp->bluetoothDevice()->connected()) { + lamp->bluetoothDevice()->connectDevice(); } } } -void DevicePluginSenic::onConnectedChanged(bool connected) +void DevicePluginLukeRoberts::onConnectedChanged(bool connected) { - Nuimo *nuimo = static_cast(sender()); - Device *device = m_nuimos.value(nuimo); - device->setStateValue(nuimoConnectedStateTypeId, connected); + LukeRoberts *lamp = static_cast(sender()); + Device *device = m_lamps.value(lamp); + device->setStateValue(modelFConnectedStateTypeId, connected); } - -void DevicePluginSenic::onButtonPressed() +void DevicePluginLukeRoberts::onDeviceInformationChanged(const QString &firmwareRevision, const QString &hardwareRevision, const QString &softwareRevision) { - Nuimo *nuimo = static_cast(sender()); - Device *device = m_nuimos.value(nuimo); - emitEvent(Event(nuimoPressedEventTypeId, device->id(), ParamList() << Param(nuimoPressedEventButtonNameParamTypeId, "•"))); + LukeRoberts *lamp = static_cast(sender()); + Device *device = m_lamps.value(lamp); - if (m_autoSymbolMode) { - nuimo->showImage(Nuimo::MatrixTypeCircle); - } + qDebug(dcLukeRoberts()) << device->name() << "Firmware" << firmwareRevision << "Hardware" << hardwareRevision << "Software" << softwareRevision; + //device->setStateValue(modelFFirmwareRevisionStateTypeId, firmwareRevision); + //device->setStateValue(modelFardwareRevisionStateTypeId, hardwareRevision); + //device->setStateValue(modelFSoftwareRevisionStateTypeId, softwareRevision); } -void DevicePluginSenic::onButtonLongPressed() -{ - Nuimo *nuimo = static_cast(sender()); - Device *device = m_nuimos.value(nuimo); - emitEvent(Event(nuimoLongPressedEventTypeId, device->id())); - - if (m_autoSymbolMode) { - nuimo->showImage(Nuimo::MatrixTypeFilledCircle); - } -} - - -void DevicePluginSenic::onSwipeDetected(const Nuimo::SwipeDirection &direction) -{ - Nuimo *nuimo = static_cast(sender()); - Device *device = m_nuimos.value(nuimo); - - switch (direction) { - case Nuimo::SwipeDirectionLeft: - emitEvent(Event(nuimoPressedEventTypeId, device->id(), ParamList() << Param(nuimoPressedEventButtonNameParamTypeId, "←"))); - break; - case Nuimo::SwipeDirectionRight: - emitEvent(Event(nuimoPressedEventTypeId, device->id(), ParamList() << Param(nuimoPressedEventButtonNameParamTypeId, "→"))); - break; - case Nuimo::SwipeDirectionUp: - emitEvent(Event(nuimoPressedEventTypeId, device->id(), ParamList() << Param(nuimoPressedEventButtonNameParamTypeId, "↑"))); - break; - case Nuimo::SwipeDirectionDown: - emitEvent(Event(nuimoPressedEventTypeId, device->id(), ParamList() << Param(nuimoPressedEventButtonNameParamTypeId, "↓"))); - break; - } - - if (m_autoSymbolMode) { - switch (direction) { - case Nuimo::SwipeDirectionLeft: - nuimo->showImage(Nuimo::MatrixType::MatrixTypeLeft); - break; - case Nuimo::SwipeDirectionRight: - nuimo->showImage(Nuimo::MatrixType::MatrixTypeRight); - break; - case Nuimo::SwipeDirectionUp: - nuimo->showImage(Nuimo::MatrixType::MatrixTypeUp); - break; - case Nuimo::SwipeDirectionDown: - nuimo->showImage(Nuimo::MatrixType::MatrixTypeDown); - break; - } - } -} - - -void DevicePluginSenic::onRotationValueChanged(const uint &value) -{ - Nuimo *nuimo = static_cast(sender()); - Device *device = m_nuimos.value(nuimo); - device->setStateValue(nuimoRotationStateTypeId, value); -} - - -void DevicePluginSenic::onDeviceInformationChanged(const QString &firmwareRevision, const QString &hardwareRevision, const QString &softwareRevision) -{ - Nuimo *nuimo = static_cast(sender()); - Device *device = m_nuimos.value(nuimo); - - device->setStateValue(nuimoFirmwareRevisionStateTypeId, firmwareRevision); - device->setStateValue(nuimoHardwareRevisionStateTypeId, hardwareRevision); - device->setStateValue(nuimoSoftwareRevisionStateTypeId, softwareRevision); -} - - -void DevicePluginSenic::onPluginConfigurationChanged(const ParamTypeId ¶mTypeId, const QVariant &value) -{ - qCDebug(dcSenic()) << "Plugin configuration changed"; - - // Check auto symbol mode - if (paramTypeId == senicPluginAutoSymbolsParamTypeId) { - qCDebug(dcSenic()) << "Auto symbol mode" << (value.toBool() ? "enabled." : "disabled."); - m_autoSymbolMode = value.toBool(); - } - - if (paramTypeId == senicPluginLongPressTimeParamTypeId) { - qCDebug(dcSenic()) << "Long press time" << value.toInt(); - foreach(Nuimo *nuimo, m_nuimos.keys()) { - nuimo->setLongPressTime(value.toInt()); - } - } -} - -void DevicePluginSenic::onBatteryValueChanged(const uint &percentage) -{ - Nuimo *nuimo = static_cast(sender()); - Device *device = m_nuimos.value(nuimo); - - device->setStateValue(nuimoBatteryLevelStateTypeId, percentage); - if (percentage < 20) { - device->setStateValue(nuimoBatteryCriticalStateTypeId, true); - } else { - device->setStateValue(nuimoBatteryCriticalStateTypeId, false); - } -} - void DevicePluginLukeRoberts::onStatusCodeReceived(LukeRoberts::StatusCodes statusCode) { - - + qDebug(dcLukeRoberts()) << "Status code received" << statusCode; } diff --git a/lukeroberts/devicepluginlukeroberts.h b/lukeroberts/devicepluginlukeroberts.h index 97e1507d..a0f9709e 100644 --- a/lukeroberts/devicepluginlukeroberts.h +++ b/lukeroberts/devicepluginlukeroberts.h @@ -48,7 +48,7 @@ public: void deviceRemoved(Device *device) override; private: - QHash m_nuimos; + QHash m_lamps; PluginTimer *m_reconnectTimer = nullptr; bool m_autoSymbolMode = true; diff --git a/lukeroberts/devicepluginlukeroberts.json b/lukeroberts/devicepluginlukeroberts.json index 92449c4c..aa22cdcd 100644 --- a/lukeroberts/devicepluginlukeroberts.json +++ b/lukeroberts/devicepluginlukeroberts.json @@ -1,16 +1,16 @@ { - "displayName": "LukeRoberts", + "displayName": "Luke Roberts", "id": "40f368ee-1815-46fd-9d74-4b8e5b5f21e7", - "name": "Luke Roberts", + "name": "LukeRoberts", "vendors": [ { "displayName": "Luke Roberts", - "name": "senic", + "name": "LukeRoberts", "id": "4e942850-9553-493c-8a4a-5a468e124099", "deviceClasses": [ { "id": "5de74ba9-4a46-4235-94b4-8aefc1e2b27f", - "name": "Model F", + "name": "modelF", "displayName": "Model F", "createMethods": ["discovery"], "interfaces": ["colorlight", "connectable"], @@ -33,7 +33,6 @@ "type": "bool", "defaultValue": false }, - , { "id": "ac3a55e1-48af-402d-b9ef-6020d4a1cb4f", "name": "power", @@ -82,29 +81,8 @@ } ], "actionTypes": [ - - ], "eventTypes": [ - { - "id": "24649eb2-47d1-4a2b-8c09-1f074382e2c4", - "name": "pressed", - "displayName": "Button pressed", - "paramTypes": [ - { - "id": "8ed643c0-1b8a-4709-8abf-717cf213f4a4", - "name": "buttonName", - "displayName": "Button name", - "type": "QString", - "allowedValues": ["•", "←", "↑", "→", "↓"] - } - ] - }, - { - "id": "a2f4add5-f76a-4dca-ae68-4107533bee0e", - "name": "longPressed", - "displayName": "Button long pressed" - } ], "browserItemActionTypes": [ { diff --git a/lukeroberts/lukeroberts.cpp b/lukeroberts/lukeroberts.cpp index 975eab0a..de8f6f01 100644 --- a/lukeroberts/lukeroberts.cpp +++ b/lukeroberts/lukeroberts.cpp @@ -1,6 +1,6 @@ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * - * Copyright (C) 2016-2018 Simon Stürz * + * Copyright (C) 2019 Bernhard Trinnes * * * * This file is part of nymea. * * * @@ -20,14 +20,14 @@ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ -#include "LukeRoberts.h" +#include "lukeroberts.h" #include "extern-plugininfo.h" #include #include -static QBluetoothUuid ledMatrinxServiceUuid = QBluetoothUuid(QUuid("f29b1523-cb19-40f3-be5c-7241ecb82fd1")); -static QBluetoothUuid ledMatrixCharacteristicUuid = QBluetoothUuid(QUuid("f29b1524-cb19-40f3-be5c-7241ecb82fd1")); +static QBluetoothUuid customControlServiceUuid = QBluetoothUuid(QUuid("​44092840-0567-11E6-B862-0002A5D5C51B")); +static QBluetoothUuid externalApiEndpointCharacteristicUuid = QBluetoothUuid(QUuid("44092842-0567-11E6-B862-0002A5D5C51B")); LukeRoberts::LukeRoberts(BluetoothLowEnergyDevice *bluetoothDevice, QObject *parent) : @@ -47,9 +47,9 @@ BluetoothLowEnergyDevice *LukeRoberts::bluetoothDevice() void LukeRoberts::printService(QLowEnergyService *service) { foreach (const QLowEnergyCharacteristic &characteristic, service->characteristics()) { - qCDebug(dcSenic()) << " -->" << characteristic.name() << characteristic.uuid().toString() << characteristic.value(); + qCDebug(dcLukeRoberts()) << " -->" << characteristic.name() << characteristic.uuid().toString() << characteristic.value(); foreach (const QLowEnergyDescriptor &descriptor, characteristic.descriptors()) { - qCDebug(dcSenic()) << " -->" << descriptor.name() << descriptor.uuid().toString() << descriptor.value(); + qCDebug(dcLukeRoberts()) << " -->" << descriptor.name() << descriptor.uuid().toString() << descriptor.value(); } } } @@ -57,44 +57,33 @@ void LukeRoberts::printService(QLowEnergyService *service) void LukeRoberts::onConnectedChanged(bool connected) { - qCDebug(dcSenic()) << m_bluetoothDevice->name() << m_bluetoothDevice->address().toString() << (connected ? "connected" : "disconnected"); + qCDebug(dcLukeRoberts()) << m_bluetoothDevice->name() << m_bluetoothDevice->address().toString() << (connected ? "connected" : "disconnected"); - m_longPressTimer->stop(); emit connectedChanged(connected); if (!connected) { // Clean up services m_deviceInfoService->deleteLater(); - m_batteryService->deleteLater(); - m_ledMatrixService->deleteLater(); - m_inputService->deleteLater(); + m_controlService->deleteLater(); m_deviceInfoService = nullptr; - m_batteryService = nullptr; - m_ledMatrixService = nullptr; - m_inputService = nullptr; + m_controlService = nullptr; } } void LukeRoberts::onServiceDiscoveryFinished() { - qCDebug(dcSenic()) << "Service scan finised"; + qCDebug(dcLukeRoberts()) << "Service scan finised"; if (!m_bluetoothDevice->serviceUuids().contains(QBluetoothUuid::DeviceInformation)) { - qCWarning(dcSenic()) << "Device Information service not found for device" << bluetoothDevice()->name() << bluetoothDevice()->address().toString(); + qCWarning(dcLukeRoberts()) << "Device Information service not found for device" << bluetoothDevice()->name() << bluetoothDevice()->address().toString(); emit deviceInitializationFinished(false); return; } - if (!m_bluetoothDevice->serviceUuids().contains(ledMatrinxServiceUuid)) { - qCWarning(dcSenic()) << "Led matrix service not found for device" << bluetoothDevice()->name() << bluetoothDevice()->address().toString(); - emit deviceInitializationFinished(false); - return; - } - - if (!m_bluetoothDevice->serviceUuids().contains(inputServiceUuid)) { - qCWarning(dcSenic()) << "Input service not found for device" << bluetoothDevice()->name() << bluetoothDevice()->address().toString(); + if (!m_bluetoothDevice->serviceUuids().contains(customControlServiceUuid)) { + qCWarning(dcLukeRoberts()) << "Input service not found for device" << bluetoothDevice()->name() << bluetoothDevice()->address().toString(); emit deviceInitializationFinished(false); return; } @@ -103,7 +92,7 @@ void LukeRoberts::onServiceDiscoveryFinished() if (!m_deviceInfoService) { m_deviceInfoService = m_bluetoothDevice->controller()->createServiceObject(QBluetoothUuid::DeviceInformation, this); if (!m_deviceInfoService) { - qCWarning(dcSenic()) << "Could not create device info service."; + qCWarning(dcLukeRoberts()) << "Could not create device info service."; emit deviceInitializationFinished(false); return; } @@ -116,18 +105,18 @@ void LukeRoberts::onServiceDiscoveryFinished() } // Custom control service - if (!m_ledMatrixService) { - m_ledMatrixService = m_bluetoothDevice->controller()->createServiceObject(ledMatrinxServiceUuid, this); - if (!m_ledMatrixService) { - qCWarning(dcSenic()) << "Could not create led matrix service."; + if (!m_controlService) { + m_controlService= m_bluetoothDevice->controller()->createServiceObject(customControlServiceUuid, this); + if (!m_controlService) { + qCWarning(dcLukeRoberts()) << "Could not create led matrix service."; emit deviceInitializationFinished(false); return; } - connect(m_ledMatrixService, &QLowEnergyService::stateChanged, this, &LukeRoberts::onLedMatrixServiceStateChanged); + connect(m_controlService, &QLowEnergyService::stateChanged, this, &LukeRoberts::onControlServiceChanged); - if (m_ledMatrixService->state() == QLowEnergyService::DiscoveryRequired) { - m_ledMatrixService->discoverDetails(); + if (m_controlService->state() == QLowEnergyService::DiscoveryRequired) { + m_controlService->discoverDetails(); } } emit deviceInitializationFinished(true); @@ -139,7 +128,7 @@ void LukeRoberts::onDeviceInfoServiceStateChanged(const QLowEnergyService::Servi if (state != QLowEnergyService::ServiceDiscovered) return; - qCDebug(dcSenic()) << "Device info service discovered."; + qCDebug(dcLukeRoberts()) << "Device info service discovered."; printService(m_deviceInfoService); QString firmware = QString::fromUtf8(m_deviceInfoService->characteristic(QBluetoothUuid::FirmwareRevisionString).value()); @@ -149,155 +138,32 @@ void LukeRoberts::onDeviceInfoServiceStateChanged(const QLowEnergyService::Servi emit deviceInformationChanged(firmware, hardware, software); } -void LukeRoberts::onBatteryServiceStateChanged(const QLowEnergyService::ServiceState &state) +void LukeRoberts::onControlServiceChanged(const QLowEnergyService::ServiceState &state) { // Only continue if discovered if (state != QLowEnergyService::ServiceDiscovered) return; - qCDebug(dcSenic()) << "Battery service discovered."; + qCDebug(dcLukeRoberts()) << "Custom control service discovered."; - printService(m_batteryService); + printService(m_controlService); - m_batteryCharacteristic = m_batteryService->characteristic(QBluetoothUuid::BatteryLevel); - if (!m_batteryCharacteristic.isValid()) { - qCWarning(dcSenic()) << "Battery characteristc not found for device " << bluetoothDevice()->name() << bluetoothDevice()->address().toString(); - return; - } - - // Enable notifications - QLowEnergyDescriptor notificationDescriptor = m_batteryCharacteristic.descriptor(QBluetoothUuid::ClientCharacteristicConfiguration); - m_batteryService->writeDescriptor(notificationDescriptor, QByteArray::fromHex("0100")); - - uint batteryPercentage = m_batteryCharacteristic.value().toHex().toUInt(nullptr, 16); - emit batteryValueChanged(batteryPercentage); -} - -void LukeRoberts::onBatteryCharacteristicChanged(const QLowEnergyCharacteristic &characteristic, const QByteArray &value) -{ - if (characteristic.uuid() == m_batteryCharacteristic.uuid()) { - uint batteryPercentage = value.toHex().toUInt(nullptr, 16); - emit batteryValueChanged(batteryPercentage); - } -} - -void LukeRoberts::onInputServiceStateChanged(const QLowEnergyService::ServiceState &state) -{ - // Only continue if discovered - if (state != QLowEnergyService::ServiceDiscovered) - return; - - qCDebug(dcSenic()) << "Input service discovered."; - - printService(m_inputService); - - // Button - m_inputButtonCharacteristic = m_inputService->characteristic(inputButtonCharacteristicUuid); - if (!m_inputButtonCharacteristic.isValid()) { - qCWarning(dcSenic()) << "Input button characteristc not found for device " << bluetoothDevice()->name() << bluetoothDevice()->address().toString(); + // 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(); return; } // Enable notifications - QLowEnergyDescriptor notificationDescriptor = m_inputButtonCharacteristic.descriptor(QBluetoothUuid::ClientCharacteristicConfiguration); - m_inputService->writeDescriptor(notificationDescriptor, QByteArray::fromHex("0100")); - - - // Swipe - m_inputSwipeCharacteristic = m_inputService->characteristic(inputSwipeCharacteristicUuid); - if (!m_inputSwipeCharacteristic.isValid()) { - qCWarning(dcSenic()) << "Input swipe characteristc not found for device " << bluetoothDevice()->name() << bluetoothDevice()->address().toString(); - return; - } - // Enable notifications - notificationDescriptor = m_inputSwipeCharacteristic.descriptor(QBluetoothUuid::ClientCharacteristicConfiguration); - m_inputService->writeDescriptor(notificationDescriptor, QByteArray::fromHex("0100")); - - - // Rotation - m_inputRotationCharacteristic = m_inputService->characteristic(inputRotationCharacteristicUuid); - if (!m_inputRotationCharacteristic.isValid()) { - qCWarning(dcSenic()) << "Input rotation characteristc not found for device " << bluetoothDevice()->name() << bluetoothDevice()->address().toString(); - return; - } - // Enable notifications - notificationDescriptor = m_inputRotationCharacteristic.descriptor(QBluetoothUuid::ClientCharacteristicConfiguration); - m_inputService->writeDescriptor(notificationDescriptor, QByteArray::fromHex("0100")); + QLowEnergyDescriptor notificationDescriptor = m_externalApiEndpoint.descriptor(QBluetoothUuid::ClientCharacteristicConfiguration); + m_controlService->writeDescriptor(notificationDescriptor, QByteArray::fromHex("0100")); } -void LukeRoberts::onInputCharacteristicChanged(const QLowEnergyCharacteristic &characteristic, const QByteArray &value) +void LukeRoberts::onExternalApiEndpointCharacteristicChanged(const QLowEnergyCharacteristic &characteristic, const QByteArray &value) { - if (characteristic.uuid() == m_inputButtonCharacteristic.uuid()) { - bool pressed = (bool)value.toHex().toUInt(nullptr, 16); - qCDebug(dcSenic()) << "Button:" << (pressed ? "pressed": "released"); - if (pressed) { - m_longPressTimer->start(m_longPressTime); - } else { - if (m_longPressTimer->isActive()) { - m_longPressTimer->stop(); - emit buttonPressed(); - } - // else the time run out and has the long pressed event emittted - } - return; + if (characteristic.uuid() == m_externalApiEndpoint.uuid()) { + qCDebug(dcLukeRoberts()) << "Data received" << value; } - if (characteristic.uuid() == m_inputSwipeCharacteristic.uuid()) { - quint8 swipe = (quint8)value.toHex().toUInt(nullptr, 16); - switch (swipe) { - case 0: - qCDebug(dcSenic()) << "Swipe: Left"; - emit swipeDetected(SwipeDirectionLeft); - break; - case 1: - qCDebug(dcSenic()) << "Swipe: Right"; - emit swipeDetected(SwipeDirectionRight); - break; - case 2: - qCDebug(dcSenic()) << "Swipe: Up"; - emit swipeDetected(SwipeDirectionUp); - break; - case 3: - qCDebug(dcSenic()) << "Swipe: Down"; - emit swipeDetected(SwipeDirectionDown); - break; - default: - break; - } - return; - } - - if (characteristic.uuid() == m_inputRotationCharacteristic.uuid()) { - qint16 intValue = qFromLittleEndian((uchar *)value.constData()); - qCDebug(dcSenic()) << "Rotation" << value.toHex() << intValue; - int finalValue = m_rotationValue + qRound(intValue / 10.0); - if (finalValue <= 0) { - m_rotationValue = 0; - } else if (finalValue >= 100) { - m_rotationValue = 100; - } else { - m_rotationValue = finalValue; - } - emit rotationValueChanged(m_rotationValue); - return; - } - - qCDebug(dcSenic()) << "Service characteristic changed" << characteristic.name() << value.toHex(); -} - -void LukeRoberts::onLedMatrixServiceStateChanged(const QLowEnergyService::ServiceState &state) -{ - // Only continue if discovered - if (state != QLowEnergyService::ServiceDiscovered) - return; - - qCDebug(dcSenic()) << "Led matrix service discovered."; - - printService(m_ledMatrixService); - - // Led matrix - m_ledMatrixCharacteristic = m_ledMatrixService->characteristic(ledMatrixCharacteristicUuid); - if (!m_ledMatrixCharacteristic.isValid()) { - qCWarning(dcSenic()) << "Led matrix characteristc not found for device " << bluetoothDevice()->name() << bluetoothDevice()->address().toString(); - return; - } + qCDebug(dcLukeRoberts()) << "Service characteristic changed" << characteristic.name() << value.toHex(); } diff --git a/lukeroberts/lukeroberts.h b/lukeroberts/lukeroberts.h index 65ce1ec5..6e967023 100644 --- a/lukeroberts/lukeroberts.h +++ b/lukeroberts/lukeroberts.h @@ -96,11 +96,8 @@ private slots: void onDeviceInfoServiceStateChanged(const QLowEnergyService::ServiceState &state); - void onBatteryServiceStateChanged(const QLowEnergyService::ServiceState &state); - void onBatteryCharacteristicChanged(const QLowEnergyCharacteristic &characteristic, const QByteArray &value); - - void onInputServiceStateChanged(const QLowEnergyService::ServiceState &state); - void onInputCharacteristicChanged(const QLowEnergyCharacteristic &characteristic, const QByteArray &value); + void onControlServiceChanged(const QLowEnergyService::ServiceState &state); + void onExternalApiEndpointCharacteristicChanged(const QLowEnergyCharacteristic &characteristic, const QByteArray &value); }; #endif // NUIMO_H