added auth and connected state change
This commit is contained in:
parent
3b2f76ed80
commit
8907fb7b47
@ -234,6 +234,8 @@ void IntegrationPluginLifx::setupThing(ThingSetupInfo *info)
|
|||||||
connect(lifxCloud, &LifxCloud::lightsListReceived, this, &IntegrationPluginLifx::onLifxCloudLightsListReceived);
|
connect(lifxCloud, &LifxCloud::lightsListReceived, this, &IntegrationPluginLifx::onLifxCloudLightsListReceived);
|
||||||
connect(lifxCloud, &LifxCloud::scenesListReceived, this, &IntegrationPluginLifx::onLifxCloudScenesListReceived);
|
connect(lifxCloud, &LifxCloud::scenesListReceived, this, &IntegrationPluginLifx::onLifxCloudScenesListReceived);
|
||||||
connect(lifxCloud, &LifxCloud::requestExecuted, this, &IntegrationPluginLifx::onLifxCloudRequestExecuted);
|
connect(lifxCloud, &LifxCloud::requestExecuted, this, &IntegrationPluginLifx::onLifxCloudRequestExecuted);
|
||||||
|
connect(lifxCloud, &LifxCloud::connectionChanged, this, &IntegrationPluginLifx::onLifxCloudConnectionChanged);
|
||||||
|
connect(lifxCloud, &LifxCloud::authenticationChanged, this, &IntegrationPluginLifx::onLifxCloudAuthenticationChanged);
|
||||||
lifxCloud->setAuthorizationToken(token);
|
lifxCloud->setAuthorizationToken(token);
|
||||||
lifxCloud->listLights();
|
lifxCloud->listLights();
|
||||||
|
|
||||||
@ -245,8 +247,6 @@ void IntegrationPluginLifx::setupThing(ThingSetupInfo *info)
|
|||||||
|
|
||||||
void IntegrationPluginLifx::postSetupThing(Thing *thing)
|
void IntegrationPluginLifx::postSetupThing(Thing *thing)
|
||||||
{
|
{
|
||||||
connect(thing, &Thing::nameChanged, this, &IntegrationPluginLifx::onDeviceNameChanged);
|
|
||||||
|
|
||||||
if (!m_pluginTimer) {
|
if (!m_pluginTimer) {
|
||||||
m_pluginTimer = hardwareManager()->pluginTimerManager()->registerTimer(60);
|
m_pluginTimer = hardwareManager()->pluginTimerManager()->registerTimer(60);
|
||||||
connect(m_pluginTimer, &PluginTimer::timeout, this, [this]() {
|
connect(m_pluginTimer, &PluginTimer::timeout, this, [this]() {
|
||||||
@ -336,7 +336,7 @@ void IntegrationPluginLifx::executeAction(ThingActionInfo *info)
|
|||||||
connect(info, &ThingActionInfo::aborted, this, [requestId, this] {m_asyncActions.remove(requestId);});
|
connect(info, &ThingActionInfo::aborted, this, [requestId, this] {m_asyncActions.remove(requestId);});
|
||||||
m_asyncActions.insert(requestId, info);
|
m_asyncActions.insert(requestId, info);
|
||||||
} else {
|
} else {
|
||||||
Q_ASSERT_X(false, "executeAction", QString("Unhandled actionTypeId: %1").arg(action.actionTypeId().toString()).toUtf8());
|
Q_ASSERT_X(false, "executeAction", QString("Unhandled actionTypeId: %1").arg(action.actionTypeId().toString()).toUtf8());
|
||||||
}
|
}
|
||||||
} else if (thing->thingClassId() == dimmableBulbThingClassId) {
|
} else if (thing->thingClassId() == dimmableBulbThingClassId) {
|
||||||
QByteArray lightId = thing->paramValue(dimmableBulbThingIdParamTypeId).toByteArray();
|
QByteArray lightId = thing->paramValue(dimmableBulbThingIdParamTypeId).toByteArray();
|
||||||
@ -412,12 +412,6 @@ void IntegrationPluginLifx::executeBrowserItem(BrowserActionInfo *info)
|
|||||||
connect(info, &BrowserActionInfo::aborted, this, [requestId, this] {m_asyncBrowserItem.remove(requestId);});
|
connect(info, &BrowserActionInfo::aborted, this, [requestId, this] {m_asyncBrowserItem.remove(requestId);});
|
||||||
}
|
}
|
||||||
|
|
||||||
void IntegrationPluginLifx::onDeviceNameChanged()
|
|
||||||
{
|
|
||||||
Thing *thing = static_cast<Thing *>(sender());
|
|
||||||
Q_UNUSED(thing)
|
|
||||||
}
|
|
||||||
|
|
||||||
void IntegrationPluginLifx::onConnectionChanged(bool connected)
|
void IntegrationPluginLifx::onConnectionChanged(bool connected)
|
||||||
{
|
{
|
||||||
Q_UNUSED(connected)
|
Q_UNUSED(connected)
|
||||||
@ -437,9 +431,7 @@ void IntegrationPluginLifx::onRequestExecuted(int requestId, bool success)
|
|||||||
} else {
|
} else {
|
||||||
info->finish(Thing::ThingErrorHardwareFailure);
|
info->finish(Thing::ThingErrorHardwareFailure);
|
||||||
}
|
}
|
||||||
}
|
} else if (m_asyncBrowserItem.contains(requestId)) {
|
||||||
|
|
||||||
if (m_asyncBrowserItem.contains(requestId)) {
|
|
||||||
BrowserActionInfo *info = m_asyncBrowserItem.take(requestId);
|
BrowserActionInfo *info = m_asyncBrowserItem.take(requestId);
|
||||||
if (success) {
|
if (success) {
|
||||||
info->finish(Thing::ThingErrorNoError);
|
info->finish(Thing::ThingErrorNoError);
|
||||||
@ -463,6 +455,15 @@ void IntegrationPluginLifx::onLifxCloudConnectionChanged(bool connected)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void IntegrationPluginLifx::onLifxCloudAuthenticationChanged(bool authenticated)
|
||||||
|
{
|
||||||
|
LifxCloud *lifxCloud = static_cast<LifxCloud *>(sender());
|
||||||
|
Thing *accountThing = m_lifxCloudConnections.key(lifxCloud);
|
||||||
|
if (!accountThing)
|
||||||
|
return;
|
||||||
|
accountThing->setStateValue(lifxAccountLoggedInStateTypeId, authenticated);
|
||||||
|
}
|
||||||
|
|
||||||
void IntegrationPluginLifx::onLifxCloudLightsListReceived(const QList<LifxCloud::Light> &lights)
|
void IntegrationPluginLifx::onLifxCloudLightsListReceived(const QList<LifxCloud::Light> &lights)
|
||||||
{
|
{
|
||||||
LifxCloud *lifxCloud = static_cast<LifxCloud *>(sender());
|
LifxCloud *lifxCloud = static_cast<LifxCloud *>(sender());
|
||||||
@ -502,6 +503,8 @@ void IntegrationPluginLifx::onLifxCloudLightsListReceived(const QList<LifxCloud:
|
|||||||
}
|
}
|
||||||
ParamList params;
|
ParamList params;
|
||||||
params << Param(m_idParamTypeIds.value(thingDescriptor.thingClassId()), light.id);
|
params << Param(m_idParamTypeIds.value(thingDescriptor.thingClassId()), light.id);
|
||||||
|
params << Param(m_hostAddressParamTypeIds.value(thingDescriptor.thingClassId()), "-");
|
||||||
|
params << Param(m_portParamTypeIds.value(thingDescriptor.thingClassId()), 0);
|
||||||
thingDescriptor.setParams(params);
|
thingDescriptor.setParams(params);
|
||||||
thingDescriptors.append(thingDescriptor);
|
thingDescriptors.append(thingDescriptor);
|
||||||
}
|
}
|
||||||
@ -511,15 +514,26 @@ void IntegrationPluginLifx::onLifxCloudLightsListReceived(const QList<LifxCloud:
|
|||||||
|
|
||||||
void IntegrationPluginLifx::onLifxCloudRequestExecuted(int requestId, bool success)
|
void IntegrationPluginLifx::onLifxCloudRequestExecuted(int requestId, bool success)
|
||||||
{
|
{
|
||||||
ThingActionInfo *info = m_asyncActions.take(requestId);
|
if (m_asyncActions.contains(requestId)) {
|
||||||
if (!info) {
|
ThingActionInfo *info = m_asyncActions.take(requestId);
|
||||||
return;
|
if (!info) {
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
if (success) {
|
if (success) {
|
||||||
info->finish(Thing::ThingErrorNoError);
|
info->finish(Thing::ThingErrorNoError);
|
||||||
} else {
|
} else {
|
||||||
info->finish(Thing::ThingErrorHardwareNotAvailable);
|
info->finish(Thing::ThingErrorHardwareNotAvailable);
|
||||||
|
}
|
||||||
|
} else if (m_asyncBrowserItem.contains(requestId)) {
|
||||||
|
BrowserActionInfo *info = m_asyncBrowserItem.value(requestId);
|
||||||
|
if (!info) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (success) {
|
||||||
|
info->finish(Thing::ThingErrorNoError);
|
||||||
|
} else {
|
||||||
|
info->finish(Thing::ThingErrorHardwareNotAvailable);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -90,11 +90,11 @@ private:
|
|||||||
QHash<int, Lifx::LifxProduct> m_lifxProducts;
|
QHash<int, Lifx::LifxProduct> m_lifxProducts;
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void onDeviceNameChanged();
|
|
||||||
void onConnectionChanged(bool connected);
|
void onConnectionChanged(bool connected);
|
||||||
void onRequestExecuted(int requestId, bool success);
|
void onRequestExecuted(int requestId, bool success);
|
||||||
|
|
||||||
void onLifxCloudConnectionChanged(bool connected);
|
void onLifxCloudConnectionChanged(bool connected);
|
||||||
|
void onLifxCloudAuthenticationChanged(bool authenticated);
|
||||||
void onLifxCloudRequestExecuted(int requestId, bool success);
|
void onLifxCloudRequestExecuted(int requestId, bool success);
|
||||||
void onLifxCloudLightsListReceived(const QList<LifxCloud::Light> &lights);
|
void onLifxCloudLightsListReceived(const QList<LifxCloud::Light> &lights);
|
||||||
void onLifxCloudScenesListReceived(const QList<LifxCloud::Scene> &scenes);
|
void onLifxCloudScenesListReceived(const QList<LifxCloud::Scene> &scenes);
|
||||||
|
|||||||
@ -75,7 +75,6 @@ bool Lifx::enable()
|
|||||||
m_socket = nullptr;
|
m_socket = nullptr;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
connect(m_socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(error(QAbstractSocket::SocketError)));
|
|
||||||
connect(m_socket, &QUdpSocket::readyRead, this, &Lifx::onReadyRead);
|
connect(m_socket, &QUdpSocket::readyRead, this, &Lifx::onReadyRead);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -152,7 +152,7 @@ private slots:
|
|||||||
|
|
||||||
signals:
|
signals:
|
||||||
void connectionChanged(bool connected);
|
void connectionChanged(bool connected);
|
||||||
//void requestExecuted(int requestId, bool success);
|
void requestExecuted(int requestId, bool success);
|
||||||
//void errorReceived(int code, const QString &message);
|
//void errorReceived(int code, const QString &message);
|
||||||
|
|
||||||
//void powerNotificationReceived(bool status);
|
//void powerNotificationReceived(bool status);
|
||||||
|
|||||||
@ -50,6 +50,16 @@ void LifxCloud::setAuthorizationToken(const QByteArray &token)
|
|||||||
m_authorizationToken = token;
|
m_authorizationToken = token;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool LifxCloud::cloudAuthenticated()
|
||||||
|
{
|
||||||
|
return m_authenticated;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool LifxCloud::cloudConnected()
|
||||||
|
{
|
||||||
|
return m_connected;
|
||||||
|
}
|
||||||
|
|
||||||
void LifxCloud::listLights()
|
void LifxCloud::listLights()
|
||||||
{
|
{
|
||||||
if (m_authorizationToken.isEmpty()) {
|
if (m_authorizationToken.isEmpty()) {
|
||||||
@ -67,10 +77,18 @@ void LifxCloud::listLights()
|
|||||||
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||||
|
|
||||||
// check HTTP status code
|
// check HTTP status code
|
||||||
if (status != 200) {
|
if (status > 207) {
|
||||||
qCWarning(dcLifx()) << "Error get lights list" << status << reply->errorString();
|
qCWarning(dcLifx()) << "Error get lights list" << status << reply->errorString();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (!m_authenticated) {
|
||||||
|
m_authenticated = true;
|
||||||
|
emit authenticationChanged(true);
|
||||||
|
}
|
||||||
|
if (!m_connected) {
|
||||||
|
m_connected = true;
|
||||||
|
emit authenticationChanged(true);
|
||||||
|
}
|
||||||
QByteArray rawData = reply->readAll();
|
QByteArray rawData = reply->readAll();
|
||||||
|
|
||||||
QJsonDocument data; QJsonParseError error;
|
QJsonDocument data; QJsonParseError error;
|
||||||
@ -151,6 +169,14 @@ void LifxCloud::listScenes()
|
|||||||
qCWarning(dcLifx()) << "Error get scene list" << status << reply->errorString();
|
qCWarning(dcLifx()) << "Error get scene list" << status << reply->errorString();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (!m_authenticated) {
|
||||||
|
m_authenticated = true;
|
||||||
|
emit authenticationChanged(true);
|
||||||
|
}
|
||||||
|
if (!m_connected) {
|
||||||
|
m_connected = true;
|
||||||
|
emit authenticationChanged(true);
|
||||||
|
}
|
||||||
QByteArray rawData = reply->readAll();
|
QByteArray rawData = reply->readAll();
|
||||||
qCDebug(dcLifx()) << "Got list scenes reply" << rawData;
|
qCDebug(dcLifx()) << "Got list scenes reply" << rawData;
|
||||||
QJsonDocument data; QJsonParseError error;
|
QJsonDocument data; QJsonParseError error;
|
||||||
@ -166,7 +192,7 @@ void LifxCloud::listScenes()
|
|||||||
QList<Scene> scenes;
|
QList<Scene> scenes;
|
||||||
foreach (QJsonValue value, array) {
|
foreach (QJsonValue value, array) {
|
||||||
Scene scene;
|
Scene scene;
|
||||||
scene.id = value.toObject().value("id").toString().toUtf8();
|
scene.id = value.toObject().value("uuid").toString().toUtf8();
|
||||||
scene.name = value.toObject().value("name").toString();
|
scene.name = value.toObject().value("name").toString();
|
||||||
scenes.append(scene);
|
scenes.append(scene);
|
||||||
}
|
}
|
||||||
@ -208,23 +234,36 @@ int LifxCloud::activateScene(const QString &sceneId)
|
|||||||
int requestId = qrand();
|
int requestId = qrand();
|
||||||
|
|
||||||
QNetworkRequest request;
|
QNetworkRequest request;
|
||||||
request.setUrl(QUrl(QString("https://api.lifx.com/v1/scenes/scene_id::%1/activate").arg(sceneId)));
|
request.setUrl(QUrl(QString("https://api.lifx.com/v1/scenes/scene_id:%1/activate").arg(sceneId)));
|
||||||
request.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/json");
|
request.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/json");
|
||||||
request.setRawHeader("Authorization","Bearer "+m_authorizationToken);
|
request.setRawHeader("Authorization","Bearer "+m_authorizationToken);
|
||||||
QByteArray payload;
|
QNetworkReply *reply = m_networkManager->put(request, "");
|
||||||
payload.append("duration:5");
|
|
||||||
QNetworkReply *reply = m_networkManager->put(request, payload);
|
|
||||||
connect(reply, &QNetworkReply::finished, &QNetworkReply::deleteLater);
|
connect(reply, &QNetworkReply::finished, &QNetworkReply::deleteLater);
|
||||||
connect(reply, &QNetworkReply::finished, this, [reply, this] {
|
connect(reply, &QNetworkReply::finished, this, [requestId, reply, this] {
|
||||||
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||||
// check HTTP status code
|
// check HTTP status code
|
||||||
if (status != 200) {
|
if (status == 401 || status == 403) {
|
||||||
qCWarning(dcLifx()) << "Error get scene list" << status << reply->errorString();
|
if (m_authenticated) {
|
||||||
|
m_authenticated = false;
|
||||||
|
emit authenticationChanged(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (status > 207) {
|
||||||
|
qCWarning(dcLifx()) << "Error activate Scene" << status << reply->errorString();
|
||||||
|
emit requestExecuted(requestId, false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (!m_authenticated) {
|
||||||
|
m_authenticated = true;
|
||||||
|
emit authenticationChanged(true);
|
||||||
|
}
|
||||||
|
if (!m_connected) {
|
||||||
|
m_connected = true;
|
||||||
|
emit authenticationChanged(true);
|
||||||
|
}
|
||||||
|
emit requestExecuted(requestId, true);
|
||||||
QByteArray rawData = reply->readAll();
|
QByteArray rawData = reply->readAll();
|
||||||
qCDebug(dcLifx()) << "Got list lights reply" << rawData;
|
qCDebug(dcLifx()) << "Got activate scene reply" << rawData;
|
||||||
|
|
||||||
});
|
});
|
||||||
return requestId;
|
return requestId;
|
||||||
}
|
}
|
||||||
@ -271,11 +310,19 @@ int LifxCloud::setState(const QString &selector, State state, QVariant stateValu
|
|||||||
connect(reply, &QNetworkReply::finished, this, [requestId, reply, this] {
|
connect(reply, &QNetworkReply::finished, this, [requestId, reply, this] {
|
||||||
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||||
// check HTTP status code
|
// check HTTP status code
|
||||||
if (status != 200) {
|
if (status > 207) {
|
||||||
qCWarning(dcLifx()) << "Error get scene list" << status << reply->errorString();
|
qCWarning(dcLifx()) << "Error get scene list" << status << reply->errorString();
|
||||||
emit requestExecuted(requestId, false);
|
emit requestExecuted(requestId, false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (!m_authenticated) {
|
||||||
|
m_authenticated = true;
|
||||||
|
emit authenticationChanged(true);
|
||||||
|
}
|
||||||
|
if (!m_connected) {
|
||||||
|
m_connected = true;
|
||||||
|
emit authenticationChanged(true);
|
||||||
|
}
|
||||||
QByteArray rawData = reply->readAll();
|
QByteArray rawData = reply->readAll();
|
||||||
qCDebug(dcLifx()) << "Got set state reply" << rawData;
|
qCDebug(dcLifx()) << "Got set state reply" << rawData;
|
||||||
emit requestExecuted(requestId, true);
|
emit requestExecuted(requestId, true);
|
||||||
|
|||||||
@ -96,6 +96,8 @@ public:
|
|||||||
|
|
||||||
explicit LifxCloud(NetworkAccessManager *networkManager, QObject *parent = nullptr);
|
explicit LifxCloud(NetworkAccessManager *networkManager, QObject *parent = nullptr);
|
||||||
void setAuthorizationToken(const QByteArray &token);
|
void setAuthorizationToken(const QByteArray &token);
|
||||||
|
bool cloudAuthenticated();
|
||||||
|
bool cloudConnected();
|
||||||
|
|
||||||
void listLights();
|
void listLights();
|
||||||
void listScenes();
|
void listScenes();
|
||||||
@ -112,8 +114,11 @@ private:
|
|||||||
QByteArray m_authorizationToken;
|
QByteArray m_authorizationToken;
|
||||||
|
|
||||||
int setState(const QString &lightId, State state, QVariant stateValue, int duration);
|
int setState(const QString &lightId, State state, QVariant stateValue, int duration);
|
||||||
|
bool m_authenticated = false;
|
||||||
|
bool m_connected = false;
|
||||||
signals:
|
signals:
|
||||||
|
void connectionChanged(bool m_connected);
|
||||||
|
void authenticationChanged(bool m_authenticated);
|
||||||
void lightsListReceived(const QList<Light> &lights);
|
void lightsListReceived(const QList<Light> &lights);
|
||||||
void scenesListReceived(const QList<Scene> &scenes);
|
void scenesListReceived(const QList<Scene> &scenes);
|
||||||
void requestExecuted(int requestId, bool susccess);
|
void requestExecuted(int requestId, bool susccess);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user