-
Notifications
You must be signed in to change notification settings - Fork 8
/
estado.lua
59 lines (51 loc) · 2.11 KB
/
estado.lua
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
estado={}
function estado.send()
latch1 = 3
latch2 = 4
gpio.mode(latch1, gpio.OUTPUT)
gpio.mode(latch2, gpio.OUTPUT)
urls="https://latch.elevenpaths.com/api/1.3/status/"
tmr.alarm(1, 10000, 1, function() estado.estado() end)
end
function estado.estado()
sntp.sync("150.214.94.5", function() -- success
tm = rtctime.epoch2cal(rtctime.get()+3600)
timestamp=(string.format("%04d-%02d-%02d %02d:%02d:%02d", tm["year"], tm["mon"], tm["day"], tm["hour"], tm["min"], tm["sec"]))
--funcion
end)
if (timestamp==nil)then
print "esperando sincronia sntp"else
---construir headers y firma
if file.exists("ACOUNTS") then
file.open("ACOUNTS", "r")
cuenta=(file.read('\n'))
file.close()
file.open("APPID", "r")
apid=(file.read('\n'))
file.close()
file.open("SK", "r")
secretos=(file.read('\n'))
file.close()
end
local statuso="GET\n"..timestamp.."\n\n/api/1.3/status/"..cuenta
firma=(crypto.hmac("sha1",statuso,secretos))
local b64= crypto.toBase64(firma)
local header_table={}
table.insert(header_table, 'Authorization: 11PATHS '..apid..' '..b64..'\r\n')
table.insert(header_table, 'X-11Paths-Date: '..timestamp..'\r\n')
table.insert(header_table, 'Request Signature: '..b64..'\r\n')
local header=table.concat(header_table)
http.request(urls..cuenta,"GET",header,' ', function(code, data)
print(code, data)
if (code < 0) then
print("HTTP request failed")
else
local ps=string.sub(data,0,75)
local lt=string.match(ps,'"status":"on"')
if (lt=='"status":"on"')then gpio.write(latch2,gpio.HIGH) print "GPIo2 ON"
else gpio.write(latch2,gpio.LOW) print "GPIo2 OFF"end
end
end)
end
end
return estado