Stop LedDevice:write for disabled devices + Nanoleaf Fixes (#629)

* Handle Exceptions in main & Pythoninit

* Have SSDPDiscover generic again

* Have SSDPDiscover generic again

* Change Info- to Debug logs as technical service messages

* Nanoleaf - When switched on, ensure UDP mode

* Include SQL Database in Cross-Compile instructions

* Fix Clazy (QT code checker) and clang Warnings

* Stop LedDevice:write for disabled device

* Nanoleaf: Fix uint printfs

* NanoLeaf: Fix indents to tabs

* NanoLeaf - Add debug verbosity switches

* Device switchability support, FileDevice with timestamp support

* Nanoleaf Light Panels now support External Control V2

* Enhance LedDeviceFile by Timestamp + fix readyness

* Stop color stream, if LedDevice disabled

* Nanoleaf - remove switchability
This commit is contained in:
LordGrey 2019-12-08 13:12:01 +01:00 committed by Paulchen Panther
parent f917f0fceb
commit ce7c99d2cd
14 changed files with 410 additions and 321 deletions

View File

@ -439,6 +439,7 @@
"edt_dev_spec_gpioBcm_title": "GPIO Pin", "edt_dev_spec_gpioBcm_title": "GPIO Pin",
"edt_dev_spec_ledIndex_title": "LED index", "edt_dev_spec_ledIndex_title": "LED index",
"edt_dev_spec_colorComponent_title": "Farbkomponente", "edt_dev_spec_colorComponent_title": "Farbkomponente",
"edt_dev_spec_printTimeStamp_title" : "Mit Zeitstempel",
"edt_conf_general_enable_title": "Aktiviert", "edt_conf_general_enable_title": "Aktiviert",
"edt_conf_general_enable_expl": "Wenn aktiviert, ist die Komponente aktiv.", "edt_conf_general_enable_expl": "Wenn aktiviert, ist die Komponente aktiv.",
"edt_conf_general_priority_title": "Priorität", "edt_conf_general_priority_title": "Priorität",

View File

@ -438,6 +438,7 @@
"edt_dev_spec_gpioBcm_title" : "GPIO Pin", "edt_dev_spec_gpioBcm_title" : "GPIO Pin",
"edt_dev_spec_ledIndex_title" : "LED index", "edt_dev_spec_ledIndex_title" : "LED index",
"edt_dev_spec_colorComponent_title" : "Color component", "edt_dev_spec_colorComponent_title" : "Color component",
"edt_dev_spec_printTimeStamp_title" : "Add timestamp",
"edt_conf_general_enable_title" : "Activate", "edt_conf_general_enable_title" : "Activate",
"edt_conf_general_enable_expl" : "If checked, the component is enabled.", "edt_conf_general_enable_expl" : "If checked, the component is enabled.",
"edt_conf_general_priority_title" : "Priority channel", "edt_conf_general_priority_title" : "Priority channel",

View File

@ -31,6 +31,8 @@
#include <effectengine/ActiveEffectDefinition.h> #include <effectengine/ActiveEffectDefinition.h>
#include <effectengine/EffectSchema.h> #include <effectengine/EffectSchema.h>
#include <leddevice/LedDevice.h>
// settings utils // settings utils
#include <utils/settings.h> #include <utils/settings.h>
@ -227,7 +229,12 @@ public:
/// @brief Get the current active led device /// @brief Get the current active led device
/// @return The device nam /// @return The device nam
/// e /// e
const QString & getActiveDevice(); const QString & getActiveDeviceType();
///
/// @brief Get pointer to current LedDevice
///
LedDevice * getActiveDevice() const;
public slots: public slots:
/// ///

View File

@ -51,8 +51,18 @@ public:
/// ///
const QString & getColorOrder() { return _colorOrder; }; const QString & getColorOrder() { return _colorOrder; };
void setActiveDevice(QString dev); ///
const QString & getActiveDevice() { return _activeDevice; }; /// @brief Set the current active ledDevice type
///
/// @param deviceType Device's type
///
void setActiveDeviceType(QString deviceType);
///
/// @brief Get the current active ledDevice type
///
const QString & getActiveDeviceType() { return _activeDeviceType; };
void setLedCount(int ledCount); void setLedCount(int ledCount);
int getLedCount() { return _ledCount; } int getLedCount() { return _ledCount; }
@ -66,7 +76,7 @@ public slots:
/// ///
/// Is called on thread start, all construction tasks and init should run here /// Is called on thread start, all construction tasks and init should run here
/// ///
virtual void start() { _deviceReady = open(); }; virtual void start() { _deviceReady = (open() == 0 ? true : false);}
/// ///
/// Writes the RGB-Color values to the leds. /// Writes the RGB-Color values to the leds.
@ -102,6 +112,14 @@ protected:
/// ///
virtual int open(); virtual int open();
///
/// Writes "BLACK" to the output stream
///
/// @return Zero on success else negative
///
virtual int writeBlack();
// Helper to pipe device config from constructor to start() // Helper to pipe device config from constructor to start()
QJsonObject _devConfig; QJsonObject _devConfig;
@ -113,7 +131,7 @@ protected:
bool _deviceReady; bool _deviceReady;
QString _activeDevice; QString _activeDeviceType;
int _ledCount; int _ledCount;
int _ledRGBCount; int _ledRGBCount;

View File

@ -50,9 +50,9 @@ public:
int getLatchTime(); int getLatchTime();
/// ///
/// @brief Get the current active ledDevice /// @brief Get the current active ledDevice type
/// ///
const QString & getActiveDevice(); const QString & getActiveDeviceType();
/// ///
/// @brief Return the last enable state /// @brief Return the last enable state

View File

@ -495,9 +495,9 @@ const VideoMode & Hyperion::getCurrentVideoMode()
return _currVideoMode; return _currVideoMode;
} }
const QString & Hyperion::getActiveDevice() const QString & Hyperion::getActiveDeviceType()
{ {
return _ledDeviceWrapper->getActiveDevice(); return _ledDeviceWrapper->getActiveDeviceType();
} }
void Hyperion::updatedComponentState(const hyperion::Components comp, const bool state) void Hyperion::updatedComponentState(const hyperion::Components comp, const bool state)
@ -581,9 +581,18 @@ void Hyperion::update()
// feed smoothing in pause mode to maintain a smooth transistion back to smooth mode // feed smoothing in pause mode to maintain a smooth transistion back to smooth mode
if (_deviceSmooth->enabled() || _deviceSmooth->pause()) if (_deviceSmooth->enabled() || _deviceSmooth->pause())
{
_deviceSmooth->setLedValues(_ledBuffer); _deviceSmooth->setLedValues(_ledBuffer);
}
// Smoothing is disabled
if (! _deviceSmooth->enabled()) if (! _deviceSmooth->enabled())
{
emit ledDeviceData(_ledBuffer); emit ledDeviceData(_ledBuffer);
}
}
else
{
// LEDDevice is disabled
//Debug(_log, "LEDDevice is disabled - no update required");
} }
} }

