love2d-signage/nodes/weather_thread.lua

33 lines
936 B
Lua
Raw Normal View History

2017-01-08 00:11:19 +00:00
local socket = require("socket")
local http = require("socket.http")
local json = require("vendor.json")
2017-01-08 00:11:19 +00:00
local weatherURL = 'https://openweathermap.org/data/2.5/weather?id=6695624&units=metric&appid=b1b15e88fa797225412429c1c50c122a1'
local insideURL = 'http://dht01.waw.hackerspace.pl/'
local insideData = {}
2017-01-08 00:11:19 +00:00
local r, c, h = http.request(weatherURL)
if c == 200 then
local data = json.decode(r)
local r, c, h = http.request(insideURL)
if c == 200 then
2017-01-08 16:17:43 +00:00
for n in string.gmatch(string.gsub(r, ",", "."), ":%s*(%S+)[*%%]") do
insideData[#insideData+1] = n
end
end
2017-01-08 00:11:19 +00:00
love.thread.getChannel('weather'):push({
weather = data.weather[1].main:lower(),
temperature = data.main.temp,
lastUpdate = data.dt,
sunRise = data.sys.sunrise,
sunSet = data.sys.sunset,
insideTemperature = insideData[1],
insideHumidity = insideData[2],
2017-01-08 00:11:19 +00:00
})
print("Update finished")
2017-01-08 00:11:19 +00:00
else
print("Update failed")
2017-01-08 00:11:19 +00:00
end