2017-05-29 15:59:11 +02:00
|
|
|
#include <utils/Stats.h>
|
|
|
|
#include <utils/SysInfo.h>
|
|
|
|
#include <HyperionConfig.h>
|
|
|
|
#include <leddevice/LedDevice.h>
|
|
|
|
|
|
|
|
// qt includes
|
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QNetworkInterface>
|
|
|
|
#include <QCryptographicHash>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QDateTime>
|
|
|
|
|
|
|
|
Stats::Stats()
|
|
|
|
: QObject()
|
|
|
|
, _log(Logger::getInstance("STATS"))
|
2017-06-17 23:27:41 +02:00
|
|
|
, _hyperion(Hyperion::getInstance())
|
2017-05-29 15:59:11 +02:00
|
|
|
{
|
|
|
|
// generate hash
|
|
|
|
foreach(QNetworkInterface interface, QNetworkInterface::allInterfaces())
|
|
|
|
{
|
2017-06-17 23:27:41 +02:00
|
|
|
if (!(interface.flags() & QNetworkInterface::IsLoopBack))
|
2017-05-29 15:59:11 +02:00
|
|
|
{
|
2018-12-27 23:11:32 +01:00
|
|
|
_hyperion->setId(QString(QCryptographicHash::hash(interface.hardwareAddress().toLocal8Bit().append(_hyperion->getConfigFileName().toLocal8Bit()),QCryptographicHash::Sha1).toHex()));
|
2017-05-29 15:59:11 +02:00
|
|
|
_hash = QString(QCryptographicHash::hash(interface.hardwareAddress().toLocal8Bit(),QCryptographicHash::Sha1).toHex());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-06-17 23:27:41 +02:00
|
|
|
|
2017-05-29 15:59:11 +02:00
|
|
|
// stop reporting if not found
|
|
|
|
if(_hash.isEmpty())
|
|
|
|
{
|
|
|
|
Warning(_log, "No interface found, abort");
|
2017-06-17 23:27:41 +02:00
|
|
|
// fallback id
|
2018-12-27 23:11:32 +01:00
|
|
|
_hyperion->setId(QString(QCryptographicHash::hash(_hyperion->getConfigFileName().toLocal8Bit(),QCryptographicHash::Sha1).toHex()));
|
2017-05-29 15:59:11 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-06-17 23:27:41 +02:00
|
|
|
|
2017-05-29 21:34:47 +02:00
|
|
|
// prepare content
|
2018-12-27 23:11:32 +01:00
|
|
|
QJsonObject config = _hyperion->getQJsonConfig();
|
2017-05-29 15:59:11 +02:00
|
|
|
SysInfo::HyperionSysInfo data = SysInfo::get();
|
|
|
|
|
|
|
|
QJsonObject system;
|
|
|
|
system["kType" ] = data.kernelType;
|
|
|
|
system["arch" ] = data.architecture;
|
|
|
|
system["pType" ] = data.productType;
|
|
|
|
system["pVersion" ] = data.productVersion;
|
|
|
|
system["pName" ] = data.prettyName;
|
|
|
|
system["version" ] = QString(HYPERION_VERSION);
|
2018-12-27 23:11:32 +01:00
|
|
|
system["device" ] = Hyperion::getInstance()->getActiveDevice();
|
|
|
|
system["id" ] = _hyperion->getId();
|
2017-06-17 23:27:41 +02:00
|
|
|
system["hw_id" ] = _hash;
|
2017-05-29 15:59:11 +02:00
|
|
|
system["ledCount" ] = QString::number(Hyperion::getInstance()->getLedCount());
|
|
|
|
system["comp_sm" ] = config["smoothing"].toObject().take("enable");
|
|
|
|
system["comp_bb" ] = config["blackborderdetector"].toObject().take("enable");
|
|
|
|
system["comp_fw" ] = config["forwarder"].toObject().take("enable");
|
|
|
|
system["comp_udpl" ] = config["udpListener"].toObject().take("enable");
|
|
|
|
system["comp_bobl" ] = config["boblightServer"].toObject().take("enable");
|
|
|
|
system["comp_pc" ] = config["framegrabber"].toObject().take("enable");
|
|
|
|
system["comp_uc" ] = config["grabberV4L2"].toArray().at(0).toObject().take("enable");
|
|
|
|
|
|
|
|
QJsonDocument doc(system);
|
2017-05-29 21:34:47 +02:00
|
|
|
_ba = doc.toJson();
|
2017-06-17 23:27:41 +02:00
|
|
|
|
2017-05-29 21:34:47 +02:00
|
|
|
// QNetworkRequest Header
|
|
|
|
_req.setRawHeader("Content-Type", "application/json");
|
|
|
|
_req.setRawHeader("Authorization", "Basic SHlwZXJpb25YbDQ5MlZrcXA6ZDQxZDhjZDk4ZjAwYjIw");
|
2017-06-17 23:27:41 +02:00
|
|
|
|
2017-05-29 21:34:47 +02:00
|
|
|
connect(&_mgr, SIGNAL(finished(QNetworkReply*)), this, SLOT(resolveReply(QNetworkReply*)));
|
2017-06-17 23:27:41 +02:00
|
|
|
|
2017-05-29 21:34:47 +02:00
|
|
|
// 7 days interval
|
|
|
|
QTimer *timer = new QTimer(this);
|
|
|
|
connect(timer, SIGNAL(timeout()), this, SLOT(sendHTTP()));
|
|
|
|
timer->start(604800000);
|
|
|
|
|
2017-06-17 23:27:41 +02:00
|
|
|
//delay initial check
|
|
|
|
QTimer::singleShot(60000, this, SLOT(initialExec()));
|
2017-05-29 15:59:11 +02:00
|
|
|
}
|
|
|
|
|
2017-05-29 21:34:47 +02:00
|
|
|
Stats::~Stats()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-06-17 23:27:41 +02:00
|
|
|
void Stats::initialExec()
|
|
|
|
{
|
|
|
|
if(trigger())
|
|
|
|
{
|
|
|
|
QTimer::singleShot(0,this, SLOT(sendHTTP()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-29 21:34:47 +02:00
|
|
|
void Stats::sendHTTP()
|
|
|
|
{
|
|
|
|
_req.setUrl(QUrl("https://api.hyperion-project.org/api/stats"));
|
|
|
|
_mgr.post(_req,_ba);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Stats::sendHTTPp()
|
2017-06-17 23:27:41 +02:00
|
|
|
{
|
2018-12-27 23:11:32 +01:00
|
|
|
_req.setUrl(QUrl("https://api.hyperion-project.org/api/stats/"+_hyperion->getId()));
|
2017-05-29 21:34:47 +02:00
|
|
|
_mgr.put(_req,_ba);
|
|
|
|
}
|
|
|
|
|
2017-05-29 15:59:11 +02:00
|
|
|
void Stats::resolveReply(QNetworkReply *reply)
|
|
|
|
{
|
|
|
|
if (reply->error() == QNetworkReply::NoError)
|
|
|
|
{
|
|
|
|
// update file timestamp
|
|
|
|
trigger(true);
|
|
|
|
// already created, update entry
|
|
|
|
if(reply->readAll().startsWith("null"))
|
|
|
|
{
|
2017-05-29 21:34:47 +02:00
|
|
|
QTimer::singleShot(0, this, SLOT(sendHTTPp()));
|
2017-05-29 15:59:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Stats::trigger(bool set)
|
|
|
|
{
|
2017-10-12 11:55:03 +02:00
|
|
|
QString path = _hyperion->getRootPath()+"/misc/";
|
2017-05-29 15:59:11 +02:00
|
|
|
QDir dir;
|
2018-12-27 23:11:32 +01:00
|
|
|
QFile file(path + _hyperion->getId());
|
2017-05-29 15:59:11 +02:00
|
|
|
|
|
|
|
if(set && file.open(QIODevice::ReadWrite) )
|
|
|
|
{
|
|
|
|
QTextStream stream( &file );
|
|
|
|
stream << "DO NOT DELETE" << endl;
|
|
|
|
file.close();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(!dir.exists(path))
|
|
|
|
{
|
|
|
|
dir.mkpath(path);
|
|
|
|
}
|
|
|
|
if (!file.exists())
|
|
|
|
{
|
|
|
|
if(file.open(QIODevice::ReadWrite))
|
2017-06-17 23:27:41 +02:00
|
|
|
{
|
2017-05-29 15:59:11 +02:00
|
|
|
file.close();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
QFileInfo info(file);
|
|
|
|
QDateTime newDate = QDateTime::currentDateTime();
|
|
|
|
QDateTime oldDate = info.lastModified();
|
|
|
|
int diff = oldDate.daysTo(newDate);
|
|
|
|
return diff >= 7 ? true : false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|