View File

@ -174,8 +174,18 @@ void LinearColorSmoothing::queueColors(const std::vector<ColorRgb> & ledColors)
void LinearColorSmoothing::componentStateChange(const hyperion::Components component, const bool state) void LinearColorSmoothing::componentStateChange(const hyperion::Components component, const bool state)
{ {
if(component == hyperion::COMP_SMOOTHING) if(component == hyperion::COMP_LEDDEVICE)
{
setEnable(state); setEnable(state);
}
if(component == hyperion::COMP_SMOOTHING)
{
setEnable(state);
// update comp register
_hyperion->getComponentRegister().componentStateChanged(hyperion::COMP_SMOOTHING, state);
}
} }
void LinearColorSmoothing::setEnable(bool enable) void LinearColorSmoothing::setEnable(bool enable)
@ -185,8 +195,6 @@ void LinearColorSmoothing::setEnable(bool enable)
QMetaObject::invokeMethod(_timer, "stop", Qt::QueuedConnection); QMetaObject::invokeMethod(_timer, "stop", Qt::QueuedConnection);
_previousValues.clear(); _previousValues.clear();
} }
// update comp register
_hyperion->getComponentRegister().componentStateChanged(hyperion::COMP_SMOOTHING, enable);
} }
void LinearColorSmoothing::setPause(bool pause) void LinearColorSmoothing::setPause(bool pause)
@ -224,7 +232,7 @@ bool LinearColorSmoothing::selectConfig(unsigned cfg, const bool& force)
} }
_currentConfigId = cfg; _currentConfigId = cfg;
//DebugIf( enabled() && !_pause, _log, "set smoothing cfg: %d, interval: %d ms, settlingTime: %d ms, updateDelay: %d frames", _currentConfigId, _updateInterval, _settlingTime, _outputDelay ); //DebugIf( enabled() && !_pause, _log, "set smoothing cfg: %d, interval: %d ms, settlingTime: %d ms, updateDelay: %d frames", _currentConfigId, _updateInterval, _settlingTime, _outputDelay );
DebugIf( _pause, _log, "set smoothing cfg: %d, pause", _currentConfigId ); //DebugIf( _pause, _log, "set smoothing cfg: %d, pause", _currentConfigId );
return true; return true;
} }

