From 91c7637a2bf1279dda2a38b02378eaa754be7759 Mon Sep 17 00:00:00 2001 From: brindosch Date: Sat, 17 Jun 2017 23:27:41 +0200 Subject: [PATCH] add unique id Zeroconf TXT record is missing --- include/hyperion/Hyperion.h | 3 ++ include/utils/Stats.h | 5 ++- libsrc/jsonserver/JsonClientConnection.cpp | 1 + libsrc/utils/Stats.cpp | 45 +++++++++++++--------- 4 files changed, 35 insertions(+), 19 deletions(-) diff --git a/include/hyperion/Hyperion.h b/include/hyperion/Hyperion.h index 94154f80..af587b12 100644 --- a/include/hyperion/Hyperion.h +++ b/include/hyperion/Hyperion.h @@ -181,6 +181,9 @@ public: QJsonObject getConfig() { return _qjsonConfig; }; + /// unique id per instance + QString id; + int getLatchTime() const; public slots: diff --git a/include/utils/Stats.h b/include/utils/Stats.h index 76c68518..d2c2556c 100644 --- a/include/utils/Stats.h +++ b/include/utils/Stats.h @@ -8,17 +8,19 @@ // hyperion includes #include +#include class Stats : public QObject { Q_OBJECT public: - Stats(); + Stats(); ~Stats(); private: Logger* _log; + Hyperion* _hyperion; QString _hash = ""; QByteArray _ba; QNetworkRequest _req; @@ -27,6 +29,7 @@ private: bool trigger(bool set = false); private slots: + void initialExec(); void sendHTTP(); void sendHTTPp(); void resolveReply(QNetworkReply *reply); diff --git a/libsrc/jsonserver/JsonClientConnection.cpp b/libsrc/jsonserver/JsonClientConnection.cpp index 8c60c0fb..a27d00f3 100644 --- a/libsrc/jsonserver/JsonClientConnection.cpp +++ b/libsrc/jsonserver/JsonClientConnection.cpp @@ -588,6 +588,7 @@ void JsonClientConnection::handleSysInfoCommand(const QJsonObject&, const QStrin hyperion["version" ] = QString(HYPERION_VERSION); hyperion["build" ] = QString(HYPERION_BUILD_ID); hyperion["time" ] = QString(__DATE__ " " __TIME__); + hyperion["id" ] = _hyperion->id; info["hyperion"] = hyperion; // send the result diff --git a/libsrc/utils/Stats.cpp b/libsrc/utils/Stats.cpp index 6b701c7f..8f3562ed 100644 --- a/libsrc/utils/Stats.cpp +++ b/libsrc/utils/Stats.cpp @@ -2,7 +2,6 @@ #include #include #include -#include // qt includes #include @@ -17,26 +16,30 @@ Stats::Stats() : QObject() , _log(Logger::getInstance("STATS")) + , _hyperion(Hyperion::getInstance()) { // generate hash foreach(QNetworkInterface interface, QNetworkInterface::allInterfaces()) { - if (!(interface.flags() & QNetworkInterface::IsLoopBack)) + if (!(interface.flags() & QNetworkInterface::IsLoopBack)) { + _hyperion->id = QString(QCryptographicHash::hash(interface.hardwareAddress().toLocal8Bit().append(_hyperion->getConfigFile().toLocal8Bit()),QCryptographicHash::Sha1).toHex()); _hash = QString(QCryptographicHash::hash(interface.hardwareAddress().toLocal8Bit(),QCryptographicHash::Sha1).toHex()); break; } } - + // stop reporting if not found if(_hash.isEmpty()) { Warning(_log, "No interface found, abort"); + // fallback id + _hyperion->id = QString(QCryptographicHash::hash(_hyperion->getConfigFile().toLocal8Bit(),QCryptographicHash::Sha1).toHex()); return; } - + // prepare content - QJsonObject config = Hyperion::getInstance()->getConfig(); + QJsonObject config = _hyperion->getConfig(); SysInfo::HyperionSysInfo data = SysInfo::get(); QJsonObject system; @@ -47,7 +50,8 @@ Stats::Stats() system["pName" ] = data.prettyName; system["version" ] = QString(HYPERION_VERSION); system["device" ] = LedDevice::activeDevice(); - system["id" ] = _hash; + system["id" ] = _hyperion->id; + system["hw_id" ] = _hash; system["ledCount" ] = QString::number(Hyperion::getInstance()->getLedCount()); system["comp_sm" ] = config["smoothing"].toObject().take("enable"); system["comp_bb" ] = config["blackborderdetector"].toObject().take("enable"); @@ -59,23 +63,20 @@ Stats::Stats() QJsonDocument doc(system); _ba = doc.toJson(); - + // QNetworkRequest Header _req.setRawHeader("Content-Type", "application/json"); _req.setRawHeader("Authorization", "Basic SHlwZXJpb25YbDQ5MlZrcXA6ZDQxZDhjZDk4ZjAwYjIw"); - + connect(&_mgr, SIGNAL(finished(QNetworkReply*)), this, SLOT(resolveReply(QNetworkReply*))); - + // 7 days interval QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(sendHTTP())); timer->start(604800000); - // check instant execution required - if(trigger()) - { - QTimer::singleShot(0,this, SLOT(sendHTTP())); - } + //delay initial check + QTimer::singleShot(60000, this, SLOT(initialExec())); } Stats::~Stats() @@ -83,6 +84,14 @@ Stats::~Stats() } +void Stats::initialExec() +{ + if(trigger()) + { + QTimer::singleShot(0,this, SLOT(sendHTTP())); + } +} + void Stats::sendHTTP() { _req.setUrl(QUrl("https://api.hyperion-project.org/api/stats")); @@ -90,8 +99,8 @@ void Stats::sendHTTP() } void Stats::sendHTTPp() -{ - _req.setUrl(QUrl("https://api.hyperion-project.org/api/stats/"+_hash)); +{ + _req.setUrl(QUrl("https://api.hyperion-project.org/api/stats/"+_hyperion->id)); _mgr.put(_req,_ba); } @@ -113,7 +122,7 @@ bool Stats::trigger(bool set) { QString path = QDir::homePath()+"/.hyperion/misc/"; QDir dir; - QFile file(path + "ts"); + QFile file(path + _hyperion->id); if(set && file.open(QIODevice::ReadWrite) ) { @@ -130,7 +139,7 @@ bool Stats::trigger(bool set) if (!file.exists()) { if(file.open(QIODevice::ReadWrite)) - { + { file.close(); return true; }