Added mining on battery setting

This commit is contained in:
SChernykh 2020-07-22 20:12:16 +02:00
parent 5bc89fdc8b
commit 299b180b28
10 changed files with 95 additions and 18 deletions

View file

@ -54,6 +54,8 @@ public:
static inline const char *userAgent() { return m_userAgent; }
static bool isOnBatteryPower();
private:
static char *createUserAgent();

View file

@ -29,6 +29,7 @@
#include <sys/resource.h>
#include <uv.h>
#include <thread>
#include <fstream>
#include "base/kernel/Platform.h"
@ -107,3 +108,18 @@ void xmrig::Platform::setThreadPriority(int priority)
setpriority(PRIO_PROCESS, 0, prio);
}
bool xmrig::Platform::isOnBatteryPower()
{
for (int i = 0; i <= 1; ++i) {
char buf[64];
snprintf(buf, 64, "/sys/class/power_supply/BAT%d/status", i);
std::ifstream f(buf);
if (f.is_open()) {
std::string status;
f >> status;
return (status == "Discharging");
}
}
return false;
}

View file

@ -38,6 +38,7 @@
#include <unistd.h>
#include <uv.h>
#include <thread>
#include <fstream>
#include "base/kernel/Platform.h"
@ -146,3 +147,19 @@ void xmrig::Platform::setThreadPriority(int priority)
}
# endif
}
bool xmrig::Platform::isOnBatteryPower()
{
for (int i = 0; i <= 1; ++i) {
char buf[64];
snprintf(buf, 64, "/sys/class/power_supply/BAT%d/status", i);
std::ifstream f(buf);
if (f.is_open()) {
std::string status;
f >> status;
return (status == "Discharging");
}
}
return false;
}

View file

@ -157,3 +157,12 @@ void xmrig::Platform::setThreadPriority(int priority)
SetThreadPriority(GetCurrentThread(), prio);
}
bool xmrig::Platform::isOnBatteryPower()
{
SYSTEM_POWER_STATUS st;
if (GetSystemPowerStatus(&st)) {
return (st.ACLineStatus == 0);
}
return false;
}

View file

@ -67,6 +67,7 @@ const char *BaseConfig::kTitle = "title";
const char *BaseConfig::kUserAgent = "user-agent";
const char *BaseConfig::kVerbose = "verbose";
const char *BaseConfig::kWatch = "watch";
const char *BaseConfig::kMineOnBattery = "mine-on-battery";
#ifdef XMRIG_FEATURE_TLS
@ -85,15 +86,16 @@ bool xmrig::BaseConfig::read(const IJsonReader &reader, const char *fileName)
return false;
}
m_autoSave = reader.getBool(kAutosave, m_autoSave);
m_background = reader.getBool(kBackground, m_background);
m_dryRun = reader.getBool(kDryRun, m_dryRun);
m_syslog = reader.getBool(kSyslog, m_syslog);
m_watch = reader.getBool(kWatch, m_watch);
m_logFile = reader.getString(kLogFile);
m_userAgent = reader.getString(kUserAgent);
m_printTime = std::min(reader.getUint(kPrintTime, m_printTime), 3600U);
m_title = reader.getValue(kTitle);
m_autoSave = reader.getBool(kAutosave, m_autoSave);
m_background = reader.getBool(kBackground, m_background);
m_dryRun = reader.getBool(kDryRun, m_dryRun);
m_syslog = reader.getBool(kSyslog, m_syslog);
m_watch = reader.getBool(kWatch, m_watch);
m_mineOnBattery = reader.getBool(kMineOnBattery, m_mineOnBattery);
m_logFile = reader.getString(kLogFile);
m_userAgent = reader.getString(kUserAgent);
m_printTime = std::min(reader.getUint(kPrintTime, m_printTime), 3600U);
m_title = reader.getValue(kTitle);
# ifdef XMRIG_FEATURE_TLS
m_tls = reader.getValue(kTls);

View file

@ -61,6 +61,7 @@ public:
static const char *kUserAgent;
static const char *kVerbose;
static const char *kWatch;
static const char* kMineOnBattery;
# ifdef XMRIG_FEATURE_TLS
static const char *kTls;
@ -80,6 +81,7 @@ public:
inline const String &apiWorkerId() const { return m_apiWorkerId; }
inline const Title &title() const { return m_title; }
inline uint32_t printTime() const { return m_printTime; }
inline bool mineOnBattery() const { return m_mineOnBattery; }
# ifdef XMRIG_FEATURE_TLS
inline const TlsConfig &tls() const { return m_tls; }
@ -95,12 +97,13 @@ public:
void printVersions();
protected:
bool m_autoSave = true;
bool m_background = false;
bool m_dryRun = false;
bool m_syslog = false;
bool m_upgrade = false;
bool m_watch = true;
bool m_autoSave = true;
bool m_background = false;
bool m_dryRun = false;
bool m_syslog = false;
bool m_upgrade = false;
bool m_watch = true;
bool m_mineOnBattery = true;
Http m_http;
Pools m_pools;
String m_apiId;

View file

@ -91,5 +91,6 @@
},
"user-agent": null,
"verbose": 0,
"watch": true
"watch": true,
"mine-on-battery": true
}

View file

@ -287,6 +287,7 @@ public:
bool active = false;
bool enabled = true;
bool reset = true;
bool battery_power = false;
Controller *controller;
Job job;
mutable std::map<Algorithm::Id, double> maxHashrate;
@ -429,13 +430,23 @@ void xmrig::Miner::setEnabled(bool enabled)
return;
}
if (d_ptr->battery_power && enabled) {
LOG_INFO(YELLOW_BOLD("Can't resume while on battery power"));
return;
}
d_ptr->enabled = enabled;
if (enabled) {
LOG_INFO(GREEN_BOLD("resumed"));
}
else {
LOG_INFO(YELLOW_BOLD("paused") ", press " MAGENTA_BG_BOLD(" r ") " to resume");
if (d_ptr->battery_power) {
LOG_INFO(YELLOW_BOLD("paused"));
}
else {
LOG_INFO(YELLOW_BOLD("paused") ", press " MAGENTA_BG_BOLD(" r ") " to resume");
}
}
if (!d_ptr->active) {
@ -538,6 +549,20 @@ void xmrig::Miner::onTimer(const Timer *)
}
d_ptr->ticks++;
if (!d_ptr->controller->config()->mineOnBattery()) {
const bool battery_power = xmrig::Platform::isOnBatteryPower();
if (battery_power && d_ptr->enabled) {
LOG_INFO(YELLOW_BOLD("On battery power"));
d_ptr->battery_power = true;
setEnabled(false);
}
else if (!battery_power && !d_ptr->enabled && d_ptr->battery_power) {
LOG_INFO(GREEN_BOLD("On AC power"));
d_ptr->battery_power = false;
setEnabled(true);
}
}
}

View file

@ -252,4 +252,5 @@ void xmrig::Config::getJSON(rapidjson::Document &doc) const
doc.AddMember(StringRef(kUserAgent), m_userAgent.toJSON(), allocator);
doc.AddMember(StringRef(kVerbose), Log::verbose(), allocator);
doc.AddMember(StringRef(kWatch), m_watch, allocator);
doc.AddMember(StringRef(kMineOnBattery), m_mineOnBattery, allocator);
}

View file

@ -109,7 +109,8 @@ R"===(
"retry-pause": 5,
"syslog": false,
"user-agent": null,
"watch": true
"watch": true,
"mine-on-battery": true
}
)===";
#endif