Serge Bazanski | 18c1a26 | 2022-07-07 14:24:53 +0200 | [diff] [blame] | 1 | local node = ThreadNode:extend('nodes.weather', { |
| 2 | threadFile = 'nodes/weather_thread.lua', |
| 3 | threadChannel = 'weather', |
| 4 | |
| 5 | updateInterval = 5 * 60, |
| 6 | }) |
| 7 | |
| 8 | local weatherFont = love.graphics.newFont('fonts/weathericons-regular-webfont.ttf', 400) |
| 9 | local textFont = love.graphics.newFont('fonts/Lato-Thin.ttf', 300) |
| 10 | local smallFont = love.graphics.newFont('fonts/Lato-Light.ttf', 30) |
| 11 | |
| 12 | local weatherGlyphs = {} |
| 13 | |
| 14 | local weatherGlyphsSet = { |
| 15 | day = { |
| 16 | snow = "", |
| 17 | mist = "", |
| 18 | clear = "", |
| 19 | -- clouds = "", |
| 20 | clouds = "", -- x---DDD |
| 21 | drizzle = "", |
| 22 | }, |
| 23 | night = { |
| 24 | snow = "", |
| 25 | mist = "", |
| 26 | clear = "", |
| 27 | clouds = "", |
| 28 | drizzle = "", |
| 29 | |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | function node:timeOfDay() |
| 34 | local sunRise = tonumber(os.date("%H%M", self.state.sunRise)) |
| 35 | local sunSet = tonumber(os.date("%H%M", self.state.sunSet)) |
| 36 | local now = tonumber(os.date("%H%M")) |
| 37 | if sunRise == nil or sunSet == nil then |
| 38 | return weatherGlyphsSet["day"] -- smth gone wrong. assume daylight |
| 39 | end |
| 40 | if now < sunSet and now > sunRise then |
| 41 | print('day') |
| 42 | return weatherGlyphsSet["day"] |
| 43 | else |
| 44 | print('night') |
| 45 | return weatherGlyphsSet["night"] |
| 46 | end |
| 47 | end |
| 48 | |
| 49 | function node:beforeEnter() |
| 50 | if self.state then |
| 51 | weatherGlyphs = self:timeOfDay() |
| 52 | else |
| 53 | weatherGlyphs = weatherGlyphsSet["day"] -- do not know sunraise and sunset yet. assume daylight |
| 54 | end |
| 55 | end |
| 56 | |
| 57 | function node:render() |
| 58 | love.graphics.setColor( 0, 0, 0 ) |
| 59 | love.graphics.rectangle("fill", 0, 0, love.graphics.getWidth(), love.graphics.getHeight()) |
| 60 | |
| 61 | if self.state then |
| 62 | love.graphics.setColor( 1.0, 1.0, 1.0 ) |
| 63 | |
| 64 | if weatherGlyphs[self.state.weather] then |
| 65 | love.graphics.setFont(weatherFont) |
| 66 | love.graphics.print(weatherGlyphs[self.state.weather], 120, 35) |
| 67 | else |
| 68 | love.graphics.setFont(smallFont) |
| 69 | love.graphics.print(self.state.weather, 150, love.graphics.getHeight()/2 - 60) |
| 70 | end |
| 71 | |
| 72 | love.graphics.setFont(textFont) |
| 73 | love.graphics.printf(tostring(math.floor(self.state.temperature + 0.5)) .. "°", 600, 150, 650, 'center') |
| 74 | |
| 75 | love.graphics.setFont(smallFont) |
| 76 | love.graphics.printf(os.date("Last update: %Y/%m/%d %H:%M", self.state.lastUpdate), 0, love.graphics.getHeight() - 60, love.graphics.getWidth(), 'center') |
| 77 | if self.state.insideTemperature then |
| 78 | love.graphics.printf("Room: " .. tostring(self.state.insideTemperature) .. "° / " .. tostring(self.state.insideHumidity) .. "%RH", 0, love.graphics.getHeight() - 100, love.graphics.getWidth(), 'center') |
| 79 | end |
| 80 | else |
| 81 | love.graphics.setColor( 1.0, 1.0, 1.0, 0.4 ) |
| 82 | |
| 83 | love.graphics.setFont(smallFont) |
| 84 | love.graphics.printf("Loading weather...", 0, love.graphics.getHeight() - 200, love.graphics.getWidth(), 'center') |
| 85 | end |
| 86 | end |
| 87 | |
| 88 | return node |