Merge pull request #552 from Lord-Grey/master

Support additional Nanoleaf devices
This commit is contained in:
Rick164 2019-04-25 23:43:21 +02:00 committed by GitHub
commit 4db756b239
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 670 additions and 271 deletions

View File

@ -348,7 +348,7 @@
"wiz_cc_morethanone" : "Du hast mehr als 1 Profil, bitte wähle das zu kalibrierende Profil", "wiz_cc_morethanone" : "Du hast mehr als 1 Profil, bitte wähle das zu kalibrierende Profil",
"wiz_cc_btn_stop" : "Stoppe Video", "wiz_cc_btn_stop" : "Stoppe Video",
"wiz_cc_summary" : "Im folgenden eine Zusammenfassung deiner Einstellungen. Während du ein Video abspielst, kannst du hier weiter ausprobieren. Wenn du fertig bist, klicke auf speichern.", "wiz_cc_summary" : "Im folgenden eine Zusammenfassung deiner Einstellungen. Während du ein Video abspielst, kannst du hier weiter ausprobieren. Wenn du fertig bist, klicke auf speichern.",
"edt_dev_auth_key_title" : "Aurora API Schlüssel", "edt_dev_auth_key_title" : "Authentisierungstoken",
"edt_dev_enum_subtract_minimum" : "Subtrahiere minimum", "edt_dev_enum_subtract_minimum" : "Subtrahiere minimum",
"edt_dev_enum_sub_min_cool_adjust" : "Minimale Anpassung: cool", "edt_dev_enum_sub_min_cool_adjust" : "Minimale Anpassung: cool",
"edt_dev_enum_sub_min_warm_adjust" : "Minimale Anpassung: warm", "edt_dev_enum_sub_min_warm_adjust" : "Minimale Anpassung: warm",

View File

@ -348,7 +348,7 @@
"wiz_cc_morethanone" : "You have more than one profile, please choose the profile you want to calibrate.", "wiz_cc_morethanone" : "You have more than one profile, please choose the profile you want to calibrate.",
"wiz_cc_btn_stop" : "Stop video", "wiz_cc_btn_stop" : "Stop video",
"wiz_cc_summary" : "A conclusion of your settings. During video playback, you could change or test values again. If you are done, click on save.", "wiz_cc_summary" : "A conclusion of your settings. During video playback, you could change or test values again. If you are done, click on save.",
"edt_dev_auth_key_title" : "Aurora API Key", "edt_dev_auth_key_title" : "Authentication Token",
"edt_dev_enum_subtract_minimum" : "Substract minimum", "edt_dev_enum_subtract_minimum" : "Substract minimum",
"edt_dev_enum_sub_min_cool_adjust" : "Subtract cool white", "edt_dev_enum_sub_min_cool_adjust" : "Subtract cool white",
"edt_dev_enum_sub_min_warm_adjust" : "Subtract warm white", "edt_dev_enum_sub_min_warm_adjust" : "Subtract warm white",

View File

@ -473,7 +473,7 @@ $(document).ready(function() {
devRPiSPI = ['apa102', 'apa104', 'ws2801', 'lpd6803', 'lpd8806', 'p9813', 'sk6812spi', 'sk6822spi', 'ws2812spi']; devRPiSPI = ['apa102', 'apa104', 'ws2801', 'lpd6803', 'lpd8806', 'p9813', 'sk6812spi', 'sk6822spi', 'ws2812spi'];
devRPiPWM = ['ws281x']; devRPiPWM = ['ws281x'];
devRPiGPIO = ['piblaster']; devRPiGPIO = ['piblaster'];
devNET = ['atmoorb', 'fadecandy', 'philipshue', 'aurora', 'tinkerforge', 'tpm2net', 'udpe131', 'udpartnet', 'udph801', 'udpraw']; devNET = ['atmoorb', 'fadecandy', 'philipshue', 'nanoleaf', 'tinkerforge', 'tpm2net', 'udpe131', 'udpartnet', 'udph801', 'udpraw'];
devUSB = ['adalight', 'dmx', 'atmo', 'hyperionusbasp', 'lightpack', 'multilightpack', 'paintpack', 'rawhid', 'sedu', 'tpm2', 'karate']; devUSB = ['adalight', 'dmx', 'atmo', 'hyperionusbasp', 'lightpack', 'multilightpack', 'paintpack', 'rawhid', 'sedu', 'tpm2', 'karate'];
var optArr = [[]]; var optArr = [[]];

View File

@ -87,6 +87,7 @@ target_link_libraries(leddevice
${CMAKE_THREAD_LIBS_INIT} ${CMAKE_THREAD_LIBS_INIT}
Qt5::Network Qt5::Network
Qt5::SerialPort Qt5::SerialPort
ssdp
) )
if(ENABLE_TINKERFORGE) if(ENABLE_TINKERFORGE)

View File

@ -52,6 +52,12 @@ void LedDevice::setEnable(bool enable)
{ {
switchOff(); switchOff();
} }
else {
if ( !_enabled && enable)
{
switchOn();
}
}
_enabled = enable; _enabled = enable;
} }

