1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
/*
* SPDX-FileCopyrightText: 2015 David Edmundson <david@davidedmundson.co.uk>
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
*/
#include "statisticsprovider.h"
#include <QDBusArgument>
#include <QDBusConnection>
#include <QDBusInterface>
#include <QDBusMessage>
#include <QDBusMetaType> // qDBusRegisterMetaType
#include <QDBusPendingReply>
#include <QDebug>
const QDBusArgument &operator<<(QDBusArgument &argument, const HistoryReply &data)
{
argument.beginStructure();
argument << data.time << data.value << data.charging;
argument.endStructure();
return argument;
}
const QDBusArgument &operator>>(const QDBusArgument &arg, HistoryReply &attrs)
{
arg.beginStructure();
arg >> attrs.time >> attrs.value >> attrs.charging;
arg.endStructure();
return arg;
}
StatisticsProvider::StatisticsProvider(QObject *parent)
: QObject(parent)
{
m_type = StatisticsProvider::ChargeType;
m_duration = 120;
qDBusRegisterMetaType<HistoryReply>();
qDBusRegisterMetaType<QList<HistoryReply>>();
}
void StatisticsProvider::setDevice(const QString &device)
{
if (device == m_device) {
return;
}
m_device = device;
Q_EMIT deviceChanged();
load();
}
void StatisticsProvider::setDuration(uint duration)
{
if (duration == m_duration) {
return;
}
m_duration = duration;
Q_EMIT durationChanged();
load();
}
void StatisticsProvider::setType(StatisticsProvider::HistoryType type)
{
if (m_type == type) {
return;
}
m_type = type;
Q_EMIT typeChanged();
load();
}
void StatisticsProvider::classBegin()
{
}
void StatisticsProvider::componentComplete()
{
m_isComplete = true;
load();
}
QVariantList StatisticsProvider::asPoints() const
{
QVariantList points;
points.reserve(m_values.count());
for (const HistoryReply &h : m_values) {
points.append(QPointF(h.time, h.value));
}
if (!points.isEmpty()) {
points.takeLast();
}
return points;
}
int StatisticsProvider::count() const
{
return m_values.count();
}
int StatisticsProvider::firstDataPointTime() const
{
if (m_values.isEmpty()) {
return 0;
}
return m_values.first().time;
}
int StatisticsProvider::lastDataPointTime() const
{
if (m_values.isEmpty()) {
return 0;
}
return m_values.last().time;
}
int StatisticsProvider::largestValue() const
{
if (m_values.isEmpty()) {
return 0;
}
int max = 0; // TODO std::max or something?
for (auto it = m_values.constBegin(), end = m_values.constEnd(); it != end; ++it) {
if ((*it).value > max) {
max = (*it).value;
}
}
return max;
}
void StatisticsProvider::refresh()
{
load();
}
void StatisticsProvider::load()
{
if (!m_isComplete || m_device.isEmpty()) {
return;
}
auto msg = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.UPower"),
m_device,
QStringLiteral("org.freedesktop.UPower.Device"),
QStringLiteral("GetHistory"));
if (m_type == RateType) {
msg << QLatin1String("rate");
} else { // m_type must = ChargeType
msg << QLatin1String("charge");
}
uint resolution = 100;
msg << m_duration << resolution;
QDBusPendingReply<QList<HistoryReply>> reply = QDBusConnection::systemBus().asyncCall(msg);
auto *watcher = new QDBusPendingCallWatcher(reply, this);
QObject::connect(watcher, &QDBusPendingCallWatcher::finished, this, [this](QDBusPendingCallWatcher *watcher) {
QDBusPendingReply<QList<HistoryReply>> reply = *watcher;
watcher->deleteLater();
m_values.clear();
if (reply.isError()) {
qWarning() << "Failed to get device history from UPower" << reply.error().message();
return;
}
for (const HistoryReply &r : reply.value()) {
if (r.value > 0) { // we get back some values which contain no value, possibly to indicate if charging changes, ignore them
m_values.prepend(r);
}
}
Q_EMIT dataChanged();
});
}
|