love2d-signage/screens/weather.lua

67 lines
2.0 KiB
Lua

local node = {}
local inspect = require('vendor.inspect')
local weatherFont = love.graphics.newFont('fonts/weathericons-regular-webfont.ttf', 400)
local textFont = love.graphics.newFont('fonts/Lato-Thin.ttf', 300)
local smallFont = love.graphics.newFont('fonts/Lato-Light.ttf', 30)
local weatherGlyphs = {
snow = "",
mist = "",
clear = "",
}
local updateInterval = 5 * 60
local lastUpdate = 0
local state = nil
function node.load()
end
function node.unload()
end
function node.render()
love.graphics.setColor( 0, 0, 0 )
love.graphics.rectangle("fill", 0, 0, love.graphics.getWidth(), love.graphics.getHeight())
if state then
love.graphics.setColor( 255, 255, 255 )
if weatherGlyphs[state.weather] then
love.graphics.setFont(weatherFont)
love.graphics.print(weatherGlyphs[state.weather], 150, 35)
end
love.graphics.setFont(textFont)
love.graphics.printf(tostring(state.temperature) .. "°", 600, 150, 650, 'center')
love.graphics.setFont(smallFont)
love.graphics.printf(os.date("Last update: %Y/%m/%d %H:%M", state.lastUpdate), 0, love.graphics.getHeight() - 60, love.graphics.getWidth(), 'center')
if state.insideTemperature then
love.graphics.printf("Room: " .. tostring(state.insideTemperature) .. "° / " .. tostring(state.insideHumidity) .. "%RH", 0, love.graphics.getHeight() - 100, love.graphics.getWidth(), 'center')
end
else
love.graphics.setColor( 255, 255, 255, 80 )
love.graphics.setFont(smallFont)
love.graphics.printf("Loading weather...", 0, love.graphics.getHeight() - 200, love.graphics.getWidth(), 'center')
end
end
function node.update(dt)
if lastUpdate < love.timer.getTime() - updateInterval or
(not state and lastUpdate < love.timer.getTime() - 5) then
lastUpdate = love.timer.getTime()
print("Updating...")
local updateThread = love.thread.newThread('screens/weather_thread.lua')
updateThread:start()
end
state = love.thread.getChannel('weather'):pop() or state
end
return node