View File

@ -32,6 +32,6 @@
<file alias="schema-apa104">schemas/schema-apa104.json</file> <file alias="schema-apa104">schemas/schema-apa104.json</file>
<file alias="schema-ws281x">schemas/schema-ws281x.json</file> <file alias="schema-ws281x">schemas/schema-ws281x.json</file>
<file alias="schema-karate">schemas/schema-karate.json</file> <file alias="schema-karate">schemas/schema-karate.json</file>
<file alias="schema-aurora">schemas/schema-aurora.json</file> <file alias="schema-nanoleaf">schemas/schema-nanoleaf.json</file>
</qresource> </qresource>
</RCC> </RCC>

View File

@ -1,196 +0,0 @@
// Local-Hyperion includes
#include "LedDeviceAurora.h"
#include <netdb.h>
#include <assert.h>
// qt includes
#include <QtCore/qmath.h>
#include <QEventLoop>
#include <QNetworkReply>
#define ll ss
struct addrinfo vints, *serverinfo, *pt;
//char udpbuffer[1024];
int sockfp;
int update_num;
LedDevice* LedDeviceAurora::construct(const QJsonObject &deviceConfig)
{
return new LedDeviceAurora(deviceConfig);
}
LedDeviceAurora::LedDeviceAurora(const QJsonObject &deviceConfig) {
init(deviceConfig);
}
bool LedDeviceAurora::init(const QJsonObject &deviceConfig) {
const QString hostname = deviceConfig["output"].toString();
const QString key = deviceConfig["key"].toString();
manager = new QNetworkAccessManager();
QString port;
// Read Panel count and panel Ids
QByteArray response = get(hostname, key, "panelLayout/layout");
QJsonParseError error;
QJsonDocument doc = QJsonDocument::fromJson(response, &error);
if (error.error != QJsonParseError::NoError)
{
throw std::runtime_error("No Layout found. Check hostname and auth key");
}
//Debug
QString strJson(doc.toJson(QJsonDocument::Compact));
std::cout << strJson.toUtf8().constData() << std::endl;
QJsonObject json = doc.object();
panelCount = json["numPanels"].toInt();
std::cout << panelCount << std::endl;
QJsonObject positionDataJson = doc.object()["positionData"].toObject();
QJsonArray positionData = json["positionData"].toArray();
// Loop over all children.
foreach (const QJsonValue & value, positionData) {
QJsonObject panelObj = value.toObject();
int panelId = panelObj["panelId"].toInt();
panelIds.push_back(panelId);
}
// Check if we found enough lights.
if (panelIds.size() != panelCount) {
throw std::runtime_error("Not enough lights found");
}else {
std::cout << "All panel Ids found: "<< panelIds.size() << std::endl;
}
// Set Aurora to UDP Mode
QByteArray modeResponse = changeMode(hostname, key, "effects");
QJsonDocument configDoc = QJsonDocument::fromJson(modeResponse, &error);
//Debug
//QString strConf(configDoc.toJson(QJsonDocument::Compact));
//std::cout << strConf.toUtf8().constData() << std::endl;
if (error.error != QJsonParseError::NoError)
{
throw std::runtime_error("Could not change mode");
}
// Get UDP port
port = QString::number(configDoc.object()["streamControlPort"].toInt());
std::cout << "hostname " << hostname.toStdString() << " port " << port.toStdString() << std::endl;
int rv;
memset(&vints, 0, sizeof vints);
vints.ai_family = AF_UNSPEC;
vints.ai_socktype = SOCK_DGRAM;
if ((rv = getaddrinfo(hostname.toUtf8().constData() , port.toUtf8().constData(), &vints, &serverinfo)) != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
assert(rv==0);
}
// loop through all the results and make a socket
for(pt = serverinfo; pt != NULL; pt = pt->ai_next) {
if ((sockfp = socket(pt->ai_family, pt->ai_socktype,
pt->ai_protocol)) == -1) {
perror("talker: socket");
continue;
}
break;
}
if (pt == NULL) {
fprintf(stderr, "talker: failed to create socket\n");
assert(pt!=NULL);
}
std::cout << "Started successfully ";
return true;
}
QString LedDeviceAurora::getUrl(QString host, QString token, QString route) {
return QString("http://%1:16021/api/v1/%2/%3").arg(host).arg(token).arg(route);
}
QByteArray LedDeviceAurora::get(QString host, QString token, QString route) {
QString url = getUrl(host, token, route);
// Perfrom request
QNetworkRequest request(url);
QNetworkReply* reply = manager->get(request);
// Connect requestFinished signal to quit slot of the loop.
QEventLoop loop;
loop.connect(reply, SIGNAL(finished()), SLOT(quit()));
// Go into the loop until the request is finished.
loop.exec();
// Read all data of the response.
QByteArray response = reply->readAll();
// Free space.
reply->deleteLater();
// Return response
return response;
}
QByteArray LedDeviceAurora::putJson(QString url, QString json) {
// Perfrom request
QNetworkRequest request(url);
QNetworkReply* reply = manager->put(request, json.toUtf8());
// Connect requestFinished signal to quit slot of the loop.
QEventLoop loop;
loop.connect(reply, SIGNAL(finished()), SLOT(quit()));
// Go into the loop until the request is finished.
loop.exec();
// Read all data of the response.
QByteArray response = reply->readAll();
// Free space.
reply->deleteLater();
// Return response
return response;
}
QByteArray LedDeviceAurora::changeMode(QString host, QString token, QString route) {
QString url = getUrl(host, token, route);
QString jsondata( "{\"write\" : {\"command\" : \"display\", \"animType\" : \"extControl\"}}"); //Enable UDP Mode
return putJson(url, jsondata);
}
LedDeviceAurora::~LedDeviceAurora()
{
delete manager;
}
int LedDeviceAurora::write(const std::vector<ColorRgb> & ledValues)
{
uint udpBufferSize = panelCount * 7 + 1;
char udpbuffer[udpBufferSize];
update_num++;
update_num &= 0xf;
int i=0;
int panelCounter = 0;
udpbuffer[i++] = panelCount;
for (const ColorRgb& color : ledValues)
{
if ((unsigned)i < udpBufferSize) {
udpbuffer[i++] = panelIds[panelCounter++ % panelCount];
udpbuffer[i++] = 1; // No of Frames
udpbuffer[i++] = color.red;
udpbuffer[i++] = color.green;
udpbuffer[i++] = color.blue;
udpbuffer[i++] = 0; // W not set manually
udpbuffer[i++] = 1; // currently fixed at value 1 which corresponds to 100ms
}
if((unsigned)panelCounter > panelCount) {
break;
}
//printf ("c.red %d sz c.red %d\n", color.red, sizeof(color.red));
}
sendto(sockfp, udpbuffer, i, 0, pt->ai_addr, pt->ai_addrlen);
return 0;
}
int LedDeviceAurora::switchOff()
{
return 0;
}

