2016-11-10 06:52:11 +00:00
|
|
|
#include "modules/temperature.hpp"
|
2016-11-19 14:49:03 +00:00
|
|
|
|
|
|
|
#include "drawtypes/label.hpp"
|
|
|
|
#include "drawtypes/ramp.hpp"
|
2016-11-11 18:35:08 +00:00
|
|
|
#include "utils/file.hpp"
|
|
|
|
#include "utils/math.hpp"
|
2016-11-10 06:52:11 +00:00
|
|
|
|
2016-11-20 22:04:31 +00:00
|
|
|
#include "modules/meta/base.inl"
|
|
|
|
|
2016-11-19 05:22:44 +00:00
|
|
|
POLYBAR_NS
|
2016-11-10 06:52:11 +00:00
|
|
|
|
|
|
|
namespace modules {
|
2016-11-20 22:04:31 +00:00
|
|
|
template class module<temperature_module>;
|
|
|
|
|
2016-12-21 07:00:09 +00:00
|
|
|
temperature_module::temperature_module(const bar_settings& bar, string name_)
|
|
|
|
: timer_module<temperature_module>(bar, move(name_)) {
|
2016-11-10 06:52:11 +00:00
|
|
|
m_zone = m_conf.get<int>(name(), "thermal-zone", 0);
|
|
|
|
m_tempwarn = m_conf.get<int>(name(), "warn-temperature", 80);
|
|
|
|
m_interval = chrono::duration<double>(m_conf.get<float>(name(), "interval", 1));
|
|
|
|
|
|
|
|
m_path = string_util::replace(PATH_TEMPERATURE_INFO, "%zone%", to_string(m_zone));
|
|
|
|
|
2016-11-25 12:55:15 +00:00
|
|
|
if (!file_util::exists(m_path)) {
|
2016-11-10 06:52:11 +00:00
|
|
|
throw module_error("The file '" + m_path + "' does not exist");
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-10 06:52:11 +00:00
|
|
|
|
2016-11-11 18:35:08 +00:00
|
|
|
m_formatter->add(DEFAULT_FORMAT, TAG_LABEL, {TAG_LABEL, TAG_RAMP});
|
|
|
|
m_formatter->add(FORMAT_WARN, TAG_LABEL_WARN, {TAG_LABEL_WARN, TAG_RAMP});
|
|
|
|
|
2016-11-25 12:55:15 +00:00
|
|
|
if (m_formatter->has(TAG_LABEL)) {
|
2016-11-10 06:52:11 +00:00
|
|
|
m_label[temp_state::NORMAL] = load_optional_label(m_conf, name(), TAG_LABEL, "%temperature%");
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
|
|
|
if (m_formatter->has(TAG_LABEL_WARN)) {
|
2016-11-10 06:52:11 +00:00
|
|
|
m_label[temp_state::WARN] = load_optional_label(m_conf, name(), TAG_LABEL_WARN, "%temperature%");
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
|
|
|
if (m_formatter->has(TAG_RAMP)) {
|
2016-11-11 18:35:08 +00:00
|
|
|
m_ramp = load_ramp(m_conf, name(), TAG_RAMP);
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-10 06:52:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool temperature_module::update() {
|
|
|
|
m_temp = std::atoi(file_util::get_contents(m_path).c_str()) / 1000.0f + 0.5f;
|
2016-11-14 16:56:29 +00:00
|
|
|
m_perc = math_util::cap(math_util::percentage(m_temp, 0, m_tempwarn), 0, 100);
|
2016-11-10 06:52:11 +00:00
|
|
|
|
|
|
|
const auto replace_tokens = [&](label_t& label) {
|
|
|
|
label->reset_tokens();
|
|
|
|
label->replace_token("%temperature%", to_string(m_temp) + "°C");
|
|
|
|
};
|
2016-11-11 18:35:08 +00:00
|
|
|
|
2016-11-25 12:55:15 +00:00
|
|
|
if (m_label[temp_state::NORMAL]) {
|
2016-11-10 06:52:11 +00:00
|
|
|
replace_tokens(m_label[temp_state::NORMAL]);
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
|
|
|
if (m_label[temp_state::WARN]) {
|
2016-11-10 06:52:11 +00:00
|
|
|
replace_tokens(m_label[temp_state::WARN]);
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-11 18:35:08 +00:00
|
|
|
|
2016-11-10 06:52:11 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
string temperature_module::get_format() const {
|
2016-11-25 12:55:15 +00:00
|
|
|
if (m_temp > m_tempwarn) {
|
2016-11-10 06:52:11 +00:00
|
|
|
return FORMAT_WARN;
|
2016-11-25 12:55:15 +00:00
|
|
|
} else {
|
2016-11-10 06:52:11 +00:00
|
|
|
return DEFAULT_FORMAT;
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-10 06:52:11 +00:00
|
|
|
}
|
|
|
|
|
2016-11-25 12:55:15 +00:00
|
|
|
bool temperature_module::build(builder* builder, const string& tag) const {
|
|
|
|
if (tag == TAG_LABEL) {
|
2016-11-10 06:52:11 +00:00
|
|
|
builder->node(m_label.at(temp_state::NORMAL));
|
2016-11-25 12:55:15 +00:00
|
|
|
} else if (tag == TAG_LABEL_WARN) {
|
2016-11-10 06:52:11 +00:00
|
|
|
builder->node(m_label.at(temp_state::WARN));
|
2016-11-25 12:55:15 +00:00
|
|
|
} else if (tag == TAG_RAMP) {
|
2016-11-14 16:56:29 +00:00
|
|
|
builder->node(m_ramp->get_by_percentage(m_perc));
|
2016-11-25 12:55:15 +00:00
|
|
|
} else {
|
2016-11-10 06:52:11 +00:00
|
|
|
return false;
|
2016-11-25 12:55:15 +00:00
|
|
|
}
|
2016-11-10 06:52:11 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-19 05:22:44 +00:00
|
|
|
POLYBAR_NS_END
|