summaryrefslogtreecommitdiffstats
path: root/nodes/newdash_thread.lua
blob: d5a1b557ab34684a0f652ee40628774befdf6569 (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
local socket = require("socket")
local http = require("socket.http")
local json = require("vendor.json")

local weatherURL = 'https://openweathermap.org/data/2.5/weather?id=6695624&units=metric&appid=b1b15e88fa797225412429c1c50c122a1'
local spejsiotURL = 'http://spejsiot.waw.hackerspace.pl/api/1/devices'
local atURL = 'http://at.hackerspace.pl/api'
local spejsiotData = {}
local atData = nil

local r, c, h = http.request(weatherURL)
if c == 200 then
  local data = json.decode(r)

  local r, c, h = http.request(spejsiotURL)
  if c == 200 then
    spejsiotData = json.decode(r)
  end

  local r, c, h = http.request(atURL)

  if c == 200 then
    atData = json.decode(r)
  end

  love.thread.getChannel('newdash'):push({
    weather = data.weather[1].main:lower(),
    temperature = data.main.temp,
    lastUpdate = data.dt,
    spejsiot = spejsiotData,
    at = atData,
  })
  print("Update finished")
else
  print("Update failed")
end