View File

@ -1,63 +0,0 @@
#pragma once
// Leddevice includes
#include <leddevice/LedDevice.h>
// Qt includes
#include <QObject>
#include <QString>
#include <QNetworkAccessManager>
#include <QTimer>
///
/// Implementation of the LedDevice that write the led-colors to an
/// ASCII-textfile('/home/pi/LedDevice.out')
///
class LedDeviceAurora : public LedDevice
{
public:
///
/// Constructs the test-device, which opens an output stream to the file
///
LedDeviceAurora(const QJsonObject &deviceConfig);
///
/// Destructor of this test-device
///
virtual ~LedDeviceAurora();
/// Switch the leds off
virtual int switchOff();
/// constructs leddevice
static LedDevice* construct(const QJsonObject &deviceConfig);
protected:
///
/// Writes the RGB-Color values to the leds.
///
/// @param[in] ledValues The RGB-color per led
///
/// @return Zero on success else negative
///
virtual int write(const std::vector<ColorRgb> & ledValues);
bool init(const QJsonObject &deviceConfig);
private:
/// The outputstream
// std::ofstream _ofs;
// QNetworkAccessManager object for sending requests.
QNetworkAccessManager* manager;
// the number of leds (needed when switching off)
size_t panelCount;
/// Array of the pannel ids.
std::vector<unsigned int> panelIds;
QByteArray get(QString host, QString token, QString route);
QByteArray putJson(QString url, QString json);
QByteArray changeMode(QString host, QString token, QString route);
///
/// @param route
///
/// @return the full URL of the request.
///
QString getUrl(QString host, QString token, QString route);
};

View File

