2016-05-26 23:44:27 +02:00
|
|
|
#pragma once
|
|
|
|
|
2017-03-31 10:17:14 +02:00
|
|
|
// STL includes
|
|
|
|
#include <set>
|
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
// Qt includes
|
|
|
|
#include <QNetworkAccessManager>
|
|
|
|
#include <QTimer>
|
2016-09-23 08:49:22 +02:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
// Leddevice includes
|
|
|
|
#include <leddevice/LedDevice.h>
|
|
|
|
|
2017-03-31 10:17:14 +02:00
|
|
|
// Forward declaration
|
|
|
|
struct CiColorTriangle;
|
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
/**
|
|
|
|
* A color point in the color space of the hue system.
|
|
|
|
*/
|
2017-03-31 10:17:14 +02:00
|
|
|
struct CiColor
|
|
|
|
{
|
2016-05-26 23:44:27 +02:00
|
|
|
/// X component.
|
|
|
|
float x;
|
|
|
|
/// Y component.
|
|
|
|
float y;
|
|
|
|
/// The brightness.
|
|
|
|
float bri;
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Converts an RGB color to the Hue xy color space and brightness.
|
|
|
|
/// https://github.com/PhilipsHue/PhilipsHueSDK-iOS-OSX/blob/master/ApplicationDesignNotes/RGB%20to%20xy%20Color%20conversion.md
|
|
|
|
///
|
|
|
|
/// @param red the red component in [0, 1]
|
2017-03-31 10:17:14 +02:00
|
|
|
///
|
2016-05-26 23:44:27 +02:00
|
|
|
/// @param green the green component in [0, 1]
|
2017-03-31 10:17:14 +02:00
|
|
|
///
|
2016-05-26 23:44:27 +02:00
|
|
|
/// @param blue the blue component in [0, 1]
|
|
|
|
///
|
|
|
|
/// @return color point
|
|
|
|
///
|
2017-03-31 10:17:14 +02:00
|
|
|
static CiColor rgbToCiColor(float red, float green, float blue, CiColorTriangle colorSpace);
|
2016-05-26 23:44:27 +02:00
|
|
|
|
|
|
|
///
|
|
|
|
/// @param p the color point to check
|
2017-03-31 10:17:14 +02:00
|
|
|
///
|
2016-05-26 23:44:27 +02:00
|
|
|
/// @return true if the color point is covered by the lamp color space
|
|
|
|
///
|
2017-03-31 10:17:14 +02:00
|
|
|
static bool isPointInLampsReach(CiColor p, CiColorTriangle colorSpace);
|
2016-05-26 23:44:27 +02:00
|
|
|
|
|
|
|
///
|
|
|
|
/// @param p1 point one
|
2017-03-31 10:17:14 +02:00
|
|
|
///
|
2016-05-26 23:44:27 +02:00
|
|
|
/// @param p2 point tow
|
|
|
|
///
|
|
|
|
/// @return the cross product between p1 and p2
|
|
|
|
///
|
2017-03-31 10:17:14 +02:00
|
|
|
static float crossProduct(CiColor p1, CiColor p2);
|
2016-05-26 23:44:27 +02:00
|
|
|
|
|
|
|
///
|
|
|
|
/// @param a reference point one
|
2017-03-31 10:17:14 +02:00
|
|
|
///
|
2016-05-26 23:44:27 +02:00
|
|
|
/// @param b reference point two
|
2017-03-31 10:17:14 +02:00
|
|
|
///
|
2016-05-26 23:44:27 +02:00
|
|
|
/// @param p the point to which the closest point is to be found
|
|
|
|
///
|
|
|
|
/// @return the closest color point of p to a and b
|
|
|
|
///
|
2017-03-31 10:17:14 +02:00
|
|
|
static CiColor getClosestPointToPoint(CiColor a, CiColor b, CiColor p);
|
2016-05-26 23:44:27 +02:00
|
|
|
|
|
|
|
///
|
|
|
|
/// @param p1 point one
|
2017-03-31 10:17:14 +02:00
|
|
|
///
|
2016-05-26 23:44:27 +02:00
|
|
|
/// @param p2 point tow
|
|
|
|
///
|
|
|
|
/// @return the distance between the two points
|
|
|
|
///
|
2017-03-31 10:17:14 +02:00
|
|
|
static float getDistanceBetweenTwoPoints(CiColor p1, CiColor p2);
|
|
|
|
};
|
|
|
|
|
|
|
|
bool operator==(CiColor p1, CiColor p2);
|
|
|
|
bool operator!=(CiColor p1, CiColor p2);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Color triangle to define an available color space for the hue lamps.
|
|
|
|
*/
|
|
|
|
struct CiColorTriangle
|
|
|
|
{
|
|
|
|
CiColor red, green, blue;
|
|
|
|
};
|
|
|
|
|
2017-09-16 00:18:17 +02:00
|
|
|
class PhilipsHueBridge : public QObject
|
2017-03-31 10:17:14 +02:00
|
|
|
{
|
2017-09-16 00:18:17 +02:00
|
|
|
Q_OBJECT
|
|
|
|
|
2017-03-31 10:17:14 +02:00
|
|
|
private:
|
|
|
|
Logger* log;
|
2017-09-16 00:18:17 +02:00
|
|
|
/// QNetworkAccessManager for sending requests.
|
|
|
|
QNetworkAccessManager manager;
|
2017-03-31 10:17:14 +02:00
|
|
|
/// Ip address of the bridge
|
|
|
|
QString host;
|
|
|
|
/// User name for the API ("newdeveloper")
|
|
|
|
QString username;
|
2017-09-16 00:18:17 +02:00
|
|
|
/// Timer for bridge reconnect interval
|
|
|
|
QTimer bTimer;
|
2017-03-31 10:17:14 +02:00
|
|
|
|
2017-09-16 00:18:17 +02:00
|
|
|
private slots:
|
|
|
|
///
|
|
|
|
/// Receive all replies and check for error, schedule reconnect on issues
|
|
|
|
/// Emits newLights() on success when triggered from connect()
|
|
|
|
///
|
|
|
|
void resolveReply(QNetworkReply* reply);
|
2017-03-31 10:17:14 +02:00
|
|
|
|
2017-09-16 00:18:17 +02:00
|
|
|
public slots:
|
2017-03-31 10:17:14 +02:00
|
|
|
///
|
2017-09-16 00:18:17 +02:00
|
|
|
/// Connect to bridge to check availbility and user
|
2017-03-31 10:17:14 +02:00
|
|
|
///
|
2017-09-16 00:18:17 +02:00
|
|
|
void bConnect(void);
|
|
|
|
|
|
|
|
signals:
|
|
|
|
///
|
|
|
|
/// Emits with a QMap of current bridge light/value pairs
|
2017-03-31 10:17:14 +02:00
|
|
|
///
|
2017-09-16 00:18:17 +02:00
|
|
|
void newLights(QMap<quint16,QJsonObject> map);
|
|
|
|
|
|
|
|
public:
|
|
|
|
PhilipsHueBridge(Logger* log, QString host, QString username);
|
2017-03-31 10:17:14 +02:00
|
|
|
|
|
|
|
///
|
|
|
|
/// @param route the route of the POST request.
|
|
|
|
///
|
|
|
|
/// @param content the content of the POST request.
|
|
|
|
///
|
|
|
|
void post(QString route, QString content);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simple class to hold the id, the latest color, the color space and the original state.
|
|
|
|
*/
|
|
|
|
class PhilipsHueLight
|
|
|
|
{
|
|
|
|
private:
|
2017-09-16 00:18:17 +02:00
|
|
|
Logger* log;
|
2019-01-01 19:47:07 +01:00
|
|
|
PhilipsHueBridge* bridge;
|
2017-09-16 00:18:17 +02:00
|
|
|
/// light id
|
2017-03-31 10:17:14 +02:00
|
|
|
unsigned int id;
|
|
|
|
bool on;
|
|
|
|
unsigned int transitionTime;
|
|
|
|
CiColor color;
|
|
|
|
/// The model id of the hue lamp which is used to determine the color space.
|
|
|
|
QString modelId;
|
|
|
|
CiColorTriangle colorSpace;
|
|
|
|
/// The json string of the original state.
|
|
|
|
QString originalState;
|
|
|
|
|
|
|
|
///
|
|
|
|
/// @param state the state as json object to set
|
|
|
|
///
|
|
|
|
void set(QString state);
|
|
|
|
|
|
|
|
public:
|
|
|
|
// Hue system model ids (http://www.developers.meethue.com/documentation/supported-lights).
|
|
|
|
// Light strips, color iris, ...
|
|
|
|
static const std::set<QString> GAMUT_A_MODEL_IDS;
|
|
|
|
// Hue bulbs, spots, ...
|
|
|
|
static const std::set<QString> GAMUT_B_MODEL_IDS;
|
|
|
|
// Hue Lightstrip plus, go ...
|
|
|
|
static const std::set<QString> GAMUT_C_MODEL_IDS;
|
|
|
|
|
|
|
|
///
|
|
|
|
/// Constructs the light.
|
|
|
|
///
|
|
|
|
/// @param log the logger
|
|
|
|
/// @param bridge the bridge
|
|
|
|
/// @param id the light id
|
|
|
|
///
|
2019-01-01 19:47:07 +01:00
|
|
|
PhilipsHueLight(Logger* log, PhilipsHueBridge* bridge, unsigned int id, QJsonObject values);
|
2017-03-31 10:17:14 +02:00
|
|
|
~PhilipsHueLight();
|
|
|
|
|
|
|
|
///
|
|
|
|
/// @param on
|
|
|
|
///
|
|
|
|
void setOn(bool on);
|
|
|
|
|
|
|
|
///
|
|
|
|
/// @param transitionTime the transition time between colors in multiples of 100 ms
|
|
|
|
///
|
|
|
|
void setTransitionTime(unsigned int transitionTime);
|
|
|
|
|
|
|
|
///
|
|
|
|
/// @param color the color to set
|
|
|
|
/// @param brightnessFactor the factor to apply to the CiColor#bri value
|
|
|
|
///
|
|
|
|
void setColor(CiColor color, float brightnessFactor = 1.0f);
|
|
|
|
CiColor getColor() const;
|
|
|
|
|
|
|
|
///
|
|
|
|
/// @return the color space of the light determined by the model id reported by the bridge.
|
|
|
|
CiColorTriangle getColorSpace() const;
|
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implementation for the Philips Hue system.
|
|
|
|
*
|
|
|
|
* To use set the device to "philipshue".
|
|
|
|
* Uses the official Philips Hue API (http://developers.meethue.com).
|
|
|
|
*
|
|
|
|
* @author ntim (github), bimsarck (github)
|
|
|
|
*/
|
2017-03-31 10:17:14 +02:00
|
|
|
class LedDevicePhilipsHue: public LedDevice
|
|
|
|
{
|
2016-08-14 10:46:44 +02:00
|
|
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
public:
|
|
|
|
///
|
2016-08-23 20:07:12 +02:00
|
|
|
/// Constructs specific LedDevice
|
2016-05-26 23:44:27 +02:00
|
|
|
///
|
2016-08-23 20:07:12 +02:00
|
|
|
/// @param deviceConfig json device config
|
2016-05-26 23:44:27 +02:00
|
|
|
///
|
2016-10-13 21:59:58 +02:00
|
|
|
LedDevicePhilipsHue(const QJsonObject &deviceConfig);
|
2016-05-26 23:44:27 +02:00
|
|
|
|
|
|
|
///
|
|
|
|
/// Destructor of this device
|
|
|
|
///
|
|
|
|
virtual ~LedDevicePhilipsHue();
|
|
|
|
|
2016-08-23 20:07:12 +02:00
|
|
|
/// constructs leddevice
|
2016-10-13 21:59:58 +02:00
|
|
|
static LedDevice* construct(const QJsonObject &deviceConfig);
|
2017-03-31 10:17:14 +02:00
|
|
|
|
2019-01-01 19:47:07 +01:00
|
|
|
public slots:
|
|
|
|
/// thread start
|
|
|
|
virtual void start();
|
|
|
|
|
2016-09-23 08:49:22 +02:00
|
|
|
private slots:
|
2017-09-16 00:18:17 +02:00
|
|
|
/// creates new PhilipsHueLight(s) based on user lightid with bridge feedback
|
|
|
|
///
|
|
|
|
/// @param map Map of lightid/value pairs of bridge
|
|
|
|
///
|
|
|
|
void newLights(QMap<quint16, QJsonObject> map);
|
|
|
|
|
|
|
|
void stateChanged(bool newState);
|
2016-09-23 08:49:22 +02:00
|
|
|
|
2017-03-31 10:17:14 +02:00
|
|
|
protected:
|
2016-05-26 23:44:27 +02:00
|
|
|
///
|
2017-03-31 10:17:14 +02:00
|
|
|
/// Writes the RGB-Color values to the leds.
|
2016-05-26 23:44:27 +02:00
|
|
|
///
|
2017-03-31 10:17:14 +02:00
|
|
|
/// @param[in] ledValues The RGB-color per led
|
2016-05-26 23:44:27 +02:00
|
|
|
///
|
|
|
|
/// @return Zero on success else negative
|
|
|
|
///
|
|
|
|
virtual int write(const std::vector<ColorRgb> & ledValues);
|
2017-03-31 10:17:14 +02:00
|
|
|
bool init(const QJsonObject &deviceConfig);
|
2016-05-26 23:44:27 +02:00
|
|
|
|
2017-03-31 10:17:14 +02:00
|
|
|
private:
|
2017-09-16 00:18:17 +02:00
|
|
|
/// bridge class
|
2019-01-01 19:47:07 +01:00
|
|
|
PhilipsHueBridge* _bridge;
|
2017-09-16 00:18:17 +02:00
|
|
|
|
2016-05-26 23:44:27 +02:00
|
|
|
///
|
|
|
|
bool switchOffOnBlack;
|
2017-03-31 10:17:14 +02:00
|
|
|
/// The brightness factor to multiply on color change.
|
|
|
|
float brightnessFactor;
|
2016-05-26 23:44:27 +02:00
|
|
|
/// Transition time in multiples of 100 ms.
|
2017-03-31 10:17:14 +02:00
|
|
|
/// The default of the Hue lights is 400 ms, but we may want it snapier.
|
|
|
|
int transitionTime;
|
2016-05-26 23:44:27 +02:00
|
|
|
/// Array of the light ids.
|
|
|
|
std::vector<unsigned int> lightIds;
|
2017-03-31 10:17:14 +02:00
|
|
|
/// Array to save the lamps.
|
|
|
|
std::vector<PhilipsHueLight> lights;
|
2016-05-26 23:44:27 +02:00
|
|
|
};
|