| 1 |
local awful = require 'awful' |
| 2 |
local naughty = require 'naughty' |
| 3 |
local wibox = require 'wibox' |
| 4 |
local forecast = require 'obvious.weather.forecast' |
| 5 |
local hooks = require 'obvious.lib.hooks' |
| 6 |
local cqueues = require 'cqueues' |
| 7 |
|
| 8 |
local widget = wibox.widget.textbox() |
| 9 |
local timer_running |
| 10 |
|
| 11 |
local api_key |
| 12 |
local latitude |
| 13 |
local longitude |
| 14 |
local metric = true |
| 15 |
|
| 16 |
local icons = { |
| 17 |
['clear-day'] = '☼', |
| 18 |
['clear-night'] = '🌙', |
| 19 |
['partly-cloudy-day'] = '⛅', |
| 20 |
['partly-cloudy-night'] = '⛅', |
| 21 |
cloudy = '☁', |
| 22 |
rain = '🌧', |
| 23 |
sleet = '', |
| 24 |
snow = '❄', |
| 25 |
wind = '', |
| 26 |
fog = '🌁', |
| 27 |
} |
| 28 |
|
| 29 |
local request_in_flight = false |
| 30 |
local previous_fetch_time |
| 31 |
local sunrise_time |
| 32 |
local sunset_time |
| 33 |
|
| 34 |
local function background_update() |
| 35 |
local response = forecast.get(api_key, latitude, longitude, metric and 'si' or 'us') |
| 36 |
|
| 37 |
local icon = icons[response.currently.icon] or '' |
| 38 |
local description = string.format('%.1f °%s', response.currently.temperature, metric and 'C' or 'F') |
| 39 |
widget:set_text(icon .. ' ' ..description) |
| 40 |
|
| 41 |
previous_fetch_time = os.time() |
| 42 |
request_in_flight = false |
| 43 |
|
| 44 |
sunrise_time = response.daily.data[1].sunriseTime |
| 45 |
sunset_time = response.daily.data[1].sunsetTime |
| 46 |
end |
| 47 |
|
| 48 |
local function update() |
| 49 |
if request_in_flight then |
| 50 |
return |
| 51 |
end |
| 52 |
request_in_flight = os.time() |
| 53 |
|
| 54 |
local c = cqueues.new() |
| 55 |
c:wrap(background_update) |
| 56 |
local function check_up_on_request() |
| 57 |
local ok, err = c:step(0) |
| 58 |
|
| 59 |
if not ok then |
| 60 |
widget:set_text 'Unable to retrieve forecast @_@' |
| 61 |
naughty.notify { |
| 62 |
title = 'Forecast Error', |
| 63 |
text = err, |
| 64 |
preset = naughty.config.presets.critical, |
| 65 |
} |
| 66 |
request_in_flight = false |
| 67 |
end |
| 68 |
|
| 69 |
if not request_in_flight then |
| 70 |
hooks.timer.unregister(check_up_on_request) |
| 71 |
end |
| 72 |
end |
| 73 |
hooks.timer.register(0.1, 0.1, check_up_on_request) |
| 74 |
end |
| 75 |
|
| 76 |
local function is_setup() |
| 77 |
return api_key and latitude and longitude |
| 78 |
end |
| 79 |
|
| 80 |
local function init_timer() |
| 81 |
if not is_setup() or timer_running then |
| 82 |
return |
| 83 |
end |
| 84 |
|
| 85 |
timer_running = true |
| 86 |
widget:set_text 'Retrieving forecast...' |
| 87 |
update() |
| 88 |
hooks.timer.register(15 * 60, 60 * 60, update, 'weather widget refresh rate') |
| 89 |
end |
| 90 |
|
| 91 |
local _M = {} |
| 92 |
|
| 93 |
function _M.set_api_key(key) |
| 94 |
api_key = key |
| 95 |
init_timer() |
| 96 |
end |
| 97 |
|
| 98 |
function _M.set_latitude(lat) |
| 99 |
latitude = lat |
| 100 |
init_timer() |
| 101 |
end |
| 102 |
|
| 103 |
function _M.set_longitude(long) |
| 104 |
longitude = long |
| 105 |
init_timer() |
| 106 |
end |
| 107 |
|
| 108 |
function _M.set_metric(is_metric) |
| 109 |
metric = is_metric |
| 110 |
end |
| 111 |
|
| 112 |
widget:buttons(awful.util.table.join( |
| 113 |
awful.button({ }, 1, update) |
| 114 |
)) |
| 115 |
|
| 116 |
do |
| 117 |
local notification |
| 118 |
|
| 119 |
widget:connect_signal('mouse::enter', function() |
| 120 |
local lines = {} |
| 121 |
|
| 122 |
if previous_fetch_time then |
| 123 |
lines[#lines + 1] = 'Last refresh: ' .. os.date('%F %T', previous_fetch_time) |
| 124 |
end |
| 125 |
|
| 126 |
|
| 127 |
if request_in_flight then |
| 128 |
lines[#lines + 1] = 'Retrieving status since ' .. os.date('%F %T', request_in_flight) |
| 129 |
end |
| 130 |
|
| 131 |
if sunrise_time then |
| 132 |
lines[#lines + 1] = 'Sunrise: ' .. os.date('%T', sunrise_time) |
| 133 |
end |
| 134 |
|
| 135 |
if sunset_time then |
| 136 |
lines[#lines + 1] = 'Sunset: ' .. os.date('%T', sunset_time) |
| 137 |
end |
| 138 |
|
| 139 |
local text = table.concat(lines, '\n') |
| 140 |
local args = { |
| 141 |
title = 'Weather', |
| 142 |
text = text, |
| 143 |
} |
| 144 |
if notification and notification.box.visible then |
| 145 |
args.replaces_id = notification.id |
| 146 |
end |
| 147 |
notification = naughty.notify(args) |
| 148 |
end) |
| 149 |
|
| 150 |
widget:connect_signal('mouse::leave', function() |
| 151 |
if notification then |
| 152 |
naughty.destroy(notification, naughty.notificationClosedReason.dismissedByCommand) |
| 153 |
notification = nil |
| 154 |
end |
| 155 |
end) |
| 156 |
end |
| 157 |
|
| 158 |
return setmetatable(_M, { __call = function() |
| 159 |
if is_setup() then |
| 160 |
init_timer() |
| 161 |
else |
| 162 |
widget:set_text 'Setup Required' |
| 163 |
end |
| 164 |
|
| 165 |
return widget |
| 166 |
end }) |