@ -0,0 +1,500 @@
// Local-Hyperion includes
#include "LedDeviceNanoleaf.h"
// ssdp discover
#include <ssdp/SSDPDiscover.h>
// Qt includes
#include <QEventLoop>
#include <QNetworkReply>
// Controller configuration settings
const QString CONFIG_ADDRESS = "output";
const QString CONFIG_PORT = "port";
const QString CONFIG_AUTH_TOKEN ="token";
// Panel configuration settings
const QString PANEL_LAYOUT = "layout";
const QString PANEL_NUM = "numPanels";
const QString PANEL_ID = "panelId";
const QString PANEL_POSITIONDATA = "positionData";
const QString PANEL_SHAPE_TYPE = "shapeType";
const QString PANEL_ORIENTATION = "0";
const QString PANEL_POS_X = "x";
const QString PANEL_POS_Y = "y";
// List of State Information
const QString STATE_ON = "on";
const QString STATE_ONOFF_VALUE = "value";
const QString STATE_VALUE_TRUE = "true";
const QString STATE_VALUE_FALSE = "false";
//Device Data elements
const QString DEV_DATA_NAME = "name";
const QString DEV_DATA_MODEL = "model";
const QString DEV_DATA_MANUFACTURER = "manufacturer";
const QString DEV_DATA_FIRMWAREVERSION = "firmwareVersion";
//Nanoleaf Stream Control elements
const QString STREAM_CONTROL_IP = "streamControlIpAddr";
const QString STREAM_CONTROL_PORT = "streamControlPort";
const QString STREAM_CONTROL_PROTOCOL = "streamControlProtocol";
const quint16 STREAM_CONTROL_DEFAULT_PORT = 60222; //Fixed port for Canvas;
// Nanoleaf OpenAPI URLs
const QString API_DEFAULT_PORT = "16021";
const QString API_URL_FORMAT = "http://%1:%2/api/v1/%3/%4";
const QString API_ROOT = "";
const QString API_EXT_MODE_STRING_V1 = "{\"write\" : {\"command\" : \"display\", \"animType\" : \"extControl\"}}";
const QString API_EXT_MODE_STRING_V2 = "{\"write\" : {\"command\" : \"display\", \"animType\" : \"extControl\", \"extControlVersion\" : \"v2\"}}";
const QString API_STATE ="state";
const QString API_PANELLAYOUT = "panelLayout";
const QString API_EFFECT = "effects";
//Nanoleaf ssdp services
const QString SSDP_CANVAS = "nanoleaf:nl29";
const QString SSDP_LIGHTPANELS = "nanoleaf_aurora:light";
const int SSDP_TIMEOUT = 5000; // timout in ms
// Nanoleaf Panel Shapetypes
enum SHAPETYPES {
TRIANGLE,
RHYTM,
SQUARE,
CONTROL_SQUARE_PRIMARY,
CONTROL_SQUARE_PASSIVE,
POWER_SUPPLY,
};
// Nanoleaf external control versions
enum EXTCONTROLVERSIONS {
EXTCTRLVER_V1 = 1,
EXTCTRLVER_V2
};
LedDevice* LedDeviceNanoleaf::construct(const QJsonObject &deviceConfig)
{
return new LedDeviceNanoleaf(deviceConfig);
}
LedDeviceNanoleaf::LedDeviceNanoleaf(const QJsonObject &deviceConfig)
: ProviderUdp()
{
init(deviceConfig);
}
bool LedDeviceNanoleaf::init(const QJsonObject &deviceConfig) {
LedDevice::init(deviceConfig);
int configuredLedCount = this->getLedCount();
Debug(_log, "ActiveDevice : %s", QSTRING_CSTR( this->getActiveDevice() ));
Debug(_log, "LedCount : %d", configuredLedCount);
Debug(_log, "ColorOrder : %s", QSTRING_CSTR( this->getColorOrder() ));
Debug(_log, "LatchTime : %d", this->getLatchTime());
//Set hostname as per configuration and default port
_hostname = deviceConfig[ CONFIG_ADDRESS ].toString();
_api_port = API_DEFAULT_PORT;
_auth_token = deviceConfig[ CONFIG_AUTH_TOKEN ].toString();
//If host not configured then discover device
if ( _hostname.isEmpty() )
//Discover Nanoleaf device
if ( !discoverNanoleafDevice() ) {
throw std::runtime_error("No target IP defined nor Nanoleaf device discovered");
}
//Get Nanoleaf device details and configuration
_networkmanager = new QNetworkAccessManager();
// Read Panel count and panel Ids
QString url = getUrl(_hostname, _api_port, _auth_token, API_ROOT );
QJsonDocument doc = getJson( url );
QJsonObject jsonAllPanelInfo = doc.object();
QString deviceName = jsonAllPanelInfo[DEV_DATA_NAME].toString();
_deviceModel = jsonAllPanelInfo[DEV_DATA_MODEL].toString();
QString deviceManufacturer = jsonAllPanelInfo[DEV_DATA_MANUFACTURER].toString();
_deviceFirmwareVersion = jsonAllPanelInfo[DEV_DATA_FIRMWAREVERSION].toString();
Debug(_log, "Name : %s", QSTRING_CSTR( deviceName ));
Debug(_log, "Model : %s", QSTRING_CSTR( _deviceModel ));
Debug(_log, "Manufacturer : %s", QSTRING_CSTR( deviceManufacturer ));
Debug(_log, "FirmwareVersion: %s", QSTRING_CSTR( _deviceFirmwareVersion));
// Get panel details from /panelLayout/layout
QJsonObject jsonPanelLayout = jsonAllPanelInfo[API_PANELLAYOUT].toObject();
QJsonObject jsonLayout = jsonPanelLayout[PANEL_LAYOUT].toObject();
int panelNum = jsonLayout[PANEL_NUM].toInt();
QJsonArray positionData = jsonLayout[PANEL_POSITIONDATA].toArray();
std::map<int, std::map<int, int>> panelMap;
// Loop over all children.
foreach (const QJsonValue & value, positionData) {
QJsonObject panelObj = value.toObject();
int panelId = panelObj[PANEL_ID].toInt();
int panelX = panelObj[PANEL_POS_X].toInt();
int panelY = panelObj[PANEL_POS_Y].toInt();
int panelshapeType = panelObj[PANEL_SHAPE_TYPE].toInt();
//int panelOrientation = panelObj[PANEL_ORIENTATION].toInt();
//std::cout << "Panel [" << panelId << "]" << " (" << panelX << "," << panelY << ") - Type: [" << panelshapeType << "]" << std::endl;
// Skip Rhythm panels
if ( panelshapeType != RHYTM ) {
panelMap[panelY][panelX] = panelId;
} else {
Info(_log, "Rhythm panel skipped.");
}
}
// Sort panels top down, left right
for(auto posY = panelMap.crbegin(); posY != panelMap.crend(); ++posY) {
// posY.first is the first key
for(auto const &posX : posY->second) {
// posX.first is the second key, posX.second is the data
//std::cout << "panelMap[" << posY->first << "][" << posX.first << "]=" << posX.second << std::endl;
_panelIds.push_back(posX.second);
}
}
this->_panelLedCount = _panelIds.size();
Debug(_log, "PanelsNum : %d", panelNum);
Debug(_log, "PanelLedCount : %d", _panelLedCount);
// Check. if enough panelds were found.
if (_panelLedCount < configuredLedCount) {
throw std::runtime_error ( (QString ("Not enough panels [%1] for configured LEDs [%2] found!").arg(_panelLedCount).arg(configuredLedCount)).toStdString() );
} else {
if ( _panelLedCount > this->getLedCount() ) {
Warning(_log, "Nanoleaf: More panels [%d] than configured LEDs [%d].", _panelLedCount, configuredLedCount );
}
}
switchOn();
// Set Nanoleaf to External Control (UDP) mode
Debug(_log, "Set Nanoleaf to External Control (UDP) streaming mode");
QJsonDocument responseDoc = changeToExternalControlMode();
// Set UDP streaming port
_port = STREAM_CONTROL_DEFAULT_PORT;
// Resolve port for Ligh Panels
QJsonObject jsonStreamControllInfo = responseDoc.object();
if ( ! jsonStreamControllInfo.isEmpty() ) {
_port = jsonStreamControllInfo[STREAM_CONTROL_PORT].toInt();
}
_defaultHost = _hostname;
ProviderUdp::init(deviceConfig);
Debug(_log, "Started successfully" );
return true;
}
bool LedDeviceNanoleaf::discoverNanoleafDevice() {
bool isDeviceFound (false);
// device searching by ssdp
QString address;
SSDPDiscover discover;
// Discover Canvas device
address = discover.getFirstService(STY_WEBSERVER, SSDP_CANVAS, SSDP_TIMEOUT);
//No Canvas device not found
if ( address.isEmpty() ) {
// Discover Light Panels (Aurora) device
address = discover.getFirstService(STY_WEBSERVER, SSDP_LIGHTPANELS, SSDP_TIMEOUT);
if ( address.isEmpty() ) {
Warning(_log, "No Nanoleaf device discovered");
}
}
// Canvas or Light Panels found
if ( ! address.isEmpty() ) {
Info(_log, "Nanoleaf device discovered at [%s]", QSTRING_CSTR( address ));
isDeviceFound = true;
QStringList addressparts = address.split(":", QString::SkipEmptyParts);
_hostname = addressparts[0];
_api_port = addressparts[1];
}
return isDeviceFound;
}
QJsonDocument LedDeviceNanoleaf::changeToExternalControlMode() {
QString url = getUrl(_hostname, _api_port, _auth_token, API_EFFECT );
QJsonDocument jsonDoc;
// If device model is Light Panels (Aurora)
if ( _deviceModel == "NL22") {
_extControlVersion = EXTCTRLVER_V1;
//Enable UDP Mode v1
jsonDoc = putJson(url, API_EXT_MODE_STRING_V1);
}
else {
_extControlVersion = EXTCTRLVER_V2;
//Enable UDP Mode v2
jsonDoc= putJson(url, API_EXT_MODE_STRING_V2);
}
return jsonDoc;
}
QString LedDeviceNanoleaf::getUrl(QString host, QString port, QString auth_token, QString endpoint) const {
return API_URL_FORMAT.arg(host).arg(port).arg(auth_token).arg(endpoint);
}
QJsonDocument LedDeviceNanoleaf::getJson(QString url) const {
Debug(_log, "GET: [%s]", QSTRING_CSTR( url ));
// Perfrom request
QNetworkRequest request(url);
QNetworkReply* reply = _networkmanager->get(request);
// Connect requestFinished signal to quit slot of the loop.
QEventLoop loop;
loop.connect(reply, SIGNAL(finished()), SLOT(quit()));
// Go into the loop until the request is finished.
loop.exec();
QJsonDocument jsonDoc;
if(reply->operation() == QNetworkAccessManager::GetOperation)
{
jsonDoc = handleReply( reply );
}
// Free space.
reply->deleteLater();
// Return response
return jsonDoc;
}
QJsonDocument LedDeviceNanoleaf::putJson(QString url, QString json) const {
Debug(_log, "PUT: [%s] [%s]", QSTRING_CSTR( url ), QSTRING_CSTR( json ) );
// Perfrom request
QNetworkRequest request(url);
QNetworkReply* reply = _networkmanager->put(request, json.toUtf8());
// Connect requestFinished signal to quit slot of the loop.
QEventLoop loop;
loop.connect(reply, SIGNAL(finished()), SLOT(quit()));
// Go into the loop until the request is finished.
loop.exec();
QJsonDocument jsonDoc;
if(reply->operation() == QNetworkAccessManager::PutOperation)
{
jsonDoc = handleReply( reply );
}
// Free space.
reply->deleteLater();
// Return response
return jsonDoc;
}
QJsonDocument LedDeviceNanoleaf::handleReply(QNetworkReply* const &reply ) const {
QJsonDocument jsonDoc;
int httpStatusCode = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute ).toInt();
if(reply->error() ==
QNetworkReply::NoError)
{
if ( httpStatusCode != 204 ){
QByteArray response = reply->readAll();
QJsonParseError error;
jsonDoc = QJsonDocument::fromJson(response, &error);
if (error.error != QJsonParseError::NoError)
{
Error (_log, "Got invalid response");
throw std::runtime_error("");
}
else {
//Debug
// QString strJson(jsonDoc.toJson(QJsonDocument::Compact));
// std::cout << strJson.toUtf8().constData() << std::endl;
}
}
}
else
{
QString errorReason;
if ( httpStatusCode > 0 ) {
QString httpReason = reply->attribute( QNetworkRequest::HttpReasonPhraseAttribute ).toString();
QString advise;
switch ( httpStatusCode ) {
case 400:
advise = "Check Request Body";
break;
case 401:
advise = "Check Authentication Token (API Key)";
break;
case 404:
advise = "Check Resource given";
break;
default:
break;
}
errorReason = QString ("%1:%2 [%3 %4] - %5").arg(_hostname).arg(_api_port).arg(httpStatusCode).arg(httpReason).arg(advise);
}
else {
errorReason = QString ("%1:%2 - %3").arg(_hostname).arg(_api_port).arg(reply->errorString());
}
Error (_log, "%s", QSTRING_CSTR( errorReason ));
throw std::runtime_error("Network Error");
}
// Return response
return jsonDoc;
}
LedDeviceNanoleaf::~LedDeviceNanoleaf()
{
delete _networkmanager;
}
int LedDeviceNanoleaf::write(const std::vector<ColorRgb> & ledValues)
{
int retVal = 0;
uint udpBufferSize;
//Light Panels
// nPanels 1B
// nFrames 1B
// panelID 1B
// <R> <G> <B> 3B
// <W> 1B
// tranitionTime 1B
//
//Canvas
//In order to support the much larger number of panels on Canvas, the size of the nPanels,
//panelId and tranitionTime fields have been been increased from 1B to 2B.
//The nFrames field has been dropped as it was set to 1 in v1 anyway
//
// nPanels 2B
// panelID 2B
// <R> <G> <B> 3B
// <W> 1B
// tranitionTime 2B
//udpBufferSize = _panelLedCount * 7 + 1; // Buffersize for LightPanels
udpBufferSize = _panelLedCount * 8 + 2;
uint8_t udpbuffer[udpBufferSize];
uchar lowByte; // lower byte
uchar highByte; // upper byte
uint i=0;
// Set number of panels
highByte = (uchar) (_panelLedCount >>8 );
lowByte = (uchar) (_panelLedCount & 0xFF);
if ( _extControlVersion == EXTCTRLVER_V2 ) {
udpbuffer[i++] = highByte;
}
udpbuffer[i++] = lowByte;
ColorRgb color;
for ( int panelCounter=0; panelCounter < _panelLedCount; panelCounter++ )
{
uint panelID = _panelIds[panelCounter];
highByte = (uchar) (panelID >>8 );
lowByte = (uchar) (panelID & 0xFF);
// Set panels configured
if( panelCounter < this->getLedCount() ) {
color = (ColorRgb) ledValues.at(panelCounter);
}
else
{
// Set panels not configed to black;
color = ColorRgb::BLACK;
//printf ("panelCounter [%d] >= panelLedCount [%d]\n", panelCounter, _panelLedCount );
}
// Set panelID
if ( _extControlVersion == EXTCTRLVER_V2 ) {
udpbuffer[i++] = highByte;
}
udpbuffer[i++] = lowByte;
// Set number of frames - V1 only
if ( _extControlVersion == EXTCTRLVER_V1 ) {
udpbuffer[i++] = 1; // No of Frames
}
// Set panel's color LEDs
udpbuffer[i++] = color.red;
udpbuffer[i++] = color.green;
udpbuffer[i++] = color.blue;
// Set white LED
udpbuffer[i++] = 0; // W not set manually
// Set transition time
unsigned char tranitionTime = 1; // currently fixed at value 1 which corresponds to 100ms
highByte = (uchar) (tranitionTime >>8 );
lowByte = (uchar) (tranitionTime & 0xFF);
if ( _extControlVersion == EXTCTRLVER_V2 ) {
udpbuffer[i++] = highByte;
}
udpbuffer[i++] = lowByte;
//std::cout << "[" << panelCounter << "]" << " Color: " << color << std::endl;
}
// printf ("udpBufferSize[%d], Bytes to send [%d]\n", udpBufferSize, i);
// for ( uint c= 0; c < udpBufferSize;c++ )
// {
// printf ("%x ", (uchar) udpbuffer[c]);
// }
// printf("\n");
retVal &= writeBytes( i , udpbuffer);
return retVal;
}
QString LedDeviceNanoleaf::getOnOffRequest (bool isOn ) const {
QString state = isOn ? STATE_VALUE_TRUE : STATE_VALUE_FALSE;
return QString( "{\"%1\":{\"%2\":%3}}" ).arg(STATE_ON).arg(STATE_ONOFF_VALUE).arg(state);
}
int LedDeviceNanoleaf::switchOn() {
Debug(_log, "switchOn()");
//Switch on Nanoleaf device
QString url = getUrl(_hostname, _api_port, _auth_token, API_STATE );
putJson(url, this->getOnOffRequest(true) );
return 0;
}
int LedDeviceNanoleaf::switchOff() {
Debug(_log, "switchOff()");
//Set all LEDs to Black
LedDevice::switchOff();
//Switch off Nanoleaf device physically
QString url = getUrl(_hostname, _api_port, _auth_token, API_STATE );
putJson(url, getOnOffRequest(false) );
return _deviceReady ? write(std::vector<ColorRgb>(_ledCount, ColorRgb::BLACK )) : -1;
return 0;
}

