fixed connected state

This commit is contained in:
bernhard.trinnes 2020-03-20 09:55:15 +01:00
parent 292bfee516
commit 9bdbee4c48
4 changed files with 125 additions and 74 deletions

View File

@ -1,30 +1,32 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright 2013 - 2020, nymea GmbH * Copyright 2013 - 2020, nymea GmbH
* Contact: contact@nymea.io * Contact: contact@nymea.io
* *
* This file is part of nymea. * This file is part of nymea.
* This project including source code and documentation is protected by copyright law, and * This project including source code and documentation is protected by
* remains the property of nymea GmbH. All rights, including reproduction, publication, * copyright law, and remains the property of nymea GmbH. All rights, including
* editing and translation, are reserved. The use of this project is subject to the terms of a * reproduction, publication, editing and translation, are reserved. The use of
* license agreement to be concluded with nymea GmbH in accordance with the terms * this project is subject to the terms of a license agreement to be concluded
* of use of nymea GmbH, available under https://nymea.io/license * with nymea GmbH in accordance with the terms of use of nymea GmbH, available
* under https://nymea.io/license
* *
* GNU Lesser General Public License Usage * GNU Lesser General Public License Usage
* This project may also contain libraries licensed under the open source software license GNU GPL v.3. * Alternatively, this project may be redistributed and/or modified under the
* Alternatively, this project may be redistributed and/or modified under the terms of the GNU * terms of the GNU Lesser General Public License as published by the Free
* Lesser General Public License as published by the Free Software Foundation; version 3. * Software Foundation; version 3. This project is distributed in the hope that
* this project is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* See the GNU Lesser General Public License for more details. * Lesser General Public License for more details.
* *
* You should have received a copy of the GNU Lesser General Public License along with this project. * You should have received a copy of the GNU Lesser General Public License
* If not, see <https://www.gnu.org/licenses/>. * along with this project. If not, see <https://www.gnu.org/licenses/>.
* *
* For any further details and any questions please contact us under contact@nymea.io * For any further details and any questions please contact us under
* or see our FAQ/Licensing Information on https://nymea.io/license/faq * contact@nymea.io or see our FAQ/Licensing Information on
* https://nymea.io/license/faq
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "integrationplugindynatrace.h" #include "integrationplugindynatrace.h"
#include "plugininfo.h" #include "plugininfo.h"
@ -54,7 +56,7 @@ void IntegrationPluginDynatrace::discoverThings(ThingDiscoveryInfo *info)
ThingDescriptor descriptor(ufoThingClassId, "Ufo", address.toString()); ThingDescriptor descriptor(ufoThingClassId, "Ufo", address.toString());
ParamList params; ParamList params;
//FIXME Rediscovery
/*Thing *existingThing = myThings().findByParams(ParamList() << Param(ufoThingIdParamTypeId, "")); /*Thing *existingThing = myThings().findByParams(ParamList() << Param(ufoThingIdParamTypeId, ""));
if (existingThing) { if (existingThing) {
//For Thing re-discovery //For Thing re-discovery
@ -99,13 +101,16 @@ void IntegrationPluginDynatrace::setupThing(ThingSetupInfo *info)
// Discovery Thing setup or Things setup caused by nymea restart // Discovery Thing setup or Things setup caused by nymea restart
info->finish(Thing::ThingErrorNoError); info->finish(Thing::ThingErrorNoError);
} }
} else {
qCWarning(dcDynatrace()) << "Setup thing: Unhandled ThingClassId" << info->thing()->thingClassId();
info->finish(Thing::ThingErrorSetupFailed);
} }
} }
void IntegrationPluginDynatrace::postSetupThing(Thing *thing) void IntegrationPluginDynatrace::postSetupThing(Thing *thing)
{ {
if (thing->thingClassId() == ufoThingClassId) { if (thing->thingClassId() == ufoThingClassId) {
thing->setStateValue(ufoConnectedStateTypeId, true); //FIXME thing->setStateValue(ufoConnectedStateTypeId, true);
thing->setStateValue(ufoPowerStateTypeId, false); thing->setStateValue(ufoPowerStateTypeId, false);
thing->setStateValue(ufoLogoStateTypeId, false); thing->setStateValue(ufoLogoStateTypeId, false);
thing->setStateValue(ufoEffectTopStateTypeId, "None"); thing->setStateValue(ufoEffectTopStateTypeId, "None");
@ -113,6 +118,7 @@ void IntegrationPluginDynatrace::postSetupThing(Thing *thing)
QHostAddress address = QHostAddress(thing->paramValue(ufoThingHostParamTypeId).toString()); QHostAddress address = QHostAddress(thing->paramValue(ufoThingHostParamTypeId).toString());
Ufo *ufo = new Ufo(hardwareManager()->networkManager(), address, this); Ufo *ufo = new Ufo(hardwareManager()->networkManager(), address, this);
connect(ufo, &Ufo::connectionChanged, this, &IntegrationPluginDynatrace::onConnectionChanged);
m_ufoConnections.insert(thing->id(), ufo); m_ufoConnections.insert(thing->id(), ufo);
// Set all off // Set all off
ufo->setLogo(QColor(Qt::black), QColor(Qt::black), QColor(Qt::black), QColor(Qt::black)); ufo->setLogo(QColor(Qt::black), QColor(Qt::black), QColor(Qt::black), QColor(Qt::black));
@ -122,7 +128,9 @@ void IntegrationPluginDynatrace::postSetupThing(Thing *thing)
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]() {
//TODO check if Thing is reachable foreach (Ufo *ufo, m_ufoConnections.values()) {
ufo->getId();
}
}); });
} }
} }
@ -315,5 +323,9 @@ void IntegrationPluginDynatrace::getId(const QHostAddress &address)
void IntegrationPluginDynatrace::onConnectionChanged(bool connected) void IntegrationPluginDynatrace::onConnectionChanged(bool connected)
{ {
Q_UNUSED(connected) Ufo *ufo = static_cast<Ufo *>(sender());
Thing *thing = myThings().findById(m_ufoConnections.key(ufo));
if (!thing)
return;
thing->setStateValue(ufoConnectedStateTypeId, connected);
} }

View File

@ -1,30 +1,32 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright 2013 - 2020, nymea GmbH * Copyright 2013 - 2020, nymea GmbH
* Contact: contact@nymea.io * Contact: contact@nymea.io
* *
* This file is part of nymea. * This file is part of nymea.
* This project including source code and documentation is protected by copyright law, and * This project including source code and documentation is protected by
* remains the property of nymea GmbH. All rights, including reproduction, publication, * copyright law, and remains the property of nymea GmbH. All rights, including
* editing and translation, are reserved. The use of this project is subject to the terms of a * reproduction, publication, editing and translation, are reserved. The use of
* license agreement to be concluded with nymea GmbH in accordance with the terms * this project is subject to the terms of a license agreement to be concluded
* of use of nymea GmbH, available under https://nymea.io/license * with nymea GmbH in accordance with the terms of use of nymea GmbH, available
* under https://nymea.io/license
* *
* GNU Lesser General Public License Usage * GNU Lesser General Public License Usage
* This project may also contain libraries licensed under the open source software license GNU GPL v.3. * Alternatively, this project may be redistributed and/or modified under the
* Alternatively, this project may be redistributed and/or modified under the terms of the GNU * terms of the GNU Lesser General Public License as published by the Free
* Lesser General Public License as published by the Free Software Foundation; version 3. * Software Foundation; version 3. This project is distributed in the hope that
* this project is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* See the GNU Lesser General Public License for more details. * Lesser General Public License for more details.
* *
* You should have received a copy of the GNU Lesser General Public License along with this project. * You should have received a copy of the GNU Lesser General Public License
* If not, see <https://www.gnu.org/licenses/>. * along with this project. If not, see <https://www.gnu.org/licenses/>.
* *
* For any further details and any questions please contact us under contact@nymea.io * For any further details and any questions please contact us under
* or see our FAQ/Licensing Information on https://nymea.io/license/faq * contact@nymea.io or see our FAQ/Licensing Information on
* https://nymea.io/license/faq
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef INTEGRATIONPLUGINDYNATRACE_H #ifndef INTEGRATIONPLUGINDYNATRACE_H
#define INTEGRATIONPLUGINDYNATRACE_H #define INTEGRATIONPLUGINDYNATRACE_H

View File

@ -1,34 +1,38 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright 2013 - 2020, nymea GmbH * Copyright 2013 - 2020, nymea GmbH
* Contact: contact@nymea.io * Contact: contact@nymea.io
* *
* This file is part of nymea. * This file is part of nymea.
* This project including source code and documentation is protected by copyright law, and * This project including source code and documentation is protected by
* remains the property of nymea GmbH. All rights, including reproduction, publication, * copyright law, and remains the property of nymea GmbH. All rights, including
* editing and translation, are reserved. The use of this project is subject to the terms of a * reproduction, publication, editing and translation, are reserved. The use of
* license agreement to be concluded with nymea GmbH in accordance with the terms * this project is subject to the terms of a license agreement to be concluded
* of use of nymea GmbH, available under https://nymea.io/license * with nymea GmbH in accordance with the terms of use of nymea GmbH, available
* under https://nymea.io/license
* *
* GNU Lesser General Public License Usage * GNU Lesser General Public License Usage
* This project may also contain libraries licensed under the open source software license GNU GPL v.3. * Alternatively, this project may be redistributed and/or modified under the
* Alternatively, this project may be redistributed and/or modified under the terms of the GNU * terms of the GNU Lesser General Public License as published by the Free
* Lesser General Public License as published by the Free Software Foundation; version 3. * Software Foundation; version 3. This project is distributed in the hope that
* this project is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* See the GNU Lesser General Public License for more details. * Lesser General Public License for more details.
* *
* You should have received a copy of the GNU Lesser General Public License along with this project. * You should have received a copy of the GNU Lesser General Public License
* If not, see <https://www.gnu.org/licenses/>. * along with this project. If not, see <https://www.gnu.org/licenses/>.
* *
* For any further details and any questions please contact us under contact@nymea.io * For any further details and any questions please contact us under
* or see our FAQ/Licensing Information on https://nymea.io/license/faq * contact@nymea.io or see our FAQ/Licensing Information on
* https://nymea.io/license/faq
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "ufo.h" #include "ufo.h"
#include "extern-plugininfo.h" #include "extern-plugininfo.h"
#include <QUrlQuery> #include <QUrlQuery>
#include <QJsonDocument>
Ufo::Ufo(NetworkAccessManager *networkManager, const QHostAddress &address, QObject *parent) : Ufo::Ufo(NetworkAccessManager *networkManager, const QHostAddress &address, QObject *parent) :
QObject(parent), QObject(parent),
@ -38,6 +42,37 @@ Ufo::Ufo(NetworkAccessManager *networkManager, const QHostAddress &address, QObj
} }
void Ufo::getId()
{
QUrl url;
url.setScheme("http");
url.setHost(m_address.toString());
url.setPath("/info", QUrl::ParsingMode::TolerantMode);
QNetworkRequest request;
request.setUrl(url);
QNetworkReply *reply = m_networkManager->get(request);
connect(reply, &QNetworkReply::finished, this, [reply, this] {
reply->deleteLater();
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
// Check HTTP status code
if (status != 200 || reply->error() != QNetworkReply::NoError) {
qCWarning(dcDynatrace()) << "Request error:" << status << reply->errorString();
emit connectionChanged(false);
return;
}
emit connectionChanged(true);
QJsonParseError error;
QJsonDocument data = QJsonDocument::fromJson(reply->readAll(), &error);
if (error.error != QJsonParseError::NoError){
qCWarning(dcDynatrace()) << "JSON parsing error:" << error.errorString();
return;
}
QString id = data.toVariant().toMap().value("ufoid").toString();
emit idReceived(id);
});
}
void Ufo::resetLogo() void Ufo::resetLogo()
{ {
QUrl url; QUrl url;
@ -61,7 +96,6 @@ void Ufo::resetLogo()
} }
emit connectionChanged(true); emit connectionChanged(true);
}); });
} }
void Ufo::setLogo(QColor led1, QColor led2, QColor led3, QColor led4) void Ufo::setLogo(QColor led1, QColor led2, QColor led3, QColor led4)

View File

@ -1,30 +1,32 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright 2013 - 2020, nymea GmbH * Copyright 2013 - 2020, nymea GmbH
* Contact: contact@nymea.io * Contact: contact@nymea.io
* *
* This file is part of nymea. * This file is part of nymea.
* This project including source code and documentation is protected by copyright law, and * This project including source code and documentation is protected by
* remains the property of nymea GmbH. All rights, including reproduction, publication, * copyright law, and remains the property of nymea GmbH. All rights, including
* editing and translation, are reserved. The use of this project is subject to the terms of a * reproduction, publication, editing and translation, are reserved. The use of
* license agreement to be concluded with nymea GmbH in accordance with the terms * this project is subject to the terms of a license agreement to be concluded
* of use of nymea GmbH, available under https://nymea.io/license * with nymea GmbH in accordance with the terms of use of nymea GmbH, available
* under https://nymea.io/license
* *
* GNU Lesser General Public License Usage * GNU Lesser General Public License Usage
* This project may also contain libraries licensed under the open source software license GNU GPL v.3. * Alternatively, this project may be redistributed and/or modified under the
* Alternatively, this project may be redistributed and/or modified under the terms of the GNU * terms of the GNU Lesser General Public License as published by the Free
* Lesser General Public License as published by the Free Software Foundation; version 3. * Software Foundation; version 3. This project is distributed in the hope that
* this project is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* See the GNU Lesser General Public License for more details. * Lesser General Public License for more details.
* *
* You should have received a copy of the GNU Lesser General Public License along with this project. * You should have received a copy of the GNU Lesser General Public License
* If not, see <https://www.gnu.org/licenses/>. * along with this project. If not, see <https://www.gnu.org/licenses/>.
* *
* For any further details and any questions please contact us under contact@nymea.io * For any further details and any questions please contact us under
* or see our FAQ/Licensing Information on https://nymea.io/license/faq * contact@nymea.io or see our FAQ/Licensing Information on
* https://nymea.io/license/faq
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef DYNATRACE_UFO_H #ifndef DYNATRACE_UFO_H
#define DYNATRACE_UFO_H #define DYNATRACE_UFO_H
@ -43,6 +45,7 @@ class Ufo : public QObject
public: public:
explicit Ufo(NetworkAccessManager *networkManager, const QHostAddress &address, QObject *parent = nullptr); explicit Ufo(NetworkAccessManager *networkManager, const QHostAddress &address, QObject *parent = nullptr);
void getId();
void resetLogo(); void resetLogo();
void setLogo(QColor led1, QColor led2, QColor led3, QColor led4); void setLogo(QColor led1, QColor led2, QColor led3, QColor led4);
void initBackgroundColor(bool top, bool bottom); void initBackgroundColor(bool top, bool bottom);
@ -53,13 +56,13 @@ public:
void startMorph(bool top, bool bottom, QColor color, int time, int speed); //time in ms, speed 0-10 void startMorph(bool top, bool bottom, QColor color, int time, int speed); //time in ms, speed 0-10
void stopMorph(bool top, bool bottom); void stopMorph(bool top, bool bottom);
private: private:
NetworkAccessManager *m_networkManager; NetworkAccessManager *m_networkManager;
QHostAddress m_address; QHostAddress m_address;
signals: signals:
void connectionChanged(bool reachable); void connectionChanged(bool reachable);
void idReceived(const QString &id);
}; };
#endif //DYNATRACE_UFO_H #endif //DYNATRACE_UFO_H