refactured nuimo class

master
nymea 2019-08-08 16:04:45 +02:00
parent c807790086
commit 3bf9ca94c7
4 changed files with 64 additions and 42 deletions

View File

@ -37,6 +37,7 @@ void DevicePluginSenic::init()
connect(this, &DevicePluginSenic::configValueChanged, this, &DevicePluginSenic::onPluginConfigurationChanged);
}
Device::DeviceError DevicePluginSenic::discoverDevices(const DeviceClassId &deviceClassId, const ParamList &params)
{
Q_UNUSED(params)
@ -56,6 +57,7 @@ Device::DeviceError DevicePluginSenic::discoverDevices(const DeviceClassId &devi
return Device::DeviceErrorAsync;
}
Device::DeviceSetupStatus DevicePluginSenic::setupDevice(Device *device)
{
if (!m_reconnectTimer) {
@ -70,11 +72,13 @@ Device::DeviceSetupStatus DevicePluginSenic::setupDevice(Device *device)
BluetoothLowEnergyDevice *bluetoothDevice = hardwareManager()->bluetoothLowEnergyManager()->registerDevice(deviceInfo, QLowEnergyController::RandomAddress);
Nuimo *nuimo = new Nuimo(device, bluetoothDevice, this);
Nuimo *nuimo = new Nuimo(bluetoothDevice, this);
connect(nuimo, &Nuimo::buttonPressed, this, &DevicePluginSenic::onButtonPressed);
connect(nuimo, &Nuimo::buttonReleased, this, &DevicePluginSenic::onButtonReleased);
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);
m_nuimos.insert(nuimo, device);
nuimo->bluetoothDevice()->connectDevice();
@ -82,6 +86,7 @@ Device::DeviceSetupStatus DevicePluginSenic::setupDevice(Device *device)
return Device::DeviceSetupStatusSuccess;
}
Device::DeviceError DevicePluginSenic::executeAction(Device *device, const Action &action)
{
QPointer<Nuimo> nuimo = m_nuimos.key(device);
@ -127,6 +132,7 @@ Device::DeviceError DevicePluginSenic::executeAction(Device *device, const Actio
return Device::DeviceErrorActionTypeNotFound;
}
void DevicePluginSenic::deviceRemoved(Device *device)
{
if (!m_nuimos.values().contains(device))
@ -156,9 +162,10 @@ void DevicePluginSenic::onReconnectTimeout()
void DevicePluginSenic::onBluetoothDiscoveryFinished()
{
BluetoothDiscoveryReply *reply = static_cast<BluetoothDiscoveryReply *>(sender());
reply->deleteLater();
if (reply->error() != BluetoothDiscoveryReply::BluetoothDiscoveryReplyErrorNoError) {
qCWarning(dcSenic()) << "Bluetooth discovery error:" << reply->error();
reply->deleteLater();
emit devicesDiscovered(nuimoDeviceClassId, QList<DeviceDescriptor>());
return;
}
@ -180,11 +187,17 @@ void DevicePluginSenic::onBluetoothDiscoveryFinished()
deviceDescriptors.append(descriptor);
}
}
reply->deleteLater();
emit devicesDiscovered(nuimoDeviceClassId, deviceDescriptors);
}
void DevicePluginSenic::onConnectedChanged(bool connected)
{
Nuimo *nuimo = static_cast<Nuimo *>(sender());
Device *device = m_nuimos.value(nuimo);
device->setStateValue(nuimoConnectedStateTypeId, connected);
}
void DevicePluginSenic::onButtonPressed()
{
@ -200,9 +213,10 @@ void DevicePluginSenic::onButtonPressed()
void DevicePluginSenic::onButtonReleased()
{
// TODO: user timer for detekt long pressed (if needed)
// ENHANCEMENT: user timer to detekt long press events
}
void DevicePluginSenic::onSwipeDetected(const Nuimo::SwipeDirection &direction)
{
Nuimo *nuimo = static_cast<Nuimo *>(sender());
@ -241,6 +255,7 @@ void DevicePluginSenic::onSwipeDetected(const Nuimo::SwipeDirection &direction)
}
}
void DevicePluginSenic::onRotationValueChanged(const uint &value)
{
Nuimo *nuimo = static_cast<Nuimo *>(sender());
@ -248,6 +263,18 @@ void DevicePluginSenic::onRotationValueChanged(const uint &value)
device->setStateValue(nuimoRotationStateTypeId, value);
}
void DevicePluginSenic::onDeviceInformationChanged(const QString &firmwareRevision, const QString &hardwareRevision, const QString &softwareRevision)
{
Nuimo *nuimo = static_cast<Nuimo *>(sender());
Device *device = m_nuimos.value(nuimo);
device->setStateValue(nuimoFirmwareRevisionStateTypeId, firmwareRevision);
device->setStateValue(nuimoHardwareRevisionStateTypeId, hardwareRevision);
device->setStateValue(nuimoSoftwareRevisionStateTypeId, softwareRevision);
}
void DevicePluginSenic::onPluginConfigurationChanged(const ParamTypeId &paramTypeId, const QVariant &value)
{
qCDebug(dcSenic()) << "Plugin configuration changed";
@ -258,3 +285,16 @@ void DevicePluginSenic::onPluginConfigurationChanged(const ParamTypeId &paramTyp
m_autoSymbolMode = value.toBool();
}
}
void DevicePluginSenic::onBatteryValueChanged(const uint &percentage)
{
Nuimo *nuimo = static_cast<Nuimo *>(sender());
Device *device = m_nuimos.value(nuimo);
device->setStateValue(nuimoBatteryLevelStateTypeId, percentage);
if (percentage < 20) {
device->setStateValue(nuimoBatteryCriticalStateTypeId, true);
} else {
device->setStateValue(nuimoBatteryCriticalStateTypeId, false);
}
}

View File

@ -56,11 +56,13 @@ private slots:
void onReconnectTimeout();
void onBluetoothDiscoveryFinished();
void onConnectedChanged(bool connected);
void onBatteryValueChanged(const uint &percentage);
void onButtonPressed();
void onButtonReleased();
void onSwipeDetected(const Nuimo::SwipeDirection &direction);
void onRotationValueChanged(const uint &value);
void onDeviceInformationChanged(const QString &firmwareRevision, const QString &hardwareRevision, const QString &softwareRevision);
};
#endif // DEVICEPLUGINSENIC_H

