Update weather widget type to use Awesome async API

Timeout for fetching data could be removed
since this widget would run on another thread.
This commit is contained in:
Nguyễn Gia Phong 2019-02-12 20:43:45 +07:00
parent 94037efc6c
commit 36abb4f26c
2 changed files with 41 additions and 28 deletions

View File

@ -398,7 +398,8 @@ Provides number of pending updates on UNIX systems. Be aware that some package
managers need to update their local databases (as root) before showing the managers need to update their local databases (as root) before showing the
correct number of updates. correct number of updates.
Supported platforms: platform independent. Supported platforms: platform independent, although it requires Awesome
`awful.spawn` library for non-blocking spawning.
* Argument: distribution name, e.g. `"Arch"`, `"Arch C"`, `"Arch S"`, * Argument: distribution name, e.g. `"Arch"`, `"Arch C"`, `"Arch S"`,
`"Debian"`, `"Ubuntu"`, `"Fedora"`, `"FreeBSD"`, `"Mandriva"`. `"Debian"`, `"Ubuntu"`, `"Fedora"`, `"FreeBSD"`, `"Mandriva"`.
@ -481,7 +482,7 @@ Supported platforms: GNU/Linux (requiring `amixer`), FreeBSD.
Provides weather information for a requested station. Provides weather information for a requested station.
Supported platforms: any having `curl` installed. Supported platforms: any having Awesome and `curl` installed.
* Argument: the ICAO station code, e.g. `"LDRI"` * Argument: the ICAO station code, e.g. `"LDRI"`
* Returns a table with string keys: `${city}`, `${wind}`, `${windmph}`, * Returns a table with string keys: `${city}`, `${wind}`, `${windmph}`,

View File

@ -9,6 +9,10 @@ local io = { popen = io.popen }
local setmetatable = setmetatable local setmetatable = setmetatable
local math = { ceil = math.ceil } local math = { ceil = math.ceil }
local string = { match = string.match } local string = { match = string.match }
-- Awesome library for spawning programs
local spawn = require"awful.spawn"
local helpers = require("vicious.helpers") local helpers = require("vicious.helpers")
-- }}} -- }}}
@ -18,35 +22,26 @@ local helpers = require("vicious.helpers")
local weather_all = {} local weather_all = {}
-- Initialize function tables
local _weather = {
["{city}"] = "N/A",
["{wind}"] = "N/A",
["{windmph}"] = "N/A",
["{windkmh}"] = "N/A",
["{sky}"] = "N/A",
["{weather}"] = "N/A",
["{tempf}"] = "N/A",
["{tempc}"] = "N/A",
["{dewf}"] = "N/A",
["{dewc}"] = "N/A",
["{humid}"] = "N/A",
["{press}"] = "N/A"
}
-- {{{ Weather widget type -- {{{ Weather widget type
local function worker(format, warg) local function parse(ws)
if not warg then return end -- Initialize function tables
local _weather = {
-- Get weather forceast by the station ICAO code, from: ["{city}"] = "N/A",
-- * US National Oceanic and Atmospheric Administration ["{wind}"] = "N/A",
local url = "https://tgftp.nws.noaa.gov/data/observations/metar/decoded/"..warg ["{windmph}"] = "N/A",
local f = io.popen("curl --connect-timeout 1 -fsm 3 "..helpers.shellquote(url)..".TXT") ["{windkmh}"] = "N/A",
local ws = f:read("*all") ["{sky}"] = "N/A",
f:close() ["{weather}"] = "N/A",
["{tempf}"] = "N/A",
["{tempc}"] = "N/A",
["{dewf}"] = "N/A",
["{dewc}"] = "N/A",
["{humid}"] = "N/A",
["{press}"] = "N/A"
}
-- Check if there was a timeout or a problem with the station -- Check if there was a timeout or a problem with the station
if ws == nil then return _weather end if ws == '' then return _weather end
_weather["{city}"] = -- City and/or area _weather["{city}"] = -- City and/or area
string.match(ws, "^(.+)%,.*%([%u]+%)") or _weather["{city}"] string.match(ws, "^(.+)%,.*%([%u]+%)") or _weather["{city}"]
@ -89,6 +84,23 @@ local function worker(format, warg)
return _weather return _weather
end end
function weather_all.async(format, warg, callback)
if not warg then return end
-- Get weather forceast by the station ICAO code, from:
-- * US National Oceanic and Atmospheric Administration
local url = ("https://tgftp.nws.noaa.gov/data/observations/metar/decoded/%s.TXT"):format(warg)
local cmd = "curl -fs " .. helpers.shellquote(url)
spawn.easy_async(cmd, function (stdout) callback(parse(stdout)) end)
end
local function worker(format, warg)
local ret
weather_all.async(format, warg, function (weather) ret = weather end)
while ret == nil do end
return ret
end
-- }}} -- }}}
return setmetatable(weather_all, { __call = function(_, ...) return worker(...) end }) return setmetatable(weather_all, { __call = function(_, ...) return worker(...) end })