initial import of telemetry handler

This commit is contained in:
Evgenii Alekseev 2016-09-07 10:23:05 +03:00
parent 65c8f552de
commit c5cb5cb359
3 changed files with 221 additions and 0 deletions

View File

@ -24,6 +24,7 @@
#include "awconfighelper.h"
#include "awformatterconfigfactory.h"
#include "awkeys.h"
#include "awtelemetryhandler.h"
void AWPlugin::registerTypes(const char *uri)
@ -36,4 +37,5 @@ void AWPlugin::registerTypes(const char *uri)
qmlRegisterType<AWFormatterConfigFactory>(uri, 1, 0,
"AWFormatterConfigFactory");
qmlRegisterType<AWKeys>(uri, 1, 0, "AWKeys");
qmlRegisterType<AWTelemetryHandler>(uri, 1, 0, "AWTelemetryHandler");
}

View File

@ -0,0 +1,165 @@
/***************************************************************************
* This file is part of awesome-widgets *
* *
* awesome-widgets is free software: you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation, either version 3 of the *
* License, or (at your option) any later version. *
* *
* awesome-widgets is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with awesome-widgets. If not, see http://www.gnu.org/licenses/ *
***************************************************************************/
#include "awtelemetryhandler.h"
#include <QJsonDocument>
#include <QJsonParseError>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QSettings>
#include <QStandardPaths>
#include <QUuid>
#include "awdebug.h"
AWTelemetryHandler::AWTelemetryHandler(QObject *parent)
: QObject(parent)
{
qCDebug(LOG_AW) << __PRETTY_FUNCTION__;
m_genericConfig = QString("%1/awesomewidgets/general.ini")
.arg(QStandardPaths::writableLocation(
QStandardPaths::GenericDataLocation));
m_localFile = QString("%1/awesomewidgets/telemetry.ini")
.arg(QStandardPaths::writableLocation(
QStandardPaths::GenericDataLocation));
init();
}
AWTelemetryHandler::~AWTelemetryHandler()
{
qCDebug(LOG_AW) << __PRETTY_FUNCTION__;
}
QStringList AWTelemetryHandler::get(const QString group) const
{
qCDebug(LOG_AW) << "Get stored data for group" << group;
QStringList values;
QSettings settings(m_localFile, QSettings::IniFormat);
settings.beginGroup(group);
for (auto key : settings.childKeys())
values.append(settings.value(key).toString());
settings.endGroup();
return values;
}
QString AWTelemetryHandler::getLast(const QString group) const
{
qCDebug(LOG_AW) << "Get last stored data for group" << group;
return get(group).last();
}
bool AWTelemetryHandler::put(const QString group, const QString value) const
{
qCDebug(LOG_AW) << "Store data with group" << group << "and value" << value;
QSettings settings(m_localFile, QSettings::IniFormat);
settings.beginGroup(group);
// values will be stored as num=value inside specified group
QString key
= QString("%1").arg(settings.childKeys().count(), 3, 10, QChar('0'));
settings.setValue(key, value);
settings.endGroup();
// sync settings
settings.sync();
// check status
if (settings.status() != QSettings::NoError)
return false;
// rotate
return rotate();
}
bool AWTelemetryHandler::rotate() const
{
QSettings settings(m_localFile, QSettings::IniFormat);
for (auto group : settings.childGroups()) {
QStringList data;
settings.beginGroup(group);
// load current data
for (auto key : settings.childKeys())
data.append(settings.value(key).toString());
// remove first item from list
while (data.count() > m_storeCount)
data.takeFirst();
// clear values
settings.remove(QString(""));
// save new values
for (auto value : data) {
QString key = QString("%1").arg(settings.childKeys().count(), 3, 10,
QChar('0'));
settings.setValue(key, value);
}
settings.endGroup();
}
// sync settings
settings.sync();
// return status
return (settings.status() == QSettings::NoError);
}
void AWTelemetryHandler::init()
{
QSettings settings(m_genericConfig, QSettings::IniFormat);
settings.beginGroup(QString("Telemetry"));
// unique client id
m_clientId
= settings.value(QString("ClientID"), QUuid::createUuid().toString())
.toString();
setConfiguration(QString("ClientID"), m_clientId, false);
// count items to store
m_storeCount = settings.value(QString("StoreHistory"), 100).toInt();
setConfiguration(QString("StoreHistory"), m_storeCount, false);
settings.endGroup();
}
bool AWTelemetryHandler::setConfiguration(const QString key,
const QVariant value,
const bool override) const
{
qCDebug(LOG_AW) << "Set configuration key" << key << "to value" << value
<< "force override enabled" << override;
QSettings settings(m_genericConfig, QSettings::IniFormat);
settings.beginGroup(QString("Telemetry"));
if (!settings.childKeys().contains(key) || !override)
return true;
settings.setValue(key, value);
settings.endGroup();
settings.sync();
return (settings.status() == QSettings::NoError);
}

View File

@ -0,0 +1,54 @@
/***************************************************************************
* This file is part of awesome-widgets *
* *
* awesome-widgets is free software: you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation, either version 3 of the *
* License, or (at your option) any later version. *
* *
* awesome-widgets is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with awesome-widgets. If not, see http://www.gnu.org/licenses/ *
***************************************************************************/
#ifndef AWTELEMETRYHANDLER_H
#define AWTELEMETRYHANDLER_H
#include <QObject>
#include <QtCore/QVariant>
class QAbstractButton;
class QNetworkReply;
class AWTelemetryHandler : public QObject
{
Q_OBJECT
public:
explicit AWTelemetryHandler(QObject *parent = nullptr);
virtual ~AWTelemetryHandler();
Q_INVOKABLE QStringList get(const QString group) const;
Q_INVOKABLE QString getLast(const QString group) const;
Q_INVOKABLE bool put(const QString group, const QString value) const;
Q_INVOKABLE bool rotate() const;
private slots:
private:
void init();
bool setConfiguration(const QString key, const QVariant value,
const bool override) const;
QString m_clientId;
QString m_genericConfig;
QString m_localFile;
int m_storeCount = 0;
};
#endif /* AWTELEMETRYHANDLER_H */