add more battery retalted keys

This commit is contained in:
Evgenii Alekseev 2017-08-24 01:20:27 +03:00
parent 081f7a3c88
commit ea5d73d8fd
4 changed files with 167 additions and 43 deletions

View File

@ -1,8 +1,6 @@
cmake_minimum_required(VERSION 2.8.12)
# some fucking magic
cmake_policy(SET CMP0003 OLD)
cmake_policy(SET CMP0002 OLD)
cmake_policy(SET CMP0011 NEW)
cmake_policy(SET CMP0015 NEW)
if (POLICY CMP0063)
@ -41,40 +39,23 @@ option(BUILD_TESTING "Build with additional test abilities" OFF)
set(PROJECT_CHANGELOG "Changelog" CACHE INTERNAL "")
include(changelog.cmake)
# flags
if (CMAKE_COMPILER_IS_GNUCXX)
set(CMAKE_CXX_FLAGS "-Wall -Wno-cpp -std=c++11")
set(CMAKE_CXX_FLAGS_DEBUG "-g -O0")
set(CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG")
set(CMAKE_CXX_FLAGS_OPTIMIZATION "-Ofast -DNDEBUG")
# avoid newer gcc warnings
add_definitions(-D_DEFAULT_SOURCE)
elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(CMAKE_CXX_FLAGS "-Wall -std=c++11")
set(CMAKE_CXX_FLAGS_DEBUG "-g -O0")
set(CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG")
set(CMAKE_CXX_FLAGS_OPTIMIZATION "-Ofast -DNDEBUG")
# linker flags
else ()
message(FATAL_ERROR "Unknown compiler")
endif ()
if (CMAKE_BUILD_TYPE MATCHES Debug)
set(CMAKE_VERBOSE_MAKEFILE ON)
endif ()
# required by successfully coverity and cppcheck build
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# directories
set(PROJECT_TRDPARTY_DIR ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty)
set(PROJECT_LIBRARY awesomewidgets)
set(PROJECT_MONITORSOURCES extsysmonsources)
# modules
include(compiler.cmake)
include(libraries.cmake)
include(clang-format.cmake)
include(cppcheck.cmake)
include(coverity.cmake)
# pre-configure
get_directory_property(CMAKE_DEFINITIONS COMPILE_DEFINITIONS)
configure_file(${CMAKE_SOURCE_DIR}/version.h.in ${CMAKE_CURRENT_BINARY_DIR}/version.h)
# build components
add_subdirectory(awesomewidgets)
add_subdirectory(extsysmonsources)
add_subdirectory(extsysmon)
@ -90,4 +71,3 @@ if (BUILD_TESTING)
endif ()
include(packages-recipe.cmake)

29
sources/compiler.cmake Normal file
View File

@ -0,0 +1,29 @@
# flags
if (CMAKE_COMPILER_IS_GNUCXX)
set(CMAKE_CXX_FLAGS "-Wall")
set(CMAKE_CXX_FLAGS_DEBUG "-g -O0")
set(CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG")
set(CMAKE_CXX_FLAGS_OPTIMIZATION "-Ofast -DNDEBUG")
# avoid newer gcc warnings
add_definitions(-D_DEFAULT_SOURCE)
elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(CMAKE_CXX_FLAGS "-Wall")
set(CMAKE_CXX_FLAGS_DEBUG "-g -O0")
set(CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG")
set(CMAKE_CXX_FLAGS_OPTIMIZATION "-Ofast -DNDEBUG")
# linker flags
else ()
message(FATAL_ERROR "Unknown compiler")
endif ()
# some flags
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# verbose output for debug builds
if (CMAKE_BUILD_TYPE MATCHES Debug)
set(CMAKE_VERBOSE_MAKEFILE ON)
endif ()
# required by successfully coverity and cppcheck build
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

View File

@ -31,6 +31,7 @@ BatterySource::BatterySource(QObject *_parent, const QStringList &_args)
m_acpiPath = _args.at(0);
m_sources = getSources();
m_timestamp = QDateTime::currentDateTimeUtc();
}
@ -45,14 +46,23 @@ QStringList BatterySource::getSources()
QStringList sources;
sources.append("battery/ac");
sources.append("battery/bat");
sources.append("battery/batleft");
sources.append("battery/batnow");
sources.append("battery/batrate");
sources.append("battery/battotal");
m_batteriesCount
= QDir(m_acpiPath)
.entryList(QStringList({"BAT*"}),
QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name)
.count();
qCInfo(LOG_ESS) << "Init batteries count as" << m_batteriesCount;
for (int i = 0; i < m_batteriesCount; i++)
for (int i = 0; i < m_batteriesCount; i++) {
sources.append(QString("battery/bat%1").arg(i));
sources.append(QString("battery/batleft%1").arg(i));
sources.append(QString("battery/batnow%1").arg(i));
sources.append(QString("battery/batrate%1").arg(i));
sources.append(QString("battery/battotal%1").arg(i));
}
qCInfo(LOG_ESS) << "Sources list" << sources;
return sources;
@ -87,7 +97,55 @@ QVariantMap BatterySource::initialData(const QString &_source) const
data["name"] = "Average battery usage";
data["type"] = "integer";
data["units"] = "%";
} else {
} else if (_source == "battery/batleft") {
data["min"] = 0;
data["max"] = 0;
data["name"] = "Battery discharge time";
data["type"] = "integer";
data["units"] = "s";
} else if (_source == "battery/batnow") {
data["min"] = 0;
data["max"] = 0;
data["name"] = "Current battery capacity";
data["type"] = "integer";
data["units"] = "";
} else if (_source == "battery/batrate") {
data["min"] = 0;
data["max"] = 0;
data["name"] = "Average battery discharge rate";
data["type"] = "float";
data["units"] = "1/s";
} else if (_source == "battery/battotal") {
data["min"] = 0;
data["max"] = 0;
data["name"] = "Full battery capacity";
data["type"] = "integer";
data["units"] = "";
} else if (_source == "battery/batleft") {
data["min"] = 0;
data["max"] = 0;
data["name"] = QString("Battery %1 discharge time").arg(index(_source));
data["type"] = "integer";
data["units"] = "s";
} else if (_source.startsWith("battery/batnow")) {
data["min"] = 0;
data["max"] = 0;
data["name"] = QString("Battery %1 capacity").arg(index(_source));
data["type"] = "integer";
data["units"] = "";
} else if (_source.startsWith("battery/battotal")) {
data["min"] = 0;
data["max"] = 0;
data["name"] = QString("Battery %1 full capacity").arg(index(_source));
data["type"] = "integer";
data["units"] = "";
} else if (_source.startsWith("battery/batrate")) {
data["min"] = 0;
data["max"] = 0;
data["name"] = QString("Battery %1 discharge rate").arg(index(_source));
data["type"] = "float";
data["units"] = "1/s";
} else if (_source.startsWith("battery/bat")) {
data["min"] = 0;
data["max"] = 100;
data["name"] = QString("Battery %1 usage").arg(index(_source));
@ -101,6 +159,9 @@ QVariantMap BatterySource::initialData(const QString &_source) const
void BatterySource::run()
{
// swap data
m_previousValues.swap(m_values);
// adaptor
QFile acFile(QString("%1/AC/online").arg(m_acpiPath));
if (acFile.open(QIODevice::ReadOnly | QIODevice::Text))
@ -109,28 +170,37 @@ void BatterySource::run()
acFile.close();
// batteries
float currentLevel = 0.0;
float fullLevel = 0.0;
float currentLevel = 0.0, fullLevel = 0.0;
for (int i = 0; i < m_batteriesCount; i++) {
// current level
QFile currentLevelFile(
QString("%1/BAT%2/energy_now").arg(m_acpiPath).arg(i));
if (currentLevelFile.open(QIODevice::ReadOnly | QIODevice::Text))
m_values[QString("battery/batnow%1").arg(i)]
= QString(currentLevelFile.readLine()).toInt();
currentLevelFile.close();
// total
QFile fullLevelFile(
QString("%1/BAT%2/energy_full").arg(m_acpiPath).arg(i));
if ((currentLevelFile.open(QIODevice::ReadOnly | QIODevice::Text))
&& (fullLevelFile.open(QIODevice::ReadOnly | QIODevice::Text))) {
float batCurrent
= QString(currentLevelFile.readLine()).trimmed().toFloat();
float batFull
= QString(fullLevelFile.readLine()).trimmed().toFloat();
m_values[QString("battery/bat%1").arg(i)]
= static_cast<int>(100 * batCurrent / batFull);
currentLevel += batCurrent;
fullLevel += batFull;
}
currentLevelFile.close();
if (fullLevelFile.open(QIODevice::ReadOnly | QIODevice::Text))
m_values[QString("battery/battotal%1").arg(i)]
= QString(fullLevelFile.readLine()).toInt();
fullLevelFile.close();
m_values[QString("battery/bat%1").arg(i)] = static_cast<int>(
100 * m_values[QString("battery/batnow%1").arg(i)].toFloat()
/ m_values[QString("battery/battotal%1").arg(i)].toFloat());
// accumulate
currentLevel += m_values[QString("battery/batnow%1").arg(i)].toFloat();
fullLevel += m_values[QString("battery/battotal%1").arg(i)].toFloat();
}
// total
m_values["battery/batnow"] = static_cast<int>(currentLevel);
m_values["battery/battotal"] = static_cast<int>(fullLevel);
m_values["battery/bat"] = static_cast<int>(100 * currentLevel / fullLevel);
calculateRates();
}
@ -138,3 +208,44 @@ QStringList BatterySource::sources() const
{
return m_sources;
}
void BatterySource::calculateRates()
{
// we are using moving average to get good numbers
// check time interval
auto now = QDateTime::currentDateTimeUtc();
auto interval
= 1000 * (now.toMSecsSinceEpoch() - m_timestamp.toMSecsSinceEpoch());
m_timestamp.swap(now);
// check time
if (m_previousValues.empty()) {
qCInfo(LOG_ESS) << "No historical data found for charges, exit";
return;
}
// per battery
for (int i = 0; i < m_batteriesCount; i++) {
auto rate
= (m_values[QString("battery/batnow%1").arg(i)].toInt()
- m_previousValues[QString("battery/batnow%1").arg(i)].toInt())
/ interval;
m_values[QString("battery/batrate%1").arg(i)]
= (m_previousValues[QString("battery/batrate%1").arg(i)].toInt()
+ rate)
/ 2;
m_values[QString("battery/batleft%1").arg(i)]
= m_values[QString("battery/batnow%1").arg(i)].toInt()
/ m_values[QString("battery/batrate%1").arg(i)].toInt();
}
// total
auto totalRate = (m_values["battery/batnow"].toInt()
- m_previousValues["battery/batnow"].toInt())
/ interval;
m_values["battery/batrate"]
= (totalRate + m_previousValues["battery/batrate"].toInt()) / 2;
m_values["battery/batleft"] = m_values["battery/batnow"].toInt()
/ m_values["battery/batrate"].toInt();
}

View File

@ -18,6 +18,7 @@
#ifndef BATTERYSOURCE_H
#define BATTERYSOURCE_H
#include <QDateTime>
#include <QObject>
#include "abstractextsysmonsource.h"
@ -37,11 +38,14 @@ public:
QStringList sources() const;
private:
void calculateRates();
// configuration and values
int m_batteriesCount = 0;
QString m_acpiPath;
QStringList m_sources;
QDateTime m_timestamp;
QVariantHash m_values;
QVariantHash m_previousValues;
};