feat: service polls headset status and warns on low battery
This commit is contained in:
1 parent
b20181d942
commit
bb0ef89d99
1 file changed
+87
@@ -0,0 +1,87 @@
|
||||
local hsc = require("./lib/hsc.luau")
|
||||
|
||||
local warned = false
|
||||
|
||||
local function config()
|
||||
return {
|
||||
device = noctalia.getConfig("device"),
|
||||
mock = noctalia.getConfig("mock"),
|
||||
poll_interval = noctalia.getConfig("poll_interval"),
|
||||
battery_low = noctalia.getConfig("battery_low"),
|
||||
}
|
||||
end
|
||||
|
||||
local function publishCaps(info)
|
||||
if info == nil then
|
||||
noctalia.state.set("caps", {})
|
||||
noctalia.state.set("equalizerPresets", 0)
|
||||
noctalia.state.set("error", "no_caps")
|
||||
return
|
||||
end
|
||||
noctalia.state.set("caps", info.caps or {})
|
||||
noctalia.state.set("equalizerPresets", info.equalizerPresets or 0)
|
||||
noctalia.state.set("device", {
|
||||
name = info.name,
|
||||
product = info.product,
|
||||
vendor = info.vendor,
|
||||
})
|
||||
end
|
||||
|
||||
local function handleStatus(status)
|
||||
if status == nil then
|
||||
noctalia.state.set("error", "unavailable")
|
||||
noctalia.state.set("battery", { status = "BATTERY_UNAVAILABLE", level = -1 })
|
||||
return
|
||||
end
|
||||
noctalia.state.set("error", "")
|
||||
local battery = status.battery
|
||||
noctalia.state.set("battery", battery)
|
||||
noctalia.state.set("chatmix", status.chatmix)
|
||||
noctalia.state.set("lastUpdate", noctalia.nowMs())
|
||||
if battery.level and battery.level > 0 and battery.status ~= "BATTERY_UNAVAILABLE" then
|
||||
local low = config().battery_low
|
||||
if battery.level <= low and not warned then
|
||||
warned = true
|
||||
noctalia.notifyError(
|
||||
noctalia.tr("notify.battery_low.title"),
|
||||
string.format(noctalia.tr("notify.battery_low.body"), battery.level)
|
||||
)
|
||||
elseif battery.level > low + 5 then
|
||||
warned = false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function poll()
|
||||
hsc.fetchStatus(handleStatus)
|
||||
end
|
||||
|
||||
local function init()
|
||||
local cfg = config()
|
||||
hsc.init(cfg)
|
||||
if not hsc.isMock() and not noctalia.commandExists("headsetcontrol") then
|
||||
noctalia.state.set("error", "no_binary")
|
||||
noctalia.state.set("caps", {})
|
||||
noctalia.log("headsetcontrol binary not found; polling disabled")
|
||||
return
|
||||
end
|
||||
hsc.fetchCapabilities(publishCaps)
|
||||
noctalia.setUpdateInterval(cfg.poll_interval * 1000)
|
||||
poll()
|
||||
end
|
||||
|
||||
function update()
|
||||
poll()
|
||||
end
|
||||
|
||||
function onConfigChanged()
|
||||
init()
|
||||
end
|
||||
|
||||
function onIpc(event, _payload)
|
||||
if event == "refresh" then
|
||||
poll()
|
||||
end
|
||||
end
|
||||
|
||||
init()
|
||||
Reference in new issue
Block a user