Skip to content

Instantly share code, notes, and snippets.

@gboncoffee
Created October 29, 2023 23:07
Show Gist options
  • Save gboncoffee/e927d5fe72fa1de09097afeca89f2fad to your computer and use it in GitHub Desktop.
Save gboncoffee/e927d5fe72fa1de09097afeca89f2fad to your computer and use it in GitHub Desktop.
Lua script to monitor the battery status
#!/usr/bin/env lua
--
-- System battery notifier ;)
--
sleep = function(sec)
os.execute("sleep " .. sec)
end
-- BATTERY_PATH = ""
BATTERY_PATH = "/sys/class/power_supply/BAT1/"
open_file_or_panic = function(path)
local file = io.open(path, "r")
if file then
return file
end
print("Could not open file " .. path)
os.exit(false)
end
generate_getter = function(path)
local input = open_file_or_panic(path)
local asrt = asrt or function() return true end
return function()
input:flush()
input:seek("set")
return input:read()
end
end
getstate = generate_getter(BATTERY_PATH .. "status")
getcapac = generate_getter(BATTERY_PATH .. "capacity")
notify_send = function(head, body, critical)
if critical then
os.execute("notify-send --urgency=critical '" .. head .. "' '" .. body .. "'")
else
os.execute("notify-send '" .. head .. "' '" .. body .. "'")
end
end
notify_critical = function()
notify_send("Critical battery level.", "Please connect the computer to the charger now.", true)
end
notify = function()
notify_send("Low battery level.", "You may connect the computer to the charger.")
end
connected = function()
notify_send("Charger connected.", "The computer is now on AV.")
end
disconnected = function()
notify_send("Charger disconnected.", "The computer is now on battery power.")
end
full = function()
notify_send("Battery in full capacity.", "You can disconnect the charger now.")
end
update_and_notify = function(state)
local newstate = {
state = getstate(),
notf = state.notf,
notc = state.notc,
}
local capac = getcapac()
if (newstate.state == state.state) then
if (not state.notf) and ((capac + 0) <= 20) then
newstate.notf = true
notify()
end
if (not state.notc) and ((capac + 0) <= 10) then
newstate.notc = true
notify_critical()
end
else
if (newstate.state == "Charging") then
connected()
newstate.notf = true
newstate.notc = true
elseif (newstate.state == "Full") then
full()
newstate.notfull = true
elseif (newstate.state == "Discharging") then
disconnected()
newstate.notf = false
newstate.notc = false
end
end
return newstate
end
init_state = getstate()
state = {
state = init_state,
notf = (init_state == "Charging") or false,
notc = (init_state == "Charging") or false,
}
while true do
state = update_and_notify(state)
sleep(10)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment