From 1a8ed79269af48a2979e372c608f95e2ee35b63c Mon Sep 17 00:00:00 2001 From: Boernsman Date: Mon, 23 Dec 2019 20:44:34 +0100 Subject: [PATCH 1/6] added nanoleaf plugin --- debian/control | 16 ++ debian/nymea-plugin-nanoleaf.install.in | 1 + nanoleaf/README.md | 1 + nanoleaf/devicepluginnanoleaf.cpp | 183 ++++++++++++++++++++ nanoleaf/devicepluginnanoleaf.h | 64 +++++++ nanoleaf/devicepluginnanoleaf.json | 115 +++++++++++++ nanoleaf/nanoleaf.cpp | 213 ++++++++++++++++++++++++ nanoleaf/nanoleaf.h | 98 +++++++++++ nanoleaf/nanoleaf.pro | 16 ++ nymea-plugins.pro | 1 + 10 files changed, 708 insertions(+) create mode 100644 debian/nymea-plugin-nanoleaf.install.in create mode 100644 nanoleaf/README.md create mode 100644 nanoleaf/devicepluginnanoleaf.cpp create mode 100644 nanoleaf/devicepluginnanoleaf.h create mode 100644 nanoleaf/devicepluginnanoleaf.json create mode 100644 nanoleaf/nanoleaf.cpp create mode 100644 nanoleaf/nanoleaf.h create mode 100644 nanoleaf/nanoleaf.pro diff --git a/debian/control b/debian/control index 1840e055..2cbfbb1d 100644 --- a/debian/control +++ b/debian/control @@ -434,6 +434,21 @@ Description: nymea.io plugin for netatmo This package will install the nymea.io plugin for netatmo +Package: nymea-plugin-nanoleaf +Architecture: any +Depends: ${shlibs:Depends}, + ${misc:Depends}, + nymea-plugins-translations, +Description: nymea.io plugin for nanoleaf + The nymea daemon is a plugin based IoT (Internet of Things) server. The + server works like a translator for devices, things and services and + allows them to interact. + With the powerful rule engine you are able to connect any device available + in the system and create individual scenes and behaviors for your environment. + . + This package will install the nymea.io plugin for nanoleaf + + Package: nymea-plugin-networkdetector Architecture: any Depends: ${shlibs:Depends}, @@ -845,6 +860,7 @@ Depends: nymea-plugin-anel, nymea-plugin-lgsmarttv, nymea-plugin-mailnotification, nymea-plugin-texasinstruments, + nymea-plugin-nanoleaf, nymea-plugin-netatmo, nymea-plugin-networkdetector, nymea-plugin-openweathermap, diff --git a/debian/nymea-plugin-nanoleaf.install.in b/debian/nymea-plugin-nanoleaf.install.in new file mode 100644 index 00000000..c196777d --- /dev/null +++ b/debian/nymea-plugin-nanoleaf.install.in @@ -0,0 +1 @@ +usr/lib/@DEB_HOST_MULTIARCH@/nymea/plugins/libnymea_devicepluginnanoleaf.so diff --git a/nanoleaf/README.md b/nanoleaf/README.md new file mode 100644 index 00000000..55a87ec8 --- /dev/null +++ b/nanoleaf/README.md @@ -0,0 +1 @@ +# Nanoleaf diff --git a/nanoleaf/devicepluginnanoleaf.cpp b/nanoleaf/devicepluginnanoleaf.cpp new file mode 100644 index 00000000..e2e19cfc --- /dev/null +++ b/nanoleaf/devicepluginnanoleaf.cpp @@ -0,0 +1,183 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * Copyright (C) 2020 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 "devicepluginnanoleaf.h" +#include "plugininfo.h" + +#include "network/zeroconf/zeroconfservicebrowser.h" +#include "platform/platformzeroconfcontroller.h" + +#include +#include + +DevicePluginNanoleaf::DevicePluginNanoleaf() +{ + +} + + +void DevicePluginNanoleaf::init() +{ + m_zeroconfBrowser = hardwareManager()->zeroConfController()->createServiceBrowser("_nanoleafapi._tcp"); +} + +void DevicePluginNanoleaf::discoverDevices(DeviceDiscoveryInfo *info) +{ + QStringList serialNumbers; + foreach (const ZeroConfServiceEntry &entry, m_zeroconfBrowser->serviceEntries()) { + if (info->deviceClassId() == lightPanelsDeviceClassId) { + + } + //TODO skip duplicated devices + + DeviceDescriptor descriptor(lightPanelsDeviceClassId, entry.name(), entry.hostAddress().toString()); + ParamList params; + + QString serialNo; + QString model; + QString firmwareVersion; + + foreach (QString value, entry.txt()) { + if (value.contains("id=")) { + serialNo = value.split("=").last(); + } else if (value.contains("md=")) { + model = value.split("=").last(); + } else if (value.contains("srcvers=")) { + firmwareVersion = value.split("=").last(); + } + } + if (serialNumbers.contains(serialNo)) { + continue; //To avoid duplicated devices + } + + Device *existingDevice = myDevices().findByParams(ParamList() << Param(lightPanelsDeviceSerialNoParamTypeId, serialNo)); + if (existingDevice) { + descriptor.setDeviceId(existingDevice->id()); + } + + serialNumbers.append(serialNo); + qCDebug(dcNanoleaf()) << "Have device" << entry.name() << serialNo << model << firmwareVersion; + params << Param(lightPanelsDeviceAddressParamTypeId, entry.hostAddress().toString()); + params << Param(lightPanelsDevicePortParamTypeId, entry.port()); + params << Param(lightPanelsDeviceModelParamTypeId, model); + params << Param(lightPanelsDeviceSerialNoParamTypeId, serialNo); + params << Param(lightPanelsDeviceFirmwareVersionParamTypeId, firmwareVersion); + descriptor.setParams(params); + + info->addDeviceDescriptor(descriptor); + + } + info->finish(Device::DeviceErrorNoError); +} + +void DevicePluginNanoleaf::startPairing(DevicePairingInfo *info) +{ + info->finish(Device::DeviceErrorNoError, tr("Please press the button")); +} + +void DevicePluginNanoleaf::confirmPairing(DevicePairingInfo *info, const QString &username, const QString &secret) +{ + Q_UNUSED(username) + Q_UNUSED(secret) + Nanoleaf *nanoleaf = new Nanoleaf(hardwareManager()->networkManager(), QHostAddress(info->params().paramValue(lightPanelsDeviceAddressParamTypeId).toString()), info->params().paramValue(lightPanelsDevicePortParamTypeId).toInt(), this); + nanoleaf->addUser(); + info->finish(Device::DeviceErrorNoError); +} + +void DevicePluginNanoleaf::setupDevice(DeviceSetupInfo *info) +{ + Device *device = info->device(); + if(device->deviceClassId() == lightPanelsDeviceClassId) { + return info->finish(Device::DeviceErrorNoError); + } +} + +void DevicePluginNanoleaf::postSetupDevice(Device *device) +{ + if (device->deviceClassId() == lightPanelsDeviceClassId) { + //TODO get all the information and set the device states + } + + if(!m_pluginTimer) { + m_pluginTimer = hardwareManager()->pluginTimerManager()->registerTimer(5); + connect(m_pluginTimer, &PluginTimer::timeout, this, [this]() { + + }); + } +} + +void DevicePluginNanoleaf::deviceRemoved(Device *device) +{ + if(device->deviceClassId() == lightPanelsDeviceClassId) { + + } + + if (myDevices().isEmpty()) { + hardwareManager()->pluginTimerManager()->unregisterTimer(m_pluginTimer); + m_pluginTimer = nullptr; + } +} + +void DevicePluginNanoleaf::executeAction(DeviceActionInfo *info) +{ + Device *device = info->device(); + Action action = info->action(); + + if (device->deviceClassId() == lightPanelsDeviceClassId) { + Nanoleaf *nanoleaf = m_nanoleafConnections.value(device->id()); + if (action.actionTypeId() == lightPanelsPowerActionTypeId) { + bool power = action.param(lightPanelsPowerActionPowerParamTypeId).value().toBool(); + QUuid requestId = nanoleaf->setPower(power); + connect(info, &DeviceActionInfo::aborted,[requestId, this](){m_asyncActions.remove(requestId);}); + m_asyncActions.insert(requestId, info); + + } else if (action.actionTypeId() == lightPanelsBrightnessActionTypeId) { + int brightness = action.param(lightPanelsBrightnessActionBrightnessParamTypeId).value().toInt(); + QUuid requestId = nanoleaf->setBrightness(brightness); + connect(info, &DeviceActionInfo::aborted,[requestId, this](){m_asyncActions.remove(requestId);}); + m_asyncActions.insert(requestId, info); + + } else if (action.actionTypeId() == lightPanelsColorActionTypeId) { + QColor color(action.param(lightPanelsColorActionColorParamTypeId).value().toString()); + QUuid requestId = nanoleaf->setHue(color); + connect(info, &DeviceActionInfo::aborted,[requestId, this](){m_asyncActions.remove(requestId);}); + m_asyncActions.insert(requestId, info); + + } else if (action.actionTypeId() == lightPanelsColorTemperatureActionTypeId) { + int colorTemperature = action.param(lightPanelsColorTemperatureActionColorTemperatureParamTypeId).value().toInt(); + QUuid requestId = nanoleaf->setColorTemperature(colorTemperature); + connect(info, &DeviceActionInfo::aborted,[requestId, this](){m_asyncActions.remove(requestId);}); + m_asyncActions.insert(requestId, info); + } + } +} + +void DevicePluginNanoleaf::getDeviceStates(Nanoleaf *nanoleaf) +{ + nanoleaf->getPower(); + nanoleaf->getHue(); + nanoleaf->getColorMode(); + nanoleaf->getBrightness(); + nanoleaf->getSaturation(); + nanoleaf->getColorTemperature(); +} + diff --git a/nanoleaf/devicepluginnanoleaf.h b/nanoleaf/devicepluginnanoleaf.h new file mode 100644 index 00000000..cdc8d063 --- /dev/null +++ b/nanoleaf/devicepluginnanoleaf.h @@ -0,0 +1,64 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * Copyright (C) 2020 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 * + * . * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#ifndef DEVICEPLUGINNANOLEAF_H +#define DEVICEPLUGINNANOLEAF_H + +#include "devices/deviceplugin.h" +#include "nanoleaf.h" + +#include "plugintimer.h" +#include "network/networkaccessmanager.h" +#include "network/zeroconf/zeroconfservicebrowser.h" + +#include + +class DevicePluginNanoleaf: public DevicePlugin +{ + Q_OBJECT + + Q_PLUGIN_METADATA(IID "io.nymea.DevicePlugin" FILE "devicepluginnanoleaf.json") + Q_INTERFACES(DevicePlugin) + +public: + explicit DevicePluginNanoleaf(); + + void init() override; + void discoverDevices(DeviceDiscoveryInfo *info) override; + void startPairing(DevicePairingInfo *info) override; + void confirmPairing(DevicePairingInfo *info, const QString &username, const QString &secret) override; + void setupDevice(DeviceSetupInfo *info) override; + void postSetupDevice(Device *device) override; + void deviceRemoved(Device *device) override; + void executeAction(DeviceActionInfo *info) override; + + +private: + ZeroConfServiceBrowser *m_zeroconfBrowser = nullptr; + PluginTimer *m_pluginTimer = nullptr; + QHash m_nanoleafConnections; + QHash m_asyncActions; + + void getDeviceStates(Nanoleaf *nanoleaf); +}; + +#endif // DEVICEPLUGINNANOLEAF_H diff --git a/nanoleaf/devicepluginnanoleaf.json b/nanoleaf/devicepluginnanoleaf.json new file mode 100644 index 00000000..86ae6b86 --- /dev/null +++ b/nanoleaf/devicepluginnanoleaf.json @@ -0,0 +1,115 @@ +{ + "name": "Nanoleaf", + "displayName": "Nanoleaf", + "id": "360867ec-1594-498d-8182-fbab1fe17489", + "vendors": [ + { + "id": "3d7fdaa6-7896-419b-8be3-c90c42bcac7f", + "name": "nanoleaf", + "displayName": "Nanoleaf", + "deviceClasses": [ + { + "id": "d44ee383-9fa5-4751-babd-1129ac20896a", + "name": "lightPanels", + "displayName": "Light panels", + "interfaces": ["colorlight", "colortemperaturelight", "connectable"], + "createMethods": ["discovery"], + "paramTypes": [ + { + "id": "ff57079f-d5ab-4511-8a5c-0726e7b82af6", + "name": "address", + "displayName": "Address", + "type" : "QString" + }, + { + "id": "ba4fd45b-990d-480a-859d-fff7ffba3ba4", + "name": "port", + "displayName": "Port", + "type" : "int", + "readOnly": true + }, + { + "id": "353d3c71-0ad2-40d5-99f6-cc305e2073f1", + "name": "model", + "displayName": "Model", + "type" : "QString", + "readOnly": true + }, + { + "id": "18be4a5f-e2c2-4070-bc3e-ea9fe64f2276", + "name": "serialNo", + "displayName": "Serial number", + "type" : "QString", + "readOnly": true + }, + { + "id": "1b85eebe-3b1a-49a9-bddb-2175d6599b95", + "name": "firmwareVersion", + "displayName": "Firmware version", + "type" : "QString", + "readOnly": true + } + ], + "stateTypes": [ + { + "id": "a3102107-a825-4ec8-a9ec-b2c2a9fb5c89", + "name": "connected", + "displayName": "Reachable", + "displayNameEvent": "Reachable changed", + "defaultValue": false, + "type": "bool", + "cached": false + }, + { + "id": "44bee9ec-513d-4834-991a-ee9ae69d9f2a", + "name": "power", + "displayName": "Power", + "displayNameEvent": "Power changed", + "displayNameAction": "Set power", + "type": "bool", + "defaultValue": false, + "writable": true + }, + { + "id": "41248127-844b-40be-87e6-38aee48b6687", + "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": "d4a52cdc-93b2-44fc-a36c-ae65f1d98f2e", + "name": "color", + "displayName": "Color", + "displayNameEvent": "Color changed", + "displayNameAction": "Set color", + "type": "QColor", + "defaultValue": "#000000", + "writable": true + }, + { + "id": "4e5d6460-d42e-4b7c-a8f3-6e953451c1ef", + "name": "brightness", + "displayName": "Brightness", + "displayNameEvent": "Brightness changed", + "displayNameAction": "Set brigtness", + "type": "int", + "unit": "Percentage", + "defaultValue": 0, + "minValue": 0, + "maxValue": 100, + "writable": true + + } + ] + } + ] + } + ] +} diff --git a/nanoleaf/nanoleaf.cpp b/nanoleaf/nanoleaf.cpp new file mode 100644 index 00000000..d385efa4 --- /dev/null +++ b/nanoleaf/nanoleaf.cpp @@ -0,0 +1,213 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * Copyright (C) 2020 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 "nanoleaf.h" +#include "extern-plugininfo.h" + +#include +#include +#include +#include + +Nanoleaf::Nanoleaf(NetworkAccessManager *networkManager, const QHostAddress &address, int port, QObject *parent) : + QObject(parent), + m_networkManager(networkManager), + m_address(address), + m_port(port) +{ + +} + +void Nanoleaf::setIpAddress(const QHostAddress &address) +{ + m_address = address; +} + +QHostAddress Nanoleaf::ipAddress() +{ + return m_address; +} + +void Nanoleaf::setPort(int port) +{ + m_port = port; +} + +int Nanoleaf::port() +{ + return m_port; +} + +void Nanoleaf::addUser() +{ + QUrl url; + url.setHost(m_address.toString()); + url.setPort(m_port); + url.setPath("/api/v1/new"); + + QNetworkRequest request; + request.setUrl(url); + QNetworkReply *reply = m_networkManager->post(request, ""); + qDebug(dcNanoleaf()) << "Sending request" << request.url(); + connect(reply, &QNetworkReply::finished, this, [reply, this] { + reply->deleteLater(); + int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + + // Check HTTP status code + if (status != 200 || reply->error() != QNetworkReply::NoError) { + if (reply->error() == QNetworkReply::HostNotFoundError) { + emit connectionChanged(false); + } + if (status == 400 || status == 401) { + emit authenticationStatusChanged(false); + } + qCWarning(dcNanoleaf()) << "Request error:" << status << reply->errorString(); + return; + } + emit connectionChanged(true); + emit authenticationStatusChanged(true); + + QJsonParseError error; + QJsonDocument data = QJsonDocument::fromJson(reply->readAll(), &error); + if (error.error != QJsonParseError::NoError) { + qDebug(dcNanoleaf()) << "Recieved invalide JSON object"; + return; + } + + m_authToken = data.toVariant().toMap().value("auth_token").toString(); + }); +} + +void Nanoleaf::deleteUser() +{ + QUrl url; + url.setHost(m_address.toString()); + url.setPort(m_port); + url.setPath("/api/v1/"+m_authToken); + + QNetworkRequest request; + request.setUrl(url); + QNetworkReply *reply = m_networkManager->deleteResource(request); + qDebug(dcNanoleaf()) << "Sending request" << request.url(); + connect(reply, &QNetworkReply::finished, this, [reply, this] { + reply->deleteLater(); + int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + + if (status != 204 || reply->error() != QNetworkReply::NoError) { + qCWarning(dcNanoleaf()) << "Request error:" << status << reply->errorString(); + return; + } + }); +} + +void Nanoleaf::getPower() +{ + +} + +void Nanoleaf::getHue() +{ + +} + +void Nanoleaf::getBrightness() +{ + +} + +void Nanoleaf::getSaturation() +{ + +} + +void Nanoleaf::getColorTemperature() +{ + +} + +void Nanoleaf::getColorMode() +{ + +} + +QUuid Nanoleaf::setPower(bool power) +{ + QUuid requestId = QUuid::createUuid(); + QUrl url; + url.setHost(m_address.toString()); + url.setPort(m_port); + url.setPath(QString("/api/v1/%1/state/on").arg(m_authToken)); + + QVariantMap map; + QVariantMap value; + value["value"] = power; + map.insert("on", value); + QJsonDocument body = QJsonDocument::fromVariant(map); + + QNetworkRequest request; + request.setUrl(url); + QNetworkReply *reply = m_networkManager->put(request, body.toJson()); + qDebug(dcNanoleaf()) << "Sending request" << request.url(); + connect(reply, &QNetworkReply::finished, this, [requestId, reply, this] { + reply->deleteLater(); + int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + + if (status != 204 || reply->error() != QNetworkReply::NoError) { + emit requestedExecuted(requestId, false); + qCWarning(dcNanoleaf()) << "Request error:" << status << reply->errorString(); + return; + } + emit requestedExecuted(requestId, true); + }); + return requestId; +} + +QUuid Nanoleaf::setHue(QColor color) +{ + Q_UNUSED(color); + QUuid requestId = QUuid::createUuid(); + return requestId; +} + +QUuid Nanoleaf::setBrightness(int percentage) +{ + Q_UNUSED(percentage); + QUuid requestId = QUuid::createUuid(); + return requestId; +} + +QUuid Nanoleaf::setSaturation(int percentage) +{ + Q_UNUSED(percentage); + QUuid requestId = QUuid::createUuid(); + return requestId; +} + +QUuid Nanoleaf::setColorTemperature(int mired) +{ + Q_UNUSED(mired); + QUuid requestId = QUuid::createUuid(); + return requestId; +} + + + diff --git a/nanoleaf/nanoleaf.h b/nanoleaf/nanoleaf.h new file mode 100644 index 00000000..250d0a97 --- /dev/null +++ b/nanoleaf/nanoleaf.h @@ -0,0 +1,98 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * Copyright (C) 2020 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 * + * . * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#ifndef NANOLEAF_H +#define NANOLEAF_H + +#include +#include +#include +#include +#include + +#include "network/networkaccessmanager.h" +#include "devices/device.h" + +class Nanoleaf : public QObject +{ + Q_OBJECT +public: + + explicit Nanoleaf(NetworkAccessManager *networkManager, const QHostAddress &address, int port = 16021, QObject *parent = nullptr); + void setIpAddress(const QHostAddress &address); + QHostAddress ipAddress(); + + void setPort(int port); + int port(); + + //AUTHORIZATION + void addUser(); + void deleteUser(); + + //GET ALL PANEL INFORMATION + + //STATES + void getPower(); + void getHue(); + void getBrightness(); + void getSaturation(); + void getColorTemperature(); + void getColorMode(); + + QUuid setPower(bool power); + QUuid setHue(QColor color); + QUuid setBrightness(int percentage); + QUuid setSaturation(int percentage); + QUuid setColorTemperature(int mired); + + + //EFFECTS + + //PANEL LAYOUT + + //IDENTIFY + + + //EXTERNAL CONTROL + + + //RHYTHM + +private: + NetworkAccessManager *m_networkManager = nullptr; + QString m_authToken; + QHostAddress m_address; + int m_port; + +signals: + void connectionChanged(bool connected); + void authenticationStatusChanged(bool authenticated); + void requestedExecuted(QUuid requestId, bool success); + + void powerReceived(bool power); + void brightnessReceived(int percentage); + void colorModeReceived(); + void hueReceived(QColor color); + void saturationReceived(int percentage); +}; + +#endif // NANOLEAF_H diff --git a/nanoleaf/nanoleaf.pro b/nanoleaf/nanoleaf.pro new file mode 100644 index 00000000..9d3e6f6b --- /dev/null +++ b/nanoleaf/nanoleaf.pro @@ -0,0 +1,16 @@ +include(../plugins.pri) + +TARGET = $$qtLibraryTarget(nymea_devicepluginnanoleaf) + +QT += network + +SOURCES += \ + devicepluginnanoleaf.cpp \ + nanoleaf.cpp \ + +HEADERS += \ + devicepluginnanoleaf.h \ + nanoleaf.h \ + + + diff --git a/nymea-plugins.pro b/nymea-plugins.pro index 9e1177a0..24e1cb3a 100644 --- a/nymea-plugins.pro +++ b/nymea-plugins.pro @@ -28,6 +28,7 @@ PLUGIN_DIRS = \ lgsmarttv \ mailnotification \ mqttclient \ + nanoleaf \ netatmo \ networkdetector \ onewire \ From 9e53feb9855c165a232cc6c3de32ae9fc38ce383 Mon Sep 17 00:00:00 2001 From: Boernsman Date: Tue, 24 Dec 2019 00:57:45 +0100 Subject: [PATCH 2/6] added effect browsing --- nanoleaf/devicepluginnanoleaf.cpp | 260 +++++++++++++++++++- nanoleaf/devicepluginnanoleaf.h | 25 ++ nanoleaf/devicepluginnanoleaf.json | 22 +- nanoleaf/nanoleaf.cpp | 367 ++++++++++++++++++++++++++++- nanoleaf/nanoleaf.h | 17 +- 5 files changed, 669 insertions(+), 22 deletions(-) diff --git a/nanoleaf/devicepluginnanoleaf.cpp b/nanoleaf/devicepluginnanoleaf.cpp index e2e19cfc..f8694b5e 100644 --- a/nanoleaf/devicepluginnanoleaf.cpp +++ b/nanoleaf/devicepluginnanoleaf.cpp @@ -40,14 +40,11 @@ void DevicePluginNanoleaf::init() m_zeroconfBrowser = hardwareManager()->zeroConfController()->createServiceBrowser("_nanoleafapi._tcp"); } + void DevicePluginNanoleaf::discoverDevices(DeviceDiscoveryInfo *info) { QStringList serialNumbers; foreach (const ZeroConfServiceEntry &entry, m_zeroconfBrowser->serviceEntries()) { - if (info->deviceClassId() == lightPanelsDeviceClassId) { - - } - //TODO skip duplicated devices DeviceDescriptor descriptor(lightPanelsDeviceClassId, entry.name(), entry.hostAddress().toString()); ParamList params; @@ -89,46 +86,108 @@ void DevicePluginNanoleaf::discoverDevices(DeviceDiscoveryInfo *info) info->finish(Device::DeviceErrorNoError); } + void DevicePluginNanoleaf::startPairing(DevicePairingInfo *info) { - info->finish(Device::DeviceErrorNoError, tr("Please press the button")); + info->finish(Device::DeviceErrorNoError, tr("On the Nanoleaf controller, hold the on-off button for 5-7 seconds until the LED starts flashing.")); } + void DevicePluginNanoleaf::confirmPairing(DevicePairingInfo *info, const QString &username, const QString &secret) { Q_UNUSED(username) Q_UNUSED(secret) Nanoleaf *nanoleaf = new Nanoleaf(hardwareManager()->networkManager(), QHostAddress(info->params().paramValue(lightPanelsDeviceAddressParamTypeId).toString()), info->params().paramValue(lightPanelsDevicePortParamTypeId).toInt(), this); - nanoleaf->addUser(); - info->finish(Device::DeviceErrorNoError); + connect(nanoleaf, &Nanoleaf::authTokenRecieved, this, &DevicePluginNanoleaf::onAuthTokenReceived); + connect(nanoleaf, &Nanoleaf::authenticationStatusChanged, this, &DevicePluginNanoleaf::onAuthenticationStatusChanged); + connect(nanoleaf, &Nanoleaf::requestExecuted, this, &DevicePluginNanoleaf::onRequestExecuted); + connect(nanoleaf, &Nanoleaf::connectionChanged, this, &DevicePluginNanoleaf::onConnectionChanged); + + connect(nanoleaf, &Nanoleaf::brightnessReceived, this, &DevicePluginNanoleaf::onBrightnessReceived); + connect(nanoleaf, &Nanoleaf::powerReceived, this, &DevicePluginNanoleaf::onPowerReceived); + connect(nanoleaf, &Nanoleaf::colorModeReceived, this, &DevicePluginNanoleaf::onColorModeReceived); + connect(nanoleaf, &Nanoleaf::colorTemperatureReceived, this, &DevicePluginNanoleaf::onColorTemperatureReceived); + connect(nanoleaf, &Nanoleaf::saturationReceived, this, &DevicePluginNanoleaf::onSaturationReceived); + connect(nanoleaf, &Nanoleaf::hueReceived, this, &DevicePluginNanoleaf::onHueReceived); + connect(nanoleaf, &Nanoleaf::effectListReceived, this, &DevicePluginNanoleaf::onEffectListReceived); + connect(nanoleaf, &Nanoleaf::selectedEffectReceived, this, &DevicePluginNanoleaf::onSelectedEffectReceived); + nanoleaf->addUser(); //push button pairing + m_unfinishedNanoleafConnections.insert(info->deviceId(), nanoleaf); + m_unfinishedPairing.insert(nanoleaf, info); + connect(info, &DevicePairingInfo::aborted, this, [info, this] { + Nanoleaf *nanoleaf = m_unfinishedNanoleafConnections.take(info->deviceId()); + m_unfinishedPairing.remove(nanoleaf); + nanoleaf->deleteLater(); + }); } + void DevicePluginNanoleaf::setupDevice(DeviceSetupInfo *info) { Device *device = info->device(); if(device->deviceClassId() == lightPanelsDeviceClassId) { - return info->finish(Device::DeviceErrorNoError); + pluginStorage()->beginGroup(device->id().toString()); + QString token = pluginStorage()->value("authToken").toString(); + pluginStorage()->endGroup(); + + Nanoleaf *nanoleaf; + if (m_unfinishedNanoleafConnections.contains(device->id())) { + nanoleaf = m_unfinishedNanoleafConnections.take(device->id()); + m_nanoleafConnections.insert(device->id(), nanoleaf); + return info->finish(Device::DeviceErrorNoError); + } else { + QHostAddress address(device->paramValue(lightPanelsDeviceAddressParamTypeId).toString()); + int port = device->paramValue(lightPanelsDevicePortParamTypeId).toInt(); + nanoleaf = new Nanoleaf(hardwareManager()->networkManager(), address, port, this); + connect(nanoleaf, &Nanoleaf::authTokenRecieved, this, &DevicePluginNanoleaf::onAuthTokenReceived); + + connect(nanoleaf, &Nanoleaf::authenticationStatusChanged, this, &DevicePluginNanoleaf::onAuthenticationStatusChanged); + connect(nanoleaf, &Nanoleaf::requestExecuted, this, &DevicePluginNanoleaf::onRequestExecuted); + connect(nanoleaf, &Nanoleaf::connectionChanged, this, &DevicePluginNanoleaf::onConnectionChanged); + + connect(nanoleaf, &Nanoleaf::brightnessReceived, this, &DevicePluginNanoleaf::onBrightnessReceived); + connect(nanoleaf, &Nanoleaf::powerReceived, this, &DevicePluginNanoleaf::onPowerReceived); + connect(nanoleaf, &Nanoleaf::colorModeReceived, this, &DevicePluginNanoleaf::onColorModeReceived); + connect(nanoleaf, &Nanoleaf::saturationReceived, this, &DevicePluginNanoleaf::onSaturationReceived); + connect(nanoleaf, &Nanoleaf::hueReceived, this, &DevicePluginNanoleaf::onHueReceived); + connect(nanoleaf, &Nanoleaf::colorTemperatureReceived, this, &DevicePluginNanoleaf::onColorTemperatureReceived); + connect(nanoleaf, &Nanoleaf::effectListReceived, this, &DevicePluginNanoleaf::onEffectListReceived); + connect(nanoleaf, &Nanoleaf::selectedEffectReceived, this, &DevicePluginNanoleaf::onSelectedEffectReceived); + nanoleaf->setAuthToken(token); + nanoleaf->getControllerInfo(); //we don't care about the controller info, this is just to check if the device is available + + m_nanoleafConnections.insert(device->id(), nanoleaf); + m_asyncDeviceSetup.insert(nanoleaf, info); + connect(info, &DeviceSetupInfo::aborted, this, [nanoleaf, this](){m_asyncDeviceSetup.remove(nanoleaf);}); + return; + } } } + void DevicePluginNanoleaf::postSetupDevice(Device *device) { if (device->deviceClassId() == lightPanelsDeviceClassId) { - //TODO get all the information and set the device states + //Nanoleaf *nanoleaf = m_nanoleafConnections.value(device->id()); + //getDeviceStates(nanoleaf); } if(!m_pluginTimer) { m_pluginTimer = hardwareManager()->pluginTimerManager()->registerTimer(5); connect(m_pluginTimer, &PluginTimer::timeout, this, [this]() { - + foreach (Nanoleaf *nanoleaf, m_nanoleafConnections) { + getDeviceStates(nanoleaf); + } }); } } + void DevicePluginNanoleaf::deviceRemoved(Device *device) { if(device->deviceClassId() == lightPanelsDeviceClassId) { - + Nanoleaf *nanoleaf = m_nanoleafConnections.take(device->id()); + nanoleaf->deleteLater(); } if (myDevices().isEmpty()) { @@ -137,6 +196,7 @@ void DevicePluginNanoleaf::deviceRemoved(Device *device) } } + void DevicePluginNanoleaf::executeAction(DeviceActionInfo *info) { Device *device = info->device(); @@ -171,13 +231,189 @@ void DevicePluginNanoleaf::executeAction(DeviceActionInfo *info) } } +void DevicePluginNanoleaf::browseDevice(BrowseResult *result) +{ + Device *device = result->device(); + Nanoleaf *nanoleaf = m_nanoleafConnections.value(device->id()); + nanoleaf->getEffects(); + m_asyncBrowseResults.insert(nanoleaf, result); + connect(result, &BrowseResult::aborted, this, [nanoleaf, this]{m_asyncBrowseResults.remove(nanoleaf);}); +} + +void DevicePluginNanoleaf::browserItem(BrowserItemResult *result) +{ + Q_UNUSED(result) + qCDebug(dcNanoleaf()) << "BrowserItem called"; + //Device *device = result->device(); + //Nanoleaf *nanoleaf = m_nanoleafConnections.value(device->id()); + //nanoleaf->setEffect(info.); + //result-> + //m_asyncBrowseItems.insert(nanoleaf, result);*/ +} + +void DevicePluginNanoleaf::executeBrowserItem(BrowserActionInfo *info) +{ + Device *device = info->device(); + Nanoleaf *nanoleaf = m_nanoleafConnections.value(device->id()); + QUuid requestId = nanoleaf->setEffect(info->browserAction().itemId()); + m_asyncBrowserItem.insert(requestId, info); + connect(info, &BrowserActionInfo::aborted, this, [requestId, this]{m_asyncBrowserItem.remove(requestId);}); +} + + void DevicePluginNanoleaf::getDeviceStates(Nanoleaf *nanoleaf) { nanoleaf->getPower(); nanoleaf->getHue(); nanoleaf->getColorMode(); nanoleaf->getBrightness(); - nanoleaf->getSaturation(); nanoleaf->getColorTemperature(); + nanoleaf->getSelectedEffect(); +} + +void DevicePluginNanoleaf::onAuthTokenReceived(const QString &token) +{ + Nanoleaf *nanoleaf = static_cast(sender()); + if (m_unfinishedPairing.contains(nanoleaf)) { + DevicePairingInfo *info = m_unfinishedPairing.take(nanoleaf); + pluginStorage()->beginGroup(info->deviceId().toString()); + pluginStorage()->setValue("authToken", token); + pluginStorage()->endGroup(); + info->finish(Device::DeviceErrorNoError); + } +} + +void DevicePluginNanoleaf::onAuthenticationStatusChanged(bool authenticated) +{ + Nanoleaf *nanoleaf = static_cast(sender()); + if (m_asyncDeviceSetup.contains(nanoleaf)) { + DeviceSetupInfo *info = m_asyncDeviceSetup.take(nanoleaf); + if (authenticated) { + info->finish(Device::DeviceErrorNoError); + } else { + info->finish(Device::DeviceErrorSetupFailed); + } + } +} + +void DevicePluginNanoleaf::onRequestExecuted(QUuid requestId, bool success) +{ + if (m_asyncActions.contains(requestId)) { + DeviceActionInfo *info = m_asyncActions.take(requestId); + if (success) { + info->finish(Device::DeviceErrorNoError); + } else { + info->finish(Device::DeviceErrorHardwareNotAvailable); + } + } + + if (m_asyncBrowserItem.contains(requestId)) { + BrowserActionInfo *info = m_asyncBrowserItem.take(requestId); + if (success) { + info->finish(Device::DeviceErrorNoError); + } else { + info->finish(Device::DeviceErrorHardwareNotAvailable); + } + } +} + +void DevicePluginNanoleaf::onConnectionChanged(bool connected) +{ + Nanoleaf *nanoleaf = static_cast(sender()); + Device *device = myDevices().findById(m_nanoleafConnections.key(nanoleaf)); + if (!device) + return; + device->setStateValue(lightPanelsConnectedStateTypeId, connected); +} + +void DevicePluginNanoleaf::onPowerReceived(bool power) +{ + Nanoleaf *nanoleaf = static_cast(sender()); + Device *device = myDevices().findById(m_nanoleafConnections.key(nanoleaf)); + if (!device) + return; + device->setStateValue(lightPanelsPowerStateTypeId, power); +} + +void DevicePluginNanoleaf::onBrightnessReceived(int percentage) +{ + Nanoleaf *nanoleaf = static_cast(sender()); + Device *device = myDevices().findById(m_nanoleafConnections.key(nanoleaf)); + if (!device) + return; + device->setStateValue(lightPanelsBrightnessStateTypeId, percentage); +} + +void DevicePluginNanoleaf::onColorModeReceived(const QString &colorMode) +{ + Nanoleaf *nanoleaf = static_cast(sender()); + Device *device = myDevices().findById(m_nanoleafConnections.key(nanoleaf)); + if (!device) + return; + device->setStateValue(lightPanelsColorModeStateTypeId, colorMode); +} + +void DevicePluginNanoleaf::onHueReceived(int hue) +{ + Nanoleaf *nanoleaf = static_cast(sender()); + Device *device = myDevices().findById(m_nanoleafConnections.key(nanoleaf)); + if (!device) + return; + m_hues.insert(device->id(), hue); + nanoleaf->getSaturation(); +} + +void DevicePluginNanoleaf::onSaturationReceived(int saturation) +{ + Nanoleaf *nanoleaf = static_cast(sender()); + Device *device = myDevices().findById(m_nanoleafConnections.key(nanoleaf)); + if (!device) + return; + qCDebug(dcNanoleaf()) << "Saturation received" << saturation; + QColor color; + color.setHsv(m_hues.value(device->id()), saturation, 100); + //TODO get hue + device->setStateValue(lightPanelsColorStateTypeId, color); +} + +void DevicePluginNanoleaf::onEffectListReceived(const QStringList &effects) +{ + Nanoleaf *nanoleaf = static_cast(sender()); + Device *device = myDevices().findById(m_nanoleafConnections.key(nanoleaf)); + if (!device) + return; + qCDebug(dcNanoleaf()) << "Effect list received" << effects; + + if (m_asyncBrowseResults.contains(nanoleaf)) { + BrowseResult *result = m_asyncBrowseResults.take(nanoleaf); + foreach (QString effect, effects) { + BrowserItem item; + item.setId(effect); + item.setBrowsable(false); + item.setExecutable(true); + item.setDisplayName(effect); + item.setDisabled(false); + result->addItem(item); + } + result->finish(Device::DeviceErrorNoError); + } +} + +void DevicePluginNanoleaf::onColorTemperatureReceived(int mired) +{ + Nanoleaf *nanoleaf = static_cast(sender()); + Device *device = myDevices().findById(m_nanoleafConnections.key(nanoleaf)); + if (!device) + return; + device->setStateValue(lightPanelsColorTemperatureStateTypeId, mired); +} + +void DevicePluginNanoleaf::onSelectedEffectReceived(const QString &effect) +{ + Nanoleaf *nanoleaf = static_cast(sender()); + Device *device = myDevices().findById(m_nanoleafConnections.key(nanoleaf)); + if (!device) + return; + device->setStateValue(lightPanelsEffectNameStateTypeId, effect); } diff --git a/nanoleaf/devicepluginnanoleaf.h b/nanoleaf/devicepluginnanoleaf.h index cdc8d063..219e9fd7 100644 --- a/nanoleaf/devicepluginnanoleaf.h +++ b/nanoleaf/devicepluginnanoleaf.h @@ -51,14 +51,39 @@ public: void deviceRemoved(Device *device) override; void executeAction(DeviceActionInfo *info) override; + void browseDevice(BrowseResult *result) override; + void browserItem(BrowserItemResult *result) override; + void executeBrowserItem(BrowserActionInfo *info) override; private: ZeroConfServiceBrowser *m_zeroconfBrowser = nullptr; PluginTimer *m_pluginTimer = nullptr; QHash m_nanoleafConnections; + QHash m_unfinishedNanoleafConnections; QHash m_asyncActions; + QHash m_unfinishedPairing; + QHash m_asyncDeviceSetup; + + QHash m_asyncBrowseResults; + QHash m_asyncBrowserItem; + QHash m_hues; void getDeviceStates(Nanoleaf *nanoleaf); + +public slots: + void onAuthTokenReceived(const QString &token); + void onAuthenticationStatusChanged(bool authenticated); + void onRequestExecuted(QUuid requestId, bool success); + void onConnectionChanged(bool connected); + + void onPowerReceived(bool power); + void onBrightnessReceived(int percentage); + void onColorModeReceived(const QString &colorMode); + void onHueReceived(int hue); + void onSaturationReceived(int percentage); + void onEffectListReceived(const QStringList &effects); + void onColorTemperatureReceived(int mired); + void onSelectedEffectReceived(const QString &effect); }; #endif // DEVICEPLUGINNANOLEAF_H diff --git a/nanoleaf/devicepluginnanoleaf.json b/nanoleaf/devicepluginnanoleaf.json index 86ae6b86..5069321a 100644 --- a/nanoleaf/devicepluginnanoleaf.json +++ b/nanoleaf/devicepluginnanoleaf.json @@ -14,6 +14,8 @@ "displayName": "Light panels", "interfaces": ["colorlight", "colortemperaturelight", "connectable"], "createMethods": ["discovery"], + "setupMethod": "pushButton", + "browsable": true, "paramTypes": [ { "id": "ff57079f-d5ab-4511-8a5c-0726e7b82af6", @@ -105,7 +107,25 @@ "minValue": 0, "maxValue": 100, "writable": true - + }, + { + "id": "bdd2ea1e-9ef9-4967-9678-2c601b826199", + "name": "colorMode", + "displayName": "Color mode", + "displayNameEvent": "Color mode changed", + "displayNameAction": "Set color", + "type": "QString", + "defaultValue": "Color temperature", + "writable": true + }, + { + "id": "57f9831e-1b98-41c1-a21c-6073ff327237", + "name": "effectName", + "displayName": "Effect name", + "displayNameEvent": "Effect name changed", + "displayNameAction": "Set color", + "type": "QString", + "defaultValue": "-" } ] } diff --git a/nanoleaf/nanoleaf.cpp b/nanoleaf/nanoleaf.cpp index d385efa4..1db80348 100644 --- a/nanoleaf/nanoleaf.cpp +++ b/nanoleaf/nanoleaf.cpp @@ -57,15 +57,27 @@ int Nanoleaf::port() return m_port; } +void Nanoleaf::setAuthToken(const QString &token) +{ + m_authToken = token; +} + +QString Nanoleaf::authToken() +{ + return m_authToken; +} + void Nanoleaf::addUser() { QUrl url; url.setHost(m_address.toString()); url.setPort(m_port); + url.setScheme("http"); url.setPath("/api/v1/new"); QNetworkRequest request; request.setUrl(url); + request.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/json"); QNetworkReply *reply = m_networkManager->post(request, ""); qDebug(dcNanoleaf()) << "Sending request" << request.url(); connect(reply, &QNetworkReply::finished, this, [reply, this] { @@ -77,14 +89,13 @@ void Nanoleaf::addUser() if (reply->error() == QNetworkReply::HostNotFoundError) { emit connectionChanged(false); } - if (status == 400 || status == 401) { + if (status >= 400 && status <= 410) { emit authenticationStatusChanged(false); } qCWarning(dcNanoleaf()) << "Request error:" << status << reply->errorString(); return; } emit connectionChanged(true); - emit authenticationStatusChanged(true); QJsonParseError error; QJsonDocument data = QJsonDocument::fromJson(reply->readAll(), &error); @@ -94,6 +105,9 @@ void Nanoleaf::addUser() } m_authToken = data.toVariant().toMap().value("auth_token").toString(); + + emit authTokenRecieved(m_authToken); + emit authenticationStatusChanged(true); }); } @@ -102,6 +116,7 @@ void Nanoleaf::deleteUser() QUrl url; url.setHost(m_address.toString()); url.setPort(m_port); + url.setScheme("http"); url.setPath("/api/v1/"+m_authToken); QNetworkRequest request; @@ -116,37 +131,225 @@ void Nanoleaf::deleteUser() qCWarning(dcNanoleaf()) << "Request error:" << status << reply->errorString(); return; } + emit authenticationStatusChanged(false); + }); +} + +void Nanoleaf::getControllerInfo() +{ + QUrl url; + url.setHost(m_address.toString()); + url.setPort(m_port); + url.setScheme("http"); + url.setPath("/api/v1/"+m_authToken); + + QNetworkRequest request; + request.setUrl(url); + QNetworkReply *reply = m_networkManager->get(request); + qDebug(dcNanoleaf()) << "Sending request" << request.url(); + connect(reply, &QNetworkReply::finished, this, [reply, this] { + reply->deleteLater(); + int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + + if (status != 204 || reply->error() != QNetworkReply::NoError) { + qCWarning(dcNanoleaf()) << "Request error:" << status << reply->errorString(); + emit authenticationStatusChanged(false); + return; + } + emit connectionChanged(true); + emit authenticationStatusChanged(true); }); } void Nanoleaf::getPower() { + QUrl url; + url.setHost(m_address.toString()); + url.setPort(m_port); + url.setScheme("http"); + url.setPath("/api/v1/"+m_authToken+"/state/on"); + QNetworkRequest request; + request.setUrl(url); + QNetworkReply *reply = m_networkManager->get(request); + qDebug(dcNanoleaf()) << "Sending request" << request.url(); + connect(reply, &QNetworkReply::finished, this, [reply, this] { + reply->deleteLater(); + int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + + if (status != 200 || reply->error() != QNetworkReply::NoError) { + qCWarning(dcNanoleaf()) << "Request error:" << status << reply->errorString(); + emit connectionChanged(false); + return; + } + QJsonParseError error; + QJsonDocument data = QJsonDocument::fromJson(reply->readAll(), &error); + if (error.error != QJsonParseError::NoError) { + qDebug(dcNanoleaf()) << "Recieved invalide JSON object"; + return; + } + bool power = data.toVariant().toMap().value("value").toBool(); + emit connectionChanged(true); + emit powerReceived(power); + }); } void Nanoleaf::getHue() { + QUrl url; + url.setHost(m_address.toString()); + url.setPort(m_port); + url.setScheme("http"); + url.setPath("/api/v1/"+m_authToken+"/state/hue"); + QNetworkRequest request; + request.setUrl(url); + QNetworkReply *reply = m_networkManager->get(request); + qDebug(dcNanoleaf()) << "Sending request" << request.url(); + connect(reply, &QNetworkReply::finished, this, [reply, this] { + reply->deleteLater(); + int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + + if (status != 200 || reply->error() != QNetworkReply::NoError) { + qCWarning(dcNanoleaf()) << "Request error:" << status << reply->errorString(); + return; + } + QJsonParseError error; + QJsonDocument data = QJsonDocument::fromJson(reply->readAll(), &error); + if (error.error != QJsonParseError::NoError) { + qDebug(dcNanoleaf()) << "Recieved invalide JSON object"; + return; + } + int hue = data.toVariant().toMap().value("value").toBool(); + emit connectionChanged(true); + emit hueReceived(hue); + }); } void Nanoleaf::getBrightness() { + QUrl url; + url.setHost(m_address.toString()); + url.setPort(m_port); + url.setScheme("http"); + url.setPath("/api/v1/"+m_authToken+"/state/brightness"); + QNetworkRequest request; + request.setUrl(url); + QNetworkReply *reply = m_networkManager->get(request); + connect(reply, &QNetworkReply::finished, this, [reply, this] { + reply->deleteLater(); + int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + + if (status != 200 || reply->error() != QNetworkReply::NoError) { + qCWarning(dcNanoleaf()) << "Request error:" << status << reply->errorString(); + return; + } + QJsonParseError error; + QJsonDocument data = QJsonDocument::fromJson(reply->readAll(), &error); + if (error.error != QJsonParseError::NoError) { + qDebug(dcNanoleaf()) << "Recieved invalide JSON object"; + return; + } + int brightness = data.toVariant().toMap().value("value").toInt(); + emit connectionChanged(true); + emit brightnessReceived(brightness); + }); } void Nanoleaf::getSaturation() { + QUrl url; + url.setHost(m_address.toString()); + url.setPort(m_port); + url.setScheme("http"); + url.setPath("/api/v1/"+m_authToken+"/state/sat"); + QNetworkRequest request; + request.setUrl(url); + QNetworkReply *reply = m_networkManager->get(request); + connect(reply, &QNetworkReply::finished, this, [reply, this] { + reply->deleteLater(); + int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + + if (status != 200 || reply->error() != QNetworkReply::NoError) { + qCWarning(dcNanoleaf()) << "Request error:" << status << reply->errorString(); + return; + } + QJsonParseError error; + QJsonDocument data = QJsonDocument::fromJson(reply->readAll(), &error); + if (error.error != QJsonParseError::NoError) { + qDebug(dcNanoleaf()) << "Recieved invalide JSON object"; + return; + } + int brightness = data.toVariant().toMap().value("value").toInt(); + emit connectionChanged(true); + emit saturationReceived(brightness); + }); } void Nanoleaf::getColorTemperature() { + QUrl url; + url.setHost(m_address.toString()); + url.setPort(m_port); + url.setScheme("http"); + url.setPath("/api/v1/"+m_authToken+"/state/ct"); + QNetworkRequest request; + request.setUrl(url); + QNetworkReply *reply = m_networkManager->get(request); + connect(reply, &QNetworkReply::finished, this, [reply, this] { + reply->deleteLater(); + int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + + if (status != 200 || reply->error() != QNetworkReply::NoError) { + qCWarning(dcNanoleaf()) << "Request error:" << status << reply->errorString(); + emit connectionChanged(false); + return; + } + QJsonParseError error; + QJsonDocument data = QJsonDocument::fromJson(reply->readAll(), &error); + if (error.error != QJsonParseError::NoError) { + qDebug(dcNanoleaf()) << "Recieved invalide JSON object"; + return; + } + int mired = data.toVariant().toMap().value("value").toInt(); + emit connectionChanged(true); + emit colorTemperatureReceived(mired); + }); } void Nanoleaf::getColorMode() { + QUrl url; + url.setHost(m_address.toString()); + url.setPort(m_port); + url.setScheme("http"); + url.setPath("/api/v1/"+m_authToken+"/state/colorMode"); + QNetworkRequest request; + request.setUrl(url); + QNetworkReply *reply = m_networkManager->get(request); + connect(reply, &QNetworkReply::finished, this, [reply, this] { + reply->deleteLater(); + int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + + if (status != 200 || reply->error() != QNetworkReply::NoError) { + qCWarning(dcNanoleaf()) << "Request error:" << status << reply->errorString(); + emit connectionChanged(false); + return; + } + QJsonParseError error; + QJsonDocument data = QJsonDocument::fromJson(reply->readAll(), &error); + if (error.error != QJsonParseError::NoError) { + qDebug(dcNanoleaf()) << "Recieved invalide JSON object"; + return; + } + QString colorMode = data.toVariant().toMap().value("value").toString(); + emit connectionChanged(true); + emit colorModeReceived(colorMode); + }); } QUuid Nanoleaf::setPower(bool power) @@ -155,7 +358,8 @@ QUuid Nanoleaf::setPower(bool power) QUrl url; url.setHost(m_address.toString()); url.setPort(m_port); - url.setPath(QString("/api/v1/%1/state/on").arg(m_authToken)); + url.setScheme("http"); + url.setPath(QString("/api/v1/%1/state").arg(m_authToken)); QVariantMap map; QVariantMap value; @@ -165,6 +369,7 @@ QUuid Nanoleaf::setPower(bool power) QNetworkRequest request; request.setUrl(url); + request.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/json"); QNetworkReply *reply = m_networkManager->put(request, body.toJson()); qDebug(dcNanoleaf()) << "Sending request" << request.url(); connect(reply, &QNetworkReply::finished, this, [requestId, reply, this] { @@ -172,11 +377,12 @@ QUuid Nanoleaf::setPower(bool power) int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); if (status != 204 || reply->error() != QNetworkReply::NoError) { - emit requestedExecuted(requestId, false); + emit requestExecuted(requestId, false); qCWarning(dcNanoleaf()) << "Request error:" << status << reply->errorString(); return; } - emit requestedExecuted(requestId, true); + emit connectionChanged(true); + emit requestExecuted(requestId, true); }); return requestId; } @@ -190,8 +396,35 @@ QUuid Nanoleaf::setHue(QColor color) QUuid Nanoleaf::setBrightness(int percentage) { - Q_UNUSED(percentage); QUuid requestId = QUuid::createUuid(); + QUrl url; + url.setHost(m_address.toString()); + url.setPort(m_port); + url.setScheme("http"); + url.setPath(QString("/api/v1/%1/state").arg(m_authToken)); + + QVariantMap map; + QVariantMap value; + value["value"] = percentage; + map.insert("brightness", value); + QJsonDocument body = QJsonDocument::fromVariant(map); + + QNetworkRequest request; + request.setUrl(url); + request.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/json"); + QNetworkReply *reply = m_networkManager->put(request, body.toJson()); + qDebug(dcNanoleaf()) << "Sending request" << request.url(); + connect(reply, &QNetworkReply::finished, this, [requestId, reply, this] { + reply->deleteLater(); + int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + + if (status != 204 || reply->error() != QNetworkReply::NoError) { + emit requestExecuted(requestId, false); + qCWarning(dcNanoleaf()) << "Request error:" << status << reply->errorString(); + return; + } + emit requestExecuted(requestId, true); + }); return requestId; } @@ -199,6 +432,34 @@ QUuid Nanoleaf::setSaturation(int percentage) { Q_UNUSED(percentage); QUuid requestId = QUuid::createUuid(); + QUrl url; + url.setHost(m_address.toString()); + url.setPort(m_port); + url.setScheme("http"); + url.setPath(QString("/api/v1/%1/state/sat").arg(m_authToken)); + + QVariantMap map; + QVariantMap value; + value["value"] = percentage; + map.insert("sat", value); + QJsonDocument body = QJsonDocument::fromVariant(map); + + QNetworkRequest request; + request.setUrl(url); + request.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/json"); + QNetworkReply *reply = m_networkManager->put(request, body.toJson()); + qDebug(dcNanoleaf()) << "Sending request" << request.url(); + connect(reply, &QNetworkReply::finished, this, [requestId, reply, this] { + reply->deleteLater(); + int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + + if (status != 204 || reply->error() != QNetworkReply::NoError) { + emit requestExecuted(requestId, false); + qCWarning(dcNanoleaf()) << "Request error:" << status << reply->errorString(); + return; + } + emit requestExecuted(requestId, true); + }); return requestId; } @@ -209,5 +470,99 @@ QUuid Nanoleaf::setColorTemperature(int mired) return requestId; } +void Nanoleaf::getEffects() +{ + QUrl url; + url.setHost(m_address.toString()); + url.setPort(m_port); + url.setScheme("http"); + url.setPath("/api/v1/"+m_authToken+"/effects/effectsList"); + + QNetworkRequest request; + request.setUrl(url); + QNetworkReply *reply = m_networkManager->get(request); + connect(reply, &QNetworkReply::finished, this, [reply, this] { + reply->deleteLater(); + int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + + if (status != 200 || reply->error() != QNetworkReply::NoError) { + qCWarning(dcNanoleaf()) << "Request error:" << status << reply->errorString(); + emit connectionChanged(false); + return; + } + QJsonParseError error; + QJsonDocument data = QJsonDocument::fromJson(reply->readAll(), &error); + if (error.error != QJsonParseError::NoError) { + qDebug(dcNanoleaf()) << "Recieved invalide JSON object"; + return; + } + QStringList effects; + foreach (QVariant effect, data.toVariant().toList()) { + effects.append(effect.toString()); + } + + emit connectionChanged(true); + emit effectListReceived(effects); + }); +} + +void Nanoleaf::getSelectedEffect() +{ + QUrl url; + url.setHost(m_address.toString()); + url.setPort(m_port); + url.setScheme("http"); + url.setPath("/api/v1/"+m_authToken+"/effects/select"); + + QNetworkRequest request; + request.setUrl(url); + QNetworkReply *reply = m_networkManager->get(request); + connect(reply, &QNetworkReply::finished, this, [reply, this] { + reply->deleteLater(); + int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + + if (status != 200 || reply->error() != QNetworkReply::NoError) { + qCWarning(dcNanoleaf()) << "Request error:" << status << reply->errorString(); + emit connectionChanged(false); + return; + } + QString effect = reply->readAll(); + emit connectionChanged(true); + emit selectedEffectReceived(effect); + }); +} + +QUuid Nanoleaf::setEffect(const QString &effect) +{ + QUuid requestId = QUuid::createUuid(); + QUrl url; + url.setHost(m_address.toString()); + url.setPort(m_port); + url.setScheme("http"); + url.setPath(QString("/api/v1/%1/effects").arg(m_authToken)); + + QVariantMap map; + map.insert("select", effect); + QJsonDocument body = QJsonDocument::fromVariant(map); + + QNetworkRequest request; + request.setUrl(url); + request.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/json"); + QNetworkReply *reply = m_networkManager->put(request, body.toJson()); + qDebug(dcNanoleaf()) << "Sending request" << request.url(); + connect(reply, &QNetworkReply::finished, this, [requestId, reply, this] { + reply->deleteLater(); + int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + + if (status != 204 || reply->error() != QNetworkReply::NoError) { + emit requestExecuted(requestId, false); + qCWarning(dcNanoleaf()) << "Request error:" << status << reply->errorString(); + return; + } + emit requestExecuted(requestId, true); + }); + return requestId; +} + diff --git a/nanoleaf/nanoleaf.h b/nanoleaf/nanoleaf.h index 250d0a97..ff12b40f 100644 --- a/nanoleaf/nanoleaf.h +++ b/nanoleaf/nanoleaf.h @@ -44,11 +44,15 @@ public: void setPort(int port); int port(); + void setAuthToken(const QString &token); + QString authToken(); + //AUTHORIZATION void addUser(); void deleteUser(); //GET ALL PANEL INFORMATION + void getControllerInfo(); //STATES void getPower(); @@ -66,6 +70,9 @@ public: //EFFECTS + void getEffects(); + void getSelectedEffect(); + QUuid setEffect(const QString &effect); //PANEL LAYOUT @@ -86,13 +93,17 @@ private: signals: void connectionChanged(bool connected); void authenticationStatusChanged(bool authenticated); - void requestedExecuted(QUuid requestId, bool success); + void requestExecuted(QUuid requestId, bool success); + void authTokenRecieved(const QString &token); void powerReceived(bool power); void brightnessReceived(int percentage); - void colorModeReceived(); - void hueReceived(QColor color); + void colorModeReceived(const QString &colorMode); + void hueReceived(int hue); void saturationReceived(int percentage); + void effectListReceived(const QStringList &effects); + void colorTemperatureReceived(int mired); + void selectedEffectReceived(const QString &effect); }; #endif // NANOLEAF_H From 6771504edfa69354d4e7780d18a5916330b0f96c Mon Sep 17 00:00:00 2001 From: Boernsman Date: Fri, 3 Jan 2020 14:26:18 +0100 Subject: [PATCH 3/6] added alert interface --- nanoleaf/README.md | 33 +++ nanoleaf/devicepluginnanoleaf.cpp | 176 ++++++++-------- nanoleaf/devicepluginnanoleaf.h | 53 ++--- nanoleaf/devicepluginnanoleaf.json | 16 +- nanoleaf/nanoleaf.cpp | 319 ++++++++++++++++++++++++++--- nanoleaf/nanoleaf.h | 88 +++++--- 6 files changed, 510 insertions(+), 175 deletions(-) diff --git a/nanoleaf/README.md b/nanoleaf/README.md index 55a87ec8..afca6fd9 100644 --- a/nanoleaf/README.md +++ b/nanoleaf/README.md @@ -1 +1,34 @@ # Nanoleaf + +This Plug-In allows to control Nanoleaf Light Panels. + +## Features + +Controls: + * Power + * Brightness + * Color Temperature + * Color + * Set Effect + +States: + * Connected + +Browsing: +This plug-in implements also browsing for light effects, means if a new light effect is beeing added +nymea will find that. + +## Device Setup + +The Nanoleaf App is required to connect the device to the WiFi Network. + +This Plug-In uses the local API of Nanoleaf devices, means +nymea must be in the same local area network. + +The device will be discovered through Zeroconf, if it +can't be discovered the Network might not support Zeroconf +and the IP-Address must be entered manually. + +More about Nanoleaf devices: +https://nanoleaf.me + diff --git a/nanoleaf/devicepluginnanoleaf.cpp b/nanoleaf/devicepluginnanoleaf.cpp index f8694b5e..4882aa1c 100644 --- a/nanoleaf/devicepluginnanoleaf.cpp +++ b/nanoleaf/devicepluginnanoleaf.cpp @@ -1,24 +1,30 @@ -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * - * * - * Copyright (C) 2020 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 +* This project may also contain libraries licensed under the open source software license GNU GPL v.3. +* 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 "devicepluginnanoleaf.h" #include "plugininfo.h" @@ -34,13 +40,11 @@ DevicePluginNanoleaf::DevicePluginNanoleaf() } - void DevicePluginNanoleaf::init() { m_zeroconfBrowser = hardwareManager()->zeroConfController()->createServiceBrowser("_nanoleafapi._tcp"); } - void DevicePluginNanoleaf::discoverDevices(DeviceDiscoveryInfo *info) { QStringList serialNumbers; @@ -68,6 +72,7 @@ void DevicePluginNanoleaf::discoverDevices(DeviceDiscoveryInfo *info) Device *existingDevice = myDevices().findByParams(ParamList() << Param(lightPanelsDeviceSerialNoParamTypeId, serialNo)); if (existingDevice) { + //For device rediscovery descriptor.setDeviceId(existingDevice->id()); } @@ -81,47 +86,30 @@ void DevicePluginNanoleaf::discoverDevices(DeviceDiscoveryInfo *info) descriptor.setParams(params); info->addDeviceDescriptor(descriptor); - } info->finish(Device::DeviceErrorNoError); } - void DevicePluginNanoleaf::startPairing(DevicePairingInfo *info) { info->finish(Device::DeviceErrorNoError, tr("On the Nanoleaf controller, hold the on-off button for 5-7 seconds until the LED starts flashing.")); } - void DevicePluginNanoleaf::confirmPairing(DevicePairingInfo *info, const QString &username, const QString &secret) { Q_UNUSED(username) Q_UNUSED(secret) - Nanoleaf *nanoleaf = new Nanoleaf(hardwareManager()->networkManager(), QHostAddress(info->params().paramValue(lightPanelsDeviceAddressParamTypeId).toString()), info->params().paramValue(lightPanelsDevicePortParamTypeId).toInt(), this); - connect(nanoleaf, &Nanoleaf::authTokenRecieved, this, &DevicePluginNanoleaf::onAuthTokenReceived); - connect(nanoleaf, &Nanoleaf::authenticationStatusChanged, this, &DevicePluginNanoleaf::onAuthenticationStatusChanged); - connect(nanoleaf, &Nanoleaf::requestExecuted, this, &DevicePluginNanoleaf::onRequestExecuted); - connect(nanoleaf, &Nanoleaf::connectionChanged, this, &DevicePluginNanoleaf::onConnectionChanged); - - connect(nanoleaf, &Nanoleaf::brightnessReceived, this, &DevicePluginNanoleaf::onBrightnessReceived); - connect(nanoleaf, &Nanoleaf::powerReceived, this, &DevicePluginNanoleaf::onPowerReceived); - connect(nanoleaf, &Nanoleaf::colorModeReceived, this, &DevicePluginNanoleaf::onColorModeReceived); - connect(nanoleaf, &Nanoleaf::colorTemperatureReceived, this, &DevicePluginNanoleaf::onColorTemperatureReceived); - connect(nanoleaf, &Nanoleaf::saturationReceived, this, &DevicePluginNanoleaf::onSaturationReceived); - connect(nanoleaf, &Nanoleaf::hueReceived, this, &DevicePluginNanoleaf::onHueReceived); - connect(nanoleaf, &Nanoleaf::effectListReceived, this, &DevicePluginNanoleaf::onEffectListReceived); - connect(nanoleaf, &Nanoleaf::selectedEffectReceived, this, &DevicePluginNanoleaf::onSelectedEffectReceived); + Nanoleaf *nanoleaf = createNanoleafConnection(QHostAddress(info->params().paramValue(lightPanelsDeviceAddressParamTypeId).toString()), info->params().paramValue(lightPanelsDevicePortParamTypeId).toInt()); nanoleaf->addUser(); //push button pairing m_unfinishedNanoleafConnections.insert(info->deviceId(), nanoleaf); m_unfinishedPairing.insert(nanoleaf, info); connect(info, &DevicePairingInfo::aborted, this, [info, this] { - Nanoleaf *nanoleaf = m_unfinishedNanoleafConnections.take(info->deviceId()); - m_unfinishedPairing.remove(nanoleaf); - nanoleaf->deleteLater(); + Nanoleaf *nanoleaf = m_unfinishedNanoleafConnections.take(info->deviceId()); + m_unfinishedPairing.remove(nanoleaf); + nanoleaf->deleteLater(); }); } - void DevicePluginNanoleaf::setupDevice(DeviceSetupInfo *info) { Device *device = info->device(); @@ -132,29 +120,17 @@ void DevicePluginNanoleaf::setupDevice(DeviceSetupInfo *info) Nanoleaf *nanoleaf; if (m_unfinishedNanoleafConnections.contains(device->id())) { + // This setupDevice is called after a discovery nanoleaf = m_unfinishedNanoleafConnections.take(device->id()); m_nanoleafConnections.insert(device->id(), nanoleaf); return info->finish(Device::DeviceErrorNoError); } else { + // This setupDevice is called after a (re)start, with an already added device QHostAddress address(device->paramValue(lightPanelsDeviceAddressParamTypeId).toString()); int port = device->paramValue(lightPanelsDevicePortParamTypeId).toInt(); - nanoleaf = new Nanoleaf(hardwareManager()->networkManager(), address, port, this); - connect(nanoleaf, &Nanoleaf::authTokenRecieved, this, &DevicePluginNanoleaf::onAuthTokenReceived); - - connect(nanoleaf, &Nanoleaf::authenticationStatusChanged, this, &DevicePluginNanoleaf::onAuthenticationStatusChanged); - connect(nanoleaf, &Nanoleaf::requestExecuted, this, &DevicePluginNanoleaf::onRequestExecuted); - connect(nanoleaf, &Nanoleaf::connectionChanged, this, &DevicePluginNanoleaf::onConnectionChanged); - - connect(nanoleaf, &Nanoleaf::brightnessReceived, this, &DevicePluginNanoleaf::onBrightnessReceived); - connect(nanoleaf, &Nanoleaf::powerReceived, this, &DevicePluginNanoleaf::onPowerReceived); - connect(nanoleaf, &Nanoleaf::colorModeReceived, this, &DevicePluginNanoleaf::onColorModeReceived); - connect(nanoleaf, &Nanoleaf::saturationReceived, this, &DevicePluginNanoleaf::onSaturationReceived); - connect(nanoleaf, &Nanoleaf::hueReceived, this, &DevicePluginNanoleaf::onHueReceived); - connect(nanoleaf, &Nanoleaf::colorTemperatureReceived, this, &DevicePluginNanoleaf::onColorTemperatureReceived); - connect(nanoleaf, &Nanoleaf::effectListReceived, this, &DevicePluginNanoleaf::onEffectListReceived); - connect(nanoleaf, &Nanoleaf::selectedEffectReceived, this, &DevicePluginNanoleaf::onSelectedEffectReceived); + nanoleaf = createNanoleafConnection(address, port); nanoleaf->setAuthToken(token); - nanoleaf->getControllerInfo(); //we don't care about the controller info, this is just to check if the device is available + nanoleaf->getControllerInfo(); //This is just to check if the device is available m_nanoleafConnections.insert(device->id(), nanoleaf); m_asyncDeviceSetup.insert(nanoleaf, info); @@ -164,19 +140,21 @@ void DevicePluginNanoleaf::setupDevice(DeviceSetupInfo *info) } } - void DevicePluginNanoleaf::postSetupDevice(Device *device) { if (device->deviceClassId() == lightPanelsDeviceClassId) { - //Nanoleaf *nanoleaf = m_nanoleafConnections.value(device->id()); - //getDeviceStates(nanoleaf); + Nanoleaf *nanoleaf = m_nanoleafConnections.value(device->id()); + if (!nanoleaf) + return; + nanoleaf->getControllerInfo(); + nanoleaf->registerForEvents(); } if(!m_pluginTimer) { m_pluginTimer = hardwareManager()->pluginTimerManager()->registerTimer(5); connect(m_pluginTimer, &PluginTimer::timeout, this, [this]() { foreach (Nanoleaf *nanoleaf, m_nanoleafConnections) { - getDeviceStates(nanoleaf); + nanoleaf->getControllerInfo(); } }); } @@ -204,6 +182,10 @@ void DevicePluginNanoleaf::executeAction(DeviceActionInfo *info) if (device->deviceClassId() == lightPanelsDeviceClassId) { Nanoleaf *nanoleaf = m_nanoleafConnections.value(device->id()); + if (!nanoleaf) { + return info->finish(Device::DeviceErrorHardwareFailure); + } + if (action.actionTypeId() == lightPanelsPowerActionTypeId) { bool power = action.param(lightPanelsPowerActionPowerParamTypeId).value().toBool(); QUuid requestId = nanoleaf->setPower(power); @@ -218,13 +200,17 @@ void DevicePluginNanoleaf::executeAction(DeviceActionInfo *info) } else if (action.actionTypeId() == lightPanelsColorActionTypeId) { QColor color(action.param(lightPanelsColorActionColorParamTypeId).value().toString()); - QUuid requestId = nanoleaf->setHue(color); + QUuid requestId = nanoleaf->setColor(color); connect(info, &DeviceActionInfo::aborted,[requestId, this](){m_asyncActions.remove(requestId);}); m_asyncActions.insert(requestId, info); } else if (action.actionTypeId() == lightPanelsColorTemperatureActionTypeId) { int colorTemperature = action.param(lightPanelsColorTemperatureActionColorTemperatureParamTypeId).value().toInt(); - QUuid requestId = nanoleaf->setColorTemperature(colorTemperature); + QUuid requestId = nanoleaf->setMired(colorTemperature); + connect(info, &DeviceActionInfo::aborted,[requestId, this](){m_asyncActions.remove(requestId);}); + m_asyncActions.insert(requestId, info); + } else if (action.actionTypeId() == lightPanelsAlertActionTypeId) { + QUuid requestId = nanoleaf->identify(); connect(info, &DeviceActionInfo::aborted,[requestId, this](){m_asyncActions.remove(requestId);}); m_asyncActions.insert(requestId, info); } @@ -234,10 +220,10 @@ void DevicePluginNanoleaf::executeAction(DeviceActionInfo *info) void DevicePluginNanoleaf::browseDevice(BrowseResult *result) { Device *device = result->device(); - Nanoleaf *nanoleaf = m_nanoleafConnections.value(device->id()); - nanoleaf->getEffects(); - m_asyncBrowseResults.insert(nanoleaf, result); - connect(result, &BrowseResult::aborted, this, [nanoleaf, this]{m_asyncBrowseResults.remove(nanoleaf);}); + Nanoleaf *nanoleaf = m_nanoleafConnections.value(device->id()); + nanoleaf->getEffects(); + m_asyncBrowseResults.insert(nanoleaf, result); + connect(result, &BrowseResult::aborted, this, [nanoleaf, this]{m_asyncBrowseResults.remove(nanoleaf);}); } void DevicePluginNanoleaf::browserItem(BrowserItemResult *result) @@ -260,15 +246,24 @@ void DevicePluginNanoleaf::executeBrowserItem(BrowserActionInfo *info) connect(info, &BrowserActionInfo::aborted, this, [requestId, this]{m_asyncBrowserItem.remove(requestId);}); } - -void DevicePluginNanoleaf::getDeviceStates(Nanoleaf *nanoleaf) +Nanoleaf *DevicePluginNanoleaf::createNanoleafConnection(const QHostAddress &address, int port) { - nanoleaf->getPower(); - nanoleaf->getHue(); - nanoleaf->getColorMode(); - nanoleaf->getBrightness(); - nanoleaf->getColorTemperature(); - nanoleaf->getSelectedEffect(); + Nanoleaf *nanoleaf = new Nanoleaf(hardwareManager()->networkManager(), address, port, this); + connect(nanoleaf, &Nanoleaf::authTokenRecieved, this, &DevicePluginNanoleaf::onAuthTokenReceived); + connect(nanoleaf, &Nanoleaf::authenticationStatusChanged, this, &DevicePluginNanoleaf::onAuthenticationStatusChanged); + connect(nanoleaf, &Nanoleaf::requestExecuted, this, &DevicePluginNanoleaf::onRequestExecuted); + connect(nanoleaf, &Nanoleaf::connectionChanged, this, &DevicePluginNanoleaf::onConnectionChanged); + + connect(nanoleaf, &Nanoleaf::controllerInfoReceived, this, &DevicePluginNanoleaf::onControllerInfoReceived); + connect(nanoleaf, &Nanoleaf::brightnessReceived, this, &DevicePluginNanoleaf::onBrightnessReceived); + connect(nanoleaf, &Nanoleaf::powerReceived, this, &DevicePluginNanoleaf::onPowerReceived); + connect(nanoleaf, &Nanoleaf::colorModeReceived, this, &DevicePluginNanoleaf::onColorModeReceived); + connect(nanoleaf, &Nanoleaf::saturationReceived, this, &DevicePluginNanoleaf::onSaturationReceived); + connect(nanoleaf, &Nanoleaf::hueReceived, this, &DevicePluginNanoleaf::onHueReceived); + connect(nanoleaf, &Nanoleaf::colorTemperatureReceived, this, &DevicePluginNanoleaf::onColorTemperatureReceived); + connect(nanoleaf, &Nanoleaf::effectListReceived, this, &DevicePluginNanoleaf::onEffectListReceived); + connect(nanoleaf, &Nanoleaf::selectedEffectReceived, this, &DevicePluginNanoleaf::onSelectedEffectReceived); + return nanoleaf; } void DevicePluginNanoleaf::onAuthTokenReceived(const QString &token) @@ -326,12 +321,23 @@ void DevicePluginNanoleaf::onConnectionChanged(bool connected) device->setStateValue(lightPanelsConnectedStateTypeId, connected); } +void DevicePluginNanoleaf::onControllerInfoReceived(const Nanoleaf::ControllerInfo &controllerInfo) +{ + Nanoleaf *nanoleaf = static_cast(sender()); + Device *device = myDevices().findById(m_nanoleafConnections.key(nanoleaf)); + if (!device) + return; + qCDebug(dcNanoleaf()) << "Controller Info received" << controllerInfo.name << controllerInfo.firmwareVersion; + device->setParamValue(lightPanelsDeviceFirmwareVersionParamTypeId, controllerInfo.firmwareVersion); +} + void DevicePluginNanoleaf::onPowerReceived(bool power) { Nanoleaf *nanoleaf = static_cast(sender()); Device *device = myDevices().findById(m_nanoleafConnections.key(nanoleaf)); if (!device) return; + qCDebug(dcNanoleaf()) << "Power received" << power; device->setStateValue(lightPanelsPowerStateTypeId, power); } @@ -341,6 +347,7 @@ void DevicePluginNanoleaf::onBrightnessReceived(int percentage) Device *device = myDevices().findById(m_nanoleafConnections.key(nanoleaf)); if (!device) return; + qCDebug(dcNanoleaf()) << "Brightness received" << percentage; device->setStateValue(lightPanelsBrightnessStateTypeId, percentage); } @@ -350,6 +357,7 @@ void DevicePluginNanoleaf::onColorModeReceived(const QString &colorMode) Device *device = myDevices().findById(m_nanoleafConnections.key(nanoleaf)); if (!device) return; + qCDebug(dcNanoleaf()) << "Color mode received" << colorMode; device->setStateValue(lightPanelsColorModeStateTypeId, colorMode); } @@ -359,6 +367,7 @@ void DevicePluginNanoleaf::onHueReceived(int hue) Device *device = myDevices().findById(m_nanoleafConnections.key(nanoleaf)); if (!device) return; + qCDebug(dcNanoleaf()) << "Hue received" << hue; m_hues.insert(device->id(), hue); nanoleaf->getSaturation(); } @@ -372,7 +381,6 @@ void DevicePluginNanoleaf::onSaturationReceived(int saturation) qCDebug(dcNanoleaf()) << "Saturation received" << saturation; QColor color; color.setHsv(m_hues.value(device->id()), saturation, 100); - //TODO get hue device->setStateValue(lightPanelsColorStateTypeId, color); } @@ -394,17 +402,20 @@ void DevicePluginNanoleaf::onEffectListReceived(const QStringList &effects) item.setDisplayName(effect); item.setDisabled(false); result->addItem(item); - } + } result->finish(Device::DeviceErrorNoError); } } -void DevicePluginNanoleaf::onColorTemperatureReceived(int mired) +void DevicePluginNanoleaf::onColorTemperatureReceived(int kelvin) { Nanoleaf *nanoleaf = static_cast(sender()); Device *device = myDevices().findById(m_nanoleafConnections.key(nanoleaf)); if (!device) return; + qCDebug(dcNanoleaf()) << "Color temperature received" << kelvin; + //NOTE: this is just a rough estimation of the mired value + int mired = static_cast(kelvin/11.12); //FIXME device->setStateValue(lightPanelsColorTemperatureStateTypeId, mired); } @@ -414,6 +425,7 @@ void DevicePluginNanoleaf::onSelectedEffectReceived(const QString &effect) Device *device = myDevices().findById(m_nanoleafConnections.key(nanoleaf)); if (!device) return; - device->setStateValue(lightPanelsEffectNameStateTypeId, effect); + qCDebug(dcNanoleaf()) << "Selected effect received" << effect; + device->setStateValue(lightPanelsEffectNameStateTypeId, QString(effect).remove('"').remove('*')); } diff --git a/nanoleaf/devicepluginnanoleaf.h b/nanoleaf/devicepluginnanoleaf.h index 219e9fd7..677e6d04 100644 --- a/nanoleaf/devicepluginnanoleaf.h +++ b/nanoleaf/devicepluginnanoleaf.h @@ -1,24 +1,30 @@ -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * - * * - * Copyright (C) 2020 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 +* This project may also contain libraries licensed under the open source software license GNU GPL v.3. +* 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 DEVICEPLUGINNANOLEAF_H #define DEVICEPLUGINNANOLEAF_H @@ -68,7 +74,7 @@ private: QHash m_asyncBrowserItem; QHash m_hues; - void getDeviceStates(Nanoleaf *nanoleaf); + Nanoleaf *createNanoleafConnection(const QHostAddress &address, int port); public slots: void onAuthTokenReceived(const QString &token); @@ -76,13 +82,14 @@ public slots: void onRequestExecuted(QUuid requestId, bool success); void onConnectionChanged(bool connected); + void onControllerInfoReceived(const Nanoleaf::ControllerInfo &controllerInfo); void onPowerReceived(bool power); void onBrightnessReceived(int percentage); void onColorModeReceived(const QString &colorMode); void onHueReceived(int hue); void onSaturationReceived(int percentage); void onEffectListReceived(const QStringList &effects); - void onColorTemperatureReceived(int mired); + void onColorTemperatureReceived(int kelvin); void onSelectedEffectReceived(const QString &effect); }; diff --git a/nanoleaf/devicepluginnanoleaf.json b/nanoleaf/devicepluginnanoleaf.json index 5069321a..d069bb5d 100644 --- a/nanoleaf/devicepluginnanoleaf.json +++ b/nanoleaf/devicepluginnanoleaf.json @@ -12,7 +12,7 @@ "id": "d44ee383-9fa5-4751-babd-1129ac20896a", "name": "lightPanels", "displayName": "Light panels", - "interfaces": ["colorlight", "colortemperaturelight", "connectable"], + "interfaces": ["colorlight", "colortemperaturelight", "alert", "connectable"], "createMethods": ["discovery"], "setupMethod": "pushButton", "browsable": true, @@ -52,6 +52,13 @@ "readOnly": true } ], + "actionTypes": [ + { + "id": "47a6a1a1-fb90-4f24-be8c-b4dba0aaaa84", + "name": "alert", + "displayName": "Alert" + } + ], "stateTypes": [ { "id": "a3102107-a825-4ec8-a9ec-b2c2a9fb5c89", @@ -100,7 +107,7 @@ "name": "brightness", "displayName": "Brightness", "displayNameEvent": "Brightness changed", - "displayNameAction": "Set brigtness", + "displayNameAction": "Set brightness", "type": "int", "unit": "Percentage", "defaultValue": 0, @@ -113,17 +120,14 @@ "name": "colorMode", "displayName": "Color mode", "displayNameEvent": "Color mode changed", - "displayNameAction": "Set color", "type": "QString", - "defaultValue": "Color temperature", - "writable": true + "defaultValue": "Color temperature" }, { "id": "57f9831e-1b98-41c1-a21c-6073ff327237", "name": "effectName", "displayName": "Effect name", "displayNameEvent": "Effect name changed", - "displayNameAction": "Set color", "type": "QString", "defaultValue": "-" } diff --git a/nanoleaf/nanoleaf.cpp b/nanoleaf/nanoleaf.cpp index 1db80348..f6f9dc54 100644 --- a/nanoleaf/nanoleaf.cpp +++ b/nanoleaf/nanoleaf.cpp @@ -1,24 +1,30 @@ -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * - * * - * Copyright (C) 2020 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 +* This project may also contain libraries licensed under the open source software license GNU GPL v.3. +* 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 "nanoleaf.h" #include "extern-plugininfo.h" @@ -103,7 +109,6 @@ void Nanoleaf::addUser() qDebug(dcNanoleaf()) << "Recieved invalide JSON object"; return; } - m_authToken = data.toVariant().toMap().value("auth_token").toString(); emit authTokenRecieved(m_authToken); @@ -151,13 +156,64 @@ void Nanoleaf::getControllerInfo() reply->deleteLater(); int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); - if (status != 204 || reply->error() != QNetworkReply::NoError) { + if (status < 200 || status > 204 || reply->error() != QNetworkReply::NoError) { qCWarning(dcNanoleaf()) << "Request error:" << status << reply->errorString(); emit authenticationStatusChanged(false); return; } + QJsonParseError error; + QJsonDocument data = QJsonDocument::fromJson(reply->readAll(), &error); + if (error.error != QJsonParseError::NoError) { + qDebug(dcNanoleaf()) << "Recieved invalide JSON object"; + return; + } emit connectionChanged(true); emit authenticationStatusChanged(true); + + QVariantMap map = data.toVariant().toMap(); + ControllerInfo info; + info.name = map.value("name").toString(); + info.serialNumber = map.value("serialNo").toString(); + info.model = map.value("model").toString(); + info.manufacturer = map.value("manufacturer").toString(); + info.firmwareVersion = map.value("firmwareVersion").toString(); + emit controllerInfoReceived(info); + + if (map.contains("state")) { + QVariantMap state = map.value("state").toMap(); + if (state.contains("on")) { + emit powerReceived(state["on"].toMap().value("value").toBool()); + } + if (state.contains("brightness")) { + emit brightnessReceived(state["brightness"].toMap().value("value").toInt()); + } + if (state.contains("hue")) { + emit hueReceived(state["hue"].toMap().value("value").toInt()); + } + if (state.contains("sat")) { + emit saturationReceived(state["sat"].toMap().value("value").toInt()); + } + if (state.contains("ct")) { + emit colorTemperatureReceived(state["ct"].toMap().value("value").toInt()); + } + if (state.contains("colorMode")) { + emit colorModeReceived(state["colorMode"].toString()); + } + } + if (map.contains("effects")) { + QVariantMap effects = map.value("effects").toMap(); + emit selectedEffectReceived(effects.value("select").toString()); + } + + if (map.contains("panelLayout")) { + //QVariantMap panelLayout = map.value("panelLayout").toMap(); + //emit panelLayoutReceived(); + } + + if (map.contains("rhythm")) { + //QVariantMap rhythm = map.value("rhythm").toMap(); + //emit rhythmModulReceived(rhythm.value("select").toString()); + } }); } @@ -314,9 +370,9 @@ void Nanoleaf::getColorTemperature() qDebug(dcNanoleaf()) << "Recieved invalide JSON object"; return; } - int mired = data.toVariant().toMap().value("value").toInt(); + int kelvin = data.toVariant().toMap().value("value").toInt(); emit connectionChanged(true); - emit colorTemperatureReceived(mired); + emit colorTemperatureReceived(kelvin); }); } @@ -352,6 +408,76 @@ void Nanoleaf::getColorMode() }); } +void Nanoleaf::registerForEvents() +{ + QUrl url; + url.setHost(m_address.toString()); + url.setPort(m_port); + url.setScheme("http"); + url.setPath("/api/v1/"+m_authToken+"/events"); + QUrlQuery query; + query.addQueryItem("id", "1,2,3,4"); + url.setQuery(query); + QNetworkRequest request; + request.setUrl(url); + QNetworkReply *reply = m_networkManager->get(request); + + connect(reply, &QNetworkReply::readyRead, this, [reply, this] { + QJsonParseError error; + QJsonDocument data = QJsonDocument::fromJson(reply->readAll(), &error); + if (error.error != QJsonParseError::NoError) { + qDebug(dcNanoleaf()) << "Recieved invalide JSON object"; + return; + } + qCDebug(dcNanoleaf()) << "On event stream" << data.toJson(); + }); + connect(reply, &QNetworkReply::finished, this, [reply, this] { + reply->deleteLater(); + int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + + if (status != 200 || reply->error() != QNetworkReply::NoError) { + qCWarning(dcNanoleaf()) << "Request error:" << status << reply->errorString(); + emit connectionChanged(false); + return; + } + QJsonParseError error; + QJsonDocument data = QJsonDocument::fromJson(reply->readAll(), &error); + if (error.error != QJsonParseError::NoError) { + qDebug(dcNanoleaf()) << "Recieved invalide JSON object"; + return; + } + qCDebug(dcNanoleaf()) << "Event received" << data.toJson(); + QVariantList events = data.toVariant().toList(); + + foreach (QVariant variant, events) { + QVariantMap event = variant.toMap(); + switch (event["attr"].toInt()) { + case 1: //ON + emit powerReceived(event["value"].toBool()); + break; + case 2: //Brightness + emit brightnessReceived(event["value"].toInt()); + break; + case 3: //Hue + emit hueReceived(event["value"].toInt()); + break; + case 4: //Saturation + emit saturationReceived(event["value"].toInt()); + break; + case 5: //Color Temperature + emit colorTemperatureReceived(event["value"].toInt()); + break; + case 6: //colorMode + emit colorModeReceived(event["value"].toString()); + break; + default: + qCWarning(dcNanoleaf()) << "Unrecognised Event received"; + } + + } + }); +} + QUuid Nanoleaf::setPower(bool power) { QUuid requestId = QUuid::createUuid(); @@ -387,10 +513,74 @@ QUuid Nanoleaf::setPower(bool power) return requestId; } -QUuid Nanoleaf::setHue(QColor color) +QUuid Nanoleaf::setColor(QColor color) { - Q_UNUSED(color); QUuid requestId = QUuid::createUuid(); + QUrl url; + url.setHost(m_address.toString()); + url.setPort(m_port); + url.setScheme("http"); + url.setPath(QString("/api/v1/%1/state").arg(m_authToken)); + + QVariantMap map; + QVariantMap hue; + hue["value"] = color.hue(); + map.insert("hue", hue); + QVariantMap sat; + sat["value"] = color.saturation(); + map.insert("sat", sat); + QJsonDocument body = QJsonDocument::fromVariant(map); + + QNetworkRequest request; + request.setUrl(url); + request.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/json"); + QNetworkReply *reply = m_networkManager->put(request, body.toJson()); + qDebug(dcNanoleaf()) << "Sending request" << request.url(); + connect(reply, &QNetworkReply::finished, this, [requestId, reply, this] { + reply->deleteLater(); + int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + + if (status != 204 || reply->error() != QNetworkReply::NoError) { + emit requestExecuted(requestId, false); + qCWarning(dcNanoleaf()) << "Request error:" << status << reply->errorString(); + return; + } + emit requestExecuted(requestId, true); + }); + return requestId; +} + +QUuid Nanoleaf::setHue(int hue) +{ + QUuid requestId = QUuid::createUuid(); + QUrl url; + url.setHost(m_address.toString()); + url.setPort(m_port); + url.setScheme("http"); + url.setPath(QString("/api/v1/%1/state").arg(m_authToken)); + + QVariantMap map; + QVariantMap hueMap; + hueMap["value"] = hue; + map.insert("hue", hueMap); + QJsonDocument body = QJsonDocument::fromVariant(map); + + QNetworkRequest request; + request.setUrl(url); + request.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/json"); + QNetworkReply *reply = m_networkManager->put(request, body.toJson()); + qDebug(dcNanoleaf()) << "Sending request" << request.url(); + connect(reply, &QNetworkReply::finished, this, [requestId, reply, this] { + reply->deleteLater(); + int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + + if (status != 204 || reply->error() != QNetworkReply::NoError) { + emit requestExecuted(requestId, false); + qCWarning(dcNanoleaf()) << "Request error:" << status << reply->errorString(); + return; + } + emit requestExecuted(requestId, true); + }); return requestId; } @@ -430,7 +620,6 @@ QUuid Nanoleaf::setBrightness(int percentage) QUuid Nanoleaf::setSaturation(int percentage) { - Q_UNUSED(percentage); QUuid requestId = QUuid::createUuid(); QUrl url; url.setHost(m_address.toString()); @@ -463,10 +652,45 @@ QUuid Nanoleaf::setSaturation(int percentage) return requestId; } -QUuid Nanoleaf::setColorTemperature(int mired) +QUuid Nanoleaf::setMired(int mired) +{ + //NOTE: this is just a rough estimation + int kelvin = static_cast(mired * 11.12); + QUuid requestId = setKelvin(kelvin); + return requestId; +} + +QUuid Nanoleaf::setKelvin(int kelvin) { - Q_UNUSED(mired); QUuid requestId = QUuid::createUuid(); + QUrl url; + url.setHost(m_address.toString()); + url.setPort(m_port); + url.setScheme("http"); + url.setPath(QString("/api/v1/%1/state").arg(m_authToken)); + + QVariantMap map; + QVariantMap value; + value["value"] = kelvin; + map.insert("ct", value); + QJsonDocument body = QJsonDocument::fromVariant(map); + + QNetworkRequest request; + request.setUrl(url); + request.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/json"); + QNetworkReply *reply = m_networkManager->put(request, body.toJson()); + qDebug(dcNanoleaf()) << "Sending request" << request.url(); + connect(reply, &QNetworkReply::finished, this, [requestId, reply, this] { + reply->deleteLater(); + int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + + if (status != 204 || reply->error() != QNetworkReply::NoError) { + emit requestExecuted(requestId, false); + qCWarning(dcNanoleaf()) << "Request error:" << status << reply->errorString(); + return; + } + emit requestExecuted(requestId, true); + }); return requestId; } @@ -564,5 +788,40 @@ QUuid Nanoleaf::setEffect(const QString &effect) return requestId; } +QUuid Nanoleaf::identify() +{ + QUuid requestId = QUuid::createUuid(); + QUrl url; + url.setHost(m_address.toString()); + url.setPort(m_port); + url.setScheme("http"); + url.setPath(QString("/api/v1/%1/identify").arg(m_authToken)); + + QNetworkRequest request; + request.setUrl(url); + request.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/json"); + QNetworkReply *reply = m_networkManager->put(request, ""); + qDebug(dcNanoleaf()) << "Sending request" << request.url(); + connect(reply, &QNetworkReply::finished, this, [requestId, reply, this] { + reply->deleteLater(); + int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + + // Check HTTP status code + if (status < 200 || status > 204 || reply->error() != QNetworkReply::NoError) { + if (reply->error() == QNetworkReply::HostNotFoundError) { + emit connectionChanged(false); + } + if (status >= 400 && status <= 410) { + emit authenticationStatusChanged(false); + } + emit requestExecuted(requestId, false); + qCWarning(dcNanoleaf()) << "Request error:" << status << reply->errorString(); + return; + } + emit requestExecuted(requestId, true); + }); + return requestId; +} + diff --git a/nanoleaf/nanoleaf.h b/nanoleaf/nanoleaf.h index ff12b40f..a47930fa 100644 --- a/nanoleaf/nanoleaf.h +++ b/nanoleaf/nanoleaf.h @@ -1,24 +1,30 @@ -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * - * * - * Copyright (C) 2020 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 +* This project may also contain libraries licensed under the open source software license GNU GPL v.3. +* 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 NANOLEAF_H #define NANOLEAF_H @@ -36,6 +42,22 @@ class Nanoleaf : public QObject { Q_OBJECT public: + struct ControllerInfo { + QString name; + QString serialNumber; + QString manufacturer; + QString firmwareVersion; + QString model; + }; + + enum GestureID { + SingleTap = 0, + DoubleTap = 1, + SwipeUp = 2, + SwipeDown = 3, + SwipeLeft = 4, + SwipeRight = 5 + }; explicit Nanoleaf(NetworkAccessManager *networkManager, const QHostAddress &address, int port = 16021, QObject *parent = nullptr); void setIpAddress(const QHostAddress &address); @@ -62,27 +84,21 @@ public: void getColorTemperature(); void getColorMode(); + void registerForEvents(); QUuid setPower(bool power); - QUuid setHue(QColor color); + QUuid setColor(QColor color); + QUuid setHue(int hue); QUuid setBrightness(int percentage); QUuid setSaturation(int percentage); - QUuid setColorTemperature(int mired); - + QUuid setMired(int mired); + QUuid setKelvin(int kelvin); //EFFECTS void getEffects(); void getSelectedEffect(); QUuid setEffect(const QString &effect); - //PANEL LAYOUT - - //IDENTIFY - - - //EXTERNAL CONTROL - - - //RHYTHM + QUuid identify(); private: NetworkAccessManager *m_networkManager = nullptr; @@ -95,6 +111,7 @@ signals: void authenticationStatusChanged(bool authenticated); void requestExecuted(QUuid requestId, bool success); + void controllerInfoReceived(const ControllerInfo &controllerInfo); void authTokenRecieved(const QString &token); void powerReceived(bool power); void brightnessReceived(int percentage); @@ -102,8 +119,11 @@ signals: void hueReceived(int hue); void saturationReceived(int percentage); void effectListReceived(const QStringList &effects); - void colorTemperatureReceived(int mired); + void colorTemperatureReceived(int kelvin); void selectedEffectReceived(const QString &effect); + + //Only supported by Canvas + void touchEventReceived(GestureID gesture); }; #endif // NANOLEAF_H From a642f25988a5d7663d7be6f0f85e9dbb58a7de44 Mon Sep 17 00:00:00 2001 From: Boernsman Date: Fri, 3 Jan 2020 15:26:34 +0100 Subject: [PATCH 4/6] fixed QColor to saturation conversion --- nanoleaf/devicepluginnanoleaf.cpp | 55 +++++---- nanoleaf/devicepluginnanoleaf.h | 4 +- nanoleaf/nanoleaf.cpp | 183 +++++++++++++++--------------- nanoleaf/nanoleaf.h | 9 +- 4 files changed, 136 insertions(+), 115 deletions(-) diff --git a/nanoleaf/devicepluginnanoleaf.cpp b/nanoleaf/devicepluginnanoleaf.cpp index 4882aa1c..bd68ef87 100644 --- a/nanoleaf/devicepluginnanoleaf.cpp +++ b/nanoleaf/devicepluginnanoleaf.cpp @@ -230,11 +230,6 @@ void DevicePluginNanoleaf::browserItem(BrowserItemResult *result) { Q_UNUSED(result) qCDebug(dcNanoleaf()) << "BrowserItem called"; - //Device *device = result->device(); - //Nanoleaf *nanoleaf = m_nanoleafConnections.value(device->id()); - //nanoleaf->setEffect(info.); - //result-> - //m_asyncBrowseItems.insert(nanoleaf, result);*/ } void DevicePluginNanoleaf::executeBrowserItem(BrowserActionInfo *info) @@ -327,7 +322,7 @@ void DevicePluginNanoleaf::onControllerInfoReceived(const Nanoleaf::ControllerIn Device *device = myDevices().findById(m_nanoleafConnections.key(nanoleaf)); if (!device) return; - qCDebug(dcNanoleaf()) << "Controller Info received" << controllerInfo.name << controllerInfo.firmwareVersion; + //qCDebug(dcNanoleaf()) << "Controller Info received" << controllerInfo.name << controllerInfo.firmwareVersion; device->setParamValue(lightPanelsDeviceFirmwareVersionParamTypeId, controllerInfo.firmwareVersion); } @@ -337,7 +332,7 @@ void DevicePluginNanoleaf::onPowerReceived(bool power) Device *device = myDevices().findById(m_nanoleafConnections.key(nanoleaf)); if (!device) return; - qCDebug(dcNanoleaf()) << "Power received" << power; + //qCDebug(dcNanoleaf()) << "Power received" << power; device->setStateValue(lightPanelsPowerStateTypeId, power); } @@ -347,18 +342,37 @@ void DevicePluginNanoleaf::onBrightnessReceived(int percentage) Device *device = myDevices().findById(m_nanoleafConnections.key(nanoleaf)); if (!device) return; - qCDebug(dcNanoleaf()) << "Brightness received" << percentage; + //qCDebug(dcNanoleaf()) << "Brightness received" << percentage; device->setStateValue(lightPanelsBrightnessStateTypeId, percentage); } -void DevicePluginNanoleaf::onColorModeReceived(const QString &colorMode) +void DevicePluginNanoleaf::onColorReceived(QColor color) { Nanoleaf *nanoleaf = static_cast(sender()); Device *device = myDevices().findById(m_nanoleafConnections.key(nanoleaf)); if (!device) return; - qCDebug(dcNanoleaf()) << "Color mode received" << colorMode; - device->setStateValue(lightPanelsColorModeStateTypeId, colorMode); + //qCDebug(dcNanoleaf()) << "Color received" << color.toRgb(); + device->setStateValue(lightPanelsColorStateTypeId, color); +} + +void DevicePluginNanoleaf::onColorModeReceived(Nanoleaf::ColorMode colorMode) +{ + Nanoleaf *nanoleaf = static_cast(sender()); + Device *device = myDevices().findById(m_nanoleafConnections.key(nanoleaf)); + if (!device) + return; + switch (colorMode) { + case Nanoleaf::ColorMode::ColorTemperatureMode: + device->setStateValue(lightPanelsColorModeStateTypeId, tr("Color temperature")); + break; + case Nanoleaf::ColorMode::HueSaturationMode: + device->setStateValue(lightPanelsColorModeStateTypeId, tr("Hue/Saturation")); + break; + case Nanoleaf::ColorMode::EffectMode: + device->setStateValue(lightPanelsColorModeStateTypeId, tr("Effect")); + break; + } } void DevicePluginNanoleaf::onHueReceived(int hue) @@ -367,9 +381,10 @@ void DevicePluginNanoleaf::onHueReceived(int hue) Device *device = myDevices().findById(m_nanoleafConnections.key(nanoleaf)); if (!device) return; - qCDebug(dcNanoleaf()) << "Hue received" << hue; - m_hues.insert(device->id(), hue); - nanoleaf->getSaturation(); + //qCDebug(dcNanoleaf()) << "Hue received" << hue; + QColor color = QColor(device->stateValue(lightPanelsColorStateTypeId).toString()); + color.setHsv(hue, color.saturation(), color.value()); + device->setStateValue(lightPanelsColorStateTypeId, color); } void DevicePluginNanoleaf::onSaturationReceived(int saturation) @@ -378,9 +393,9 @@ void DevicePluginNanoleaf::onSaturationReceived(int saturation) Device *device = myDevices().findById(m_nanoleafConnections.key(nanoleaf)); if (!device) return; - qCDebug(dcNanoleaf()) << "Saturation received" << saturation; - QColor color; - color.setHsv(m_hues.value(device->id()), saturation, 100); + //qCDebug(dcNanoleaf()) << "Saturation received" << saturation; + QColor color = QColor(device->stateValue(lightPanelsColorStateTypeId).toString()); + color.setHsv(color.hue(), saturation, color.value()); device->setStateValue(lightPanelsColorStateTypeId, color); } @@ -390,7 +405,7 @@ void DevicePluginNanoleaf::onEffectListReceived(const QStringList &effects) Device *device = myDevices().findById(m_nanoleafConnections.key(nanoleaf)); if (!device) return; - qCDebug(dcNanoleaf()) << "Effect list received" << effects; + //qCDebug(dcNanoleaf()) << "Effect list received" << effects; if (m_asyncBrowseResults.contains(nanoleaf)) { BrowseResult *result = m_asyncBrowseResults.take(nanoleaf); @@ -413,7 +428,7 @@ void DevicePluginNanoleaf::onColorTemperatureReceived(int kelvin) Device *device = myDevices().findById(m_nanoleafConnections.key(nanoleaf)); if (!device) return; - qCDebug(dcNanoleaf()) << "Color temperature received" << kelvin; + //qCDebug(dcNanoleaf()) << "Color temperature received" << kelvin; //NOTE: this is just a rough estimation of the mired value int mired = static_cast(kelvin/11.12); //FIXME device->setStateValue(lightPanelsColorTemperatureStateTypeId, mired); @@ -425,7 +440,7 @@ void DevicePluginNanoleaf::onSelectedEffectReceived(const QString &effect) Device *device = myDevices().findById(m_nanoleafConnections.key(nanoleaf)); if (!device) return; - qCDebug(dcNanoleaf()) << "Selected effect received" << effect; + //qCDebug(dcNanoleaf()) << "Selected effect received" << effect; device->setStateValue(lightPanelsEffectNameStateTypeId, QString(effect).remove('"').remove('*')); } diff --git a/nanoleaf/devicepluginnanoleaf.h b/nanoleaf/devicepluginnanoleaf.h index 677e6d04..9f464119 100644 --- a/nanoleaf/devicepluginnanoleaf.h +++ b/nanoleaf/devicepluginnanoleaf.h @@ -72,7 +72,6 @@ private: QHash m_asyncBrowseResults; QHash m_asyncBrowserItem; - QHash m_hues; Nanoleaf *createNanoleafConnection(const QHostAddress &address, int port); @@ -85,7 +84,8 @@ public slots: void onControllerInfoReceived(const Nanoleaf::ControllerInfo &controllerInfo); void onPowerReceived(bool power); void onBrightnessReceived(int percentage); - void onColorModeReceived(const QString &colorMode); + void onColorReceived(QColor color); + void onColorModeReceived(Nanoleaf::ColorMode colorMode); void onHueReceived(int hue); void onSaturationReceived(int percentage); void onEffectListReceived(const QStringList &effects); diff --git a/nanoleaf/nanoleaf.cpp b/nanoleaf/nanoleaf.cpp index f6f9dc54..3cc4648e 100644 --- a/nanoleaf/nanoleaf.cpp +++ b/nanoleaf/nanoleaf.cpp @@ -85,13 +85,13 @@ void Nanoleaf::addUser() request.setUrl(url); request.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/json"); QNetworkReply *reply = m_networkManager->post(request, ""); - qDebug(dcNanoleaf()) << "Sending request" << request.url(); + //qDebug(dcNanoleaf()) << "Sending request" << request.url(); connect(reply, &QNetworkReply::finished, this, [reply, this] { reply->deleteLater(); int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); // Check HTTP status code - if (status != 200 || reply->error() != QNetworkReply::NoError) { + if (status < 200 || status > 204 || reply->error() != QNetworkReply::NoError) { if (reply->error() == QNetworkReply::HostNotFoundError) { emit connectionChanged(false); } @@ -127,12 +127,12 @@ void Nanoleaf::deleteUser() QNetworkRequest request; request.setUrl(url); QNetworkReply *reply = m_networkManager->deleteResource(request); - qDebug(dcNanoleaf()) << "Sending request" << request.url(); + //qDebug(dcNanoleaf()) << "Sending request" << request.url(); connect(reply, &QNetworkReply::finished, this, [reply, this] { reply->deleteLater(); int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); - if (status != 204 || reply->error() != QNetworkReply::NoError) { + if (status < 200 || status > 204 || reply->error() != QNetworkReply::NoError) { qCWarning(dcNanoleaf()) << "Request error:" << status << reply->errorString(); return; } @@ -151,7 +151,7 @@ void Nanoleaf::getControllerInfo() QNetworkRequest request; request.setUrl(url); QNetworkReply *reply = m_networkManager->get(request); - qDebug(dcNanoleaf()) << "Sending request" << request.url(); + //qDebug(dcNanoleaf()) << "Sending request" << request.url(); connect(reply, &QNetworkReply::finished, this, [reply, this] { reply->deleteLater(); int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); @@ -184,20 +184,31 @@ void Nanoleaf::getControllerInfo() if (state.contains("on")) { emit powerReceived(state["on"].toMap().value("value").toBool()); } - if (state.contains("brightness")) { - emit brightnessReceived(state["brightness"].toMap().value("value").toInt()); - } - if (state.contains("hue")) { - emit hueReceived(state["hue"].toMap().value("value").toInt()); - } - if (state.contains("sat")) { - emit saturationReceived(state["sat"].toMap().value("value").toInt()); + if (state.contains("hue") && state.contains("sat") && state.contains("brightness")) { + int brightness = state["brightness"].toMap().value("value").toInt(); + emit brightnessReceived(brightness); + int hue = state["hue"].toMap().value("value").toInt(); + emit hueReceived(hue); + int sat = state["sat"].toMap().value("value").toInt(); + emit saturationReceived(sat); + QColor color; + color.setHsv(hue, sat, brightness); + emit colorReceived(color); } if (state.contains("ct")) { emit colorTemperatureReceived(state["ct"].toMap().value("value").toInt()); } if (state.contains("colorMode")) { - emit colorModeReceived(state["colorMode"].toString()); + QString colorModeString = state["colorMode"].toString(); + if (colorModeString == "effect") { + emit colorModeReceived(ColorMode::EffectMode); + } else if (colorModeString == "hs") { + emit colorModeReceived(ColorMode::HueSaturationMode); + } else if (colorModeString == "ct") { + emit colorModeReceived(ColorMode::ColorTemperatureMode); + } else { + qCWarning(dcNanoleaf()) << "Unrecognized color mode"; + } } } if (map.contains("effects")) { @@ -228,12 +239,12 @@ void Nanoleaf::getPower() QNetworkRequest request; request.setUrl(url); QNetworkReply *reply = m_networkManager->get(request); - qDebug(dcNanoleaf()) << "Sending request" << request.url(); + //qDebug(dcNanoleaf()) << "Sending request" << request.url(); connect(reply, &QNetworkReply::finished, this, [reply, this] { reply->deleteLater(); int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); - if (status != 200 || reply->error() != QNetworkReply::NoError) { + if (status < 200 || status > 204 || reply->error() != QNetworkReply::NoError) { qCWarning(dcNanoleaf()) << "Request error:" << status << reply->errorString(); emit connectionChanged(false); return; @@ -261,12 +272,12 @@ void Nanoleaf::getHue() QNetworkRequest request; request.setUrl(url); QNetworkReply *reply = m_networkManager->get(request); - qDebug(dcNanoleaf()) << "Sending request" << request.url(); + //qDebug(dcNanoleaf()) << "Sending request" << request.url(); connect(reply, &QNetworkReply::finished, this, [reply, this] { reply->deleteLater(); int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); - if (status != 200 || reply->error() != QNetworkReply::NoError) { + if (status < 200 || status > 204 || reply->error() != QNetworkReply::NoError) { qCWarning(dcNanoleaf()) << "Request error:" << status << reply->errorString(); return; } @@ -297,7 +308,7 @@ void Nanoleaf::getBrightness() reply->deleteLater(); int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); - if (status != 200 || reply->error() != QNetworkReply::NoError) { + if (status < 200 || status > 204 || reply->error() != QNetworkReply::NoError) { qCWarning(dcNanoleaf()) << "Request error:" << status << reply->errorString(); return; } @@ -328,7 +339,7 @@ void Nanoleaf::getSaturation() reply->deleteLater(); int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); - if (status != 200 || reply->error() != QNetworkReply::NoError) { + if (status < 200 || status > 204 || reply->error() != QNetworkReply::NoError) { qCWarning(dcNanoleaf()) << "Request error:" << status << reply->errorString(); return; } @@ -359,7 +370,7 @@ void Nanoleaf::getColorTemperature() reply->deleteLater(); int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); - if (status != 200 || reply->error() != QNetworkReply::NoError) { + if (status < 200 || status > 204 || reply->error() != QNetworkReply::NoError) { qCWarning(dcNanoleaf()) << "Request error:" << status << reply->errorString(); emit connectionChanged(false); return; @@ -391,7 +402,7 @@ void Nanoleaf::getColorMode() reply->deleteLater(); int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); - if (status != 200 || reply->error() != QNetworkReply::NoError) { + if (status < 200 || status > 204 || reply->error() != QNetworkReply::NoError) { qCWarning(dcNanoleaf()) << "Request error:" << status << reply->errorString(); emit connectionChanged(false); return; @@ -402,9 +413,17 @@ void Nanoleaf::getColorMode() qDebug(dcNanoleaf()) << "Recieved invalide JSON object"; return; } - QString colorMode = data.toVariant().toMap().value("value").toString(); emit connectionChanged(true); - emit colorModeReceived(colorMode); + QString colorModeString = data.toVariant().toMap().value("value").toString(); + if (colorModeString == "effect") { + emit colorModeReceived(ColorMode::EffectMode); + } else if (colorModeString == "hs") { + emit colorModeReceived(ColorMode::HueSaturationMode); + } else if (colorModeString == "ct") { + emit colorModeReceived(ColorMode::ColorTemperatureMode); + } else { + qCWarning(dcNanoleaf()) << "Unrecognized color mode"; + } }); } @@ -435,7 +454,7 @@ void Nanoleaf::registerForEvents() reply->deleteLater(); int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); - if (status != 200 || reply->error() != QNetworkReply::NoError) { + if (status < 200 || status > 204 || reply->error() != QNetworkReply::NoError) { qCWarning(dcNanoleaf()) << "Request error:" << status << reply->errorString(); emit connectionChanged(false); return; @@ -450,29 +469,39 @@ void Nanoleaf::registerForEvents() QVariantList events = data.toVariant().toList(); foreach (QVariant variant, events) { - QVariantMap event = variant.toMap(); - switch (event["attr"].toInt()) { - case 1: //ON - emit powerReceived(event["value"].toBool()); - break; - case 2: //Brightness - emit brightnessReceived(event["value"].toInt()); - break; - case 3: //Hue - emit hueReceived(event["value"].toInt()); - break; - case 4: //Saturation - emit saturationReceived(event["value"].toInt()); - break; - case 5: //Color Temperature - emit colorTemperatureReceived(event["value"].toInt()); - break; - case 6: //colorMode - emit colorModeReceived(event["value"].toString()); - break; - default: - qCWarning(dcNanoleaf()) << "Unrecognised Event received"; - } + QVariantMap event = variant.toMap(); + switch (event["attr"].toInt()) { + case 1: //ON + emit powerReceived(event["value"].toBool()); + break; + case 2: //Brightness + emit brightnessReceived(event["value"].toInt()); + break; + case 3: //Hue + emit hueReceived(event["value"].toInt()); + break; + case 4: //Saturation + emit saturationReceived(event["value"].toInt()); + break; + case 5: //Color Temperature + emit colorTemperatureReceived(event["value"].toInt()); + break; + case 6: { //colorMode + QString colorModeString = event["value"].toString(); + if (colorModeString == "effect") { + emit colorModeReceived(ColorMode::EffectMode); + } else if (colorModeString == "hs") { + emit colorModeReceived(ColorMode::HueSaturationMode); + } else if (colorModeString == "ct") { + emit colorModeReceived(ColorMode::ColorTemperatureMode); + } else { + qCWarning(dcNanoleaf()) << "Unrecognized color mode"; + } + break; + } + default: + qCWarning(dcNanoleaf()) << "Unrecognised Event received"; + } } }); @@ -497,12 +526,12 @@ QUuid Nanoleaf::setPower(bool power) request.setUrl(url); request.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/json"); QNetworkReply *reply = m_networkManager->put(request, body.toJson()); - qDebug(dcNanoleaf()) << "Sending request" << request.url(); + //qDebug(dcNanoleaf()) << "Sending request" << request.url(); connect(reply, &QNetworkReply::finished, this, [requestId, reply, this] { reply->deleteLater(); int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); - if (status != 204 || reply->error() != QNetworkReply::NoError) { + if (status < 200 || status > 204 || reply->error() != QNetworkReply::NoError) { emit requestExecuted(requestId, false); qCWarning(dcNanoleaf()) << "Request error:" << status << reply->errorString(); return; @@ -515,38 +544,8 @@ QUuid Nanoleaf::setPower(bool power) QUuid Nanoleaf::setColor(QColor color) { - QUuid requestId = QUuid::createUuid(); - QUrl url; - url.setHost(m_address.toString()); - url.setPort(m_port); - url.setScheme("http"); - url.setPath(QString("/api/v1/%1/state").arg(m_authToken)); - - QVariantMap map; - QVariantMap hue; - hue["value"] = color.hue(); - map.insert("hue", hue); - QVariantMap sat; - sat["value"] = color.saturation(); - map.insert("sat", sat); - QJsonDocument body = QJsonDocument::fromVariant(map); - - QNetworkRequest request; - request.setUrl(url); - request.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/json"); - QNetworkReply *reply = m_networkManager->put(request, body.toJson()); - qDebug(dcNanoleaf()) << "Sending request" << request.url(); - connect(reply, &QNetworkReply::finished, this, [requestId, reply, this] { - reply->deleteLater(); - int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); - - if (status != 204 || reply->error() != QNetworkReply::NoError) { - emit requestExecuted(requestId, false); - qCWarning(dcNanoleaf()) << "Request error:" << status << reply->errorString(); - return; - } - emit requestExecuted(requestId, true); - }); + QUuid requestId = setHue(color.hue()); + setSaturation(static_cast(color.saturation()/2.55)); //QColor saturation is 0-255 return requestId; } @@ -569,12 +568,12 @@ QUuid Nanoleaf::setHue(int hue) request.setUrl(url); request.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/json"); QNetworkReply *reply = m_networkManager->put(request, body.toJson()); - qDebug(dcNanoleaf()) << "Sending request" << request.url(); + //qDebug(dcNanoleaf()) << "Sending request" << request.url(); connect(reply, &QNetworkReply::finished, this, [requestId, reply, this] { reply->deleteLater(); int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); - if (status != 204 || reply->error() != QNetworkReply::NoError) { + if (status < 200 || status > 204 || reply->error() != QNetworkReply::NoError) { emit requestExecuted(requestId, false); qCWarning(dcNanoleaf()) << "Request error:" << status << reply->errorString(); return; @@ -603,12 +602,12 @@ QUuid Nanoleaf::setBrightness(int percentage) request.setUrl(url); request.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/json"); QNetworkReply *reply = m_networkManager->put(request, body.toJson()); - qDebug(dcNanoleaf()) << "Sending request" << request.url(); + //qDebug(dcNanoleaf()) << "Sending request" << request.url(); connect(reply, &QNetworkReply::finished, this, [requestId, reply, this] { reply->deleteLater(); int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); - if (status != 204 || reply->error() != QNetworkReply::NoError) { + if (status < 200 || status > 204 || reply->error() != QNetworkReply::NoError) { emit requestExecuted(requestId, false); qCWarning(dcNanoleaf()) << "Request error:" << status << reply->errorString(); return; @@ -637,12 +636,12 @@ QUuid Nanoleaf::setSaturation(int percentage) request.setUrl(url); request.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/json"); QNetworkReply *reply = m_networkManager->put(request, body.toJson()); - qDebug(dcNanoleaf()) << "Sending request" << request.url(); + qDebug(dcNanoleaf()) << "Sending request" << request.url() << body.toJson(); connect(reply, &QNetworkReply::finished, this, [requestId, reply, this] { reply->deleteLater(); int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); - if (status != 204 || reply->error() != QNetworkReply::NoError) { + if (status < 200 || status > 204 || reply->error() != QNetworkReply::NoError) { emit requestExecuted(requestId, false); qCWarning(dcNanoleaf()) << "Request error:" << status << reply->errorString(); return; @@ -684,7 +683,7 @@ QUuid Nanoleaf::setKelvin(int kelvin) reply->deleteLater(); int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); - if (status != 204 || reply->error() != QNetworkReply::NoError) { + if (status < 200 || status > 204 || reply->error() != QNetworkReply::NoError) { emit requestExecuted(requestId, false); qCWarning(dcNanoleaf()) << "Request error:" << status << reply->errorString(); return; @@ -709,7 +708,7 @@ void Nanoleaf::getEffects() reply->deleteLater(); int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); - if (status != 200 || reply->error() != QNetworkReply::NoError) { + if (status < 200 || status > 204 || reply->error() != QNetworkReply::NoError) { qCWarning(dcNanoleaf()) << "Request error:" << status << reply->errorString(); emit connectionChanged(false); return; @@ -745,7 +744,7 @@ void Nanoleaf::getSelectedEffect() reply->deleteLater(); int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); - if (status != 200 || reply->error() != QNetworkReply::NoError) { + if (status < 200 || status > 204 || reply->error() != QNetworkReply::NoError) { qCWarning(dcNanoleaf()) << "Request error:" << status << reply->errorString(); emit connectionChanged(false); return; @@ -778,7 +777,7 @@ QUuid Nanoleaf::setEffect(const QString &effect) reply->deleteLater(); int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); - if (status != 204 || reply->error() != QNetworkReply::NoError) { + if (status < 200 || status > 204 || reply->error() != QNetworkReply::NoError) { emit requestExecuted(requestId, false); qCWarning(dcNanoleaf()) << "Request error:" << status << reply->errorString(); return; diff --git a/nanoleaf/nanoleaf.h b/nanoleaf/nanoleaf.h index a47930fa..cb9b2289 100644 --- a/nanoleaf/nanoleaf.h +++ b/nanoleaf/nanoleaf.h @@ -50,6 +50,12 @@ public: QString model; }; + enum ColorMode { + EffectMode, + HueSaturationMode, + ColorTemperatureMode + }; + enum GestureID { SingleTap = 0, DoubleTap = 1, @@ -115,10 +121,11 @@ signals: void authTokenRecieved(const QString &token); void powerReceived(bool power); void brightnessReceived(int percentage); - void colorModeReceived(const QString &colorMode); + void colorModeReceived(ColorMode colorMode); void hueReceived(int hue); void saturationReceived(int percentage); void effectListReceived(const QStringList &effects); + void colorReceived(QColor color); void colorTemperatureReceived(int kelvin); void selectedEffectReceived(const QString &effect); From 38e0791ffad499a8eda5a738760110b616bd146b Mon Sep 17 00:00:00 2001 From: Boernsman Date: Fri, 3 Jan 2020 15:43:11 +0100 Subject: [PATCH 5/6] fixed color temperture conversion --- nanoleaf/devicepluginnanoleaf.cpp | 6 ++++-- nanoleaf/nanoleaf.cpp | 8 ++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/nanoleaf/devicepluginnanoleaf.cpp b/nanoleaf/devicepluginnanoleaf.cpp index bd68ef87..727d859c 100644 --- a/nanoleaf/devicepluginnanoleaf.cpp +++ b/nanoleaf/devicepluginnanoleaf.cpp @@ -428,9 +428,11 @@ void DevicePluginNanoleaf::onColorTemperatureReceived(int kelvin) Device *device = myDevices().findById(m_nanoleafConnections.key(nanoleaf)); if (!device) return; - //qCDebug(dcNanoleaf()) << "Color temperature received" << kelvin; + qCDebug(dcNanoleaf()) << "Color temperature received, Kelvin:" << kelvin << "Mired:" << (653-(kelvin/13)); //NOTE: this is just a rough estimation of the mired value - int mired = static_cast(kelvin/11.12); //FIXME + //Mired: 153 - 500 + //Kelvin: 1200-6500 + int mired = static_cast(653-(kelvin/13)); device->setStateValue(lightPanelsColorTemperatureStateTypeId, mired); } diff --git a/nanoleaf/nanoleaf.cpp b/nanoleaf/nanoleaf.cpp index 3cc4648e..28f1d149 100644 --- a/nanoleaf/nanoleaf.cpp +++ b/nanoleaf/nanoleaf.cpp @@ -636,7 +636,7 @@ QUuid Nanoleaf::setSaturation(int percentage) request.setUrl(url); request.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/json"); QNetworkReply *reply = m_networkManager->put(request, body.toJson()); - qDebug(dcNanoleaf()) << "Sending request" << request.url() << body.toJson(); + //qDebug(dcNanoleaf()) << "Sending request" << request.url() << body.toJson(); connect(reply, &QNetworkReply::finished, this, [requestId, reply, this] { reply->deleteLater(); int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); @@ -653,8 +653,8 @@ QUuid Nanoleaf::setSaturation(int percentage) QUuid Nanoleaf::setMired(int mired) { - //NOTE: this is just a rough estimation - int kelvin = static_cast(mired * 11.12); + //NOTE: this is just a rough conversion between mired and kelvin + int kelvin = static_cast((653-mired) * 13); QUuid requestId = setKelvin(kelvin); return requestId; } @@ -678,7 +678,7 @@ QUuid Nanoleaf::setKelvin(int kelvin) request.setUrl(url); request.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/json"); QNetworkReply *reply = m_networkManager->put(request, body.toJson()); - qDebug(dcNanoleaf()) << "Sending request" << request.url(); + qDebug(dcNanoleaf()) << "Sending request" << request.url() << body.toJson(); connect(reply, &QNetworkReply::finished, this, [requestId, reply, this] { reply->deleteLater(); int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); From 9d2e0e3ffcf6c211313ec7ec8c7a8df0aa25729d Mon Sep 17 00:00:00 2001 From: Boernsman Date: Fri, 3 Jan 2020 15:45:28 +0100 Subject: [PATCH 6/6] added translation files --- ...867ec-1594-498d-8182-fbab1fe17489-en_US.ts | 222 ++++++++++++++++++ 1 file changed, 222 insertions(+) create mode 100644 nanoleaf/translations/360867ec-1594-498d-8182-fbab1fe17489-en_US.ts diff --git a/nanoleaf/translations/360867ec-1594-498d-8182-fbab1fe17489-en_US.ts b/nanoleaf/translations/360867ec-1594-498d-8182-fbab1fe17489-en_US.ts new file mode 100644 index 00000000..41b09df7 --- /dev/null +++ b/nanoleaf/translations/360867ec-1594-498d-8182-fbab1fe17489-en_US.ts @@ -0,0 +1,222 @@ + + + + + DevicePluginNanoleaf + + + On the Nanoleaf controller, hold the on-off button for 5-7 seconds until the LED starts flashing. + + + + + Color temperature + + + + + Hue/Saturation + + + + + Effect + + + + + Nanoleaf + + + Address + The name of the ParamType (DeviceClass: lightPanels, Type: device, ID: {ff57079f-d5ab-4511-8a5c-0726e7b82af6}) + + + + + Alert + The name of the ActionType ({47a6a1a1-fb90-4f24-be8c-b4dba0aaaa84}) of DeviceClass lightPanels + + + + + + + Brightness + The name of the ParamType (DeviceClass: lightPanels, ActionType: brightness, ID: {4e5d6460-d42e-4b7c-a8f3-6e953451c1ef}) +---------- +The name of the ParamType (DeviceClass: lightPanels, EventType: brightness, ID: {4e5d6460-d42e-4b7c-a8f3-6e953451c1ef}) +---------- +The name of the StateType ({4e5d6460-d42e-4b7c-a8f3-6e953451c1ef}) of DeviceClass lightPanels + + + + + Brightness changed + The name of the EventType ({4e5d6460-d42e-4b7c-a8f3-6e953451c1ef}) of DeviceClass lightPanels + + + + + + + Color + The name of the ParamType (DeviceClass: lightPanels, ActionType: color, ID: {d4a52cdc-93b2-44fc-a36c-ae65f1d98f2e}) +---------- +The name of the ParamType (DeviceClass: lightPanels, EventType: color, ID: {d4a52cdc-93b2-44fc-a36c-ae65f1d98f2e}) +---------- +The name of the StateType ({d4a52cdc-93b2-44fc-a36c-ae65f1d98f2e}) of DeviceClass lightPanels + + + + + Color changed + The name of the EventType ({d4a52cdc-93b2-44fc-a36c-ae65f1d98f2e}) of DeviceClass lightPanels + + + + + + Color mode + The name of the ParamType (DeviceClass: lightPanels, EventType: colorMode, ID: {bdd2ea1e-9ef9-4967-9678-2c601b826199}) +---------- +The name of the StateType ({bdd2ea1e-9ef9-4967-9678-2c601b826199}) of DeviceClass lightPanels + + + + + Color mode changed + The name of the EventType ({bdd2ea1e-9ef9-4967-9678-2c601b826199}) of DeviceClass lightPanels + + + + + + + Color temperature + The name of the ParamType (DeviceClass: lightPanels, ActionType: colorTemperature, ID: {41248127-844b-40be-87e6-38aee48b6687}) +---------- +The name of the ParamType (DeviceClass: lightPanels, EventType: colorTemperature, ID: {41248127-844b-40be-87e6-38aee48b6687}) +---------- +The name of the StateType ({41248127-844b-40be-87e6-38aee48b6687}) of DeviceClass lightPanels + + + + + Color temperature changed + The name of the EventType ({41248127-844b-40be-87e6-38aee48b6687}) of DeviceClass lightPanels + + + + + + Effect name + The name of the ParamType (DeviceClass: lightPanels, EventType: effectName, ID: {57f9831e-1b98-41c1-a21c-6073ff327237}) +---------- +The name of the StateType ({57f9831e-1b98-41c1-a21c-6073ff327237}) of DeviceClass lightPanels + + + + + Effect name changed + The name of the EventType ({57f9831e-1b98-41c1-a21c-6073ff327237}) of DeviceClass lightPanels + + + + + Firmware version + The name of the ParamType (DeviceClass: lightPanels, Type: device, ID: {1b85eebe-3b1a-49a9-bddb-2175d6599b95}) + + + + + Light panels + The name of the DeviceClass ({d44ee383-9fa5-4751-babd-1129ac20896a}) + + + + + Model + The name of the ParamType (DeviceClass: lightPanels, Type: device, ID: {353d3c71-0ad2-40d5-99f6-cc305e2073f1}) + + + + + + Nanoleaf + The name of the vendor ({3d7fdaa6-7896-419b-8be3-c90c42bcac7f}) +---------- +The name of the plugin Nanoleaf ({360867ec-1594-498d-8182-fbab1fe17489}) + + + + + Port + The name of the ParamType (DeviceClass: lightPanels, Type: device, ID: {ba4fd45b-990d-480a-859d-fff7ffba3ba4}) + + + + + + + Power + The name of the ParamType (DeviceClass: lightPanels, ActionType: power, ID: {44bee9ec-513d-4834-991a-ee9ae69d9f2a}) +---------- +The name of the ParamType (DeviceClass: lightPanels, EventType: power, ID: {44bee9ec-513d-4834-991a-ee9ae69d9f2a}) +---------- +The name of the StateType ({44bee9ec-513d-4834-991a-ee9ae69d9f2a}) of DeviceClass lightPanels + + + + + Power changed + The name of the EventType ({44bee9ec-513d-4834-991a-ee9ae69d9f2a}) of DeviceClass lightPanels + + + + + + Reachable + The name of the ParamType (DeviceClass: lightPanels, EventType: connected, ID: {a3102107-a825-4ec8-a9ec-b2c2a9fb5c89}) +---------- +The name of the StateType ({a3102107-a825-4ec8-a9ec-b2c2a9fb5c89}) of DeviceClass lightPanels + + + + + Reachable changed + The name of the EventType ({a3102107-a825-4ec8-a9ec-b2c2a9fb5c89}) of DeviceClass lightPanels + + + + + Serial number + The name of the ParamType (DeviceClass: lightPanels, Type: device, ID: {18be4a5f-e2c2-4070-bc3e-ea9fe64f2276}) + + + + + Set brightness + The name of the ActionType ({4e5d6460-d42e-4b7c-a8f3-6e953451c1ef}) of DeviceClass lightPanels + + + + + Set color + The name of the ActionType ({d4a52cdc-93b2-44fc-a36c-ae65f1d98f2e}) of DeviceClass lightPanels + + + + + Set color temperature + The name of the ActionType ({41248127-844b-40be-87e6-38aee48b6687}) of DeviceClass lightPanels + + + + + Set power + The name of the ActionType ({44bee9ec-513d-4834-991a-ee9ae69d9f2a}) of DeviceClass lightPanels + + + +