View File

@ -62,15 +62,15 @@ void LedDevice::setEnable(bool enable)
_enabled = enable; _enabled = enable;
} }
void LedDevice::setActiveDevice(QString dev) void LedDevice::setActiveDeviceType(QString deviceType)
{ {
_activeDevice = dev; _activeDeviceType = deviceType;
} }
bool LedDevice::init(const QJsonObject &deviceConfig) bool LedDevice::init(const QJsonObject &deviceConfig)
{ {
_colorOrder = deviceConfig["colorOrder"].toString("RGB"); _colorOrder = deviceConfig["colorOrder"].toString("RGB");
_activeDevice = deviceConfig["type"].toString("file").toLower(); _activeDeviceType = deviceConfig["type"].toString("file").toLower();
setLedCount(deviceConfig["currentLedCount"].toInt(1)); // property injected to reflect real led count setLedCount(deviceConfig["currentLedCount"].toInt(1)); // property injected to reflect real led count
_latchTime_ms = deviceConfig["latchTime"].toInt(_latchTime_ms); _latchTime_ms = deviceConfig["latchTime"].toInt(_latchTime_ms);
@ -108,11 +108,17 @@ int LedDevice::setLedValues(const std::vector<ColorRgb>& ledValues)
return retval; return retval;
} }
int LedDevice::switchOff() int LedDevice::writeBlack()
{ {
return _deviceReady ? write(std::vector<ColorRgb>(_ledCount, ColorRgb::BLACK )) : -1; return _deviceReady ? write(std::vector<ColorRgb>(_ledCount, ColorRgb::BLACK )) : -1;
} }
int LedDevice::switchOff()
{
int rc = writeBlack();
return rc;
}
int LedDevice::switchOn() int LedDevice::switchOn()
{ {
return 0; return 0;
@ -129,3 +135,4 @@ int LedDevice::rewriteLeds()
{ {
return _enabled ? write(_ledValues) : -1; return _enabled ? write(_ledValues) : -1;
} }

View File

@ -115,9 +115,9 @@ int LedDeviceWrapper::getLatchTime()
return _ledDevice->getLatchTime(); return _ledDevice->getLatchTime();
} }
const QString & LedDeviceWrapper::getActiveDevice() const QString & LedDeviceWrapper::getActiveDeviceType()
{ {
return _ledDevice->getActiveDevice(); return _ledDevice->getActiveDeviceType();
} }
const QString & LedDeviceWrapper::getColorOrder() const QString & LedDeviceWrapper::getColorOrder()

View File

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

View File