View File

@ -35,19 +35,14 @@ static QBluetoothUuid inputRotationCharacteristicUuid = QBluetoothUuid(QUuid("
static QBluetoothUuid inputButtonCharacteristicUuid = QBluetoothUuid(QUuid("f29b1529-cb19-40f3-be5c-7241ecb82fd2"));
Nuimo::Nuimo(Device *device, BluetoothLowEnergyDevice *bluetoothDevice, QObject *parent) :
Nuimo::Nuimo(BluetoothLowEnergyDevice *bluetoothDevice, QObject *parent) :
QObject(parent),
m_device(device),
m_bluetoothDevice(bluetoothDevice)
{
connect(m_bluetoothDevice, &BluetoothLowEnergyDevice::connectedChanged, this, &Nuimo::onConnectedChanged);
connect(m_bluetoothDevice, &BluetoothLowEnergyDevice::servicesDiscoveryFinished, this, &Nuimo::onServiceDiscoveryFinished);
}
Device *Nuimo::device()
{
return m_device;
}
BluetoothLowEnergyDevice *Nuimo::bluetoothDevice()
{
@ -269,23 +264,12 @@ void Nuimo::printService(QLowEnergyService *service)
}
}
void Nuimo::setBatteryValue(const QByteArray &data)
{
int batteryPercentage = data.toHex().toUInt(0, 16);
qCDebug(dcSenic()) << "Battery:" << batteryPercentage << "%";
device()->setStateValue(nuimoBatteryLevelStateTypeId, batteryPercentage);
if (batteryPercentage < 20) {
device()->setStateValue(nuimoBatteryCriticalStateTypeId, true);
} else {
device()->setStateValue(nuimoBatteryCriticalStateTypeId, false);
}
}
void Nuimo::onConnectedChanged(const bool &connected)
{
qCDebug(dcSenic()) << m_bluetoothDevice->name() << m_bluetoothDevice->address().toString() << (connected ? "connected" : "disconnected");
m_device->setStateValue(nuimoConnectedStateTypeId, connected);
emit connectedChanged(connected);
if (!connected) {
// Clean up services
@ -399,11 +383,11 @@ void Nuimo::onDeviceInfoServiceStateChanged(const QLowEnergyService::ServiceStat
qCDebug(dcSenic()) << "Device info service discovered.";
printService(m_deviceInfoService);
QString firmware = QString::fromUtf8(m_deviceInfoService->characteristic(QBluetoothUuid::FirmwareRevisionString).value());
QString hardware = QString::fromUtf8(m_deviceInfoService->characteristic(QBluetoothUuid::HardwareRevisionString).value());
QString software = QString::fromUtf8(m_deviceInfoService->characteristic(QBluetoothUuid::SoftwareRevisionString).value());
device()->setStateValue(nuimoFirmwareRevisionStateTypeId, QString::fromUtf8(m_deviceInfoService->characteristic(QBluetoothUuid::FirmwareRevisionString).value()));
device()->setStateValue(nuimoHardwareRevisionStateTypeId, QString::fromUtf8(m_deviceInfoService->characteristic(QBluetoothUuid::HardwareRevisionString).value()));
device()->setStateValue(nuimoSoftwareRevisionStateTypeId, QString::fromUtf8(m_deviceInfoService->characteristic(QBluetoothUuid::SoftwareRevisionString).value()));
emit deviceInformationChanged(firmware, hardware, software);
}
void Nuimo::onBatteryServiceStateChanged(const QLowEnergyService::ServiceState &state)
@ -426,13 +410,15 @@ void Nuimo::onBatteryServiceStateChanged(const QLowEnergyService::ServiceState &
QLowEnergyDescriptor notificationDescriptor = m_batteryCharacteristic.descriptor(QBluetoothUuid::ClientCharacteristicConfiguration);
m_batteryService->writeDescriptor(notificationDescriptor, QByteArray::fromHex("0100"));
setBatteryValue(m_batteryCharacteristic.value());
uint batteryPercentage = m_batteryCharacteristic.value().toHex().toUInt(nullptr, 16);
emit batteryValueChanged(batteryPercentage);
}
void Nuimo::onBatteryCharacteristicChanged(const QLowEnergyCharacteristic &characteristic, const QByteArray &value)
{
if (characteristic.uuid() == m_batteryCharacteristic.uuid()) {
setBatteryValue(value);
uint batteryPercentage = value.toHex().toUInt(nullptr, 16);
emit batteryValueChanged(batteryPercentage);
}
}
@ -482,7 +468,7 @@ void Nuimo::onInputServiceStateChanged(const QLowEnergyService::ServiceState &st
void Nuimo::onInputCharacteristicChanged(const QLowEnergyCharacteristic &characteristic, const QByteArray &value)
{
if (characteristic.uuid() == m_inputButtonCharacteristic.uuid()) {
bool pressed = (bool)value.toHex().toUInt(0, 16);
bool pressed = (bool)value.toHex().toUInt(nullptr, 16);
qCDebug(dcSenic()) << "Button:" << (pressed ? "pressed": "released");
if (pressed) {
emit buttonPressed();
@ -493,7 +479,7 @@ void Nuimo::onInputCharacteristicChanged(const QLowEnergyCharacteristic &charact
}
if (characteristic.uuid() == m_inputSwipeCharacteristic.uuid()) {
quint8 swipe = (quint8)value.toHex().toUInt(0, 16);
quint8 swipe = (quint8)value.toHex().toUInt(nullptr, 16);
switch (swipe) {
case 0:
qCDebug(dcSenic()) << "Swipe: Left";

View File

@ -27,7 +27,6 @@
#include <QBluetoothUuid>
#include "typeutils.h"
#include "devices/device.h"
#include "hardware/bluetoothlowenergy/bluetoothlowenergydevice.h"
class Nuimo : public QObject
@ -57,15 +56,13 @@ public:
MatrixTypeLight
};
explicit Nuimo(Device *device, BluetoothLowEnergyDevice *bluetoothDevice, QObject *parent = nullptr);
explicit Nuimo(BluetoothLowEnergyDevice *bluetoothDevice, QObject *parent = nullptr);
Device *device();
BluetoothLowEnergyDevice *bluetoothDevice();
void showImage(const MatrixType &matrixType);
private:
Device *m_device = nullptr;
BluetoothLowEnergyDevice *m_bluetoothDevice = nullptr;
QLowEnergyService *m_deviceInfoService = nullptr;
@ -83,19 +80,16 @@ private:
uint m_rotationValue;
void showMatrix(const QByteArray &matrix, const int &seconds);
void printService(QLowEnergyService *service);
// Set states
void setBatteryValue(const QByteArray &data);
signals:
void availableChanged();
void connectedChanged(bool connected);
void buttonPressed();
void buttonReleased();
void batteryValueChaged(const uint &percentage);
void batteryValueChanged(const uint &percentage);
void swipeDetected(const SwipeDirection &direction);
void rotationValueChanged(const uint &value);
void deviceInformationChanged(const QString &firmwareRevision, const QString &hardwareRevision, const QString &softwareRevision);
private slots:
void onConnectedChanged(const bool &connected);