View File

@ -0,0 +1,148 @@
#pragma once
// Leddevice includes
#include <leddevice/LedDevice.h>
#include "ProviderUdp.h"
// ssdp discover
#include <ssdp/SSDPDiscover.h>
// Qt includes
#include <QString>
#include <QNetworkAccessManager>
///
/// Implementation of the LedDevice interface for sending to
/// Nanoleaf devices via network by using the 'external control' protocol.
///
class LedDeviceNanoleaf : public ProviderUdp
{
public:
///
/// Constructs the LedDevice for Nanoleaf LightPanels (aka Aurora) or Canvas
///
/// following code shows all config options
/// @code
/// "device" :
/// {
/// "type" : "nanoleaf"
/// "output" : "hostname or IP", // Optional. If empty, device is tried to be discovered
/// "token" : "Authentication Token",
/// },
///@endcode
///
/// @param deviceConfig json config for nanoleaf
///
LedDeviceNanoleaf(const QJsonObject &deviceConfig);
///
/// Destructor of the LedDevice; closes the tcp client
///
virtual ~LedDeviceNanoleaf();
/// Constructs leddevice
static LedDevice* construct(const QJsonObject &deviceConfig);
/// Switch the leds on
virtual int switchOn();
/// Switch the leds off
virtual int switchOff();
protected:
///
/// Writes the led color values to the led-device
///
/// @param ledValues The color-value per led
/// @return Zero on succes else negative
///
virtual int write(const std::vector<ColorRgb> & ledValues);
///
/// Identifies a Nanoleaf device's panel configuration,
/// sets device into External Control (UDP) mode
///
/// @param deviceConfig the json device config
/// @return true if success
/// @exception runtime_error in case device cannot be initialised
/// e.g. more LEDs configured than device has panels or network problems
///
bool init(const QJsonObject &deviceConfig);
private:
// QNetworkAccessManager object for sending requests.
QNetworkAccessManager* _networkmanager;
QString _hostname;
QString _api_port;
QString _auth_token;
//Nanoleaf device details
QString _deviceModel;
QString _deviceFirmwareVersion;
ushort _extControlVersion;
/// The number of panels with leds
int _panelLedCount;
/// Array of the pannel ids.
std::vector<uint> _panelIds;
///
/// Discover Nanoleaf device via SSDP identifiers
///
/// @return True, if Nanoleaf device was found
///
bool discoverNanoleafDevice();
///
/// Change Nanoleaf device to External Control (UDP) mode
///
/// @return Response from device
///
QJsonDocument changeToExternalControlMode();
///
/// Get command to switch Nanoleaf device on or off
///
/// @param isOn True, if to switch on device
/// @return Command to switch device on/off
///
QString getOnOffRequest (bool isOn ) const;
///
/// Get command as url
///
/// @param host Hostname or IP
/// @param port IP-Port
/// @param _auth_token Authorization token
/// @param Endpoint command for request
/// @return Url to execute endpoint/command
///
QString getUrl(QString host, QString port, QString auth_token, QString endpoint) const;
///
/// Execute GET request
///
/// @param url GET request for url
/// @return Response from device
///
QJsonDocument getJson(QString url) const;
///
/// Execute PUT request
///
/// @param Url for PUT request
/// @param json Command for request
/// @return Response from device
///
QJsonDocument putJson(QString url, QString json) const;
///
/// Handle replys for GET and PUT requests
///
/// @param reply Network reply
/// @return Response for request, if no error
/// @exception runtime_error for network or request errors
///
QJsonDocument handleReply(QNetworkReply* const &reply ) const;
};

