diff --git a/widget/weather.lua b/widget/weather.lua index eb18e68..b1ad7e5 100644 --- a/widget/weather.lua +++ b/widget/weather.lua @@ -109,38 +109,43 @@ local function factory(args) end) end + function weather.is_daytime() end + function weather.update() local cmd = string.format(current_call, APPID, lat, lon, units) helpers.async(cmd, function(f) local err weather_now, _, err = json.decode(f, 1, nil) - --[[ TODO: Try nesting async calls; otherwise write a new function for the forecast call. + + --[[ TODO: Try nesting async calls; otherwise write a new function for the forecast call.--]] local sun_position_cmd = string.format(forecast_call, APPID, lat, lon, units) - sun_position, _, err = json.decode(sun_position_cmd, 1, nil) - --]] - if not err and type(weather_now) == "table" then - local sunrise = tonumber(sun_position["daily"]["data"][1]["sunriseTime"]) - local sunset = tonumber(weather_now["daily"]["data"][1]["sunsetTime"]) - local icon = weather_now["daily"]["icon"] - local loc_now = os.time() + helpers.async(sun_position_cmd, function(g) + sun_position, _, err = json.decode(g, 1, nil) - if sunrise <= loc_now and loc_now <= sunset then - icon = string.gsub(icon, "n", "d") + if not err and type(sun_position) == "table" and type(weather_now) == "table" then + local sunrise = tonumber(sun_position["daily"]["data"][1]["sunriseTime"]) + local sunset = tonumber(sun_position["daily"]["data"][1]["sunsetTime"]) + local icon = sun_position["daily"]["icon"] + local loc_now = os.time() + + if sunrise <= loc_now and loc_now <= sunset then + icon = string.gsub(icon, "n", "d") + else + icon = string.gsub(icon, "d", "n") + end + + weather.icon_path = icons_path .. icon .. ".png" + widget = weather.widget + settings() else - icon = string.gsub(icon, "d", "n") + weather.icon_path = icons_path .. "na.png" + weather.widget:set_markup(weather_na_markup) end - weather.icon_path = icons_path .. icon .. ".png" - widget = weather.widget - settings() - else - weather.icon_path = icons_path .. "na.png" - weather.widget:set_markup(weather_na_markup) - end - - weather.icon:set_image(weather.icon_path) + weather.icon:set_image(weather.icon_path) + end) end) end