Skip to content

Commit

Permalink
Fix OWM next hours
Browse files Browse the repository at this point in the history
  • Loading branch information
javalikescript committed Dec 1, 2024
1 parent fb1f4ca commit f2e1986
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
21 changes: 18 additions & 3 deletions extensions/owm/adapter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ local function sumFields(a, w)
return a
end

local function rangeHours(hours, time)
local t = time or os.time()
local tt = t + hours * 3600
return function(w)
return w.dt >= t and w.dt < tt
end
end

local function range(days, from, to, time)
local t = time or os.time()
local d = os.date('*t', t + 86400 * (days or 0))
Expand All @@ -71,8 +79,15 @@ local function ranges(from, to, time)
end

local function aggregate(list)
local w = List.reduce(List.map(list, adaptNil), sumFields, adapt({}, 0))
local a, n = {}, #list
local z = adapt({}, 0)
local n = #list
if n == 0 then
return z
elseif n == 1 then
return adapt(list[1], 0)
end
local w = List.reduce(List.map(list, adaptNil), sumFields, z)
local a = {}
for k, v in pairs(w) do
if type(v) == 'number' and not CUMULATIVE_FIELD_MAP[k] then
a[k] = (v * 100 // n) / 100
Expand All @@ -90,7 +105,7 @@ return {
return adapt(weather)
end,
computeNextHours = function(forecast, time)
return aggregate(List.filter(forecast.list, range(0, 7, 19, time)))
return aggregate(List.filter(forecast.list, rangeHours(8, time)))
end,
computeTomorrow = function(forecast, time)
return aggregate(List.filter(forecast.list, range(1, 7, 19, time)))
Expand Down
2 changes: 1 addition & 1 deletion extensions/owm/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "Open Weather Map",
"description": "Open Weather Map",
"version": "1.0",
"version": "2.0",
"script": "owm.lua",
"schema": {
"type": "object",
Expand Down

0 comments on commit f2e1986

Please sign in to comment.