From 50d05f7d61a876bcd593d25a068a659b8b1bedf9 Mon Sep 17 00:00:00 2001 From: "bernhard.trinnes" Date: Mon, 23 Mar 2020 12:51:30 +0100 Subject: [PATCH] devices to things --- lukeroberts/devicepluginlukeroberts.cpp | 279 ----------------------- lukeroberts/devicepluginlukeroberts.h | 74 ------ lukeroberts/devicepluginlukeroberts.json | 98 -------- lukeroberts/lukeroberts.cpp | 50 ++-- lukeroberts/lukeroberts.h | 50 ++-- lukeroberts/lukeroberts.pro | 6 +- 6 files changed, 61 insertions(+), 496 deletions(-) delete mode 100644 lukeroberts/devicepluginlukeroberts.cpp delete mode 100644 lukeroberts/devicepluginlukeroberts.h delete mode 100644 lukeroberts/devicepluginlukeroberts.json diff --git a/lukeroberts/devicepluginlukeroberts.cpp b/lukeroberts/devicepluginlukeroberts.cpp deleted file mode 100644 index 9cd95ccd..00000000 --- a/lukeroberts/devicepluginlukeroberts.cpp +++ /dev/null @@ -1,279 +0,0 @@ -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * - * * - * Copyright (C) 2019 Bernhard Trinnes * - * * - * This file is part of nymea. * - * * - * This library is free software; you can redistribute it and/or * - * modify it under the terms of the GNU Lesser General Public * - * License as published by the Free Software Foundation; either * - * version 2.1 of the License, or (at your option) any later version. * - * * - * This library is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * - * Lesser General Public License for more details. * - * * - * You should have received a copy of the GNU Lesser General Public * - * License along with this library; If not, see * - * . * - * * - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ - -#include "devicepluginlukeroberts.h" -#include "devices/device.h" -#include "plugininfo.h" -#include "hardware/bluetoothlowenergy/bluetoothlowenergymanager.h" - -#include -#include - -DevicePluginLukeRoberts::DevicePluginLukeRoberts() -{ - -} - -void DevicePluginLukeRoberts::init() -{ -} - - -void DevicePluginLukeRoberts::discoverDevices(DeviceDiscoveryInfo *info) -{ - if (!hardwareManager()->bluetoothLowEnergyManager()->available()) - return info->finish(Device::DeviceErrorHardwareNotAvailable, QT_TR_NOOP("Bluetooth is not available on this system.")); - - if (!hardwareManager()->bluetoothLowEnergyManager()->enabled()) - return info->finish(Device::DeviceErrorHardwareNotAvailable, QT_TR_NOOP("Bluetooth is disabled. Please enable Bluetooth and try again.")); - - BluetoothDiscoveryReply *reply = hardwareManager()->bluetoothLowEnergyManager()->discoverDevices(); - connect(reply, &BluetoothDiscoveryReply::finished, reply, &BluetoothDiscoveryReply::deleteLater); - - connect(reply, &BluetoothDiscoveryReply::finished, info, [this, info, reply](){ - - if (reply->error() != BluetoothDiscoveryReply::BluetoothDiscoveryReplyErrorNoError) { - 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.serviceUuids().contains(customControlServiceUuid)) { - DeviceDescriptor descriptor(modelFDeviceClassId, "Model F", deviceInfo.name() + " (" + deviceInfo.address().toString() + ")"); - ParamList params; - - foreach (Device *existingDevice, myDevices()) { - if (existingDevice->paramValue(modelFDeviceMacParamTypeId).toString() == deviceInfo.address().toString()) { - descriptor.setDeviceId(existingDevice->id()); - break; - } - } - params.append(Param(modelFDeviceMacParamTypeId, deviceInfo.address().toString())); - descriptor.setParams(params); - info->addDeviceDescriptor(descriptor); - } - } - info->finish(Device::DeviceErrorNoError); - }); -} - - -void DevicePluginLukeRoberts::setupDevice(DeviceSetupInfo *info) -{ - Device *device = info->device(); - - qCDebug(dcLukeRoberts()) << "Setup device" << device->name() << device->params(); - - QBluetoothAddress address = QBluetoothAddress(device->paramValue(modelFDeviceMacParamTypeId).toString()); - QBluetoothDeviceInfo deviceInfo = QBluetoothDeviceInfo(address, device->name(), 0); - - BluetoothLowEnergyDevice *bluetoothDevice = hardwareManager()->bluetoothLowEnergyManager()->registerDevice(deviceInfo, QLowEnergyController::RandomAddress); - - LukeRoberts *lamp = new LukeRoberts(bluetoothDevice, this); - connect(lamp, &LukeRoberts::connectedChanged, this, &DevicePluginLukeRoberts::onConnectedChanged); - connect(lamp, &LukeRoberts::deviceInformationChanged, this, &DevicePluginLukeRoberts::onDeviceInformationChanged); - - m_lamps.insert(lamp, device); - - 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_lamps.take(lamp); - - hardwareManager()->bluetoothLowEnergyManager()->unregisterDevice(lamp->bluetoothDevice()); - lamp->deleteLater(); - - info->finish(Device::DeviceErrorHardwareFailure, QT_TR_NOOP("Error connecting to lamp.")); - } - } - - }); - lamp->bluetoothDevice()->connectDevice(); -} - -void DevicePluginLukeRoberts::postSetupDevice(Device *device) -{ - Q_UNUSED(device) - - if (!m_reconnectTimer) { - m_reconnectTimer = hardwareManager()->pluginTimerManager()->registerTimer(10); - connect(m_reconnectTimer, &PluginTimer::timeout, this, &DevicePluginLukeRoberts::onReconnectTimeout); - } -} - - -void DevicePluginLukeRoberts::executeAction(DeviceActionInfo *info) -{ - Device *device = info->device(); - Action action = info->action(); - - QPointer lamp = m_lamps.key(device); - if (lamp.isNull()) - return info->finish(Device::DeviceErrorHardwareFailure); - - if (!lamp->bluetoothDevice()->connected()) { - return info->finish(Device::DeviceErrorHardwareNotAvailable); - } - - 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) { - 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); - } -} - - -void DevicePluginLukeRoberts::deviceRemoved(Device *device) -{ - if (!m_lamps.values().contains(device)) - return; - - LukeRoberts *lamp = m_lamps.key(device); - m_lamps.take(lamp); - - hardwareManager()->bluetoothLowEnergyManager()->unregisterDevice(lamp->bluetoothDevice()); - lamp->deleteLater(); - - if (myDevices().isEmpty()) { - hardwareManager()->pluginTimerManager()->unregisterTimer(m_reconnectTimer); - m_reconnectTimer = nullptr; - } -} - -void DevicePluginLukeRoberts::browseDevice(BrowseResult *result) -{ - LukeRoberts *lamp = m_lamps.key(result->device()); - if (!lamp) { - result->finish(Device::DeviceErrorHardwareNotAvailable); - return; - } - qDebug(dcLukeRoberts()) << "Browse device called"; - m_pendingBrowseResults.insert(lamp, result); - lamp->getSceneList(); -} - -void DevicePluginLukeRoberts::browserItem(BrowserItemResult *result) -{ - LukeRoberts *lamp = m_lamps.key(result->device()); - if (!lamp) { - result->finish(Device::DeviceErrorHardwareNotAvailable); - return; - } - qDebug(dcLukeRoberts()) << "Browse item called"; - -} - -void DevicePluginLukeRoberts::executeBrowserItem(BrowserActionInfo *info) -{ - LukeRoberts *lamp = m_lamps.key(info->device()); - if (!lamp) { - info->finish(Device::DeviceErrorHardwareNotAvailable); - return; - } - lamp->selectScene(info->browserAction().itemId().toInt()); - info->finish(Device::DeviceErrorNoError); -} - -void DevicePluginLukeRoberts::executeBrowserItemAction(BrowserItemActionInfo *info) -{ - LukeRoberts *lamp = m_lamps.key(info->device()); - if (!lamp) { - info->finish(Device::DeviceErrorHardwareNotAvailable); - return; - } -} - -void DevicePluginLukeRoberts::onPluginConfigurationChanged(const ParamTypeId ¶mTypeId, const QVariant &value) -{ - Q_UNUSED(paramTypeId) - Q_UNUSED(value) -} - - -void DevicePluginLukeRoberts::onReconnectTimeout() -{ - foreach (LukeRoberts *lamp, m_lamps.keys()) { - if (!lamp->bluetoothDevice()->connected()) { - lamp->bluetoothDevice()->connectDevice(); - } - } -} - -void DevicePluginLukeRoberts::onConnectedChanged(bool connected) -{ - LukeRoberts *lamp = static_cast(sender()); - Device *device = m_lamps.value(lamp); - device->setStateValue(modelFConnectedStateTypeId, connected); -} - -void DevicePluginLukeRoberts::onDeviceInformationChanged(const QString &firmwareRevision, const QString &hardwareRevision, const QString &softwareRevision) -{ - LukeRoberts *lamp = static_cast(sender()); - Device *device = m_lamps.value(lamp); - - qDebug(dcLukeRoberts()) << device->name() << "Firmware" << firmwareRevision << "Hardware" << hardwareRevision << "Software" << softwareRevision; - //device->setStateValue(modelFFirmwareRevisionStateTypeId, firmwareRevision); - //device->setStateValue(modelFardwareRevisionStateTypeId, hardwareRevision); - //device->setStateValue(modelFSoftwareRevisionStateTypeId, softwareRevision); -} - -void DevicePluginLukeRoberts::onSceneListReceived(QList scenes) -{ - LukeRoberts *lamp = static_cast(sender()); - if (m_pendingBrowseResults.contains(lamp)) { - BrowseResult *result = m_pendingBrowseResults.value(lamp); - foreach (LukeRoberts::Scene scene, scenes) { - BrowserItem item; - item.setId(QString::number(scene.id)); - item.setDisplayName(scene.name); - item.setBrowsable(false); - item.setExecutable(true); - item.setIcon(BrowserItem::BrowserIconApplication); - result->addItem(item); - } - } -} - -void DevicePluginLukeRoberts::onStatusCodeReceived(LukeRoberts::StatusCodes statusCode) -{ - qDebug(dcLukeRoberts()) << "Status code received" << statusCode; -} diff --git a/lukeroberts/devicepluginlukeroberts.h b/lukeroberts/devicepluginlukeroberts.h deleted file mode 100644 index c04b9a20..00000000 --- a/lukeroberts/devicepluginlukeroberts.h +++ /dev/null @@ -1,74 +0,0 @@ -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * - * * - * Copyright (C) 2019 Bernhard Trinnes . * - * * - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ - -#ifndef DEVICEPLUGINLUKEROBERTS_H -#define DEVICEPLUGINLUKEROBERTS_H - -#include "plugintimer.h" -#include "devices/deviceplugin.h" -#include "hardware/bluetoothlowenergy/bluetoothlowenergydevice.h" - -#include "lukeroberts.h" - -class DevicePluginLukeRoberts : public DevicePlugin -{ - Q_OBJECT - - Q_PLUGIN_METADATA(IID "io.nymea.DevicePlugin" FILE "devicepluginlukeroberts.json") - Q_INTERFACES(DevicePlugin) - -public: - explicit DevicePluginLukeRoberts(); - - void init() override; - void discoverDevices(DeviceDiscoveryInfo *info) override; - void setupDevice(DeviceSetupInfo *info) override; - void postSetupDevice(Device *device) override; - void executeAction(DeviceActionInfo *info) override; - - void deviceRemoved(Device *device) override; - - void browseDevice(BrowseResult *result) override; - void browserItem(BrowserItemResult *result) override; - void executeBrowserItem(BrowserActionInfo *info) override; - void executeBrowserItemAction(BrowserItemActionInfo *info) override; -private: - QHash m_lamps; - PluginTimer *m_reconnectTimer = nullptr; - bool m_autoSymbolMode = true; - - QHash m_pendingBrowseResults; - QHash m_pendingBrowserActions; - QHash m_pendingBrowserItemActions; - -private slots: - void onPluginConfigurationChanged(const ParamTypeId ¶mTypeId, const QVariant &value); - void onReconnectTimeout(); - - void onConnectedChanged(bool connected); - void onStatusCodeReceived(LukeRoberts::StatusCodes statusCode); - void onDeviceInformationChanged(const QString &firmwareRevision, const QString &hardwareRevision, const QString &softwareRevision); - - void onSceneListReceived(QList scenes); -}; - -#endif // DEVICEPLUGINLUKEROBERTS_H diff --git a/lukeroberts/devicepluginlukeroberts.json b/lukeroberts/devicepluginlukeroberts.json deleted file mode 100644 index aa22cdcd..00000000 --- a/lukeroberts/devicepluginlukeroberts.json +++ /dev/null @@ -1,98 +0,0 @@ -{ - "displayName": "Luke Roberts", - "id": "40f368ee-1815-46fd-9d74-4b8e5b5f21e7", - "name": "LukeRoberts", - "vendors": [ - { - "displayName": "Luke Roberts", - "name": "LukeRoberts", - "id": "4e942850-9553-493c-8a4a-5a468e124099", - "deviceClasses": [ - { - "id": "5de74ba9-4a46-4235-94b4-8aefc1e2b27f", - "name": "modelF", - "displayName": "Model F", - "createMethods": ["discovery"], - "interfaces": ["colorlight", "connectable"], - "browsable": true, - "paramTypes": [ - { - "id": "5cde31de-8c5d-4c10-890e-1dd3b5efcad1", - "name": "mac", - "displayName": "Mac address", - "type": "QString", - "inputType": "MacAddress" - } - ], - "stateTypes": [ - { - "id": "09b9e571-74a8-4d47-a432-ae9743915d8d", - "name": "connected", - "displayName": "Connected", - "displayNameEvent": "Connected changed", - "type": "bool", - "defaultValue": false - }, - { - "id": "ac3a55e1-48af-402d-b9ef-6020d4a1cb4f", - "name": "power", - "displayName": "power", - "displayNameEvent": "power changed", - "displayNameAction": "Set power", - "type": "bool", - "defaultValue": false, - "writable": true - }, - { - "id": "ce156b88-b142-4516-8110-5fe044ed0ba8", - "name": "colorTemperature", - "displayName": "color temperature", - "displayNameEvent": "color temperature changed", - "displayNameAction": "Set color temperature", - "type": "int", - "unit": "Mired", - "defaultValue": 170, - "minValue": 153, - "maxValue": 500, - "writable": true - }, - { - "id": "01844169-43de-42df-9908-f015bb3ea7c2", - "name": "color", - "displayName": "color", - "displayNameEvent": "color changed", - "displayNameAction": "Set color", - "type": "QColor", - "defaultValue": "#000000", - "writable": true - }, - { - "id": "70c92741-9c7f-47df-85c1-bdd05cef90b6", - "name": "brightness", - "displayName": "brightness", - "displayNameEvent": "brightness changed", - "displayNameAction": "Set brigtness", - "type": "int", - "unit": "Percentage", - "defaultValue": 0, - "minValue": 0, - "maxValue": 100, - "writable": true - } - ], - "actionTypes": [ - ], - "eventTypes": [ - ], - "browserItemActionTypes": [ - { - "id": "51407fb3-9966-43bf-b3d4-98e1fe6588ec", - "name": "updateScenes", - "displayName": "Update scenes" - } - ] - } - ] - } - ] -} diff --git a/lukeroberts/lukeroberts.cpp b/lukeroberts/lukeroberts.cpp index 83329a6c..19338892 100644 --- a/lukeroberts/lukeroberts.cpp +++ b/lukeroberts/lukeroberts.cpp @@ -1,24 +1,32 @@ -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * - * * - * Copyright (C) 2019 Bernhard Trinnes * - * * - * This file is part of nymea. * - * * - * This library is free software; you can redistribute it and/or * - * modify it under the terms of the GNU Lesser General Public * - * License as published by the Free Software Foundation; either * - * version 2.1 of the License, or (at your option) any later version. * - * * - * This library is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * - * Lesser General Public License for more details. * - * * - * You should have received a copy of the GNU Lesser General Public * - * License along with this library; If not, see * - * . * - * * - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +* +* Copyright 2013 - 2020, nymea GmbH +* Contact: contact@nymea.io +* +* This file is part of nymea. +* This project including source code and documentation is protected by +* copyright law, and remains the property of nymea GmbH. All rights, including +* reproduction, publication, editing and translation, are reserved. The use of +* this project is subject to the terms of a license agreement to be concluded +* with nymea GmbH in accordance with the terms of use of nymea GmbH, available +* under https://nymea.io/license +* +* GNU Lesser General Public License Usage +* Alternatively, this project may be redistributed and/or modified under the +* terms of the GNU Lesser General Public License as published by the Free +* Software Foundation; version 3. This project is distributed in the hope that +* it will be useful, but WITHOUT ANY WARRANTY; without even the implied +* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public License +* along with this project. If not, see . +* +* For any further details and any questions please contact us under +* contact@nymea.io or see our FAQ/Licensing Information on +* https://nymea.io/license/faq +* +* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ #include "lukeroberts.h" #include "extern-plugininfo.h" diff --git a/lukeroberts/lukeroberts.h b/lukeroberts/lukeroberts.h index a5663852..470c9654 100644 --- a/lukeroberts/lukeroberts.h +++ b/lukeroberts/lukeroberts.h @@ -1,24 +1,32 @@ -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * - * * - * Copyright (C) 2019 Bernhard Trinens * - * * - * This file is part of nymea. * - * * - * This library is free software; you can redistribute it and/or * - * modify it under the terms of the GNU Lesser General Public * - * License as published by the Free Software Foundation; either * - * version 2.1 of the License, or (at your option) any later version. * - * * - * This library is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * - * Lesser General Public License for more details. * - * * - * You should have received a copy of the GNU Lesser General Public * - * License along with this library; If not, see * - * . * - * * - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +* +* Copyright 2013 - 2020, nymea GmbH +* Contact: contact@nymea.io +* +* This file is part of nymea. +* This project including source code and documentation is protected by +* copyright law, and remains the property of nymea GmbH. All rights, including +* reproduction, publication, editing and translation, are reserved. The use of +* this project is subject to the terms of a license agreement to be concluded +* with nymea GmbH in accordance with the terms of use of nymea GmbH, available +* under https://nymea.io/license +* +* GNU Lesser General Public License Usage +* Alternatively, this project may be redistributed and/or modified under the +* terms of the GNU Lesser General Public License as published by the Free +* Software Foundation; version 3. This project is distributed in the hope that +* it will be useful, but WITHOUT ANY WARRANTY; without even the implied +* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public License +* along with this project. If not, see . +* +* For any further details and any questions please contact us under +* contact@nymea.io or see our FAQ/Licensing Information on +* https://nymea.io/license/faq +* +* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ #ifndef LUKEROBERTS_H #define LUKEROBERTS_H diff --git a/lukeroberts/lukeroberts.pro b/lukeroberts/lukeroberts.pro index 2b8782aa..965e41f5 100644 --- a/lukeroberts/lukeroberts.pro +++ b/lukeroberts/lukeroberts.pro @@ -2,14 +2,14 @@ include(../plugins.pri) QT += bluetooth -TARGET = $$qtLibraryTarget(nymea_devicepluginlukeroberts) +TARGET = $$qtLibraryTarget(nymea_integrationpluginlukeroberts) SOURCES += \ - devicepluginlukeroberts.cpp \ + integrationpluginlukeroberts.cpp \ lukeroberts.cpp HEADERS += \ - devicepluginlukeroberts.h \ + integrationpluginlukeroberts.h \ lukeroberts.h