summaryrefslogtreecommitdiffstats
path: root/nodes/weather.lua
blob: f5bf82c1e0c69f269f42e1e675cc805cee85b967 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
local node = ThreadNode:extend('nodes.weather', {
  threadFile = 'nodes/weather_thread.lua',
  threadChannel = 'weather',

  updateInterval = 5 * 60,
})

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 = {}

local weatherGlyphsSet = {
  day = {
    snow = "",
    mist = "",
    clear = "",
    -- clouds = "",
    clouds = "", -- x---DDD
    drizzle = "",
  },
  night = {
    snow = "",
    mist = "",
    clear = "",
    clouds = "",
    drizzle = "",
    
  }
}

function node:timeOfDay()
  local sunRise = tonumber(os.date("%H%M", self.state.sunRise)) 
  local sunSet = tonumber(os.date("%H%M", self.state.sunSet))
  local now = tonumber(os.date("%H%M"))
  if sunRise == nil or sunSet == nil then
    return weatherGlyphsSet["day"] -- smth gone wrong. assume daylight
  end
  if now < sunSet and now > sunRise then
     print('day')
     return weatherGlyphsSet["day"]
  else
     print('night')
     return weatherGlyphsSet["night"]
  end
end

function node:beforeEnter()
  if self.state then
    weatherGlyphs = self:timeOfDay()
  else
    weatherGlyphs = weatherGlyphsSet["day"] -- do not know sunraise and sunset yet. assume daylight
  end
end

function node:render()
  love.graphics.setColor( 0, 0, 0 )
  love.graphics.rectangle("fill", 0, 0, love.graphics.getWidth(), love.graphics.getHeight())

  if self.state then
    love.graphics.setColor( 255, 255, 255 )

    if weatherGlyphs[self.state.weather] then
      love.graphics.setFont(weatherFont)
      love.graphics.print(weatherGlyphs[self.state.weather], 120, 35)
    else
      love.graphics.setFont(smallFont)
      love.graphics.print(self.state.weather, 150, love.graphics.getHeight()/2 - 60)
    end

    love.graphics.setFont(textFont)
    love.graphics.printf(tostring(math.floor(self.state.temperature + 0.5)) .. "°", 600, 150, 650, 'center')

    love.graphics.setFont(smallFont)
    love.graphics.printf(os.date("Last update: %Y/%m/%d %H:%M", self.state.lastUpdate), 0, love.graphics.getHeight() - 60, love.graphics.getWidth(), 'center')
    if self.state.insideTemperature then
      love.graphics.printf("Room: " .. tostring(self.state.insideTemperature) .. "° / " .. tostring(self.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

return node