fix: threshold band and failure-preserving battery streak, panel back to normalize
This commit is contained in:
1 parent
32964ca02d
commit
be4878def7
3 files changed
+25
-12
No files matched your search
@@ -244,22 +244,35 @@ end
|
||||
|
||||
-- Confirms below-threshold readings: a lone reading at or below `low`
|
||||
-- is normalized to unavailable (no false widget value or warning); two
|
||||
-- in a row pass through as genuinely low. `low` is the caller's
|
||||
-- low-battery threshold (e.g. the battery_low setting). Single owner is
|
||||
-- the service entry; the panel uses it for its one-shot wake read with
|
||||
-- the same threshold. All unavailable/malformed forms reset the streak.
|
||||
-- in a row pass through as genuinely low. Readings in (low, low+5] show
|
||||
-- as-is without touching the streak (threshold hover); readings above
|
||||
-- low+5 reset it. Fetch failures (nil/malformed input) carry no
|
||||
-- information and leave the streak untouched; an explicit device
|
||||
-- unavailable (level < 0) resets it. Single owner is the service entry.
|
||||
function M.stableBattery(battery, low)
|
||||
local norm = M.normalizeBattery(battery)
|
||||
if battery == nil or type(battery) ~= "table" then
|
||||
return { status = "BATTERY_UNAVAILABLE", level = -1 }
|
||||
end
|
||||
local level = tonumber(battery.level)
|
||||
if level == nil then
|
||||
return { status = "BATTERY_UNAVAILABLE", level = -1 }
|
||||
end
|
||||
local threshold = tonumber(low) or 20
|
||||
if norm.level >= 0 and norm.level <= threshold then
|
||||
if level < 0 then
|
||||
lowStreak = 0
|
||||
return { status = "BATTERY_UNAVAILABLE", level = -1 }
|
||||
end
|
||||
if level <= threshold then
|
||||
lowStreak = lowStreak + 1
|
||||
if lowStreak >= 2 then
|
||||
return norm
|
||||
return { status = battery.status, level = level }
|
||||
end
|
||||
return { status = "BATTERY_UNAVAILABLE", level = -1 }
|
||||
end
|
||||
lowStreak = 0
|
||||
return norm
|
||||
if level > threshold + 5 then
|
||||
lowStreak = 0
|
||||
end
|
||||
return { status = battery.status, level = level }
|
||||
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", hsc.stableBattery(status.battery, noctalia.getConfig("battery_low")))
|
||||
noctalia.state.set("battery", hsc.normalizeBattery(status.battery))
|
||||
noctalia.state.set("chatmix", status.chatmix or 0)
|
||||
noctalia.state.set("lastUpdate", noctalia.nowMs())
|
||||
end
|
||||
|
||||
@@ -41,12 +41,12 @@ local function handleStatus(status)
|
||||
return
|
||||
end
|
||||
noctalia.state.set("error", "")
|
||||
local battery = hsc.stableBattery(status.battery, config().battery_low)
|
||||
local low = tonumber(config().battery_low) or 20
|
||||
local battery = hsc.stableBattery(status.battery, low)
|
||||
noctalia.state.set("battery", battery)
|
||||
noctalia.state.set("chatmix", status.chatmix or 0)
|
||||
noctalia.state.set("lastUpdate", noctalia.nowMs())
|
||||
if battery.level ~= nil and battery.level >= 0 and battery.status ~= "BATTERY_UNAVAILABLE" then
|
||||
local low = config().battery_low or 20
|
||||
if battery.level <= low and not warned then
|
||||
warned = true
|
||||
noctalia.notifyError(
|
||||
|
||||
Reference in new issue
Block a user