Merge pull request #460 from ntim/support_for_philips_hue

Transition to QNetworkAccessManager for compatibility to QT5

Former-commit-id: 07f30e86eaf0677a3199f75bf5f7c8f760822634
This commit is contained in:
tvdzwan 2016-01-21 20:49:02 +01:00
commit 6b052081f7
2 changed files with 23 additions and 66 deletions

View File

@ -5,15 +5,9 @@
#include <json/json.h> #include <json/json.h>
// qt includes // qt includes
#include <QtCore/qmath.h> #include <QtCore/qmath.h>
#include <QUrl>
#ifdef ENABLE_QT5
#else
#include <QHttpRequestHeader>
#endif
#include <QEventLoop> #include <QEventLoop>
#include <QNetworkReply>
#include <set> #include <set>
@ -149,22 +143,14 @@ LedDevicePhilipsHue::LedDevicePhilipsHue(const std::string& output, const std::s
int transitiontime, std::vector<unsigned int> lightIds) : int transitiontime, std::vector<unsigned int> lightIds) :
host(output.c_str()), username(username.c_str()), switchOffOnBlack(switchOffOnBlack), transitiontime( host(output.c_str()), username(username.c_str()), switchOffOnBlack(switchOffOnBlack), transitiontime(
transitiontime), lightIds(lightIds) { transitiontime), lightIds(lightIds) {
#ifdef ENABLE_QT5 manager = new QNetworkAccessManager();
#else
http = new QHttp(host);
timer.setInterval(3000); timer.setInterval(3000);
timer.setSingleShot(true); timer.setSingleShot(true);
connect(&timer, SIGNAL(timeout()), this, SLOT(restoreStates())); connect(&timer, SIGNAL(timeout()), this, SLOT(restoreStates()));
#endif
} }
LedDevicePhilipsHue::~LedDevicePhilipsHue() { LedDevicePhilipsHue::~LedDevicePhilipsHue() {
#ifdef ENABLE_QT5 delete manager;
#else
delete http;
#endif
} }
int LedDevicePhilipsHue::write(const std::vector<ColorRgb> & ledValues) { int LedDevicePhilipsHue::write(const std::vector<ColorRgb> & ledValues) {
@ -214,64 +200,44 @@ int LedDevicePhilipsHue::write(const std::vector<ColorRgb> & ledValues) {
// Next light id. // Next light id.
idx++; idx++;
} }
#ifdef ENABLE_QT5
#else
timer.start(); timer.start();
#endif
return 0; return 0;
} }
int LedDevicePhilipsHue::switchOff() { int LedDevicePhilipsHue::switchOff() {
#ifdef ENABLE_QT5
#else
timer.stop(); timer.stop();
// If light states have been saved before, ... // If light states have been saved before, ...
if (areStatesSaved()) { if (areStatesSaved()) {
// ... restore them. // ... restore them.
restoreStates(); restoreStates();
} }
#endif
return 0; return 0;
} }
void LedDevicePhilipsHue::put(QString route, QString content) { void LedDevicePhilipsHue::put(QString route, QString content) {
#ifdef ENABLE_QT5 QString url = QString("http://%1/api/%2/%3").arg(host).arg(username).arg(route);
#else
QString url = QString("/api/%1/%2").arg(username).arg(route);
QHttpRequestHeader header("PUT", url);
header.setValue("Host", host);
header.setValue("Accept-Encoding", "identity");
header.setValue("Connection", "keep-alive");
header.setValue("Content-Length", QString("%1").arg(content.size()));
QEventLoop loop;
// Connect requestFinished signal to quit slot of the loop.
loop.connect(http, SIGNAL(requestFinished(int, bool)), SLOT(quit()));
// Perfrom request // Perfrom request
http->request(header, content.toAscii()); QNetworkRequest request(url);
QNetworkReply* reply = manager->put(request, content.toAscii());
// Connect finished 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. // Go into the loop until the request is finished.
loop.exec(); loop.exec();
#endif
} }
QByteArray LedDevicePhilipsHue::get(QString route) { QByteArray LedDevicePhilipsHue::get(QString route) {
#ifdef ENABLE_QT5 QString url = QString("http://%1/api/%2/%3").arg(host).arg(username).arg(route);
return 0;
#else
QString url = QString("/api/%1/%2").arg(username).arg(route);
// Event loop to block until request finished.
QEventLoop loop;
// Connect requestFinished signal to quit slot of the loop.
loop.connect(http, SIGNAL(requestFinished(int, bool)), SLOT(quit()));
// Perfrom request // Perfrom request
http->get(url); 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. // Go into the loop until the request is finished.
loop.exec(); loop.exec();
// Read all data of the response. // Read all data of the response.
return http->readAll(); return reply->readAll();
#endif
} }
QString LedDevicePhilipsHue::getStateRoute(unsigned int lightId) { QString LedDevicePhilipsHue::getStateRoute(unsigned int lightId) {

View File

@ -4,14 +4,10 @@
#include <string> #include <string>
// Qt includes // Qt includes
#ifdef ENABLE_QT5
#include <QNetworkAccessManager>
#else
#include <QObject> #include <QObject>
#include <QString> #include <QString>
#include <QHttp> #include <QNetworkAccessManager>
#include <QTimer> #include <QTimer>
#endif
// Leddevice includes // Leddevice includes
#include <leddevice/LedDevice.h> #include <leddevice/LedDevice.h>
@ -167,15 +163,10 @@ private:
QString host; QString host;
/// User name for the API ("newdeveloper") /// User name for the API ("newdeveloper")
QString username; QString username;
/// Qhttp object for sending requests. /// QNetworkAccessManager object for sending requests.
#ifdef ENABLE_QT5 QNetworkAccessManager* manager;
// TODO QNetworkAcessManager stuff
#else
QHttp* http;
/// Use timer to reset lights when we got into "GRABBINGMODE_OFF". /// Use timer to reset lights when we got into "GRABBINGMODE_OFF".
QTimer timer; QTimer timer;
#endif
/// ///
bool switchOffOnBlack; bool switchOffOnBlack;
/// Transition time in multiples of 100 ms. /// Transition time in multiples of 100 ms.