2020-04-26 11:30:30 +03:00
|
|
|
/**
|
|
|
|
* Battery Buddy, a Sailfish application to prolong battery lifetime
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019-2020 Matti Viljanen
|
|
|
|
*
|
|
|
|
* Battery Buddy 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.
|
|
|
|
*
|
|
|
|
* Battery Buddy 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 Battery Buddy. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* Author: Matti Viljanen
|
|
|
|
*/
|
|
|
|
#include "battery.h"
|
|
|
|
|
2021-07-25 20:54:27 +03:00
|
|
|
Battery::Battery(Logger* newLogger, bool loglevelSet, QCoreApplication *app, QObject *parent) : QObject(parent)
|
2020-04-26 11:30:30 +03:00
|
|
|
{
|
2021-04-17 22:01:19 +03:00
|
|
|
logger = newLogger;
|
|
|
|
settings = new Settings(logger, this);
|
2021-04-24 18:14:26 +03:00
|
|
|
|
|
|
|
// Read log level from config - if not already set
|
|
|
|
if(!loglevelSet) {
|
|
|
|
int logLevel = settings->getLogLevel();
|
|
|
|
logger->debug = (logLevel == 2);
|
|
|
|
logger->verbose = (logLevel > 1);
|
2021-04-24 18:27:37 +03:00
|
|
|
logE(QString("Log level set to %1").arg((logLevel == 0 ? "low" : (logLevel == 1 ? "medium" : "high"))));
|
2021-04-24 18:14:26 +03:00
|
|
|
}
|
|
|
|
|
2021-05-23 01:15:40 +03:00
|
|
|
chargeNotification = new MyNotification(this);
|
|
|
|
healthNotification = new MyNotification(this);
|
2022-03-20 20:37:47 +03:00
|
|
|
|
2022-08-07 22:42:29 +03:00
|
|
|
QStringList filenames;
|
|
|
|
|
|
|
|
// Battery charge percentage, number, e.g. 42
|
|
|
|
filenames << "/sys/class/power_supply/battery/capacity"
|
|
|
|
<< "/sys/class/power_supply/dollar_cove_battery/capacity";
|
|
|
|
foreach(const QString& file, filenames) {
|
|
|
|
if(!chargeFile && QFile::exists(file)) {
|
|
|
|
chargeFile = new QFile(file, this);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(chargeFile) logE("Battery charge file: " + chargeFile->fileName());
|
|
|
|
else logE("Battery charge file: not found!");
|
|
|
|
|
2022-08-07 22:48:07 +03:00
|
|
|
// Charging/discharging current in microamps, e.g. -1450000 (-145mA)
|
|
|
|
filenames.clear();
|
|
|
|
filenames << "/sys/class/power_supply/battery/current_now"
|
|
|
|
<< "/sys/class/power_supply/dollar_cove_battery/current_now";
|
|
|
|
|
|
|
|
foreach(const QString& file, filenames) {
|
|
|
|
if(!currentFile && QFile::exists(file)) {
|
|
|
|
currentFile = new QFile(file, this);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(currentFile) logE("Charging/discharging current file: " + currentFile->fileName());
|
|
|
|
else logE("Charging/discharging current file: not found!");
|
|
|
|
|
2022-08-07 22:42:29 +03:00
|
|
|
// Battery/charging status: charging, discharging, full, empty, unknown (others?)
|
|
|
|
filenames.clear();
|
|
|
|
filenames << "/sys/class/power_supply/battery/status"
|
|
|
|
<< "/sys/class/power_supply/dollar_cove_battery/status";
|
|
|
|
|
|
|
|
foreach(const QString& file, filenames) {
|
|
|
|
if(!stateFile && QFile::exists(file)) {
|
|
|
|
stateFile = new QFile(file, this);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(stateFile) logE("Status file: " + stateFile->fileName());
|
|
|
|
else logE("Status file: not found!");
|
2020-04-26 11:30:30 +03:00
|
|
|
|
2022-08-07 22:42:29 +03:00
|
|
|
// Charger connected, bool (number): 0 or 1
|
|
|
|
filenames.clear();
|
|
|
|
filenames << "/sys/class/power_supply/usb/present"
|
|
|
|
<< "/sys/class/power_supply/dollar_cove_charger/present";
|
2020-04-26 11:30:30 +03:00
|
|
|
|
2022-08-07 22:42:29 +03:00
|
|
|
foreach(const QString& file, filenames) {
|
|
|
|
if(!chargerConnectedFile && QFile::exists(file)) {
|
|
|
|
chargerConnectedFile = new QFile(file, this);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-04-26 11:30:30 +03:00
|
|
|
|
2022-08-07 22:42:29 +03:00
|
|
|
if(chargerConnectedFile) logE("Charger status file: " + chargerConnectedFile->fileName());
|
|
|
|
else logE("Charger status file: not found!");
|
2021-05-02 22:14:29 +03:00
|
|
|
|
2021-05-23 14:26:42 +03:00
|
|
|
// Number: temperature
|
2022-08-07 22:42:29 +03:00
|
|
|
filenames.clear();
|
|
|
|
filenames << "/sys/class/power_supply/battery/temp"
|
|
|
|
<< "/sys/class/power_supply/dollar_cove_battery/temp";
|
|
|
|
|
|
|
|
foreach(const QString& file, filenames) {
|
|
|
|
if(!temperatureFile && QFile::exists(file)) {
|
|
|
|
temperatureFile = new QFile(file, this);
|
|
|
|
break;
|
|
|
|
}
|
2021-05-23 14:26:42 +03:00
|
|
|
}
|
2022-08-07 22:42:29 +03:00
|
|
|
|
|
|
|
if(temperatureFile) logE("Battery temperature file: " + temperatureFile->fileName());
|
|
|
|
else logE("Battery temperature file: not found!");
|
2021-05-02 22:14:29 +03:00
|
|
|
|
2021-05-23 14:26:42 +03:00
|
|
|
// String: health state
|
2022-08-07 22:42:29 +03:00
|
|
|
filenames.clear();
|
|
|
|
filenames << "/sys/class/power_supply/battery/health"
|
|
|
|
<< "/sys/class/power_supply/dollar_cove_battery/health";
|
|
|
|
foreach(const QString& file, filenames) {
|
|
|
|
if(!healthFile && QFile::exists(file)) {
|
|
|
|
healthFile = new QFile(file, this);
|
|
|
|
break;
|
|
|
|
}
|
2021-05-23 14:26:42 +03:00
|
|
|
}
|
2022-08-07 22:42:29 +03:00
|
|
|
|
|
|
|
if(healthFile) logE("Battery health file: " + healthFile->fileName());
|
|
|
|
else logE("Battery health file: not found!");
|
|
|
|
|
|
|
|
// Charger control file
|
|
|
|
filenames.clear();
|
|
|
|
filenames << "/sys/class/power_supply/battery/input_suspend" // e.g. Sony Xperia XA2
|
|
|
|
<< "/sys/class/power_supply/battery/charging_enabled" // e.g. for Sony Xperia Z3 Compact Tablet
|
|
|
|
<< "/sys/class/power_supply/usb/charger_disable" // e.g. for Jolla Phone
|
|
|
|
<< "/sys/class/power_supply/dollar_cove_battery/enable_charging"; // e.g. for Jolla Tablet
|
|
|
|
|
|
|
|
foreach(const QString& file, filenames) {
|
|
|
|
if(!chargingEnabledFile && QFile::exists(file)) {
|
|
|
|
chargingEnabledFile = new QFile(file, this);
|
|
|
|
break;
|
|
|
|
}
|
2021-04-26 17:34:44 +03:00
|
|
|
}
|
2020-04-26 11:30:30 +03:00
|
|
|
|
2022-08-07 22:42:29 +03:00
|
|
|
// Flip the charging control bits if necessary
|
|
|
|
if(chargingEnabledFile && chargingEnabledFile->fileName().contains("enable")) {
|
2021-04-26 17:34:44 +03:00
|
|
|
enableChargingValue = 1;
|
|
|
|
disableChargingValue = 0;
|
|
|
|
}
|
2020-04-26 11:30:30 +03:00
|
|
|
|
|
|
|
// If we found a usable file, check that it is writable
|
2020-06-12 14:16:46 +03:00
|
|
|
if(chargingEnabledFile) {
|
2022-08-07 22:42:29 +03:00
|
|
|
logE("Charger control file: " + chargingEnabledFile->fileName());
|
2020-06-12 14:16:46 +03:00
|
|
|
if(chargingEnabledFile->open(QIODevice::WriteOnly)) {
|
|
|
|
chargingEnabledFile->close();
|
|
|
|
}
|
|
|
|
else {
|
2022-08-07 22:42:29 +03:00
|
|
|
logE("Charger control file is not writable - feature disabled");
|
2021-04-26 17:34:44 +03:00
|
|
|
delete chargingEnabledFile;
|
|
|
|
chargingEnabledFile = Q_NULLPTR;
|
2020-06-12 14:16:46 +03:00
|
|
|
}
|
2020-04-26 11:30:30 +03:00
|
|
|
}
|
2022-03-19 23:49:05 +03:00
|
|
|
else if(!QSysInfo::machineHostName().contains("SailfishEmul")) {
|
2021-04-26 17:34:44 +03:00
|
|
|
logE("Charger control file not found!");
|
|
|
|
logE("Please contact the developer with your device model!");
|
|
|
|
}
|
2022-08-07 22:42:29 +03:00
|
|
|
else logE("Charger control file: not found!");
|
2021-04-26 17:34:44 +03:00
|
|
|
|
2020-11-30 23:33:27 +03:00
|
|
|
connect(settings, SIGNAL(resetTimers()), this, SLOT(resetTimers()));
|
2021-07-25 20:54:27 +03:00
|
|
|
|
|
|
|
updateTimer = new BackgroundActivity(app);
|
|
|
|
highNotifyTimer = new BackgroundActivity(app);
|
|
|
|
lowNotifyTimer = new BackgroundActivity(app);
|
|
|
|
healthNotifyTimer = new BackgroundActivity(app);
|
|
|
|
|
|
|
|
connect(updateTimer, SIGNAL(running()), this, SLOT(updateData()));
|
|
|
|
connect(highNotifyTimer, SIGNAL(running()), this, SLOT(showHighNotification()));
|
|
|
|
connect(lowNotifyTimer, SIGNAL(running()), this, SLOT(showLowNotification()));
|
|
|
|
connect(healthNotifyTimer, SIGNAL(running()), this, SLOT(showHealthNotification()));
|
|
|
|
|
|
|
|
connect(updateTimer, SIGNAL(running()), updateTimer, SLOT(wait()));
|
|
|
|
connect(highNotifyTimer, SIGNAL(running()), highNotifyTimer, SLOT(wait()));
|
|
|
|
connect(lowNotifyTimer, SIGNAL(running()), lowNotifyTimer, SLOT(wait()));
|
|
|
|
connect(healthNotifyTimer, SIGNAL(running()), healthNotifyTimer, SLOT(wait()));
|
2020-04-26 18:20:28 +03:00
|
|
|
|
2020-11-30 23:41:09 +03:00
|
|
|
updateData();
|
2021-07-25 20:54:27 +03:00
|
|
|
|
|
|
|
updateTimer->setWakeupFrequency(BackgroundActivity::ThirtySeconds);
|
|
|
|
updateTimer->wait();
|
2020-12-27 17:21:15 +03:00
|
|
|
|
|
|
|
// If updateData() didn't start the timers
|
|
|
|
// aka. "charging" status didn't change
|
|
|
|
// (or if both times are disabled, actually)
|
|
|
|
// manually trigger the timer startup.
|
2021-07-25 20:54:27 +03:00
|
|
|
if(!highNotifyTimer->isWaiting() && !lowNotifyTimer->isWaiting() && !healthNotifyTimer->isWaiting()) {
|
2020-12-27 17:21:15 +03:00
|
|
|
resetTimers();
|
|
|
|
}
|
2020-04-26 11:30:30 +03:00
|
|
|
}
|
|
|
|
|
2021-07-25 20:54:27 +03:00
|
|
|
Battery::~Battery() {
|
|
|
|
updateTimer->stop();
|
|
|
|
highNotifyTimer->stop();
|
|
|
|
lowNotifyTimer->stop();
|
|
|
|
healthNotifyTimer->stop();
|
|
|
|
|
|
|
|
delete updateTimer;
|
|
|
|
delete highNotifyTimer;
|
|
|
|
delete lowNotifyTimer;
|
|
|
|
delete healthNotifyTimer;
|
|
|
|
}
|
2020-04-26 11:30:30 +03:00
|
|
|
|
|
|
|
void Battery::updateData()
|
|
|
|
{
|
|
|
|
if(chargeFile->open(QIODevice::ReadOnly)) {
|
|
|
|
nextCharge = chargeFile->readLine().trimmed().toInt();
|
|
|
|
if(nextCharge != charge) {
|
|
|
|
charge = nextCharge;
|
2021-04-17 23:42:22 +03:00
|
|
|
logV(QString("Battery: %1%").arg(charge));
|
2020-04-26 11:30:30 +03:00
|
|
|
}
|
|
|
|
chargeFile->close();
|
|
|
|
}
|
|
|
|
if(chargerConnectedFile->open(QIODevice::ReadOnly)) {
|
|
|
|
nextChargerConnected = chargerConnectedFile->readLine().trimmed().toInt();
|
|
|
|
if(nextChargerConnected != chargerConnected) {
|
|
|
|
chargerConnected = nextChargerConnected;
|
2021-04-26 17:34:44 +03:00
|
|
|
logV(QString("Charger: %1").arg(chargerConnected ? "connected" : "disconnected"));
|
2020-04-26 11:30:30 +03:00
|
|
|
}
|
|
|
|
chargerConnectedFile->close();
|
|
|
|
}
|
2022-08-07 22:48:07 +03:00
|
|
|
|
|
|
|
if(currentFile->open(QIODevice::ReadOnly)) {
|
|
|
|
nextCurrent = currentFile->readLine().trimmed().toInt();
|
|
|
|
if(nextCurrent != current) {
|
|
|
|
current = nextCurrent;
|
|
|
|
logV(QString("Current: %1mA").arg(current / 1000));
|
|
|
|
}
|
|
|
|
currentFile->close();
|
|
|
|
}
|
|
|
|
|
2020-04-26 11:30:30 +03:00
|
|
|
if(stateFile->open(QIODevice::ReadOnly)) {
|
|
|
|
nextState = (QString(stateFile->readLine().trimmed().toLower()));
|
|
|
|
if(nextState != state) {
|
|
|
|
state = nextState;
|
2021-04-26 17:34:44 +03:00
|
|
|
logV("State: " + state);
|
2020-12-27 17:21:15 +03:00
|
|
|
|
|
|
|
// Hide/show notification right away
|
|
|
|
resetTimers();
|
2020-04-26 11:30:30 +03:00
|
|
|
}
|
|
|
|
stateFile->close();
|
|
|
|
}
|
2021-04-24 18:27:37 +03:00
|
|
|
|
2021-05-23 14:26:42 +03:00
|
|
|
if(temperatureFile && temperatureFile->open(QIODevice::ReadOnly)) {
|
2021-05-02 22:14:29 +03:00
|
|
|
nextTemperature = temperatureFile->readLine().trimmed().toInt();
|
|
|
|
if(nextTemperature != temperature) {
|
2022-03-29 21:05:58 +03:00
|
|
|
if((nextTemperature / 10) != (temperature / 10)) {
|
|
|
|
logV(QString("Temperature: %1°C").arg(nextTemperature / 10));
|
|
|
|
}
|
2021-05-02 22:14:29 +03:00
|
|
|
temperature = nextTemperature;
|
|
|
|
}
|
|
|
|
temperatureFile->close();
|
|
|
|
}
|
2021-05-23 14:26:42 +03:00
|
|
|
|
|
|
|
if(healthFile && healthFile->open(QIODevice::ReadOnly)) {
|
2021-05-02 22:14:29 +03:00
|
|
|
nextHealth = (QString(healthFile->readLine().trimmed().toLower()));
|
|
|
|
if(nextHealth != health) {
|
|
|
|
health = nextHealth;
|
|
|
|
logV("Health: " + health);
|
|
|
|
|
|
|
|
// Hide/show notification right away
|
|
|
|
resetTimers();
|
|
|
|
}
|
|
|
|
healthFile->close();
|
|
|
|
}
|
|
|
|
|
2020-04-26 11:30:30 +03:00
|
|
|
if(chargingEnabledFile && settings->getLimitEnabled()) {
|
|
|
|
if(chargingEnabled && charge >= settings->getHighLimit()) {
|
2021-04-24 18:27:37 +03:00
|
|
|
logD("Disabling charging...");
|
2020-04-26 11:30:30 +03:00
|
|
|
setChargingEnabled(false);
|
|
|
|
}
|
|
|
|
else if(!chargingEnabled && charge <= settings->getLowLimit()) {
|
2021-04-24 18:27:37 +03:00
|
|
|
logD("Enabling charging...");
|
2020-04-26 11:30:30 +03:00
|
|
|
setChargingEnabled(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-30 23:33:27 +03:00
|
|
|
void Battery::resetTimers() {
|
2020-06-14 19:22:41 +03:00
|
|
|
highNotifyTimer->stop();
|
|
|
|
lowNotifyTimer->stop();
|
2021-05-02 22:14:29 +03:00
|
|
|
healthNotifyTimer->stop();
|
2021-04-17 23:42:22 +03:00
|
|
|
|
2021-07-25 20:54:27 +03:00
|
|
|
|
|
|
|
if(settings->getHighNotificationsInterval() > 0) {
|
|
|
|
highNotifyTimer->setWakeupFrequency(frequencies[settings->getHighNotificationsInterval()]);
|
|
|
|
logD(QString("High notifications frequency %1 => %2 seconds")
|
|
|
|
.arg(settings->getHighNotificationsInterval())
|
|
|
|
.arg(static_cast<int>(frequencies[settings->getHighNotificationsInterval()])));
|
2021-04-17 23:42:22 +03:00
|
|
|
logD("Starting high battery timer");
|
2021-07-25 20:54:27 +03:00
|
|
|
highNotifyTimer->wait();
|
2020-12-01 01:53:48 +03:00
|
|
|
showHighNotification();
|
2020-11-30 23:41:09 +03:00
|
|
|
}
|
|
|
|
else {
|
2021-04-17 23:42:22 +03:00
|
|
|
logD("High battery timer not started");
|
2020-11-30 23:41:09 +03:00
|
|
|
}
|
2021-04-17 23:42:22 +03:00
|
|
|
|
2021-07-25 20:54:27 +03:00
|
|
|
if(settings->getLowNotificationsInterval() > 0) {
|
|
|
|
lowNotifyTimer->setWakeupFrequency(frequencies[settings->getLowNotificationsInterval()]);
|
|
|
|
logD(QString("Low notifications frequency %1 => %2 seconds")
|
|
|
|
.arg(settings->getLowNotificationsInterval())
|
|
|
|
.arg(static_cast<int>(frequencies[settings->getLowNotificationsInterval()])));
|
2021-04-17 23:42:22 +03:00
|
|
|
logD("Start low battery timer");
|
2021-07-25 20:54:27 +03:00
|
|
|
lowNotifyTimer->wait();
|
2020-12-01 01:53:48 +03:00
|
|
|
showLowNotification();
|
2020-11-30 23:41:09 +03:00
|
|
|
}
|
|
|
|
else {
|
2021-04-17 23:42:22 +03:00
|
|
|
logD("Low battery timer not started");
|
2020-11-30 23:41:09 +03:00
|
|
|
}
|
2021-05-02 22:14:29 +03:00
|
|
|
|
2021-07-25 20:54:27 +03:00
|
|
|
if(settings->getHealthNotificationsInterval() > 0) {
|
|
|
|
healthNotifyTimer->setWakeupFrequency(frequencies[settings->getHealthNotificationsInterval()]);
|
|
|
|
logD(QString("Health notifications frequency %1 => %2 seconds")
|
|
|
|
.arg(settings->getHealthNotificationsInterval())
|
|
|
|
.arg(static_cast<int>(frequencies[settings->getHealthNotificationsInterval()])));
|
2021-05-02 22:14:29 +03:00
|
|
|
logD("Start health timer");
|
2021-07-25 20:54:27 +03:00
|
|
|
healthNotifyTimer->wait();
|
2021-05-02 22:14:29 +03:00
|
|
|
showHealthNotification();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
logD("Health timer not started");
|
|
|
|
}
|
2020-04-26 18:20:28 +03:00
|
|
|
}
|
|
|
|
|
2020-06-14 19:22:41 +03:00
|
|
|
void Battery::showHighNotification() {
|
2021-07-25 20:54:27 +03:00
|
|
|
if(settings->getHighNotificationsInterval() > 0 && (charge >= settings->getHighAlert() && state != "discharging")
|
2020-06-14 19:22:41 +03:00
|
|
|
&& !(charge == 100 && state == "idle")) {
|
2021-04-24 18:27:37 +03:00
|
|
|
logV(QString("Notification: %1").arg(settings->getNotificationTitle().arg(charge)));
|
2021-05-23 01:15:40 +03:00
|
|
|
chargeNotification->send(settings->getNotificationTitle().arg(charge), settings->getNotificationHighText(), settings->getHighAlertFile());
|
2022-03-20 20:17:50 +03:00
|
|
|
unclosedChargeNotification = true;
|
2020-12-01 01:53:48 +03:00
|
|
|
if(settings->getHighNotificationsInterval() == 50) {
|
2021-04-17 23:42:22 +03:00
|
|
|
logD("Stop high battery timer");
|
2020-11-30 23:41:09 +03:00
|
|
|
highNotifyTimer->stop();
|
|
|
|
}
|
2020-06-14 19:22:41 +03:00
|
|
|
}
|
2022-03-20 20:17:50 +03:00
|
|
|
else if(unclosedChargeNotification && charge > settings->getLowAlert()) {
|
2021-04-17 23:42:22 +03:00
|
|
|
logD("Close high battery notification");
|
2021-05-23 01:15:40 +03:00
|
|
|
chargeNotification->close();
|
2022-03-20 20:17:50 +03:00
|
|
|
unclosedChargeNotification = true;
|
2020-06-14 19:22:41 +03:00
|
|
|
}
|
|
|
|
}
|
2020-04-26 18:20:28 +03:00
|
|
|
|
2020-06-14 19:22:41 +03:00
|
|
|
void Battery::showLowNotification() {
|
2021-07-25 20:54:27 +03:00
|
|
|
if(settings->getLowNotificationsInterval() > 0 && charge <= settings->getLowAlert() && state != "charging") {
|
2021-04-24 18:27:37 +03:00
|
|
|
logV(QString("Notification: %1").arg(settings->getNotificationTitle().arg(charge)));
|
2021-05-23 01:15:40 +03:00
|
|
|
chargeNotification->send(settings->getNotificationTitle().arg(charge), settings->getNotificationLowText(), settings->getLowAlertFile());
|
2022-03-20 20:17:50 +03:00
|
|
|
unclosedChargeNotification = true;
|
2020-11-30 23:41:09 +03:00
|
|
|
if(settings->getLowNotificationsInterval() == 50) {
|
2021-04-17 23:42:22 +03:00
|
|
|
logD("Stop low battery timer");
|
2020-11-30 23:41:09 +03:00
|
|
|
lowNotifyTimer->stop();
|
|
|
|
}
|
2020-04-26 18:20:28 +03:00
|
|
|
}
|
2022-03-20 20:17:50 +03:00
|
|
|
else if(unclosedChargeNotification && charge < settings->getHighAlert()) {
|
2021-04-17 23:42:22 +03:00
|
|
|
logD("Close low battery notification");
|
2021-05-23 01:15:40 +03:00
|
|
|
chargeNotification->close();
|
2022-03-20 20:17:50 +03:00
|
|
|
unclosedChargeNotification = true;
|
2020-04-26 18:20:28 +03:00
|
|
|
}
|
|
|
|
}
|
2020-06-14 19:22:41 +03:00
|
|
|
|
2021-05-02 22:14:29 +03:00
|
|
|
void Battery::showHealthNotification() {
|
|
|
|
// set up alert categories
|
|
|
|
// TODO: manage this more globally, use better data types(?), align with QML/Settings part
|
|
|
|
static const QMap<QString, int> HealthThresh {
|
|
|
|
{ "ok" , 0},
|
|
|
|
{ "warn" , 1},
|
|
|
|
{ "crit" , 2},
|
|
|
|
};
|
|
|
|
// map string values from sysfs file to alert category
|
|
|
|
static const QMap<QString, int> HealthState {
|
2021-05-22 23:42:57 +03:00
|
|
|
{ "unknown" , HealthThresh["ok"] },
|
2021-05-02 22:14:29 +03:00
|
|
|
{ "good" , HealthThresh["ok"] },
|
|
|
|
{ "warm" , HealthThresh["warn"] },
|
|
|
|
{ "cool" , HealthThresh["warn"] },
|
|
|
|
{ "overheat" , HealthThresh["crit"] },
|
|
|
|
{ "cold" , HealthThresh["crit"] }
|
|
|
|
};
|
2021-07-25 20:54:27 +03:00
|
|
|
if(settings->getHealthNotificationsInterval() > 0 && temperature != 0x7FFFFFFF && ( HealthState[health] != HealthThresh["ok"] && HealthState[health] >= settings->getHealthAlert() ) ) {
|
2021-05-02 22:14:29 +03:00
|
|
|
QString displayTemp = QString::number(temperature / 10.0);
|
|
|
|
if (QLocale().measurementSystem() == QLocale::ImperialUSSystem)
|
|
|
|
displayTemp = QString::number((temperature / 10) * 1.8 + 32) + " F";
|
|
|
|
|
|
|
|
QString titleArgs;
|
|
|
|
titleArgs = health + " (" + displayTemp + "), " + state; // might show other things in the future
|
|
|
|
|
|
|
|
// show different test depending on severity
|
|
|
|
QString notificationText = "";
|
|
|
|
if (HealthState[health] == HealthThresh["warn"]) {
|
|
|
|
notificationText = settings->getNotificationHealthWarnText();
|
|
|
|
} else if (HealthState[health] == HealthThresh["crit"]) {
|
|
|
|
notificationText = settings->getNotificationHealthCritText();
|
|
|
|
}
|
|
|
|
logD(QString("Notification: %1").arg(settings->getNotificationHealthTitle().arg(titleArgs)));
|
2021-05-23 01:15:40 +03:00
|
|
|
healthNotification->send(settings->getNotificationHealthTitle().arg(titleArgs), notificationText, settings->getHealthAlertFile());
|
2022-03-20 20:17:50 +03:00
|
|
|
unclosedHealthNotification = true;
|
2021-05-02 22:14:29 +03:00
|
|
|
if(settings->getHealthNotificationsInterval() == 50) {
|
|
|
|
logD("Stop health timer");
|
|
|
|
healthNotifyTimer->stop();
|
|
|
|
}
|
|
|
|
}
|
2022-03-20 20:17:50 +03:00
|
|
|
else if(unclosedHealthNotification && (HealthState[health] == HealthThresh["ok"] || HealthState[health] < settings->getHealthAlert())) {
|
2021-05-02 22:14:29 +03:00
|
|
|
logD("Close health notification");
|
2021-05-23 01:15:40 +03:00
|
|
|
healthNotification->close();
|
2021-05-02 22:14:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-26 18:20:28 +03:00
|
|
|
int Battery::getCharge() { return charge; }
|
2020-04-26 11:30:30 +03:00
|
|
|
|
|
|
|
QString Battery::getState() { return state; }
|
|
|
|
|
2021-05-02 22:14:29 +03:00
|
|
|
int Battery::getTemperature() { return temperature; }
|
|
|
|
|
|
|
|
QString Battery::getHealth() { return health; }
|
|
|
|
|
2020-04-26 11:30:30 +03:00
|
|
|
bool Battery::getChargingEnabled() { return chargingEnabled; }
|
|
|
|
|
2021-04-17 22:13:57 +03:00
|
|
|
bool Battery::setChargingEnabled(const bool isEnabled) {
|
2020-06-12 14:26:00 +03:00
|
|
|
bool success = false;
|
2020-06-12 14:16:46 +03:00
|
|
|
if(chargingEnabledFile) {
|
|
|
|
if(chargingEnabledFile->open(QIODevice::WriteOnly)) {
|
|
|
|
if(chargingEnabledFile->write(QString("%1").arg(isEnabled ? enableChargingValue : disableChargingValue).toLatin1())) {
|
|
|
|
chargingEnabled = isEnabled;
|
2020-06-12 14:26:00 +03:00
|
|
|
success = true;
|
2020-06-12 14:16:46 +03:00
|
|
|
|
|
|
|
if(isEnabled) {
|
2021-04-17 22:01:19 +03:00
|
|
|
logV("Charging resumed");
|
2020-06-12 14:16:46 +03:00
|
|
|
}
|
|
|
|
else {
|
2021-04-17 22:01:19 +03:00
|
|
|
logV("Charging paused");
|
2020-06-12 14:16:46 +03:00
|
|
|
}
|
2020-04-26 11:30:30 +03:00
|
|
|
}
|
|
|
|
else {
|
2021-04-17 23:42:22 +03:00
|
|
|
logE("Could not write new charger state");
|
2020-04-26 11:30:30 +03:00
|
|
|
}
|
2020-06-12 14:16:46 +03:00
|
|
|
chargingEnabledFile->close();
|
|
|
|
}
|
|
|
|
else {
|
2021-04-17 23:42:22 +03:00
|
|
|
logE("Could not open charger control file");
|
2020-04-26 11:30:30 +03:00
|
|
|
}
|
|
|
|
}
|
2020-06-12 14:26:00 +03:00
|
|
|
return success;
|
2020-04-26 11:30:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Battery::getChargerConnected() {
|
|
|
|
return chargerConnected;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Battery::shutdown() {
|
2021-04-26 17:36:02 +03:00
|
|
|
logV("Shutting down...");
|
2021-05-23 01:15:40 +03:00
|
|
|
chargeNotification->close();
|
2020-04-26 18:20:28 +03:00
|
|
|
blockSignals(true);
|
|
|
|
if(updateTimer) {
|
|
|
|
updateTimer->stop();
|
2021-04-17 22:01:19 +03:00
|
|
|
logD("Update timer stopped");
|
2020-04-26 18:20:28 +03:00
|
|
|
}
|
2020-06-14 19:22:41 +03:00
|
|
|
if(highNotifyTimer) {
|
|
|
|
highNotifyTimer->stop();
|
2021-04-17 22:01:19 +03:00
|
|
|
logD("High battery notification stopped");
|
2020-06-14 19:22:41 +03:00
|
|
|
}
|
|
|
|
if(lowNotifyTimer) {
|
|
|
|
lowNotifyTimer->stop();
|
2021-04-17 22:01:19 +03:00
|
|
|
logD("Low battery notification stopped");
|
2020-04-26 18:39:27 +03:00
|
|
|
}
|
2021-05-02 22:14:29 +03:00
|
|
|
if(healthNotifyTimer) {
|
|
|
|
healthNotifyTimer->stop();
|
|
|
|
logD("Health notification stopped");
|
|
|
|
}
|
2021-04-09 23:34:06 +03:00
|
|
|
// ENABLE/DISABLE CHARGING
|
2022-03-19 23:49:05 +03:00
|
|
|
if(!setChargingEnabled(true) && !QSysInfo::machineHostName().contains("SailfishEmul")) {
|
2021-04-17 23:42:22 +03:00
|
|
|
logE("ERROR! Could not restore charger status! Your device "
|
2021-04-26 17:34:44 +03:00
|
|
|
"may not charge until reboot! If that doesn't help, "
|
|
|
|
"uninstall Battery Buddy and reboot your device.");
|
2020-06-12 14:32:25 +03:00
|
|
|
}
|
2020-04-26 11:30:30 +03:00
|
|
|
}
|