From ea5d73d8fddd6db2f83515be851ae08dec4bcb1e Mon Sep 17 00:00:00 2001 From: Evgeniy Alekseev Date: Thu, 24 Aug 2017 01:20:27 +0300 Subject: [PATCH] add more battery retalted keys --- sources/CMakeLists.txt | 34 +---- sources/compiler.cmake | 29 +++++ sources/extsysmonsources/batterysource.cpp | 143 ++++++++++++++++++--- sources/extsysmonsources/batterysource.h | 4 + 4 files changed, 167 insertions(+), 43 deletions(-) create mode 100644 sources/compiler.cmake diff --git a/sources/CMakeLists.txt b/sources/CMakeLists.txt index f7209f0..80d2ebc 100644 --- a/sources/CMakeLists.txt +++ b/sources/CMakeLists.txt @@ -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) - diff --git a/sources/compiler.cmake b/sources/compiler.cmake new file mode 100644 index 0000000..9e6a05c --- /dev/null +++ b/sources/compiler.cmake @@ -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) diff --git a/sources/extsysmonsources/batterysource.cpp b/sources/extsysmonsources/batterysource.cpp index 1219de6..b658e61 100644 --- a/sources/extsysmonsources/batterysource.cpp +++ b/sources/extsysmonsources/batterysource.cpp @@ -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(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( + 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(currentLevel); + m_values["battery/battotal"] = static_cast(fullLevel); m_values["battery/bat"] = static_cast(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(); +} diff --git a/sources/extsysmonsources/batterysource.h b/sources/extsysmonsources/batterysource.h index c914213..9840193 100644 --- a/sources/extsysmonsources/batterysource.h +++ b/sources/extsysmonsources/batterysource.h @@ -18,6 +18,7 @@ #ifndef BATTERYSOURCE_H #define BATTERYSOURCE_H +#include #include #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; };