fix: normalize battery tables, validate panel wake publish
This commit is contained in:
1 parent
a3455dd586
commit
1100240732
3 files changed
+20
-14
No files matched your search
+18
-12
@@ -227,31 +227,37 @@ function M.fetchConnected(cb)
|
||||
end)
|
||||
end
|
||||
|
||||
-- Debounces transient 0% readings: a lone 0 between good readings is
|
||||
-- normalized to unavailable (no false widget 0% or warning); two 0s in a
|
||||
-- row pass through as a genuinely empty battery. Single owner is the
|
||||
-- service entry (service.luau); the panel publishes its one-shot wake
|
||||
-- read raw. Non-table input or a missing/non-numeric level resets the
|
||||
-- streak and normalizes to unavailable, as does a nil reading.
|
||||
function M.stableBattery(battery)
|
||||
-- Validates and normalizes a battery table to {status, level} with a
|
||||
-- numeric level. Anything malformed (non-table, missing/non-numeric
|
||||
-- level) becomes unavailable. No streak side effects; safe for one-shot
|
||||
-- publishers like the panel wake read.
|
||||
function M.normalizeBattery(battery)
|
||||
if type(battery) ~= "table" then
|
||||
zeroStreak = 0
|
||||
return { status = "BATTERY_UNAVAILABLE", level = -1 }
|
||||
end
|
||||
local level = tonumber(battery.level)
|
||||
if level == nil then
|
||||
zeroStreak = 0
|
||||
return { status = "BATTERY_UNAVAILABLE", level = -1 }
|
||||
end
|
||||
if level == 0 then
|
||||
return { status = battery.status, level = level }
|
||||
end
|
||||
|
||||
-- Debounces transient 0% readings: a lone 0 between good readings is
|
||||
-- normalized to unavailable (no false widget 0% or warning); two 0s in a
|
||||
-- row pass through as a genuinely empty battery. Single owner is the
|
||||
-- service entry (service.luau); the panel publishes its one-shot wake
|
||||
-- read via normalizeBattery (no streak). All unavailable forms reset.
|
||||
function M.stableBattery(battery)
|
||||
local norm = M.normalizeBattery(battery)
|
||||
if norm.level == 0 then
|
||||
zeroStreak = zeroStreak + 1
|
||||
if zeroStreak >= 2 then
|
||||
return battery
|
||||
return norm
|
||||
end
|
||||
return { status = "BATTERY_UNAVAILABLE", level = -1 }
|
||||
end
|
||||
zeroStreak = 0
|
||||
return battery
|
||||
return norm
|
||||
end
|
||||
|
||||
-- chatmix direction is device-ambiguous. Negative = game side on signed-convention
|
||||
|
||||
@@ -110,7 +110,7 @@ function onWake()
|
||||
end
|
||||
hsc.fetchStatus(function(status)
|
||||
if status ~= nil then
|
||||
noctalia.state.set("battery", status.battery or { status = "BATTERY_UNAVAILABLE", level = -1 })
|
||||
noctalia.state.set("battery", hsc.normalizeBattery(status.battery))
|
||||
noctalia.state.set("chatmix", status.chatmix or 0)
|
||||
noctalia.state.set("lastUpdate", noctalia.nowMs())
|
||||
end
|
||||
|
||||
@@ -71,7 +71,7 @@ local function init()
|
||||
noctalia.state.set("error", "no_binary")
|
||||
noctalia.state.set("caps", {})
|
||||
noctalia.state.set("equalizerPresets", 0)
|
||||
noctalia.state.set("battery", { status = "BATTERY_UNAVAILABLE", level = -1 })
|
||||
noctalia.state.set("battery", hsc.stableBattery(nil))
|
||||
noctalia.state.set("chatmix", 0)
|
||||
noctalia.log("headsetcontrol binary not found; polling disabled")
|
||||
return
|
||||
|
||||
Reference in new issue
Block a user