externalize config for weather widget

This commit is contained in:
streetturtle 2019-05-03 21:46:49 -04:00
parent 57ae9fef44
commit 7df447fb50
1 changed files with 131 additions and 116 deletions

View File

@ -16,31 +16,42 @@ local secrets = require("awesome-wm-widgets.secrets")
local path_to_icons = "/usr/share/icons/Arc/status/symbolic/"
local icon_widget = wibox.widget {
local weather_widget = {}
local function worker(args)
local args = args or {}
local font = args.font or 'Play 9'
local city = args.city or 'Montreal,ca'
local api_key = args.api_key or naughty.notify{preset = naughty.config.presets.critical, text = 'OpenweatherMap API key is not set'}
local units = args.units or 'metric'
local icon_widget = wibox.widget {
{
id = "icon",
resize = false,
widget = wibox.widget.imagebox,
},
layout = wibox.container.margin(_ , 0, 0, 3),
layout = wibox.container.margin(_, 0, 0, 3),
set_image = function(self, path)
self.icon.image = path
end,
}
}
local temp_widget = wibox.widget{
font = "Play 9",
local temp_widget = wibox.widget {
font = font,
widget = wibox.widget.textbox,
}
}
local weather_widget = wibox.widget {
weather_widget = wibox.widget {
icon_widget,
temp_widget,
layout = wibox.layout.fixed.horizontal,
}
}
--- Maps openWeatherMap icons to Arc icons
local icon_map = {
--- Maps openWeatherMap icons to Arc icons
local icon_map = {
["01d"] = "weather-clear-symbolic.svg",
["02d"] = "weather-few-clouds-symbolic.svg",
["03d"] = "weather-clouds-symbolic.svg",
@ -59,10 +70,10 @@ local icon_map = {
["11n"] = "weather-storm-symbolic.svg",
["13n"] = "weather-snow-symbolic.svg",
["50n"] = "weather-fog-symbolic.svg"
}
}
--- Return wind direction as a string.
local function to_direction(degrees)
--- Return wind direction as a string.
local function to_direction(degrees)
-- Ref: https://www.campbellsci.eu/blog/convert-wind-directions
if degrees == nil then
return "Unknown dir"
@ -87,19 +98,19 @@ local function to_direction(degrees)
"N",
}
return directions[math.floor((degrees % 360) / 22.5) + 1]
end
end
local weather_timer = gears.timer({ timeout = 60 })
local resp
local weather_timer = gears.timer({ timeout = 60 })
local resp
weather_timer:connect_signal("timeout", function ()
weather_timer:connect_signal("timeout", function()
local resp_json, status = http.request('https://api.openweathermap.org/data/2.5/weather?q='
.. secrets.weather_widget_city
.. '&appid=' .. secrets.weather_widget_api_key
.. '&units=' .. secrets.weather_widget_units)
.. city
.. '&appid=' .. api_key
.. '&units=' .. units)
if (status ~= 200 and resp_json ~= nil) then
local err_resp = json.decode(resp_json)
naughty.notify{
naughty.notify {
title = 'Weather Widget Error',
text = err_resp.message,
preset = naughty.config.presets.critical,
@ -109,20 +120,19 @@ weather_timer:connect_signal("timeout", function ()
icon_widget.image = path_to_icons .. icon_map[resp.weather[1].icon]
temp_widget:set_text(string.gsub(resp.main.temp, "%.%d+", "")
.. '°'
.. (secrets.weather_widget_units == 'metric' and 'C' or 'F'))
.. (units == 'metric' and 'C' or 'F'))
end
end)
weather_timer:start()
weather_timer:emit_signal("timeout")
end)
weather_timer:start()
weather_timer:emit_signal("timeout")
--- Notification with weather information. Popups when mouse hovers over the icon
local notification
weather_widget:connect_signal("mouse::enter", function()
notification = naughty.notify{
--- Notification with weather information. Popups when mouse hovers over the icon
local notification
weather_widget:connect_signal("mouse::enter", function()
notification = naughty.notify {
icon = path_to_icons .. icon_map[resp.weather[1].icon],
icon_size=20,
text =
'<big>' .. resp.weather[1].main .. ' (' .. resp.weather[1].description .. ')</big><br>' ..
icon_size = 20,
text = '<big>' .. resp.weather[1].main .. ' (' .. resp.weather[1].description .. ')</big><br>' ..
'<b>Humidity:</b> ' .. resp.main.humidity .. '%<br>' ..
'<b>Temperature:</b> ' .. resp.main.temp .. '°'
.. (secrets.weather_widget_units == 'metric' and 'C' or 'F') .. '<br>' ..
@ -132,10 +142,15 @@ weather_widget:connect_signal("mouse::enter", function()
timeout = 5, hover_timeout = 10,
width = 200
}
end)
end)
weather_widget:connect_signal("mouse::leave", function()
weather_widget:connect_signal("mouse::leave", function()
naughty.destroy(notification)
end)
end)
return weather_widget
return weather_widget
end
return setmetatable(weather_widget, { __call = function(_, ...)
return worker(...)
end })