refactor(battery): Poll values by default

Fixes jaagr/lemonbuddy#140
This commit is contained in:
Michael Carlberg 2016-11-03 15:20:38 +01:00
parent 52c08675e6
commit a1472dfd0f
2 changed files with 10 additions and 22 deletions

View File

@ -61,9 +61,6 @@ namespace modules {
interval_t m_interval; interval_t m_interval;
chrono::system_clock::time_point m_lastpoll; chrono::system_clock::time_point m_lastpoll;
stateflag m_notified{false};
stateflag m_polling{false};
int m_fullat = 100; int m_fullat = 100;
}; };
} }

View File

@ -10,7 +10,7 @@ namespace modules {
m_battery = m_conf.get<string>(name(), "battery", "BAT0"); m_battery = m_conf.get<string>(name(), "battery", "BAT0");
m_adapter = m_conf.get<string>(name(), "adapter", "ADP1"); m_adapter = m_conf.get<string>(name(), "adapter", "ADP1");
m_fullat = m_conf.get<int>(name(), "full-at", 100); m_fullat = m_conf.get<int>(name(), "full-at", 100);
m_interval = interval_t{m_conf.get<float>(name(), "poll-interval", 3.0f)}; m_interval = interval_t{m_conf.get<float>(name(), "poll-interval", 5.0f)};
m_lastpoll = chrono::system_clock::now(); m_lastpoll = chrono::system_clock::now();
m_path_capacity = string_util::replace(PATH_BATTERY_CAPACITY, "%battery%", m_battery); m_path_capacity = string_util::replace(PATH_BATTERY_CAPACITY, "%battery%", m_battery);
@ -76,17 +76,15 @@ namespace modules {
} }
void battery_module::idle() { void battery_module::idle() {
// Manually poll for events as a fallback for systems that // Manually poll values as a fallback for systems that
// doesn't report inotify events for files on sysfs // doesn't report inotify events for files on sysfs
if (m_interval.count() > 0 && !m_notified.load(std::memory_order_relaxed)) { if (m_interval.count() > 0) {
auto now = chrono::system_clock::now(); auto now = chrono::system_clock::now();
if (now - m_lastpoll > m_interval) { if (chrono::duration_cast<decltype(m_interval)>(now - m_lastpoll) > m_interval) {
m_log.info("%s: Polling values (inotify fallback)", name());
m_polling.store(true, std::memory_order_relaxed);
m_lastpoll = now; m_lastpoll = now;
on_event(nullptr); m_log.info("%s: Polling values (inotify fallback)", name());
broadcast(); file_util::get_contents(m_path_capacity);
} }
} }
@ -95,18 +93,11 @@ namespace modules {
bool battery_module::on_event(inotify_event* event) { bool battery_module::on_event(inotify_event* event) {
if (event != nullptr) { if (event != nullptr) {
m_log.trace("%s: %s", name(), event->filename); m_log.trace("%s: Inotify event reported for %s", name(), event->filename);
}
if (m_polling.load(std::memory_order_relaxed)) { // Reset timer to avoid unnecessary polling
m_log.info("%s: Inotify event reported, assuming fake...", name()); m_lastpoll = chrono::system_clock::now();
m_polling.store(false, std::memory_order_relaxed);
} else if (!m_notified.load(std::memory_order_relaxed)) {
m_log.info("%s: Inotify event reported, disable polling fallback...", name());
m_notified.store(true, std::memory_order_relaxed);
} else {
m_log.info("%s: Inotify event reported", name());
}
}
auto state = current_state(); auto state = current_state();
int percentage = m_percentage; int percentage = m_percentage;