refactor: Align Phillips Hue to reworked device handling (#712)

* Align PhilipsHue (Classic)

* Minor Device corrections

* Have code working with Qt < 5.10

* Fixes on Hue Wizzard

* Fixes on Hue Wizzard

* Calculate Latchtime only for lights updated by hyperion

* Allow to disable restoring original light's state

* Fix - LightIDs / LightMap vectors were not cleared when reopening the device

* Reduce API Calls for state updates by consolidation
This commit is contained in:
LordGrey 2020-03-26 18:49:44 +01:00 committed by GitHub
parent 2739aec1e3
commit aaa4235cab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 1096 additions and 422 deletions

View File

@ -426,6 +426,7 @@
"edt_dev_spec_transistionTime_title": "Übergangszeit",
"edt_dev_spec_switchOffOnBlack_title": "Aus bei schwarz",
"edt_dev_spec_brightnessFactor_title": "Helligkeitsfaktor",
"edt_dev_spec_restoreOriginalState_title" : "Lampen Originalzustand wiederhestellen",
"edt_dev_spec_ledType_title": "LED typ",
"edt_dev_spec_uid_title": "UID",
"edt_dev_spec_intervall_title": "Intervall",

View File

@ -425,6 +425,7 @@
"edt_dev_spec_transistionTime_title" : "Transition time",
"edt_dev_spec_switchOffOnBlack_title" : "Switch off on black",
"edt_dev_spec_brightnessFactor_title" : "Brightness factor",
"edt_dev_spec_restoreOriginalState_title" : "Restore lights' original state",
"edt_dev_spec_ledType_title" : "LED Type",
"edt_dev_spec_uid_title" : "UID",
"edt_dev_spec_intervall_title" : "Interval",

View File

@ -567,23 +567,35 @@ function checkHueBridge(cb,hueUser){
timeout: 2000
})
.done( function( data, textStatus, jqXHR ) {
if(Array.isArray(data) && data[0].error && data[0].error.type == 4)
cb(true);
else if(Array.isArray(data) && data[0].error)
cb(false);
if( Array.isArray(data) && data[0].error)
{
if ( data[0].error.type == 3 || data[0].error.type == 4)
{
cb(true, usr);
}
else
{
cb(false);
}
}
else
cb(true);
{
cb(true, usr);
}
})
.fail( function( jqXHR, textStatus ) {
cb(false);
});
}
function checkUserResult(reply){
function checkUserResult(reply, usr){
if(reply)
{
$('#wiz_hue_usrstate').html("");
$('#wiz_hue_create_user').toggle(false);
$('#user').val(usr);
get_hue_lights();
}
else
@ -640,17 +652,22 @@ function checkBridgeResult(reply){
function identHueId(id, off)
{
var on = true;
if(off !== true)
{
setTimeout(identHueId,1500,id,true);
var put_data = '{"on":true,"bri":254,"hue":47000,"sat":254}';
}
else
on = false;
{
var put_data = '{"on":false}';
}
$.ajax({
url: 'http://'+$('#ip').val()+'/api/'+$('#user').val()+'/lights/'+id+'/state',
type: 'PUT',
timeout: 2000,
data: ' {"on":'+on+', "sat":254, "bri":254,"hue":47000}'
data: put_data
})
}
@ -686,7 +703,9 @@ function beginWizardHue()
//check if ip is empty/reachable/search for bridge
if(conf_editor.getEditor("root.specificOptions.output").getValue() == "")
{
getHueIPs();
}
else
{
var ip = conf_editor.getEditor("root.specificOptions.output").getValue();
@ -719,6 +738,7 @@ function beginWizardHue()
}
}
var ledCount= Object.keys(lightIDs).length;
window.serverConfig.leds = hueLedConfig;
@ -736,6 +756,7 @@ function beginWizardHue()
d.lightIds = finalLightIds;
d.username = $('#user').val();
d.type = "philipshue";
d.hardwareLedCount = ledCount;
d.transitiontime = 1;
d.switchOffOnBlack = true;
@ -814,6 +835,7 @@ function get_hue_lights(){
for(var lightid in r)
{
$('.lidsb').append(createTableRow([lightid+' ('+r[lightid].name+')', '<select id="hue_'+lightid+'" class="hue_sel_watch form-control"><option value="disabled">'+$.i18n('wiz_hue_ids_disabled')+'</option><option value="top">'+$.i18n('conf_leds_layout_cl_top')+'</option><option value="bottom">'+$.i18n('conf_leds_layout_cl_bottom')+'</option><option value="left">'+$.i18n('conf_leds_layout_cl_left')+'</option><option value="right">'+$.i18n('conf_leds_layout_cl_right')+'</option><option value="entire">'+$.i18n('wiz_hue_ids_entire')+'</option></select>','<button class="btn btn-sm btn-primary" onClick=identHueId('+lightid+')>'+$.i18n('wiz_hue_blinkblue',lightid)+'</button>']));
}

View File

@ -59,7 +59,9 @@ public:
unsigned int getLedCount() const { return _ledCount; }
bool enabled() const { return _enabled; }
int getLatchTime() const { return _latchTime_ms; }
void setLatchTime( int latchTime_ms );
///
/// Check, if device is ready to be used

View File

@ -231,6 +231,12 @@ void LedDevice::setLedCount(unsigned int ledCount)
_ledRGBWCount = _ledCount * sizeof(ColorRgbw);
}
void LedDevice::setLatchTime( int latchTime_ms )
{
_latchTime_ms = latchTime_ms;
Debug(_log, "LatchTime updated to %dms", this->getLatchTime());
}
int LedDevice::rewriteLeds()
{
int retval = -1;

View File

@ -37,13 +37,13 @@ static const char STATE_ONOFF_VALUE[] = "value";
static const char STATE_VALUE_TRUE[] = "true";
static const char STATE_VALUE_FALSE[] = "false";
//Device Data elements
// Device Data elements
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
// Nanoleaf Stream Control elements
//static const char STREAM_CONTROL_IP[] = "streamControlIpAddr";
static const char STREAM_CONTROL_PORT[] = "streamControlPort";
//static const char STREAM_CONTROL_PROTOCOL[] = "streamControlProtocol";
@ -59,7 +59,7 @@ static const char API_STATE[] ="state";
static const char API_PANELLAYOUT[] = "panelLayout";
static const char API_EFFECT[] = "effects";
//Nanoleaf ssdp services
// Nanoleaf ssdp services
static const char SSDP_CANVAS[] = "nanoleaf:nl29";
static const char SSDP_LIGHTPANELS[] = "nanoleaf_aurora:light";
const int SSDP_TIMEOUT = 5000; // timout in ms
@ -132,7 +132,7 @@ bool LedDeviceNanoleaf::init(const QJsonObject &deviceConfig)
if ( _hostname.isEmpty() )
{
//Discover Nanoleaf device
if ( !discoverNanoleafDevice() )
if ( !discoverDevice() )
{
this->setInError("No target IP defined nor Nanoleaf device was discovered");
return false;
@ -256,24 +256,17 @@ int LedDeviceNanoleaf::open()
if ( init(_devConfig) )
{
if ( !initNetwork() )
if ( initLeds() )
{
this->setInError( "UDP Network error!" );
}
else
{
if ( initLeds() )
{
_deviceReady = true;
setEnable(true);
retval = 0;
}
_deviceReady = true;
setEnable(true);
retval = 0;
}
}
return retval;
}
bool LedDeviceNanoleaf::discoverNanoleafDevice()
bool LedDeviceNanoleaf::discoverDevice()
{
bool isDeviceFound (false);

View File

@ -103,7 +103,7 @@ private:
///
/// @return True, if Nanoleaf device was found
///
bool discoverNanoleafDevice();
bool discoverDevice();
///
/// Change Nanoleaf device to External Control (UDP) mode

File diff suppressed because it is too large Load Diff

View File

@ -19,11 +19,11 @@ struct CiColorTriangle;
struct CiColor
{
/// X component.
float x;
double x;
/// Y component.
float y;
double y;
/// The brightness.
float bri;
double bri;
///
/// Converts an RGB color to the Hue xy color space and brightness.
@ -37,7 +37,7 @@ struct CiColor
///
/// @return color point
///
static CiColor rgbToCiColor(float red, float green, float blue, CiColorTriangle colorSpace);
static CiColor rgbToCiColor(double red, double green, double blue, CiColorTriangle colorSpace);
///
/// @param p the color point to check
@ -53,7 +53,7 @@ struct CiColor
///
/// @return the cross product between p1 and p2
///
static float crossProduct(CiColor p1, CiColor p2);
static double crossProduct(CiColor p1, CiColor p2);
///
/// @param a reference point one
@ -73,11 +73,11 @@ struct CiColor
///
/// @return the distance between the two points
///
static float getDistanceBetweenTwoPoints(CiColor p1, CiColor p2);
static double getDistanceBetweenTwoPoints(CiColor p1, CiColor p2);
};
bool operator==(CiColor p1, CiColor p2);
bool operator!=(CiColor p1, CiColor p2);
bool operator==(const CiColor& p1, const CiColor& p2);
bool operator!=(const CiColor& p1, const CiColor& p2);
/**
* Color triangle to define an available color space for the hue lamps.
@ -87,74 +87,11 @@ struct CiColorTriangle
CiColor red, green, blue;
};
class PhilipsHueBridge : public QObject
{
Q_OBJECT
private:
Logger* _log;
/// QNetworkAccessManager for sending requests.
QNetworkAccessManager manager;
/// Ip address of the bridge
QString host;
/// User name for the API ("newdeveloper")
QString username;
/// Timer for bridge reconnect interval
QTimer bTimer;
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);
public slots:
///
/// Connect to bridge to check availbility and user
///
void bConnect(void);
signals:
///
/// Emits with a QMap of current bridge light/value pairs
///
void newLights(QMap<quint16,QJsonObject> map);
public:
PhilipsHueBridge(Logger* log, QString host, QString username);
///
/// @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:
Logger* _log;
PhilipsHueBridge* bridge;
/// light id
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).
@ -172,32 +109,183 @@ public:
/// @param bridge the bridge
/// @param id the light id
///
PhilipsHueLight(Logger* log, PhilipsHueBridge* bridge, unsigned int id, QJsonObject values);
PhilipsHueLight(Logger* log, unsigned int id, QJsonObject values, unsigned int ledidx);
~PhilipsHueLight();
///
/// @param on
///
void setOn(bool on);
void setOnOffState(bool on);
///
/// @param transitionTime the transition time between colors in multiples of 100 ms
///
void setTransitionTime(unsigned int transitionTime);
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);
void setColor(const CiColor& _color);
unsigned int getId() const;
bool getOnOffState() const;
unsigned int getTransitionTime() const;
CiColor getColor() const;
///
/// @return the color space of the light determined by the model id reported by the bridge.
CiColorTriangle getColorSpace() const;
QString getOriginalState();
private:
void saveOriginalState(const QJsonObject& values);
Logger* _log;
/// light id
unsigned int _id;
unsigned int _ledidx;
bool _on;
unsigned int _transitionTime;
CiColor _color;
/// darkes blue color in hue lamp GAMUT = black
CiColor _colorBlack;
/// The model id of the hue lamp which is used to determine the color space.
QString _modelId;
QString _lightname;
CiColorTriangle _colorSpace;
/// The json string of the original state.
QJsonObject _originalStateJSON;
QString _originalState;
CiColor _originalColor;
};
class LedDevicePhilipsHueBridge : public LedDevice
{
Q_OBJECT
public:
explicit LedDevicePhilipsHueBridge(const QJsonObject &deviceConfig);
~LedDevicePhilipsHueBridge();
///
/// Sets configuration
///
/// @param deviceConfig the json device config
/// @return true if success
virtual bool init(const QJsonObject &deviceConfig) override;
///
/// @param route the route of the POST request.
///
/// @param content the content of the POST request.
///
void post(const QString& route, const QString& content);
void setLightState(unsigned int lightId = 0, QString state = "");
const QMap<quint16,QJsonObject>& getLightMap();
// /// Set device in error state
// ///
// /// @param errorMsg The error message to be logged
// ///
// virtual void setInError( const QString& errorMsg) override;
public slots:
///
/// Connect to bridge to check availbility and user
///
virtual int open(void) override;
virtual int open( const QString& hostname, const QString& port, const QString& username );
//signals:
// ///
// /// Emits with a QMap of current bridge light/value pairs
// ///
// void newLights(QMap<quint16,QJsonObject> map);
protected:
/// Ip address of the bridge
QString _hostname;
QString _api_port;
/// User name for the API ("newdeveloper")
QString _username;
private:
///
/// Discover device via SSDP identifiers
///
/// @return True, if device was found
///
bool discoverDevice();
///
/// Get command as url
///
/// @param host Hostname or IP
/// @param port IP-Port
/// @param _auth_token Authorization token
/// @param Endpoint command for request
/// @return Url to execute endpoint/command
///
QString getUrl(QString host, QString port, QString auth_token, QString endpoint) const;
///
/// Execute GET request
///
/// @param url GET request for url
/// @return Response from device
///
QJsonDocument getJson(QString url);
///
/// Execute PUT request
///
/// @param Url for PUT request
/// @param json Command for request
/// @return Response from device
///
QJsonDocument putJson(QString url, QString json);
///
/// Handle replys for GET and PUT requests
///
/// @param reply Network reply
/// @return Response for request, if no error
///
QJsonDocument handleReply(QNetworkReply* const &reply );
/// QNetworkAccessManager for sending requests.
QNetworkAccessManager* _networkmanager;
//Philips Hue Bridge details
QString _deviceModel;
QString _deviceFirmwareVersion;
QString _deviceAPIVersion;
uint _api_major;
uint _api_minor;
uint _api_patch;
bool _isHueEntertainmentReady;
QMap<quint16,QJsonObject> _lightsMap;
};
/**
* Implementation for the Philips Hue system.
*
@ -206,7 +294,7 @@ public:
*
* @author ntim (github), bimsarck (github)
*/
class LedDevicePhilipsHue: public LedDevice
class LedDevicePhilipsHue: public LedDevicePhilipsHueBridge
{
Q_OBJECT
@ -227,20 +315,66 @@ public:
/// constructs leddevice
static LedDevice* construct(const QJsonObject &deviceConfig);
public slots:
/// thread start
virtual void start() override;
///
/// Sets configuration
///
/// @param deviceConfig the json device config
/// @return true if success
virtual bool init(const QJsonObject &deviceConfig) override;
/// Switch the device on
virtual int switchOn() override;
/// Switch the device off
virtual int switchOff() override;
private slots:
/// 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);
unsigned int getLightsCount() const { return _lightsCount; }
void setLightsCount( unsigned int lightsCount);
void setOnOffState(PhilipsHueLight& light, bool on);
void setTransitionTime(PhilipsHueLight& light, unsigned int transitionTime);
void setColor(PhilipsHueLight& light, const CiColor& color, double brightnessFactor);
void setState(PhilipsHueLight& light, bool on, const CiColor& color, double brightnessFactor, unsigned int transitionTime);
void restoreOriginalState();
public slots:
///
/// Closes the output device.
/// Includes switching-off the device and stopping refreshes
///
virtual void close() override;
private slots:
/// creates new PhilipsHueLight(s) based on user lightid with bridge feedback
///
/// @param map Map of lightid/value pairs of bridge
///
void updateLights(QMap<quint16, QJsonObject> map);
protected:
///
/// Opens and initiatialises the output device
///
/// @return Zero on succes (i.e. device is ready and enabled) else negative
///
virtual int open() override;
///
/// Get Philips Hue device details and configuration
///
/// @return True, if Nanoleaf device capabilities fit configuration
///
bool initLeds();
///
/// Writes the RGB-Color values to the leds.
///
@ -249,21 +383,27 @@ protected:
/// @return Zero on success else negative
///
virtual int write(const std::vector<ColorRgb> & ledValues) override;
bool init(const QJsonObject &deviceConfig) override;
private:
/// bridge class
PhilipsHueBridge* _bridge;
int writeSingleLights(const std::vector<ColorRgb>& ledValues);
///
bool switchOffOnBlack;
bool _switchOffOnBlack;
/// The brightness factor to multiply on color change.
float brightnessFactor;
/// Transition time in multiples of 100 ms.
double _brightnessFactor;
/// Transition time in multiples of 100 ms.
/// The default of the Hue lights is 400 ms, but we may want it snapier.
int transitionTime;
unsigned int _transitionTime;
bool _isRestoreOrigState;
/// Array of the light ids.
std::vector<unsigned int> lightIds;
std::vector<unsigned int> _lightIds;
/// Array to save the lamps.
std::vector<PhilipsHueLight> lights;
std::vector<PhilipsHueLight> _lights;
unsigned int _lightsCount;
};

View File

@ -93,7 +93,7 @@ int ProviderSpi::open()
}
if ( retval < 0 )
{
errortext = QString ("Failed to open device (%1). Error Code: %2").arg(_deviceName, retval);
errortext = QString ("Failed to open device (%1). Error Code: %2").arg(_deviceName).arg(retval);
}
}

View File

@ -35,6 +35,13 @@
"maximum" : 10.0,
"propertyOrder" : 5
},
"restoreOriginalState": {
"type": "boolean",
"title":"edt_dev_spec_restoreOriginalState_title",
"default" : true,
"propertyOrder" : 6
},
"lightIds": {
"type": "array",
"title":"edt_dev_spec_lightid_title",
@ -45,7 +52,7 @@
"minimum" : 0,
"title" : "edt_dev_spec_lightid_itemtitle"
},
"propertyOrder" : 6
"propertyOrder" : 7
}
},
"additionalProperties": true