View File

@ -4,10 +4,10 @@
"properties":{ "properties":{
"output": { "output": {
"type": "string", "type": "string",
"title":"edt_dev_spec_targetIp_title", "title":"edt_dev_spec_targetIpHost_title",
"propertyOrder" : 1 "propertyOrder" : 1
}, },
"key": { "token": {
"type": "string", "type": "string",
"title":"edt_dev_auth_key_title", "title":"edt_dev_auth_key_title",
"propertyOrder" : 2 "propertyOrder" : 2

View File

@ -36,7 +36,7 @@ void SSDPDiscover::searchForService(const QString& st)
const QString SSDPDiscover::getFirstService(const searchType& type, const QString& st, const int& timeout_ms) const QString SSDPDiscover::getFirstService(const searchType& type, const QString& st, const int& timeout_ms)
{ {
Info(_log, "Search for Hyperion server..."); Info(_log, "Search for Service [%s]", QSTRING_CSTR(st));
_searchTarget = st; _searchTarget = st;
// search // search
@ -44,7 +44,7 @@ const QString SSDPDiscover::getFirstService(const searchType& type, const QStrin
_udpSocket->waitForReadyRead(timeout_ms); _udpSocket->waitForReadyRead(timeout_ms);
while (_udpSocket->hasPendingDatagrams()) while (_udpSocket->hasPendingDatagrams())
{ {
QByteArray datagram; QByteArray datagram;
datagram.resize(_udpSocket->pendingDatagramSize()); datagram.resize(_udpSocket->pendingDatagramSize());
@ -54,6 +54,9 @@ const QString SSDPDiscover::getFirstService(const searchType& type, const QStrin
_udpSocket->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort); _udpSocket->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);
QString data(datagram); QString data(datagram);
Debug(_log, "_data: [%s]", QSTRING_CSTR(data));
QMap<QString,QString> headers; QMap<QString,QString> headers;
QString address; QString address;
// parse request // parse request
@ -88,7 +91,7 @@ const QString SSDPDiscover::getFirstService(const searchType& type, const QStrin
//Info(_log, "Received msearch response from '%s:%d'. Search target: %s",QSTRING_CSTR(sender.toString()), senderPort, QSTRING_CSTR(headers.value("st"))); //Info(_log, "Received msearch response from '%s:%d'. Search target: %s",QSTRING_CSTR(sender.toString()), senderPort, QSTRING_CSTR(headers.value("st")));
if(type == STY_WEBSERVER) if(type == STY_WEBSERVER)
{ {
Info(_log, "Found Hyperion server at: %s:%d", QSTRING_CSTR(url.host()), url.port()); Info(_log, "Found service [%s] at: %s:%d", QSTRING_CSTR(st), QSTRING_CSTR(url.host()), url.port());
return url.host()+":"+QString::number(url.port()); return url.host()+":"+QString::number(url.port());
} }
@ -101,13 +104,13 @@ const QString SSDPDiscover::getFirstService(const searchType& type, const QStrin
} }
else else
{ {
Info(_log, "Found Hyperion server at: %s:%s", QSTRING_CSTR(url.host()), QSTRING_CSTR(fbsport)); Info(_log, "Found service [%s] at: %s:%s", QSTRING_CSTR(st), QSTRING_CSTR(url.host()), QSTRING_CSTR(fbsport));
return url.host()+":"+fbsport; return url.host()+":"+fbsport;
} }
} }
} }
} }
Info(_log,"Search timeout, no Hyperion server found"); Info(_log,"Search timeout, service [%s] not found", QSTRING_CSTR(st) );
return QString(); return QString();
} }
@ -163,7 +166,7 @@ void SSDPDiscover::sendSearch(const QString& st)
{ {
const QString msg = UPNP_DISCOVER_MESSAGE.arg(st); const QString msg = UPNP_DISCOVER_MESSAGE.arg(st);
_udpSocket->writeDatagram(msg.toUtf8(), _udpSocket->writeDatagram(msg.toUtf8(),
QHostAddress(SSDP_ADDR), QHostAddress(SSDP_ADDR),
SSDP_PORT); SSDP_PORT);
} }