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) cmake_minimum_required(VERSION 2.8.12)
# some fucking magic # some fucking magic
cmake_policy(SET CMP0003 OLD)
cmake_policy(SET CMP0002 OLD)
cmake_policy(SET CMP0011 NEW) cmake_policy(SET CMP0011 NEW)
cmake_policy(SET CMP0015 NEW) cmake_policy(SET CMP0015 NEW)
if (POLICY CMP0063) if (POLICY CMP0063)
@ -41,40 +39,23 @@ option(BUILD_TESTING "Build with additional test abilities" OFF)
set(PROJECT_CHANGELOG "Changelog" CACHE INTERNAL "") set(PROJECT_CHANGELOG "Changelog" CACHE INTERNAL "")
include(changelog.cmake) include(changelog.cmake)
# flags # directories
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)
set(PROJECT_TRDPARTY_DIR ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty) set(PROJECT_TRDPARTY_DIR ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty)
set(PROJECT_LIBRARY awesomewidgets) set(PROJECT_LIBRARY awesomewidgets)
set(PROJECT_MONITORSOURCES extsysmonsources) set(PROJECT_MONITORSOURCES extsysmonsources)
# modules
include(compiler.cmake)
include(libraries.cmake) include(libraries.cmake)
include(clang-format.cmake) include(clang-format.cmake)
include(cppcheck.cmake) include(cppcheck.cmake)
include(coverity.cmake) include(coverity.cmake)
# pre-configure
get_directory_property(CMAKE_DEFINITIONS COMPILE_DEFINITIONS) get_directory_property(CMAKE_DEFINITIONS COMPILE_DEFINITIONS)
configure_file(${CMAKE_SOURCE_DIR}/version.h.in ${CMAKE_CURRENT_BINARY_DIR}/version.h) configure_file(${CMAKE_SOURCE_DIR}/version.h.in ${CMAKE_CURRENT_BINARY_DIR}/version.h)
# build components
add_subdirectory(awesomewidgets) add_subdirectory(awesomewidgets)
add_subdirectory(extsysmonsources) add_subdirectory(extsysmonsources)
add_subdirectory(extsysmon) add_subdirectory(extsysmon)
@ -90,4 +71,3 @@ if (BUILD_TESTING)
endif () endif ()
include(packages-recipe.cmake) 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_acpiPath = _args.at(0);
m_sources = getSources(); m_sources = getSources();
m_timestamp = QDateTime::currentDateTimeUtc();
} }
@ -45,14 +46,23 @@ QStringList BatterySource::getSources()
QStringList sources; QStringList sources;
sources.append("battery/ac"); sources.append("battery/ac");
sources.append("battery/bat"); sources.append("battery/bat");
sources.append("battery/batleft");
sources.append("battery/batnow");
sources.append("battery/batrate");
sources.append("battery/battotal");
m_batteriesCount m_batteriesCount
= QDir(m_acpiPath) = QDir(m_acpiPath)
.entryList(QStringList({"BAT*"}), .entryList(QStringList({"BAT*"}),
QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name) QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name)
.count(); .count();
qCInfo(LOG_ESS) << "Init batteries count as" << m_batteriesCount; 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/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; qCInfo(LOG_ESS) << "Sources list" << sources;
return sources; return sources;
@ -87,7 +97,55 @@ QVariantMap BatterySource::initialData(const QString &_source) const
data["name"] = "Average battery usage"; data["name"] = "Average battery usage";
data["type"] = "integer"; data["type"] = "integer";
data["units"] = "%"; 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["min"] = 0;
data["max"] = 100; data["max"] = 100;
data["name"] = QString("Battery %1 usage").arg(index(_source)); data["name"] = QString("Battery %1 usage").arg(index(_source));
@ -101,6 +159,9 @@ QVariantMap BatterySource::initialData(const QString &_source) const
void BatterySource::run() void BatterySource::run()
{ {
// swap data
m_previousValues.swap(m_values);
// adaptor // adaptor
QFile acFile(QString("%1/AC/online").arg(m_acpiPath)); QFile acFile(QString("%1/AC/online").arg(m_acpiPath));
if (acFile.open(QIODevice::ReadOnly | QIODevice::Text)) if (acFile.open(QIODevice::ReadOnly | QIODevice::Text))
@ -109,28 +170,37 @@ void BatterySource::run()
acFile.close(); acFile.close();
// batteries // batteries
float currentLevel = 0.0; float currentLevel = 0.0, fullLevel = 0.0;
float fullLevel = 0.0;
for (int i = 0; i < m_batteriesCount; i++) { for (int i = 0; i < m_batteriesCount; i++) {
// current level
QFile currentLevelFile( QFile currentLevelFile(
QString("%1/BAT%2/energy_now").arg(m_acpiPath).arg(i)); 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( QFile fullLevelFile(
QString("%1/BAT%2/energy_full").arg(m_acpiPath).arg(i)); QString("%1/BAT%2/energy_full").arg(m_acpiPath).arg(i));
if ((currentLevelFile.open(QIODevice::ReadOnly | QIODevice::Text)) if (fullLevelFile.open(QIODevice::ReadOnly | QIODevice::Text))
&& (fullLevelFile.open(QIODevice::ReadOnly | QIODevice::Text))) { m_values[QString("battery/battotal%1").arg(i)]
float batCurrent = QString(fullLevelFile.readLine()).toInt();
= 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();
fullLevelFile.close(); 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); m_values["battery/bat"] = static_cast<int>(100 * currentLevel / fullLevel);
calculateRates();
} }
@ -138,3 +208,44 @@ QStringList BatterySource::sources() const
{ {
return m_sources; 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 #ifndef BATTERYSOURCE_H
#define BATTERYSOURCE_H #define BATTERYSOURCE_H
#include <QDateTime>
#include <QObject> #include <QObject>
#include "abstractextsysmonsource.h" #include "abstractextsysmonsource.h"
@ -37,11 +38,14 @@ public:
QStringList sources() const; QStringList sources() const;
private: private:
void calculateRates();
// configuration and values // configuration and values
int m_batteriesCount = 0; int m_batteriesCount = 0;
QString m_acpiPath; QString m_acpiPath;
QStringList m_sources; QStringList m_sources;
QDateTime m_timestamp;
QVariantHash m_values; QVariantHash m_values;
QVariantHash m_previousValues;
}; };