This commit is contained in:
Adam Cooper 2025-03-08 12:50:24 -05:00
parent ecbb409cd7
commit f12b4d8134

View file

@ -109,7 +109,34 @@ local function factory(args)
end)
end
function weather.is_daytime() end
-- TODO: Return boolean, icon, and error
function weather.is_daytime()
local sun_position_cmd = string.format(forecast_call, APPID, lat, lon, units)
-- TODO: Return values from this callback are not returned by the outer
-- function. Try declaring local variables here, then assigning values to
-- them inside the callback, then returning them afterward.
helpers.async(sun_position_cmd, function(f)
sun_position, _, err = json.decode(f, 1, nil)
print("****DEBUG**** icon: ", sun_position["daily"]["icon"])
if not err and type(sun_position) == "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
return true, icon, nil
else
return false, icon, nil
end
else
-- TODO: Consider handling the error case instead of sending daytime in case of error
return false, icon, err
end
end)
end
function weather.update()
local cmd = string.format(current_call, APPID, lat, lon, units)
@ -117,20 +144,14 @@ local function factory(args)
helpers.async(cmd, function(f)
local err
weather_now, _, err = json.decode(f, 1, nil)
print("****DEBUG**** weather_now: ", weather_now["currently"]["temperature"])
--[[ 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)
helpers.async(sun_position_cmd, function(g)
sun_position, _, err = json.decode(g, 1, nil)
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
if not err and type(weather_now) == "table" then
-- TODO: Refactor `is_daytime` to return boolean, icon, error
is_daytime, icon, err = weather.is_daytime()
print("****DEBUG**** is_daytime, icon, err: ", is_daytime, icon, err)
if not err and type(icon) == "string" then
if is_daytime then
icon = string.gsub(icon, "n", "d")
else
icon = string.gsub(icon, "d", "n")
@ -139,13 +160,13 @@ local function factory(args)
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
else
weather.icon_path = icons_path .. "na.png"
weather.widget:set_markup(weather_na_markup)
end
weather.icon:set_image(weather.icon_path)
end)
weather.icon:set_image(weather.icon_path)
end)
end