diff --git a/extensions/owm/adapter.lua b/extensions/owm/adapter.lua index 24b1b96..5046cad 100644 --- a/extensions/owm/adapter.lua +++ b/extensions/owm/adapter.lua @@ -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)) @@ -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 @@ -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))) diff --git a/extensions/owm/manifest.json b/extensions/owm/manifest.json index 42c8ca3..e5fbd07 100644 --- a/extensions/owm/manifest.json +++ b/extensions/owm/manifest.json @@ -1,7 +1,7 @@ { "name": "Open Weather Map", "description": "Open Weather Map", - "version": "1.0", + "version": "2.0", "script": "owm.lua", "schema": { "type": "object",