@ -145,4 +145,12 @@ private:
/// @exception runtime_error for network or request errors /// @exception runtime_error for network or request errors
/// ///
QJsonDocument handleReply(QNetworkReply* const &reply ) const; QJsonDocument handleReply(QNetworkReply* const &reply ) const;
///
/// convert vector to hex string
///
/// @param uint8_t vector
/// @return vector as string of hex values
std::string uint8_vector_to_hex_string( const std::vector<uint8_t>& buffer ) const;
}; };

View File

@ -1,9 +1,13 @@
#include "LedDeviceFile.h" #include "LedDeviceFile.h"
#include <chrono>
#include <iomanip>
#include <iostream>
LedDeviceFile::LedDeviceFile(const QJsonObject &deviceConfig) LedDeviceFile::LedDeviceFile(const QJsonObject &deviceConfig)
: LedDevice() : LedDevice()
{ {
init(deviceConfig); _deviceReady = init(deviceConfig);
} }
LedDeviceFile::~LedDeviceFile() LedDeviceFile::~LedDeviceFile()
@ -17,23 +21,40 @@ LedDevice* LedDeviceFile::construct(const QJsonObject &deviceConfig)
bool LedDeviceFile::init(const QJsonObject &deviceConfig) bool LedDeviceFile::init(const QJsonObject &deviceConfig)
{ {
if ( _ofs.is_open() )
{
_ofs.close();
}
_refresh_timer_interval = 0;
LedDevice::init(deviceConfig); LedDevice::init(deviceConfig);
_refresh_timer_interval = 0;
QString fileName = deviceConfig["output"].toString("/dev/null"); _fileName = deviceConfig["output"].toString("/dev/null");
_ofs.open( QSTRING_CSTR(fileName) ); _printTimeStamp = deviceConfig["printTimeStamp"].toBool(false);
return true; return true;
} }
int LedDeviceFile::open()
{
if ( _ofs.is_open() )
{
_ofs.close();
}
_ofs.open( QSTRING_CSTR(_fileName) );
return 0;
}
int LedDeviceFile::write(const std::vector<ColorRgb> & ledValues) int LedDeviceFile::write(const std::vector<ColorRgb> & ledValues)
{ {
_ofs << "["; if ( _printTimeStamp )
{
// get a precise timestamp as a string
const auto now = std::chrono::system_clock::now();
const auto nowAsTimeT = std::chrono::system_clock::to_time_t(now);
const auto nowMs = std::chrono::duration_cast<std::chrono::milliseconds>(
now.time_since_epoch()) % 1000;
_ofs
<< std::put_time(std::localtime(&nowAsTimeT), "%Y-%m-%d %T")
<< '.' << std::setfill('0') << std::setw(3) << nowMs.count();
}
_ofs << " [";
for (const ColorRgb& color : ledValues) for (const ColorRgb& color : ledValues)
{ {
_ofs << color; _ofs << color;

View File

@ -36,6 +36,13 @@ public:
virtual bool init(const QJsonObject &deviceConfig); virtual bool init(const QJsonObject &deviceConfig);
protected: protected:
///
/// Opens and configures the output file
///
/// @return Zero on succes else negative
///
///
virtual int open();
/// ///
/// Writes the given led-color values to the output stream /// Writes the given led-color values to the output stream
/// ///
@ -47,4 +54,10 @@ protected:
/// The outputstream /// The outputstream
std::ofstream _ofs; std::ofstream _ofs;
private:
QString _fileName;
/// Timestamp for the output record
bool _printTimeStamp;
}; };

View File

@ -17,6 +17,13 @@
"maximum": 1000, "maximum": 1000,
"access" : "expert", "access" : "expert",
"propertyOrder" : 2 "propertyOrder" : 2
},
"printTimeStamp": {
"type": "boolean",
"title":"edt_dev_spec_printTimeStamp_title",
"default": false,
"access" : "expert",
"propertyOrder" : 3
} }
}, },
"additionalProperties": true "additionalProperties": true