Skip to content

Commit

Permalink
Prototyping alerts with power source
Browse files Browse the repository at this point in the history
  • Loading branch information
2hdddg committed Jan 28, 2024
1 parent 57c7b95 commit 52a4c6d
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 9 deletions.
5 changes: 4 additions & 1 deletion src/Manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ void Manager::OnChanged() {
}
}

void Manager::OnAlerted() {}
void Manager::OnAlerted() {
m_alerted = true;
OnChanged();
}

void Manager::Hide() {
m_isVisible = false;
Expand Down
23 changes: 18 additions & 5 deletions src/PowerSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <optional>
#include <string>

std::shared_ptr<PowerSource> PowerSource::Create(MainLoop& mainLoop) {
std::shared_ptr<PowerSource> PowerSource::Create(std::shared_ptr<MainLoop> mainloop) {
// Use non blocking to make sure we never hang on read
auto fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC | TFD_NONBLOCK);
if (fd == -1) {
Expand All @@ -22,8 +22,8 @@ std::shared_ptr<PowerSource> PowerSource::Create(MainLoop& mainLoop) {
spdlog::error("Failed to set timer: {}", strerror(errno));
return nullptr;
}
auto source = std::shared_ptr<PowerSource>(new PowerSource(fd));
mainLoop.RegisterIoHandler(fd, "PowerSource", source);
auto source = std::shared_ptr<PowerSource>(new PowerSource(mainloop, fd));
mainloop->RegisterIoHandler(fd, "PowerSource", source);
return source;
}

Expand Down Expand Up @@ -89,9 +89,22 @@ void PowerSource::ReadState() {
if (maybeString) {
state.IsPluggedIn = std::stoi(*maybeString) != 0;
}
state.IsAlerted =
state.Capacity < 35 /*TODO: From config*/ && !state.IsCharging && !state.IsPluggedIn;
if (state != m_sourceState) {
spdlog::info("Power status changed, capacity {}, charging {}, plugged in {}",
state.Capacity, state.IsCharging, state.IsPluggedIn);
spdlog::info("Power status changed, alert {}, capacity {}, charging {}, plugged in {}",
state.IsAlerted, state.Capacity, state.IsCharging, state.IsPluggedIn);
// Alert state changed
if (state.IsAlerted != m_sourceState.IsAlerted) {
if (state.IsAlerted) {
// To alert
spdlog::info("Power source is triggering alert");
m_mainloop->AlertAndWakeup();
} else {
// From alert to non alert
// TODO:
}
}
m_sourceState = state;
m_drawn = m_published = false;
}
Expand Down
5 changes: 3 additions & 2 deletions src/PowerSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@

class PowerSource : public Source, public IoHandler {
public:
static std::shared_ptr<PowerSource> Create(MainLoop& mainLoop);
static std::shared_ptr<PowerSource> Create(std::shared_ptr<MainLoop> mainloop);
bool Initialize();
void ReadState();
virtual bool OnRead() override;
void Publish(const std::string_view sourceName, ScriptContext& scriptContext) override;
virtual ~PowerSource();

private:
PowerSource(int fd) : m_timerfd(fd) {}
PowerSource(std::shared_ptr<MainLoop> mainloop, int fd) : m_mainloop(mainloop), m_timerfd(fd) {}
std::shared_ptr<MainLoop> m_mainloop;
std::filesystem::path m_batteryCapacity;
std::filesystem::path m_batteryStatus;
std::filesystem::path m_ac;
Expand Down
1 change: 1 addition & 0 deletions src/ScriptContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ void ScriptContextImpl::Publish(const std::string_view name, const Displays& dis

void ScriptContextImpl::Publish(const std::string_view name, const PowerState& power) {
auto table = m_lua.create_table();
table["isAlerted"] = power.IsAlerted;
table["isCharging"] = power.IsCharging;
table["isPluggedIn"] = power.IsPluggedIn;
table["capacity"] = (int)power.Capacity;
Expand Down
1 change: 1 addition & 0 deletions src/ScriptContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ struct Display {
using Displays = std::vector<Display>;

struct PowerState {
bool IsAlerted;
bool IsPluggedIn;
bool IsCharging;
uint8_t Capacity;
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ static void InitializeSource(const std::string& source, Sources& sources,
}
}
if (source == "power") {
auto powerSource = PowerSource::Create(*mainLoop);
auto powerSource = PowerSource::Create(mainLoop);
if (!powerSource) {
spdlog::error("Failed to initialize battery source");
return;
Expand Down

0 comments on commit 52a4c6d

Please sign in to comment.