WIP
This commit is contained in:
parent
ecbb409cd7
commit
f12b4d8134
1 changed files with 40 additions and 19 deletions
|
@ -109,7 +109,34 @@ local function factory(args)
|
||||||
end)
|
end)
|
||||||
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()
|
function weather.update()
|
||||||
local cmd = string.format(current_call, APPID, lat, lon, units)
|
local cmd = string.format(current_call, APPID, lat, lon, units)
|
||||||
|
@ -117,20 +144,14 @@ local function factory(args)
|
||||||
helpers.async(cmd, function(f)
|
helpers.async(cmd, function(f)
|
||||||
local err
|
local err
|
||||||
weather_now, _, err = json.decode(f, 1, nil)
|
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.--]]
|
if not err and type(weather_now) == "table" then
|
||||||
local sun_position_cmd = string.format(forecast_call, APPID, lat, lon, units)
|
-- TODO: Refactor `is_daytime` to return boolean, icon, error
|
||||||
|
is_daytime, icon, err = weather.is_daytime()
|
||||||
helpers.async(sun_position_cmd, function(g)
|
print("****DEBUG**** is_daytime, icon, err: ", is_daytime, icon, err)
|
||||||
sun_position, _, err = json.decode(g, 1, nil)
|
if not err and type(icon) == "string" then
|
||||||
|
if is_daytime then
|
||||||
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")
|
icon = string.gsub(icon, "n", "d")
|
||||||
else
|
else
|
||||||
icon = string.gsub(icon, "d", "n")
|
icon = string.gsub(icon, "d", "n")
|
||||||
|
@ -139,6 +160,7 @@ local function factory(args)
|
||||||
weather.icon_path = icons_path .. icon .. ".png"
|
weather.icon_path = icons_path .. icon .. ".png"
|
||||||
widget = weather.widget
|
widget = weather.widget
|
||||||
settings()
|
settings()
|
||||||
|
end
|
||||||
else
|
else
|
||||||
weather.icon_path = icons_path .. "na.png"
|
weather.icon_path = icons_path .. "na.png"
|
||||||
weather.widget:set_markup(weather_na_markup)
|
weather.widget:set_markup(weather_na_markup)
|
||||||
|
@ -146,7 +168,6 @@ local function factory(args)
|
||||||
|
|
||||||
weather.icon:set_image(weather.icon_path)
|
weather.icon:set_image(weather.icon_path)
|
||||||
end)
|
end)
|
||||||
end)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if showpopup == "on" then
|
if showpopup == "on" then
|
||||||
|
|
Loading…
Add table
Reference in a new issue