From b459b8723e683b7b17079ccdfe92bba610f16502 Mon Sep 17 00:00:00 2001 From: LordGrey <48840279+Lord-Grey@users.noreply.github.com> Date: Sat, 14 Sep 2019 22:54:41 +0200 Subject: [PATCH] Update SSDPDiscover, Nanoleaf Device & CrossCompile Instructions (#616) * 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 --- CrossCompileHowto.txt | 4 +- .../leddevice/dev_net/LedDeviceNanoleaf.cpp | 163 +++++++++--------- libsrc/leddevice/dev_net/LedDeviceNanoleaf.h | 2 +- libsrc/ssdp/SSDPDiscover.cpp | 14 +- 4 files changed, 91 insertions(+), 92 deletions(-) diff --git a/CrossCompileHowto.txt b/CrossCompileHowto.txt index ee31b104..3eb4343e 100644 --- a/CrossCompileHowto.txt +++ b/CrossCompileHowto.txt @@ -3,7 +3,7 @@ ############## #ON TARGET #-------------- -#sudo apt-get install qtbase5-dev libqt5serialport5-dev libusb-1.0-0-dev python3-dev libxrender-dev libavahi-core-dev libavahi-compat-libdnssd-dev aptitude show qt5-default rsync +#sudo apt-get install qtbase5-dev libqt5serialport5-dev libusb-1.0-0-dev python3-dev libxrender-dev libavahi-core-dev libavahi-compat-libdnssd-dev libjpeg-dev libqt5sql5-sqlite aptitude show qt5-default rsync ############# #ON HOST #--------- @@ -11,7 +11,7 @@ sudo apt-get update sudo apt-get upgrade # !!! TO-DO verify aptitude gcc-multilib -sudo apt-get -qq -y install git rsync cmake build-essential qtbase5-dev libqt5serialport5-dev libusb-1.0-0-dev python3-dev libxrender-dev libavahi-core-dev libavahi-compat-libdnssd-dev +sudo apt-get -qq -y install git rsync cmake build-essential qtbase5-dev libqt5serialport5-dev libusb-1.0-0-dev python3-dev libxrender-dev libavahi-core-dev libavahi-compat-libdnssd-dev libjpeg-dev libqt5sql5-sqlite #--------- diff --git a/libsrc/leddevice/dev_net/LedDeviceNanoleaf.cpp b/libsrc/leddevice/dev_net/LedDeviceNanoleaf.cpp index ff7324cd..18aad5c2 100644 --- a/libsrc/leddevice/dev_net/LedDeviceNanoleaf.cpp +++ b/libsrc/leddevice/dev_net/LedDeviceNanoleaf.cpp @@ -9,51 +9,51 @@ #include // Controller configuration settings -const QString CONFIG_ADDRESS = "output"; -const QString CONFIG_PORT = "port"; -const QString CONFIG_AUTH_TOKEN ="token"; +static const char CONFIG_ADDRESS[] = "output"; +//static const char CONFIG_PORT[] = "port"; +static const char 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"; +static const char PANEL_LAYOUT[] = "layout"; +static const char PANEL_NUM[] = "numPanels"; +static const char PANEL_ID[] = "panelId"; +static const char PANEL_POSITIONDATA[] = "positionData"; +static const char PANEL_SHAPE_TYPE[] = "shapeType"; +//static const char PANEL_ORIENTATION[] = "0"; +static const char PANEL_POS_X[] = "x"; +static const char 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"; +static const char STATE_ON[] = "on"; +static const char STATE_ONOFF_VALUE[] = "value"; +static const char STATE_VALUE_TRUE[] = "true"; +static const char 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"; +static const char DEV_DATA_NAME[] = "name"; +static const char DEV_DATA_MODEL[] = "model"; +static const char DEV_DATA_MANUFACTURER[] = "manufacturer"; +static const char 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"; +//static const char STREAM_CONTROL_IP[] = "streamControlIpAddr"; +static const char STREAM_CONTROL_PORT[] = "streamControlPort"; +//static const char 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"; +static const char API_DEFAULT_PORT[] = "16021"; +static const char API_URL_FORMAT[] = "http://%1:%2/api/v1/%3/%4"; +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_V2[] = "{\"write\" : {\"command\" : \"display\", \"animType\" : \"extControl\", \"extControlVersion\" : \"v2\"}}"; +static const char API_STATE[] ="state"; +static const char API_PANELLAYOUT[] = "panelLayout"; +static const char API_EFFECT[] = "effects"; //Nanoleaf ssdp services -const QString SSDP_CANVAS = "nanoleaf:nl29"; -const QString SSDP_LIGHTPANELS = "nanoleaf_aurora:light"; +static const char SSDP_CANVAS[] = "nanoleaf:nl29"; +static const char SSDP_LIGHTPANELS[] = "nanoleaf_aurora:light"; const int SSDP_TIMEOUT = 5000; // timout in ms // Nanoleaf Panel Shapetypes @@ -87,7 +87,7 @@ bool LedDeviceNanoleaf::init(const QJsonObject &deviceConfig) { LedDevice::init(deviceConfig); - int configuredLedCount = this->getLedCount(); + uint configuredLedCount = static_cast(this->getLedCount()); Debug(_log, "ActiveDevice : %s", QSTRING_CSTR( this->getActiveDevice() )); Debug(_log, "LedCount : %d", configuredLedCount); Debug(_log, "ColorOrder : %s", QSTRING_CSTR( this->getColorOrder() )); @@ -131,16 +131,16 @@ bool LedDeviceNanoleaf::init(const QJsonObject &deviceConfig) { int panelNum = jsonLayout[PANEL_NUM].toInt(); QJsonArray positionData = jsonLayout[PANEL_POSITIONDATA].toArray(); - std::map> panelMap; + std::map> 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(); + unsigned int panelId = static_cast(panelObj[PANEL_ID].toInt()); + unsigned int panelX = static_cast(panelObj[PANEL_POS_X].toInt()); + unsigned int panelY = static_cast(panelObj[PANEL_POS_Y].toInt()); + unsigned int panelshapeType = static_cast(panelObj[PANEL_SHAPE_TYPE].toInt()); //int panelOrientation = panelObj[PANEL_ORIENTATION].toInt(); //std::cout << "Panel [" << panelId << "]" << " (" << panelX << "," << panelY << ") - Type: [" << panelshapeType << "]" << std::endl; @@ -155,13 +155,13 @@ bool LedDeviceNanoleaf::init(const QJsonObject &deviceConfig) { // 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 //std::cout << "panelMap[" << posY->first << "][" << posX.first << "]=" << posX.second << std::endl; _panelIds.push_back(posX.second); } } - this->_panelLedCount = _panelIds.size(); + this->_panelLedCount = static_cast(_panelIds.size()); Debug(_log, "PanelsNum : %d", panelNum); @@ -172,27 +172,17 @@ bool LedDeviceNanoleaf::init(const QJsonObject &deviceConfig) { throw std::runtime_error ( (QString ("Not enough panels [%1] for configured LEDs [%2] found!").arg(_panelLedCount).arg(configuredLedCount)).toStdString() ); } else { - if ( _panelLedCount > this->getLedCount() ) { + if ( _panelLedCount > static_cast(this->getLedCount()) ) { Warning(_log, "Nanoleaf: More panels [%d] than configured LEDs [%d].", _panelLedCount, configuredLedCount ); } } + // Set UDP streaming port + _port = STREAM_CONTROL_DEFAULT_PORT; + _defaultHost = _hostname; + 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" ); @@ -249,7 +239,7 @@ QJsonDocument LedDeviceNanoleaf::changeToExternalControlMode() { } 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); + return QString(API_URL_FORMAT).arg(host, port, auth_token, endpoint); } QJsonDocument LedDeviceNanoleaf::getJson(QString url) const { @@ -344,10 +334,10 @@ QJsonDocument LedDeviceNanoleaf::handleReply(QNetworkReply* const &reply ) const default: break; } - errorReason = QString ("%1:%2 [%3 %4] - %5").arg(_hostname).arg(_api_port).arg(httpStatusCode).arg(httpReason).arg(advise); + errorReason = QString ("%1:%2 [%3 %4] - %5").arg(_hostname, _api_port, QString(httpStatusCode) , httpReason); } - else { - errorReason = QString ("%1:%2 - %3").arg(_hostname).arg(_api_port).arg(reply->errorString()); + else { + errorReason = QString ("%1:%2 - %3").arg(_hostname, _api_port, reply->errorString()); } Error (_log, "%s", QSTRING_CSTR( errorReason )); throw std::runtime_error("Network Error"); @@ -390,8 +380,9 @@ int LedDeviceNanoleaf::write(const std::vector & ledValues) //udpBufferSize = _panelLedCount * 7 + 1; // Buffersize for LightPanels - udpBufferSize = _panelLedCount * 8 + 2; - uint8_t udpbuffer[udpBufferSize]; + udpBufferSize = _panelLedCount * 8 + 2; + std::vector udpbuffer; + udpbuffer.resize(udpBufferSize); uchar lowByte; // lower byte uchar highByte; // upper byte @@ -399,8 +390,8 @@ int LedDeviceNanoleaf::write(const std::vector & ledValues) uint i=0; // Set number of panels - highByte = (uchar) (_panelLedCount >>8 ); - lowByte = (uchar) (_panelLedCount & 0xFF); + highByte = static_cast(_panelLedCount >>8 ); + lowByte = static_cast(_panelLedCount & 0xFF); if ( _extControlVersion == EXTCTRLVER_V2 ) { udpbuffer[i++] = highByte; @@ -408,16 +399,16 @@ int LedDeviceNanoleaf::write(const std::vector & ledValues) udpbuffer[i++] = lowByte; ColorRgb color; - for ( int panelCounter=0; panelCounter < _panelLedCount; panelCounter++ ) + for ( uint panelCounter=0; panelCounter < _panelLedCount; panelCounter++ ) { - uint panelID = _panelIds[panelCounter]; + uint panelID = _panelIds[panelCounter]; - highByte = (uchar) (panelID >>8 ); - lowByte = (uchar) (panelID & 0xFF); + highByte = static_cast(panelID >>8 ); + lowByte = static_cast(panelID & 0xFF); // Set panels configured - if( panelCounter < this->getLedCount() ) { - color = (ColorRgb) ledValues.at(panelCounter); + if( panelCounter < static_cast(this->getLedCount()) ) { + color = static_cast(ledValues.at(panelCounter)); } else { @@ -448,8 +439,8 @@ int LedDeviceNanoleaf::write(const std::vector & ledValues) // Set transition time unsigned char tranitionTime = 1; // currently fixed at value 1 which corresponds to 100ms - highByte = (uchar) (tranitionTime >>8 ); - lowByte = (uchar) (tranitionTime & 0xFF); + highByte = static_cast(tranitionTime >>8 ); + lowByte = static_cast(tranitionTime & 0xFF); if ( _extControlVersion == EXTCTRLVER_V2 ) { udpbuffer[i++] = highByte; @@ -459,24 +450,34 @@ int LedDeviceNanoleaf::write(const std::vector & ledValues) //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"); + // printf ("udpBufferSize[%d], Bytes to send [%d]\n", udpBufferSize, i); + // for ( uint c= 0; c < udpBufferSize;c++ ) + // { + // printf ("%x ", static_cast(udpbuffer[c])); + // } + // printf("\n"); - retVal &= writeBytes( i , udpbuffer); + retVal &= writeBytes( i , udpbuffer.data()); 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); + return QString( "{\"%1\":{\"%2\":%3}}" ).arg(STATE_ON, STATE_ONOFF_VALUE, state); } int LedDeviceNanoleaf::switchOn() { Debug(_log, "switchOn()"); + + // Set Nanoleaf to External Control (UDP) mode + Debug(_log, "Set Nanoleaf to External Control (UDP) streaming mode"); + QJsonDocument responseDoc = changeToExternalControlMode(); + // Resolve port for Ligh Panels + QJsonObject jsonStreamControllInfo = responseDoc.object(); + if ( ! jsonStreamControllInfo.isEmpty() ) { + _port = static_cast(jsonStreamControllInfo[STREAM_CONTROL_PORT].toInt()); + } + //Switch on Nanoleaf device QString url = getUrl(_hostname, _api_port, _auth_token, API_STATE ); putJson(url, this->getOnOffRequest(true) ); @@ -494,7 +495,5 @@ int LedDeviceNanoleaf::switchOff() { QString url = getUrl(_hostname, _api_port, _auth_token, API_STATE ); putJson(url, getOnOffRequest(false) ); - return _deviceReady ? write(std::vector(_ledCount, ColorRgb::BLACK )) : -1; - - return 0; + return _deviceReady ? write(std::vector(static_cast(_ledCount), ColorRgb::BLACK )) : -1; } diff --git a/libsrc/leddevice/dev_net/LedDeviceNanoleaf.h b/libsrc/leddevice/dev_net/LedDeviceNanoleaf.h index 00ad576f..c0c6d4d5 100644 --- a/libsrc/leddevice/dev_net/LedDeviceNanoleaf.h +++ b/libsrc/leddevice/dev_net/LedDeviceNanoleaf.h @@ -83,7 +83,7 @@ private: QString _deviceFirmwareVersion; ushort _extControlVersion; /// The number of panels with leds - int _panelLedCount; + uint _panelLedCount; /// Array of the pannel ids. std::vector _panelIds; diff --git a/libsrc/ssdp/SSDPDiscover.cpp b/libsrc/ssdp/SSDPDiscover.cpp index f2fef11f..b3a490cc 100644 --- a/libsrc/ssdp/SSDPDiscover.cpp +++ b/libsrc/ssdp/SSDPDiscover.cpp @@ -36,7 +36,7 @@ void SSDPDiscover::searchForService(const QString& st) const QString SSDPDiscover::getFirstService(const searchType& type, const QString& st, const int& timeout_ms) { - Info(_log, "Search for Hyperion server"); + Debug(_log, "Search for Service [%s]", QSTRING_CSTR(st)); _searchTarget = st; // search @@ -88,10 +88,10 @@ const QString SSDPDiscover::getFirstService(const searchType& type, const QStrin { _usnList << headers.value("usn"); QUrl url(headers.value("location")); - //Info(_log, "Received msearch response from '%s:%d'. Search target: %s",QSTRING_CSTR(sender.toString()), senderPort, QSTRING_CSTR(headers.value("st"))); + //Debug(_log, "Received msearch response from '%s:%d'. Search target: %s",QSTRING_CSTR(sender.toString()), senderPort, QSTRING_CSTR(headers.value("st"))); if(type == STY_WEBSERVER) { - Info(_log, "Found service at: %s:%d", QSTRING_CSTR(url.host()), url.port()); + Debug(_log, "Found service [%s] at: %s:%d", QSTRING_CSTR(st), QSTRING_CSTR(url.host()), url.port()); return url.host()+":"+QString::number(url.port()); } @@ -104,7 +104,7 @@ const QString SSDPDiscover::getFirstService(const searchType& type, const QStrin } else { - Info(_log, "Found service at: %s:%s", QSTRING_CSTR(url.host()), QSTRING_CSTR(fbsport)); + Debug(_log, "Found service [%s] at: %s:%s", QSTRING_CSTR(st), QSTRING_CSTR(url.host()), QSTRING_CSTR(fbsport)); return url.host()+":"+fbsport; } } @@ -117,13 +117,13 @@ const QString SSDPDiscover::getFirstService(const searchType& type, const QStrin } else { - Info(_log, "Found service at: %s:%s", QSTRING_CSTR(url.host()), QSTRING_CSTR(jssport)); + Debug(_log, "Found service at: %s:%s", QSTRING_CSTR(url.host()), QSTRING_CSTR(jssport)); return url.host()+":"+jssport; } } } } - Info(_log,"Search timeout, service [%s] not found", QSTRING_CSTR(st) ); + Debug(_log,"Search timeout, service [%s] not found", QSTRING_CSTR(st) ); return QString(); } @@ -168,7 +168,7 @@ void SSDPDiscover::readPendingDatagrams() if (headers.value("st") == _searchTarget) { _usnList << headers.value("usn"); - //Info(_log, "Received msearch response from '%s:%d'. Search target: %s",QSTRING_CSTR(sender.toString()), senderPort, QSTRING_CSTR(headers.value("st"))); + //Debug(_log, "Received msearch response from '%s:%d'. Search target: %s",QSTRING_CSTR(sender.toString()), senderPort, QSTRING_CSTR(headers.value("st"))); QUrl url(headers.value("location")); emit newService(url.host()+":"+QString::number(url.port())); }