finalized hue plugin
This commit is contained in:
parent
fbfbd5743c
commit
55298f639e
@ -95,7 +95,7 @@ DeviceManager::DeviceSetupStatus DevicePluginPhilipsHue::setupDevice(Device *dev
|
||||
if (device->deviceClassId() == hueBridgeDeviceClassId) {
|
||||
// unconfigured bridges (from pairing)
|
||||
foreach (HueBridge *b, m_unconfiguredBridges) {
|
||||
if (b->username() == device->paramValue("username").toString()) {
|
||||
if (b->apiKey() == device->paramValue("api key").toString()) {
|
||||
m_unconfiguredBridges.removeOne(b);
|
||||
|
||||
// set data which was not known during discovery
|
||||
@ -112,7 +112,7 @@ DeviceManager::DeviceSetupStatus DevicePluginPhilipsHue::setupDevice(Device *dev
|
||||
DeviceDescriptor descriptor(hueLightDeviceClassId, "Philips Hue Light", light->name());
|
||||
ParamList params;
|
||||
params.append(Param("name", light->name()));
|
||||
params.append(Param("username", light->username()));
|
||||
params.append(Param("api key", light->apiKey()));
|
||||
params.append(Param("bridge", device->id().toString()));
|
||||
params.append(Param("host address", light->hostAddress().toString()));
|
||||
params.append(Param("model id", light->modelId()));
|
||||
@ -127,7 +127,7 @@ DeviceManager::DeviceSetupStatus DevicePluginPhilipsHue::setupDevice(Device *dev
|
||||
}
|
||||
|
||||
// loaded bridge
|
||||
HueBridge *bridge = new HueBridge(device->paramValue("username").toString(),
|
||||
HueBridge *bridge = new HueBridge(device->paramValue("api key").toString(),
|
||||
QHostAddress(device->paramValue("host address").toString()));
|
||||
|
||||
bridge->setApiVersion(device->paramValue("api version").toString());
|
||||
@ -146,7 +146,7 @@ DeviceManager::DeviceSetupStatus DevicePluginPhilipsHue::setupDevice(Device *dev
|
||||
|
||||
// check if this is a unconfigured light
|
||||
for (int i = 0; i < m_unconfiguredLights.count(); i++) {
|
||||
if (m_unconfiguredLights.at(i)->username() == device->paramValue("username").toString()) {
|
||||
if (m_unconfiguredLights.at(i)->apiKey() == device->paramValue("api key").toString()) {
|
||||
hueLight = m_unconfiguredLights.takeAt(i);
|
||||
break;
|
||||
}
|
||||
@ -157,7 +157,7 @@ DeviceManager::DeviceSetupStatus DevicePluginPhilipsHue::setupDevice(Device *dev
|
||||
hueLight = new HueLight(device->paramValue("light id").toInt(),
|
||||
QHostAddress(device->paramValue("host address").toString()),
|
||||
device->paramValue("name").toString(),
|
||||
device->paramValue("username").toString(),
|
||||
device->paramValue("api key").toString(),
|
||||
device->paramValue("model id").toString(),
|
||||
DeviceId(device->paramValue("bridge").toString()),
|
||||
this);
|
||||
@ -165,9 +165,8 @@ DeviceManager::DeviceSetupStatus DevicePluginPhilipsHue::setupDevice(Device *dev
|
||||
connect(hueLight, &HueLight::stateChanged, this, &DevicePluginPhilipsHue::lightStateChanged);
|
||||
}
|
||||
|
||||
device->setName(hueLight->name());
|
||||
m_lights.insert(hueLight, device);
|
||||
setName(device, device->paramValue("name").toString());
|
||||
setLightName(device, device->paramValue("name").toString());
|
||||
}
|
||||
|
||||
return DeviceManager::DeviceSetupStatusAsync;
|
||||
@ -199,7 +198,7 @@ void DevicePluginPhilipsHue::upnpDiscoveryFinished(const QList<UpnpDeviceDescrip
|
||||
ParamList params;
|
||||
params.append(Param("name", QString()));
|
||||
params.append(Param("host address", upnpDevice.hostAddress().toString()));
|
||||
params.append(Param("username", "guh-" + QUuid::createUuid().toString().remove(QRegExp("[\\{\\}]*")).remove(QRegExp("\\-[0-9a-f\\-]*"))));
|
||||
params.append(Param("api key", "guh-" + QUuid::createUuid().toString().remove(QRegExp("[\\{\\}]*")).remove(QRegExp("\\-[0-9a-f\\-]*"))));
|
||||
params.append(Param("mac address", QString()));
|
||||
params.append(Param("api version", QString()));
|
||||
params.append(Param("zigbee channel", -1));
|
||||
@ -220,11 +219,11 @@ DeviceManager::DeviceSetupStatus DevicePluginPhilipsHue::confirmPairing(const Pa
|
||||
PairingInfo pairingInfo;
|
||||
pairingInfo.pairingTransactionId = pairingTransactionId;
|
||||
pairingInfo.host = QHostAddress(params.paramValue("host address").toString());
|
||||
pairingInfo.username = params.paramValue("username").toString();
|
||||
pairingInfo.apiKey = params.paramValue("api key").toString();
|
||||
|
||||
QVariantMap createUserParams;
|
||||
createUserParams.insert("devicetype", "guh");
|
||||
createUserParams.insert("username", pairingInfo.username);
|
||||
createUserParams.insert("username", pairingInfo.apiKey);
|
||||
|
||||
QJsonDocument jsonDoc = QJsonDocument::fromVariant(createUserParams);
|
||||
|
||||
@ -271,6 +270,7 @@ void DevicePluginPhilipsHue::networkManagerReplyReady(QNetworkReply *reply)
|
||||
// check HTTP status code
|
||||
if (status != 200) {
|
||||
qWarning() << "Refresh Hue Light request error:" << status << reply->errorString();
|
||||
onBridgeError(device);
|
||||
reply->deleteLater();
|
||||
return;
|
||||
}
|
||||
@ -283,6 +283,7 @@ void DevicePluginPhilipsHue::networkManagerReplyReady(QNetworkReply *reply)
|
||||
// check HTTP status code
|
||||
if (status != 200) {
|
||||
qWarning() << "Refresh Hue Bridge request error:" << status << reply->errorString();
|
||||
onBridgeError(device);
|
||||
reply->deleteLater();
|
||||
return;
|
||||
}
|
||||
@ -295,6 +296,8 @@ void DevicePluginPhilipsHue::networkManagerReplyReady(QNetworkReply *reply)
|
||||
// check HTTP status code
|
||||
if (status != 200) {
|
||||
qWarning() << "Refresh Hue Light request error:" << status << reply->errorString();
|
||||
onBridgeError(actionInfo.first);
|
||||
emit actionExecutionFinished(actionInfo.second, DeviceManager::DeviceErrorHardwareNotAvailable);
|
||||
reply->deleteLater();
|
||||
return;
|
||||
}
|
||||
@ -382,14 +385,13 @@ void DevicePluginPhilipsHue::lightStateChanged()
|
||||
device->setStateValue(hueBrightnessStateTypeId, brightnessToPercentage(light->brightness()));
|
||||
device->setStateValue(hueTemperatureStateTypeId, light->ct());
|
||||
device->setStateValue(hueEffectStateTypeId, light->effect());
|
||||
device->setStateValue(hueAlertStateTypeId, light->alert());
|
||||
}
|
||||
|
||||
void DevicePluginPhilipsHue::refreshLight(Device *device)
|
||||
{
|
||||
HueLight *light = m_lights.key(device);
|
||||
|
||||
QNetworkRequest request(QUrl("http://" + light->hostAddress().toString() + "/api/" + light->username() + "/lights/" + QString::number(light->lightId())));
|
||||
QNetworkRequest request(QUrl("http://" + light->hostAddress().toString() + "/api/" + light->apiKey() + "/lights/" + QString::number(light->lightId())));
|
||||
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
|
||||
QNetworkReply *reply = networkManagerGet(request);
|
||||
|
||||
@ -400,41 +402,27 @@ void DevicePluginPhilipsHue::refreshBridge(Device *device)
|
||||
{
|
||||
HueBridge *bridge = m_bridges.key(device);
|
||||
|
||||
QNetworkRequest request(QUrl("http://" + bridge->hostAddress().toString() + "/api/" + bridge->username() + "/lights/"));
|
||||
QNetworkRequest request(QUrl("http://" + bridge->hostAddress().toString() + "/api/" + bridge->apiKey() + "/lights/"));
|
||||
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
|
||||
QNetworkReply *reply = networkManagerGet(request);
|
||||
|
||||
m_bridgeRefreshRequests.insert(reply, device);
|
||||
}
|
||||
|
||||
void DevicePluginPhilipsHue::setName(Device *device, QString name)
|
||||
void DevicePluginPhilipsHue::setLightName(Device *device, QString name)
|
||||
{
|
||||
if (device->deviceClassId() == hueLightDeviceClassId) {
|
||||
HueLight *light = m_lights.key(device);
|
||||
HueLight *light = m_lights.key(device);
|
||||
|
||||
QVariantMap requestMap;
|
||||
requestMap.insert("name", name);
|
||||
QJsonDocument jsonDoc = QJsonDocument::fromVariant(requestMap);
|
||||
QVariantMap requestMap;
|
||||
requestMap.insert("name", name);
|
||||
QJsonDocument jsonDoc = QJsonDocument::fromVariant(requestMap);
|
||||
|
||||
QNetworkRequest request(QUrl("http://" + light->hostAddress().toString() + "/api/" + light->username() +
|
||||
"/lights/" + QString::number(light->lightId())));
|
||||
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
|
||||
QNetworkRequest request(QUrl("http://" + light->hostAddress().toString() + "/api/" + light->apiKey() +
|
||||
"/lights/" + QString::number(light->lightId())));
|
||||
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
|
||||
|
||||
QNetworkReply *reply = networkManagerPut(request,jsonDoc.toJson());
|
||||
m_lightSetNameRequests.insert(reply, device);
|
||||
} else if (device->deviceClassId() == hueBridgeDeviceClassId) {
|
||||
HueBridge *bridge = m_bridges.key(device);
|
||||
|
||||
QVariantMap requestMap;
|
||||
requestMap.insert("name", name);
|
||||
QJsonDocument jsonDoc = QJsonDocument::fromVariant(requestMap);
|
||||
|
||||
QNetworkRequest request(QUrl("http://" + bridge->hostAddress().toString() + "/api/" + bridge->username() + "/config"));
|
||||
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
|
||||
|
||||
QNetworkReply *reply = networkManagerPut(request,jsonDoc.toJson());
|
||||
m_lightSetNameRequests.insert(reply, device);
|
||||
}
|
||||
QNetworkReply *reply = networkManagerPut(request,jsonDoc.toJson());
|
||||
m_lightSetNameRequests.insert(reply, device);
|
||||
}
|
||||
|
||||
void DevicePluginPhilipsHue::processLightRefreshResponse(Device *device, const QByteArray &data)
|
||||
@ -448,14 +436,14 @@ void DevicePluginPhilipsHue::processLightRefreshResponse(Device *device, const Q
|
||||
return;
|
||||
}
|
||||
|
||||
// check pairing error
|
||||
// check response error
|
||||
if (data.contains("error")) {
|
||||
qWarning() << "Failed to refresh Hue Light:" << jsonDoc.toVariant().toList().first().toMap().value("error").toMap().value("description").toString();
|
||||
return;
|
||||
}
|
||||
|
||||
HueLight *hueLight = m_lights.key(device);
|
||||
hueLight->setStates(jsonDoc.toVariant().toMap().value("state").toMap());
|
||||
hueLight->updateStates(jsonDoc.toVariant().toMap().value("state").toMap());
|
||||
}
|
||||
|
||||
void DevicePluginPhilipsHue::processBridgeRefreshResponse(Device *device, const QByteArray &data)
|
||||
@ -469,19 +457,22 @@ void DevicePluginPhilipsHue::processBridgeRefreshResponse(Device *device, const
|
||||
return;
|
||||
}
|
||||
|
||||
// check pairing error
|
||||
// check response error
|
||||
if (data.contains("error")) {
|
||||
qWarning() << "Failed to refresh Hue Bridge:" << jsonDoc.toVariant().toList().first().toMap().value("error").toMap().value("description").toString();
|
||||
return;
|
||||
}
|
||||
|
||||
// mark bridge as reachable
|
||||
device->setStateValue(bridgeReachableStateTypeId, true);
|
||||
|
||||
QVariantMap lightsMap = jsonDoc.toVariant().toMap();
|
||||
foreach (const QString &lightId, lightsMap.keys()) {
|
||||
QVariantMap lightMap = lightsMap.value(lightId).toMap();
|
||||
// get the light of this bridge
|
||||
foreach (HueLight *light, m_lights.keys()) {
|
||||
if (light->lightId() == lightId.toInt() && light->bridgeId() == device->id()) {
|
||||
light->setStates(lightMap.value("state").toMap());
|
||||
light->updateStates(lightMap.value("state").toMap());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -499,7 +490,7 @@ void DevicePluginPhilipsHue::processSetNameResponse(Device *device, const QByteA
|
||||
return;
|
||||
}
|
||||
|
||||
// check pairing error
|
||||
// check response error
|
||||
if (data.contains("error")) {
|
||||
qWarning() << "Failed to set name of Hue:" << jsonDoc.toVariant().toList().first().toMap().value("error").toMap().value("description").toString();
|
||||
emit deviceSetupFinished(device, DeviceManager::DeviceSetupStatusFailure);
|
||||
@ -507,6 +498,7 @@ void DevicePluginPhilipsHue::processSetNameResponse(Device *device, const QByteA
|
||||
}
|
||||
|
||||
emit deviceSetupFinished(device, DeviceManager::DeviceSetupStatusSuccess);
|
||||
|
||||
if (device->deviceClassId() == hueLightDeviceClassId) {
|
||||
refreshLight(device);
|
||||
}
|
||||
@ -524,7 +516,7 @@ void DevicePluginPhilipsHue::processPairingResponse(const DevicePluginPhilipsHue
|
||||
return;
|
||||
}
|
||||
|
||||
// check pairing error
|
||||
// check response error
|
||||
if (data.contains("error")) {
|
||||
qWarning() << "Failed to pair Hue Bridge:" << jsonDoc.toVariant().toList().first().toMap().value("error").toMap().value("description").toString();
|
||||
emit pairingFinished(pairingInfo.pairingTransactionId, DeviceManager::DeviceSetupStatusFailure);
|
||||
@ -532,7 +524,7 @@ void DevicePluginPhilipsHue::processPairingResponse(const DevicePluginPhilipsHue
|
||||
}
|
||||
|
||||
// Paired successfully, check bridge/light information
|
||||
QNetworkRequest request(QUrl("http://" + pairingInfo.host.toString() + "/api/" + pairingInfo.username + ""));
|
||||
QNetworkRequest request(QUrl("http://" + pairingInfo.host.toString() + "/api/" + pairingInfo.apiKey + ""));
|
||||
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
|
||||
QNetworkReply *reply = networkManagerGet(request);
|
||||
|
||||
@ -553,7 +545,7 @@ void DevicePluginPhilipsHue::processInformationResponse(const DevicePluginPhilip
|
||||
|
||||
QVariantMap response = jsonDoc.toVariant().toMap();
|
||||
|
||||
// check json error
|
||||
// check response error
|
||||
if (response.contains("error")) {
|
||||
qWarning() << "Failed to get information from Hue Bridge:" << response.value("error").toMap().value("description").toString();
|
||||
emit pairingFinished(pairingInfo.pairingTransactionId, DeviceManager::DeviceSetupStatusFailure);
|
||||
@ -561,7 +553,7 @@ void DevicePluginPhilipsHue::processInformationResponse(const DevicePluginPhilip
|
||||
}
|
||||
|
||||
// create Bridge
|
||||
HueBridge *bridge = new HueBridge(pairingInfo.username, pairingInfo.host);
|
||||
HueBridge *bridge = new HueBridge(pairingInfo.apiKey, pairingInfo.host);
|
||||
|
||||
bridge->setApiVersion(response.value("config").toMap().value("apiversion").toString());
|
||||
bridge->setMacAddress(response.value("config").toMap().value("mac").toString());
|
||||
@ -577,12 +569,12 @@ void DevicePluginPhilipsHue::processInformationResponse(const DevicePluginPhilip
|
||||
HueLight *hueLight = new HueLight(lightId.toInt(),
|
||||
bridge->hostAddress(),
|
||||
lightMap.value("name").toString(),
|
||||
pairingInfo.username,
|
||||
pairingInfo.apiKey,
|
||||
lightMap.value("modelid").toString(),
|
||||
DeviceId(),
|
||||
this);
|
||||
|
||||
hueLight->setStates(lightMap.value("state").toMap());
|
||||
hueLight->updateStates(lightMap.value("state").toMap());
|
||||
|
||||
bridge->addLight(hueLight);
|
||||
m_unconfiguredLights.append(hueLight);
|
||||
@ -605,7 +597,7 @@ void DevicePluginPhilipsHue::processActionResponse(Device *device, const ActionI
|
||||
return;
|
||||
}
|
||||
|
||||
// check pairing error
|
||||
// check response error
|
||||
if (data.contains("error")) {
|
||||
qWarning() << "Failed to execute Hue action:" << jsonDoc.toVariant().toList().first().toMap().value("error").toMap().value("description").toString();
|
||||
emit actionExecutionFinished(actionId, DeviceManager::DeviceErrorHardwareNotAvailable);
|
||||
@ -616,6 +608,34 @@ void DevicePluginPhilipsHue::processActionResponse(Device *device, const ActionI
|
||||
emit actionExecutionFinished(actionId, DeviceManager::DeviceErrorNoError);
|
||||
}
|
||||
|
||||
void DevicePluginPhilipsHue::onBridgeError(Device *device)
|
||||
{
|
||||
// mark bridge and lamps unreachable
|
||||
if (device->deviceClassId() == hueBridgeDeviceClassId) {
|
||||
device->setStateValue(bridgeReachableStateTypeId, false);
|
||||
foreach (HueLight *light, m_lights.keys()) {
|
||||
if (light->bridgeId() == device->id()) {
|
||||
device->setStateValue(hueReachableStateTypeId, false);
|
||||
}
|
||||
}
|
||||
} else if (device->deviceClassId() == hueLightDeviceClassId) {
|
||||
DeviceId bridgeId = DeviceId(device->paramValue("bridge").toString());
|
||||
// mark lamps as unreachable
|
||||
foreach (HueLight *light, m_lights.keys()) {
|
||||
if (light->bridgeId() == bridgeId) {
|
||||
device->setStateValue(hueReachableStateTypeId, false);
|
||||
}
|
||||
}
|
||||
// mark bridge as unreachable
|
||||
foreach (Device *d, m_bridges.values()) {
|
||||
if (d->id() == bridgeId) {
|
||||
d->setStateValue(bridgeReachableStateTypeId, false);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int DevicePluginPhilipsHue::brightnessToPercentage(int brightness)
|
||||
{
|
||||
return (int)(((100.0 * brightness) / 255.0) + 0.5);
|
||||
|
||||
@ -62,7 +62,7 @@ private:
|
||||
public:
|
||||
PairingTransactionId pairingTransactionId;
|
||||
QHostAddress host;
|
||||
QString username;
|
||||
QString apiKey;
|
||||
};
|
||||
|
||||
QHash<QNetworkReply *, PairingInfo> m_pairingRequests;
|
||||
@ -82,7 +82,7 @@ private:
|
||||
void refreshLight(Device *device);
|
||||
void refreshBridge(Device *device);
|
||||
|
||||
void setName(Device *device, QString name);
|
||||
void setLightName(Device *device, QString name);
|
||||
|
||||
void processLightRefreshResponse(Device *device, const QByteArray &data);
|
||||
void processBridgeRefreshResponse(Device *device, const QByteArray &data);
|
||||
@ -91,6 +91,8 @@ private:
|
||||
void processInformationResponse(const PairingInfo &pairingInfo, const QByteArray &data);
|
||||
void processActionResponse(Device *device, const ActionId actionId, const QByteArray &data);
|
||||
|
||||
void onBridgeError(Device *device);
|
||||
|
||||
int brightnessToPercentage(int brightness);
|
||||
int percentageToBrightness(int percentage);
|
||||
};
|
||||
|
||||
@ -20,7 +20,7 @@
|
||||
"inputType": "TextLine"
|
||||
},
|
||||
{
|
||||
"name": "username",
|
||||
"name": "api key",
|
||||
"type" : "QString",
|
||||
"inputType": "TextLine",
|
||||
"readOnly": true
|
||||
@ -79,7 +79,7 @@
|
||||
"readOnly": true
|
||||
},
|
||||
{
|
||||
"name": "username",
|
||||
"name": "api key",
|
||||
"type" : "QString",
|
||||
"inputType": "TextLine",
|
||||
"readOnly": true
|
||||
@ -137,35 +137,39 @@
|
||||
"name": "brightness",
|
||||
"type": "int",
|
||||
"unit": "Percentage",
|
||||
"writable": true,
|
||||
"minValue": 0,
|
||||
"maxValue": 100,
|
||||
"defaultValue": 0,
|
||||
"writable": true
|
||||
"defaultValue": 0
|
||||
},
|
||||
{
|
||||
"id": "0b7cdd8d-4db8-4183-abe2-f3c01d1c9afc",
|
||||
"idName": "hueEffect",
|
||||
"name": "effect",
|
||||
"type": "QString",
|
||||
"writable": true,
|
||||
"defaultValue": "none",
|
||||
"allowedValues": [
|
||||
"none",
|
||||
"color loop"
|
||||
],
|
||||
"writable": true
|
||||
},
|
||||
]
|
||||
}
|
||||
],
|
||||
"actionTypes": [
|
||||
{
|
||||
"id": "acd09e71-3305-451c-910a-bc2d6e1d5144",
|
||||
"id": "d25dcfbc-d28c-4905-80e3-300ffb1248f5",
|
||||
"idName": "hueAlert",
|
||||
"name": "alert",
|
||||
"type": "QString",
|
||||
"defaultValue": "none",
|
||||
"allowedValues": [
|
||||
"none",
|
||||
"flash",
|
||||
"flash 30 seconds"
|
||||
],
|
||||
"writable": true
|
||||
"name": "flash",
|
||||
"paramTypes": [
|
||||
{
|
||||
"name": "alert",
|
||||
"type": "QString",
|
||||
"allowedValues": [
|
||||
"flash once",
|
||||
"flash 30 seconds"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@ -20,9 +20,9 @@
|
||||
|
||||
#include "huebridge.h"
|
||||
|
||||
HueBridge::HueBridge(QString username, QHostAddress hostAddress, QObject *parent) :
|
||||
HueBridge::HueBridge(QString apiKey, QHostAddress hostAddress, QObject *parent) :
|
||||
QObject(parent),
|
||||
m_username(username),
|
||||
m_apiKey(apiKey),
|
||||
m_hostAddress(hostAddress),
|
||||
m_name(QString()),
|
||||
m_macAddress(QString()),
|
||||
@ -42,14 +42,14 @@ void HueBridge::setName(const QString &name)
|
||||
m_name = name;
|
||||
}
|
||||
|
||||
QString HueBridge::username() const
|
||||
QString HueBridge::apiKey() const
|
||||
{
|
||||
return m_username;
|
||||
return m_apiKey;
|
||||
}
|
||||
|
||||
void HueBridge::setUsername(const QString &username)
|
||||
void HueBridge::setApiKey(const QString &apiKey)
|
||||
{
|
||||
m_username = username;
|
||||
m_apiKey = apiKey;
|
||||
}
|
||||
|
||||
QHostAddress HueBridge::hostAddress() const
|
||||
|
||||
@ -30,13 +30,13 @@ class HueBridge : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit HueBridge(QString username, QHostAddress hostAddress, QObject *parent = 0);
|
||||
explicit HueBridge(QString apiKey, QHostAddress hostAddress, QObject *parent = 0);
|
||||
|
||||
QString name() const;
|
||||
void setName(const QString &name);
|
||||
|
||||
QString username() const;
|
||||
void setUsername(const QString &username);
|
||||
QString apiKey() const;
|
||||
void setApiKey(const QString &apiKey);
|
||||
|
||||
QHostAddress hostAddress() const;
|
||||
void setHostAddress(const QHostAddress &hostAddress);
|
||||
@ -54,7 +54,7 @@ public:
|
||||
void addLight(HueLight *light);
|
||||
|
||||
private:
|
||||
QString m_username;
|
||||
QString m_apiKey;
|
||||
QHostAddress m_hostAddress;
|
||||
QString m_name;
|
||||
QString m_macAddress;
|
||||
@ -62,10 +62,6 @@ private:
|
||||
int m_zigbeeChannel;
|
||||
|
||||
QList<HueLight *> m_lights;
|
||||
signals:
|
||||
|
||||
|
||||
public slots:
|
||||
|
||||
};
|
||||
|
||||
|
||||
@ -21,12 +21,12 @@
|
||||
|
||||
#include "huelight.h"
|
||||
|
||||
HueLight::HueLight(const int &lightId, const QHostAddress &hostAddress, const QString &name, const QString &username, const QString &modelId, const DeviceId &bridgeId, QObject *parent) :
|
||||
HueLight::HueLight(const int &lightId, const QHostAddress &hostAddress, const QString &name, const QString &apiKey, const QString &modelId, const DeviceId &bridgeId, QObject *parent) :
|
||||
QObject(parent),
|
||||
m_lightId(lightId),
|
||||
m_hostAddress(hostAddress),
|
||||
m_name(name),
|
||||
m_username(username),
|
||||
m_apiKey(apiKey),
|
||||
m_modelId(modelId),
|
||||
m_bridgeId(bridgeId)
|
||||
{
|
||||
@ -72,14 +72,14 @@ void HueLight::setName(const QString &name)
|
||||
m_name = name;
|
||||
}
|
||||
|
||||
QString HueLight::username() const
|
||||
QString HueLight::apiKey() const
|
||||
{
|
||||
return m_username;
|
||||
return m_apiKey;
|
||||
}
|
||||
|
||||
void HueLight::setUsername(const QString &username)
|
||||
void HueLight::setApiKey(const QString &apiKey)
|
||||
{
|
||||
m_username = username;
|
||||
m_apiKey = apiKey;
|
||||
}
|
||||
|
||||
QString HueLight::modelId() const
|
||||
@ -217,7 +217,7 @@ void HueLight::setColorMode(const HueLight::ColorMode &colorMode)
|
||||
m_colorMode = colorMode;
|
||||
}
|
||||
|
||||
void HueLight::setStates(const QVariantMap &statesMap)
|
||||
void HueLight::updateStates(const QVariantMap &statesMap)
|
||||
{
|
||||
// color mode
|
||||
if (statesMap.value("colormode").toString() == "hs") {
|
||||
@ -237,7 +237,6 @@ void HueLight::setStates(const QVariantMap &statesMap)
|
||||
|
||||
// alert (none, select, lselect)
|
||||
setAlert(statesMap.value("alert").toString());
|
||||
|
||||
setBrigtness(statesMap.value("bri").toInt());
|
||||
setCt(statesMap.value("ct").toInt());
|
||||
setPower(statesMap.value("on").toBool());
|
||||
@ -286,14 +285,7 @@ void HueLight::processActionResponse(const QVariantList &responseList)
|
||||
}
|
||||
}
|
||||
if (successMap.contains("/lights/" + QString::number(m_lightId) + "/state/alert")) {
|
||||
QString alert = successMap.value("/lights/" + QString::number(m_lightId) + "/state/alert").toString();
|
||||
if (alert == "none") {
|
||||
m_alert = "none";
|
||||
} else if (alert == "select") {
|
||||
m_alert = "flash";
|
||||
} else if (alert == "lselect") {
|
||||
m_alert = "flash 30 seconds";
|
||||
}
|
||||
m_alert = successMap.value("/lights/" + QString::number(m_lightId) + "/state/alert").toString();
|
||||
}
|
||||
|
||||
}
|
||||
@ -308,7 +300,7 @@ QPair<QNetworkRequest, QByteArray> HueLight::createSetPowerRequest(const bool &p
|
||||
|
||||
QJsonDocument jsonDoc = QJsonDocument::fromVariant(requestMap);
|
||||
|
||||
QNetworkRequest request(QUrl("http://" + hostAddress().toString() + "/api/" + username() +
|
||||
QNetworkRequest request(QUrl("http://" + hostAddress().toString() + "/api/" + apiKey() +
|
||||
"/lights/" + QString::number(lightId()) + "/state"));
|
||||
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
|
||||
return QPair<QNetworkRequest, QByteArray>(request, jsonDoc.toJson());
|
||||
@ -323,7 +315,7 @@ QPair<QNetworkRequest, QByteArray> HueLight::createSetColorRequest(const QColor
|
||||
|
||||
QJsonDocument jsonDoc = QJsonDocument::fromVariant(requestMap);
|
||||
|
||||
QNetworkRequest request(QUrl("http://" + hostAddress().toString() + "/api/" + username() +
|
||||
QNetworkRequest request(QUrl("http://" + hostAddress().toString() + "/api/" + apiKey() +
|
||||
"/lights/" + QString::number(lightId()) + "/state"));
|
||||
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
|
||||
return QPair<QNetworkRequest, QByteArray>(request, jsonDoc.toJson());
|
||||
@ -341,7 +333,7 @@ QPair<QNetworkRequest, QByteArray> HueLight::createSetBrightnessRequest(const in
|
||||
|
||||
QJsonDocument jsonDoc = QJsonDocument::fromVariant(requestMap);
|
||||
|
||||
QNetworkRequest request(QUrl("http://" + hostAddress().toString() + "/api/" + username() +
|
||||
QNetworkRequest request(QUrl("http://" + hostAddress().toString() + "/api/" + apiKey() +
|
||||
"/lights/" + QString::number(lightId()) + "/state"));
|
||||
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
|
||||
return QPair<QNetworkRequest, QByteArray>(request, jsonDoc.toJson());
|
||||
@ -358,7 +350,7 @@ QPair<QNetworkRequest, QByteArray> HueLight::createSetEffectRequest(const QStrin
|
||||
}
|
||||
QJsonDocument jsonDoc = QJsonDocument::fromVariant(requestMap);
|
||||
|
||||
QNetworkRequest request(QUrl("http://" + hostAddress().toString() + "/api/" + username() +
|
||||
QNetworkRequest request(QUrl("http://" + hostAddress().toString() + "/api/" + apiKey() +
|
||||
"/lights/" + QString::number(lightId()) + "/state"));
|
||||
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
|
||||
return QPair<QNetworkRequest, QByteArray>(request, jsonDoc.toJson());
|
||||
@ -372,7 +364,7 @@ QPair<QNetworkRequest, QByteArray> HueLight::createSetTemperatureRequest(const i
|
||||
|
||||
QJsonDocument jsonDoc = QJsonDocument::fromVariant(requestMap);
|
||||
|
||||
QNetworkRequest request(QUrl("http://" + hostAddress().toString() + "/api/" + username() +
|
||||
QNetworkRequest request(QUrl("http://" + hostAddress().toString() + "/api/" + apiKey() +
|
||||
"/lights/" + QString::number(lightId()) + "/state"));
|
||||
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
|
||||
return QPair<QNetworkRequest, QByteArray>(request, jsonDoc.toJson());
|
||||
@ -381,16 +373,14 @@ QPair<QNetworkRequest, QByteArray> HueLight::createSetTemperatureRequest(const i
|
||||
QPair<QNetworkRequest, QByteArray> HueLight::createFlashRequest(const QString &alert)
|
||||
{
|
||||
QVariantMap requestMap;
|
||||
if (alert == "none") {
|
||||
requestMap.insert("alert", "none");
|
||||
} else if (alert == "flash") {
|
||||
if (alert == "flash once") {
|
||||
requestMap.insert("alert", "select");
|
||||
} else if (alert == "flash 30 seconds") {
|
||||
requestMap.insert("alert", "lselect");
|
||||
}
|
||||
QJsonDocument jsonDoc = QJsonDocument::fromVariant(requestMap);
|
||||
|
||||
QNetworkRequest request(QUrl("http://" + hostAddress().toString() + "/api/" + username() +
|
||||
QNetworkRequest request(QUrl("http://" + hostAddress().toString() + "/api/" + apiKey() +
|
||||
"/lights/" + QString::number(lightId()) + "/state"));
|
||||
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
|
||||
return QPair<QNetworkRequest, QByteArray>(request, jsonDoc.toJson());
|
||||
|
||||
@ -44,7 +44,7 @@ public:
|
||||
ColorModeCT
|
||||
};
|
||||
|
||||
explicit HueLight(const int &lightId, const QHostAddress &hostAddress, const QString &name, const QString &username, const QString &modelId, const DeviceId &bridgeId, QObject *parent = 0);
|
||||
explicit HueLight(const int &lightId, const QHostAddress &hostAddress, const QString &name, const QString &apiKey, const QString &modelId, const DeviceId &bridgeId, QObject *parent = 0);
|
||||
|
||||
int lightId() const;
|
||||
void setLightId(const int &lightId);
|
||||
@ -58,8 +58,8 @@ public:
|
||||
QString name() const;
|
||||
void setName(const QString &name);
|
||||
|
||||
QString username() const;
|
||||
void setUsername(const QString &username);
|
||||
QString apiKey() const;
|
||||
void setApiKey(const QString &apiKey);
|
||||
|
||||
QString modelId() const;
|
||||
void setModelId(const QString &modelId);
|
||||
@ -103,7 +103,7 @@ public:
|
||||
void setColorMode(const ColorMode &colorMode);
|
||||
|
||||
// update states
|
||||
void setStates(const QVariantMap &statesMap);
|
||||
void updateStates(const QVariantMap &statesMap);
|
||||
void processActionResponse(const QVariantList &responseList);
|
||||
|
||||
// create action requests
|
||||
@ -118,7 +118,7 @@ private:
|
||||
int m_lightId;
|
||||
QHostAddress m_hostAddress;
|
||||
QString m_name;
|
||||
QString m_username;
|
||||
QString m_apiKey;
|
||||
QString m_modelId;
|
||||
DeviceId m_bridgeId;
|
||||
QString m_type;
|
||||
@ -139,8 +139,6 @@ private:
|
||||
signals:
|
||||
void stateChanged();
|
||||
|
||||
public slots:
|
||||
|
||||
};
|
||||
|
||||
#endif // HUELIGHT_H
|
||||
|
||||
Reference in New Issue
Block a user