feat(temperature): Support for <ramp>

This commit is contained in:
Michael Carlberg 2016-11-11 19:35:08 +01:00
parent 63b9c38435
commit 1e08aa455b
2 changed files with 19 additions and 14 deletions

View File

@ -4,15 +4,14 @@
#include "config.hpp" #include "config.hpp"
#include "drawtypes/label.hpp" #include "drawtypes/label.hpp"
#include "drawtypes/ramp.hpp"
#include "modules/meta.hpp" #include "modules/meta.hpp"
#include "utils/file.hpp"
LEMONBUDDY_NS LEMONBUDDY_NS
namespace modules { namespace modules {
enum class temp_state { NORMAL = 0, WARN }; enum class temp_state { NORMAL = 0, WARN };
class temperature_module : public timer_module<temperature_module> { class temperature_module : public timer_module<temperature_module> {
public: public:
using timer_module::timer_module; using timer_module::timer_module;
@ -25,14 +24,15 @@ namespace modules {
private: private:
static constexpr auto TAG_LABEL = "<label>"; static constexpr auto TAG_LABEL = "<label>";
static constexpr auto TAG_LABEL_WARN = "<label-warn>"; static constexpr auto TAG_LABEL_WARN = "<label-warn>";
static constexpr auto TAG_RAMP = "<ramp>";
static constexpr auto FORMAT_WARN = "format-warn"; static constexpr auto FORMAT_WARN = "format-warn";
map<temp_state, label_t> m_label; map<temp_state, label_t> m_label;
ramp_t m_ramp;
string m_path; string m_path;
int m_zone = 0;
int m_zone; int m_tempwarn = 0;
int m_tempwarn;
int m_temp = 0; int m_temp = 0;
}; };
} }

View File

@ -1,4 +1,6 @@
#include "modules/temperature.hpp" #include "modules/temperature.hpp"
#include "utils/file.hpp"
#include "utils/math.hpp"
LEMONBUDDY_NS LEMONBUDDY_NS
@ -13,34 +15,35 @@ namespace modules {
if (!file_util::exists(m_path)) if (!file_util::exists(m_path))
throw module_error("The file '" + m_path + "' does not exist"); throw module_error("The file '" + m_path + "' does not exist");
m_formatter->add(DEFAULT_FORMAT, TAG_LABEL, {TAG_LABEL}); m_formatter->add(DEFAULT_FORMAT, TAG_LABEL, {TAG_LABEL, TAG_RAMP});
m_formatter->add(FORMAT_WARN, TAG_LABEL_WARN, {TAG_LABEL_WARN}); m_formatter->add(FORMAT_WARN, TAG_LABEL_WARN, {TAG_LABEL_WARN, TAG_RAMP});
if (m_formatter->has(TAG_LABEL)) if (m_formatter->has(TAG_LABEL))
m_label[temp_state::NORMAL] = load_optional_label(m_conf, name(), TAG_LABEL, "%temperature%"); m_label[temp_state::NORMAL] = load_optional_label(m_conf, name(), TAG_LABEL, "%temperature%");
if (m_formatter->has(TAG_LABEL_WARN)) if (m_formatter->has(TAG_LABEL_WARN))
m_label[temp_state::WARN] = load_optional_label(m_conf, name(), TAG_LABEL_WARN, "%temperature%"); m_label[temp_state::WARN] = load_optional_label(m_conf, name(), TAG_LABEL_WARN, "%temperature%");
if (m_formatter->has(TAG_RAMP))
m_ramp = load_ramp(m_conf, name(), TAG_RAMP);
} }
bool temperature_module::update() { bool temperature_module::update() {
m_temp = std::atoi(file_util::get_contents(m_path).c_str()) / 1000.0f + 0.5f; m_temp = std::atoi(file_util::get_contents(m_path).c_str()) / 1000.0f + 0.5f;
// replace tokens
const auto replace_tokens = [&](label_t& label) { const auto replace_tokens = [&](label_t& label) {
label->reset_tokens(); label->reset_tokens();
label->replace_token("%temperature%", to_string(m_temp) + "°C"); label->replace_token("%temperature%", to_string(m_temp) + "°C");
}; };
if (m_label[temp_state::NORMAL]) {
if (m_label[temp_state::NORMAL])
replace_tokens(m_label[temp_state::NORMAL]); replace_tokens(m_label[temp_state::NORMAL]);
} if (m_label[temp_state::WARN])
if (m_label[temp_state::WARN]) {
replace_tokens(m_label[temp_state::WARN]); replace_tokens(m_label[temp_state::WARN]);
}
return true; return true;
} }
string temperature_module::get_format() const { string temperature_module::get_format() const {
if(m_temp > m_tempwarn) if (m_temp > m_tempwarn)
return FORMAT_WARN; return FORMAT_WARN;
else else
return DEFAULT_FORMAT; return DEFAULT_FORMAT;
@ -51,6 +54,8 @@ namespace modules {
builder->node(m_label.at(temp_state::NORMAL)); builder->node(m_label.at(temp_state::NORMAL));
else if (tag == TAG_LABEL_WARN) else if (tag == TAG_LABEL_WARN)
builder->node(m_label.at(temp_state::WARN)); builder->node(m_label.at(temp_state::WARN));
else if (tag == TAG_RAMP)
builder->node(m_ramp->get_by_percentage(math_util::cap(m_temp, 0, m_tempwarn)));
else else
return false; return false;
return true; return true;