diff --git a/senic/devicepluginsenic.cpp b/senic/devicepluginsenic.cpp index f47ef1cb..63241891 100644 --- a/senic/devicepluginsenic.cpp +++ b/senic/devicepluginsenic.cpp @@ -32,8 +32,7 @@ DevicePluginSenic::DevicePluginSenic() void DevicePluginSenic::init() { - m_reconnectTimer = hardwareManager()->pluginTimerManager()->registerTimer(10); - connect(m_reconnectTimer, &PluginTimer::timeout, this, &DevicePluginSenic::onReconnectTimeout); + } Device::DeviceError DevicePluginSenic::discoverDevices(const DeviceClassId &deviceClassId, const ParamList ¶ms) @@ -57,11 +56,15 @@ Device::DeviceError DevicePluginSenic::discoverDevices(const DeviceClassId &devi Device::DeviceSetupStatus DevicePluginSenic::setupDevice(Device *device) { + if (!m_reconnectTimer) { + m_reconnectTimer = hardwareManager()->pluginTimerManager()->registerTimer(10); + connect(m_reconnectTimer, &PluginTimer::timeout, this, &DevicePluginSenic::onReconnectTimeout); + } + qCDebug(dcSenic()) << "Setup device" << device->name() << device->params(); - QString name = device->paramValue(nuimoDeviceNameParamTypeId).toString(); QBluetoothAddress address = QBluetoothAddress(device->paramValue(nuimoDeviceMacParamTypeId).toString()); - QBluetoothDeviceInfo deviceInfo = QBluetoothDeviceInfo(address, name, 0); + QBluetoothDeviceInfo deviceInfo = QBluetoothDeviceInfo(address, device->name(), 0); BluetoothLowEnergyDevice *bluetoothDevice = hardwareManager()->bluetoothLowEnergyManager()->registerDevice(deviceInfo, QLowEnergyController::RandomAddress); @@ -104,7 +107,7 @@ Device::DeviceError DevicePluginSenic::executeAction(Device *device, const Actio if (action.param(nuimoShowLogoActionLogoParamTypeId).value().toString() == "Stop") nuimo->showImage(Nuimo::MatrixTypeStop); if (action.param(nuimoShowLogoActionLogoParamTypeId).value().toString() == "Music") - nuimo->showImage(Nuimo::MatrixTypeStop); + nuimo->showImage(Nuimo::MatrixTypeMusic); if (action.param(nuimoShowLogoActionLogoParamTypeId).value().toString() == "Heart") nuimo->showImage(Nuimo::MatrixTypeHeart); @@ -122,6 +125,11 @@ void DevicePluginSenic::deviceRemoved(Device *device) Nuimo *nuimo = m_nuimos.key(device); m_nuimos.take(nuimo); delete nuimo; + + if (myDevices().isEmpty()) { + hardwareManager()->pluginTimerManager()->unregisterTimer(m_reconnectTimer); + m_reconnectTimer = nullptr; + } } bool DevicePluginSenic::verifyExistingDevices(const QBluetoothDeviceInfo &deviceInfo) @@ -158,9 +166,8 @@ void DevicePluginSenic::onBluetoothDiscoveryFinished() foreach (const QBluetoothDeviceInfo &deviceInfo, reply->discoveredDevices()) { if (deviceInfo.name().contains("Nuimo")) { if (!verifyExistingDevices(deviceInfo)) { - DeviceDescriptor descriptor(nuimoDeviceClassId, "Nuimo", deviceInfo.address().toString()); + DeviceDescriptor descriptor(nuimoDeviceClassId, "Nuimo", deviceInfo.name() + " (" + deviceInfo.address().toString() + ")"); ParamList params; - params.append(Param(nuimoDeviceNameParamTypeId, deviceInfo.name())); params.append(Param(nuimoDeviceMacParamTypeId, deviceInfo.address().toString())); descriptor.setParams(params); deviceDescriptors.append(descriptor); @@ -212,5 +219,3 @@ void DevicePluginSenic::onRotationValueChanged(const uint &value) Device *device = m_nuimos.value(nuimo); device->setStateValue(nuimoRotationStateTypeId, value); } - - diff --git a/senic/devicepluginsenic.json b/senic/devicepluginsenic.json index 2aa6e5ba..c36ac5fc 100644 --- a/senic/devicepluginsenic.json +++ b/senic/devicepluginsenic.json @@ -13,15 +13,8 @@ "name": "nuimo", "displayName": "Nuimo", "createMethods": ["discovery"], - "interfaces": [ "simplemultibutton", "connectable"], + "interfaces": ["batterylevel", "simplemultibutton", "connectable"], "paramTypes": [ - { - "id": "db67d1e6-26fa-44ed-ad55-c6aef45ea2ea", - "name": "name", - "displayName": "name", - "type": "QString", - "inputType": "TextLine" - }, { "id": "71553f6a-2ed4-4896-bb7b-52e7dca948b2", "name": "mac", @@ -63,11 +56,19 @@ "type": "QString", "defaultValue": "-" }, + { + "id": "aabd660f-b0c5-49f6-b7b0-6ba8e0a8cfcd", + "name": "batteryCritical", + "displayName": "Battery critical", + "displayNameEvent": "Battery critical changed", + "type": "bool", + "defaultValue": true + }, { "id": "b5ee2465-7fa1-450b-8073-f115537d3409", - "name": "battery", - "displayName": "battery", - "displayNameEvent": "battery changed", + "name": "batteryLevel", + "displayName": "Battery", + "displayNameEvent": "Battery changed", "type": "int", "minValue": 0, "maxValue": 100, diff --git a/senic/nuimo.cpp b/senic/nuimo.cpp index a5e87745..b659e474 100644 --- a/senic/nuimo.cpp +++ b/senic/nuimo.cpp @@ -224,7 +224,12 @@ void Nuimo::setBatteryValue(const QByteArray &data) int batteryPercentage = data.toHex().toUInt(0, 16); qCDebug(dcSenic()) << "Battery:" << batteryPercentage << "%"; - device()->setStateValue(nuimoBatteryStateTypeId, batteryPercentage); + device()->setStateValue(nuimoBatteryLevelStateTypeId, batteryPercentage); + if (batteryPercentage < 20) { + device()->setStateValue(nuimoBatteryCriticalStateTypeId, true); + } else { + device()->setStateValue(nuimoBatteryCriticalStateTypeId, false); + } } void Nuimo::onConnectedChanged(const bool &connected)