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
This commit is contained in:
LordGrey 2019-09-14 22:54:41 +02:00 committed by Paulchen Panther
parent 3173c9f7e3
commit b459b8723e
4 changed files with 91 additions and 92 deletions

View File

@ -3,7 +3,7 @@
############## ##############
#ON TARGET #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 #ON HOST
#--------- #---------
@ -11,7 +11,7 @@ sudo apt-get update
sudo apt-get upgrade sudo apt-get upgrade
# !!! TO-DO verify aptitude gcc-multilib # !!! 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
#--------- #---------

View File

@ -9,51 +9,51 @@
#include <QNetworkReply> #include <QNetworkReply>
// Controller configuration settings // Controller configuration settings
const QString CONFIG_ADDRESS = "output"; static const char CONFIG_ADDRESS[] = "output";
const QString CONFIG_PORT = "port"; //static const char CONFIG_PORT[] = "port";
const QString CONFIG_AUTH_TOKEN ="token"; static const char CONFIG_AUTH_TOKEN[] ="token";
// Panel configuration settings // Panel configuration settings
const QString PANEL_LAYOUT = "layout"; static const char PANEL_LAYOUT[] = "layout";
const QString PANEL_NUM = "numPanels"; static const char PANEL_NUM[] = "numPanels";
const QString PANEL_ID = "panelId"; static const char PANEL_ID[] = "panelId";
const QString PANEL_POSITIONDATA = "positionData"; static const char PANEL_POSITIONDATA[] = "positionData";
const QString PANEL_SHAPE_TYPE = "shapeType"; static const char PANEL_SHAPE_TYPE[] = "shapeType";
const QString PANEL_ORIENTATION = "0"; //static const char PANEL_ORIENTATION[] = "0";
const QString PANEL_POS_X = "x"; static const char PANEL_POS_X[] = "x";
const QString PANEL_POS_Y = "y"; static const char PANEL_POS_Y[] = "y";
// List of State Information // List of State Information
const QString STATE_ON = "on"; static const char STATE_ON[] = "on";
const QString STATE_ONOFF_VALUE = "value"; static const char STATE_ONOFF_VALUE[] = "value";
const QString STATE_VALUE_TRUE = "true"; static const char STATE_VALUE_TRUE[] = "true";
const QString STATE_VALUE_FALSE = "false"; static const char STATE_VALUE_FALSE[] = "false";
//Device Data elements //Device Data elements
const QString DEV_DATA_NAME = "name"; static const char DEV_DATA_NAME[] = "name";
const QString DEV_DATA_MODEL = "model"; static const char DEV_DATA_MODEL[] = "model";
const QString DEV_DATA_MANUFACTURER = "manufacturer"; static const char DEV_DATA_MANUFACTURER[] = "manufacturer";
const QString DEV_DATA_FIRMWAREVERSION = "firmwareVersion"; static const char DEV_DATA_FIRMWAREVERSION[] = "firmwareVersion";
//Nanoleaf Stream Control elements //Nanoleaf Stream Control elements
const QString STREAM_CONTROL_IP = "streamControlIpAddr"; //static const char STREAM_CONTROL_IP[] = "streamControlIpAddr";
const QString STREAM_CONTROL_PORT = "streamControlPort"; static const char STREAM_CONTROL_PORT[] = "streamControlPort";
const QString STREAM_CONTROL_PROTOCOL = "streamControlProtocol"; //static const char STREAM_CONTROL_PROTOCOL[] = "streamControlProtocol";
const quint16 STREAM_CONTROL_DEFAULT_PORT = 60222; //Fixed port for Canvas; const quint16 STREAM_CONTROL_DEFAULT_PORT = 60222; //Fixed port for Canvas;
// Nanoleaf OpenAPI URLs // Nanoleaf OpenAPI URLs
const QString API_DEFAULT_PORT = "16021"; static const char API_DEFAULT_PORT[] = "16021";
const QString API_URL_FORMAT = "http://%1:%2/api/v1/%3/%4"; static const char API_URL_FORMAT[] = "http://%1:%2/api/v1/%3/%4";
const QString API_ROOT = ""; static const char API_ROOT[] = "";
const QString API_EXT_MODE_STRING_V1 = "{\"write\" : {\"command\" : \"display\", \"animType\" : \"extControl\"}}"; static const char API_EXT_MODE_STRING_V1[] = "{\"write\" : {\"command\" : \"display\", \"animType\" : \"extControl\"}}";
const QString 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\"}}";
const QString API_STATE ="state"; static const char API_STATE[] ="state";
const QString API_PANELLAYOUT = "panelLayout"; static const char API_PANELLAYOUT[] = "panelLayout";
const QString API_EFFECT = "effects"; static const char API_EFFECT[] = "effects";
//Nanoleaf ssdp services //Nanoleaf ssdp services
const QString SSDP_CANVAS = "nanoleaf:nl29"; static const char SSDP_CANVAS[] = "nanoleaf:nl29";
const QString SSDP_LIGHTPANELS = "nanoleaf_aurora:light"; static const char SSDP_LIGHTPANELS[] = "nanoleaf_aurora:light";
const int SSDP_TIMEOUT = 5000; // timout in ms const int SSDP_TIMEOUT = 5000; // timout in ms
// Nanoleaf Panel Shapetypes // Nanoleaf Panel Shapetypes
@ -87,7 +87,7 @@ bool LedDeviceNanoleaf::init(const QJsonObject &deviceConfig) {
LedDevice::init(deviceConfig); LedDevice::init(deviceConfig);
int configuredLedCount = this->getLedCount(); uint configuredLedCount = static_cast<uint>(this->getLedCount());
Debug(_log, "ActiveDevice : %s", QSTRING_CSTR( this->getActiveDevice() )); Debug(_log, "ActiveDevice : %s", QSTRING_CSTR( this->getActiveDevice() ));
Debug(_log, "LedCount : %d", configuredLedCount); Debug(_log, "LedCount : %d", configuredLedCount);
Debug(_log, "ColorOrder : %s", QSTRING_CSTR( this->getColorOrder() )); Debug(_log, "ColorOrder : %s", QSTRING_CSTR( this->getColorOrder() ));
@ -131,16 +131,16 @@ bool LedDeviceNanoleaf::init(const QJsonObject &deviceConfig) {
int panelNum = jsonLayout[PANEL_NUM].toInt(); int panelNum = jsonLayout[PANEL_NUM].toInt();
QJsonArray positionData = jsonLayout[PANEL_POSITIONDATA].toArray(); QJsonArray positionData = jsonLayout[PANEL_POSITIONDATA].toArray();
std::map<int, std::map<int, int>> 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();
int panelId = panelObj[PANEL_ID].toInt(); unsigned int panelId = static_cast<uint>(panelObj[PANEL_ID].toInt());
int panelX = panelObj[PANEL_POS_X].toInt(); unsigned int panelX = static_cast<uint>(panelObj[PANEL_POS_X].toInt());
int panelY = panelObj[PANEL_POS_Y].toInt(); unsigned int panelY = static_cast<uint>(panelObj[PANEL_POS_Y].toInt());
int panelshapeType = panelObj[PANEL_SHAPE_TYPE].toInt(); unsigned int panelshapeType = static_cast<uint>(panelObj[PANEL_SHAPE_TYPE].toInt());
//int panelOrientation = panelObj[PANEL_ORIENTATION].toInt(); //int panelOrientation = panelObj[PANEL_ORIENTATION].toInt();
//std::cout << "Panel [" << panelId << "]" << " (" << panelX << "," << panelY << ") - Type: [" << panelshapeType << "]" << std::endl; //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 // Sort panels top down, left right
for(auto posY = panelMap.crbegin(); posY != panelMap.crend(); ++posY) { for(auto posY = panelMap.crbegin(); posY != panelMap.crend(); ++posY) {
// posY.first is the first key // 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; //std::cout << "panelMap[" << posY->first << "][" << posX.first << "]=" << posX.second << std::endl;
_panelIds.push_back(posX.second); _panelIds.push_back(posX.second);
} }
} }
this->_panelLedCount = _panelIds.size(); this->_panelLedCount = static_cast<uint>(_panelIds.size());
Debug(_log, "PanelsNum : %d", panelNum); 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() ); throw std::runtime_error ( (QString ("Not enough panels [%1] for configured LEDs [%2] found!").arg(_panelLedCount).arg(configuredLedCount)).toStdString() );
} else { } else {
if ( _panelLedCount > 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 [%d] than configured LEDs [%d].", _panelLedCount, configuredLedCount );
} }
} }
// Set UDP streaming port
_port = STREAM_CONTROL_DEFAULT_PORT;
_defaultHost = _hostname;
switchOn(); 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); ProviderUdp::init(deviceConfig);
Debug(_log, "Started successfully" ); Debug(_log, "Started successfully" );
@ -249,7 +239,7 @@ QJsonDocument LedDeviceNanoleaf::changeToExternalControlMode() {
} }
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 {
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 { QJsonDocument LedDeviceNanoleaf::getJson(QString url) const {
@ -344,10 +334,10 @@ QJsonDocument LedDeviceNanoleaf::handleReply(QNetworkReply* const &reply ) const
default: default:
break; 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 { else {
errorReason = QString ("%1:%2 - %3").arg(_hostname).arg(_api_port).arg(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");
@ -390,8 +380,9 @@ int LedDeviceNanoleaf::write(const std::vector<ColorRgb> & ledValues)
//udpBufferSize = _panelLedCount * 7 + 1; // Buffersize for LightPanels //udpBufferSize = _panelLedCount * 7 + 1; // Buffersize for LightPanels
udpBufferSize = _panelLedCount * 8 + 2; udpBufferSize = _panelLedCount * 8 + 2;
uint8_t udpbuffer[udpBufferSize]; std::vector<uint8_t> udpbuffer;
udpbuffer.resize(udpBufferSize);
uchar lowByte; // lower byte uchar lowByte; // lower byte
uchar highByte; // upper byte uchar highByte; // upper byte
@ -399,8 +390,8 @@ int LedDeviceNanoleaf::write(const std::vector<ColorRgb> & ledValues)
uint i=0; uint i=0;
// Set number of panels // Set number of panels
highByte = (uchar) (_panelLedCount >>8 ); highByte = static_cast<uchar>(_panelLedCount >>8 );
lowByte = (uchar) (_panelLedCount & 0xFF); lowByte = static_cast<uchar>(_panelLedCount & 0xFF);
if ( _extControlVersion == EXTCTRLVER_V2 ) { if ( _extControlVersion == EXTCTRLVER_V2 ) {
udpbuffer[i++] = highByte; udpbuffer[i++] = highByte;
@ -408,16 +399,16 @@ int LedDeviceNanoleaf::write(const std::vector<ColorRgb> & ledValues)
udpbuffer[i++] = lowByte; udpbuffer[i++] = lowByte;
ColorRgb color; 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 ); highByte = static_cast<uchar>(panelID >>8 );
lowByte = (uchar) (panelID & 0xFF); lowByte = static_cast<uchar>(panelID & 0xFF);
// Set panels configured // Set panels configured
if( panelCounter < this->getLedCount() ) { if( panelCounter < static_cast<uint>(this->getLedCount()) ) {
color = (ColorRgb) ledValues.at(panelCounter); color = static_cast<ColorRgb>(ledValues.at(panelCounter));
} }
else else
{ {
@ -448,8 +439,8 @@ int LedDeviceNanoleaf::write(const std::vector<ColorRgb> & ledValues)
// Set transition time // Set transition time
unsigned char tranitionTime = 1; // currently fixed at value 1 which corresponds to 100ms unsigned char tranitionTime = 1; // currently fixed at value 1 which corresponds to 100ms
highByte = (uchar) (tranitionTime >>8 ); highByte = static_cast<uchar>(tranitionTime >>8 );
lowByte = (uchar) (tranitionTime & 0xFF); lowByte = static_cast<uchar>(tranitionTime & 0xFF);
if ( _extControlVersion == EXTCTRLVER_V2 ) { if ( _extControlVersion == EXTCTRLVER_V2 ) {
udpbuffer[i++] = highByte; udpbuffer[i++] = highByte;
@ -459,24 +450,34 @@ int LedDeviceNanoleaf::write(const std::vector<ColorRgb> & ledValues)
//std::cout << "[" << panelCounter << "]" << " Color: " << color << std::endl; //std::cout << "[" << panelCounter << "]" << " Color: " << color << std::endl;
} }
// printf ("udpBufferSize[%d], Bytes to send [%d]\n", udpBufferSize, i); // printf ("udpBufferSize[%d], Bytes to send [%d]\n", udpBufferSize, i);
// for ( uint c= 0; c < udpBufferSize;c++ ) // for ( uint c= 0; c < udpBufferSize;c++ )
// { // {
// printf ("%x ", (uchar) udpbuffer[c]); // printf ("%x ", static_cast<uchar>(udpbuffer[c]));
// } // }
// printf("\n"); // printf("\n");
retVal &= writeBytes( i , udpbuffer); retVal &= writeBytes( i , udpbuffer.data());
return 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).arg(STATE_ONOFF_VALUE).arg(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
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<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) );
@ -494,7 +495,5 @@ int LedDeviceNanoleaf::switchOff() {
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>(_ledCount, ColorRgb::BLACK )) : -1; return _deviceReady ? write(std::vector<ColorRgb>(static_cast<uint>(_ledCount), ColorRgb::BLACK )) : -1;
return 0;
} }

View File

@ -83,7 +83,7 @@ private:
QString _deviceFirmwareVersion; QString _deviceFirmwareVersion;
ushort _extControlVersion; ushort _extControlVersion;
/// The number of panels with leds /// The number of panels with leds
int _panelLedCount; uint _panelLedCount;
/// Array of the pannel ids. /// Array of the pannel ids.
std::vector<uint> _panelIds; std::vector<uint> _panelIds;

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"); Debug(_log, "Search for Service [%s]", QSTRING_CSTR(st));
_searchTarget = st; _searchTarget = st;
// search // search
@ -88,10 +88,10 @@ const QString SSDPDiscover::getFirstService(const searchType& type, const QStrin
{ {
_usnList << headers.value("usn"); _usnList << headers.value("usn");
QUrl url(headers.value("location")); 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) 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()); return url.host()+":"+QString::number(url.port());
} }
@ -104,7 +104,7 @@ const QString SSDPDiscover::getFirstService(const searchType& type, const QStrin
} }
else 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; return url.host()+":"+fbsport;
} }
} }
@ -117,13 +117,13 @@ const QString SSDPDiscover::getFirstService(const searchType& type, const QStrin
} }
else 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; 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(); return QString();
} }
@ -168,7 +168,7 @@ void SSDPDiscover::readPendingDatagrams()
if (headers.value("st") == _searchTarget) if (headers.value("st") == _searchTarget)
{ {
_usnList << headers.value("usn"); _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")); QUrl url(headers.value("location"));
emit newService(url.host()+":"+QString::number(url.port())); emit newService(url.host()+":"+QString::number(url.port()));
} }