Skip to content

Commit 032a88c

Browse files
authored
Add files via upload
1 parent 9be1890 commit 032a88c

File tree

6 files changed

+177
-2
lines changed

6 files changed

+177
-2
lines changed

configuracion.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
configuracion={}
2+
function configuracion.latch()
3+
local idapp="sLN9r8fm7HhrCajfKAkL"
4+
local secreto="TGUqPKEA7j9z3ux3afFJah2wdDWH4FxsU7FR6AGx"
5+
local token="EPjCQk"
6+
return (idapp..secreto..token)
7+
end
8+
return configuracion

estado.lua

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
estado={}
2+
function estado.send()
3+
latch1 = 3
4+
latch2 = 4
5+
gpio.mode(latch1, gpio.OUTPUT)
6+
gpio.mode(latch2, gpio.OUTPUT)
7+
urls="https://latch.elevenpaths.com/api/1.3/status/"
8+
tmr.alarm(1, 10000, 1, function() estado.estado() end)
9+
end
10+
function estado.estado()
11+
sntp.sync("150.214.94.5", function() -- success
12+
tm = rtctime.epoch2cal(rtctime.get()+3600)
13+
timestamp=(string.format("%04d-%02d-%02d %02d:%02d:%02d", tm["year"], tm["mon"], tm["day"], tm["hour"], tm["min"], tm["sec"]))
14+
--funcion
15+
end)
16+
if (timestamp==nil)then
17+
18+
print "esperando sincronia sntp"else
19+
---construir headers y firma
20+
if file.exists("ACOUNTS") then
21+
22+
file.open("ACOUNTS", "r")
23+
cuenta=(file.read('\n'))
24+
file.close()
25+
26+
file.open("APPID", "r")
27+
apid=(file.read('\n'))
28+
file.close()
29+
30+
file.open("SK", "r")
31+
secretos=(file.read('\n'))
32+
file.close()
33+
end
34+
35+
local statuso="GET\n"..timestamp.."\n\n/api/1.3/status/"..cuenta
36+
firma=(crypto.hmac("sha1",statuso,secretos))
37+
local b64= crypto.toBase64(firma)
38+
local header_table={}
39+
table.insert(header_table, 'Authorization: 11PATHS '..apid..' '..b64..'\r\n')
40+
table.insert(header_table, 'X-11Paths-Date: '..timestamp..'\r\n')
41+
table.insert(header_table, 'Request Signature: '..b64..'\r\n')
42+
local header=table.concat(header_table)
43+
http.request(urls..cuenta,"GET",header,' ', function(code, data)
44+
print(code, data)
45+
if (code < 0) then
46+
print("HTTP request failed")
47+
else
48+
local ps=string.sub(data,0,75)
49+
local lt=string.match(ps,'"status":"on"')
50+
if (lt=='"status":"on"')then gpio.write(latch2,gpio.HIGH) print "GPIo2 ON"
51+
else gpio.write(latch2,gpio.LOW) print "GPIo2 OFF"end
52+
53+
54+
55+
end
56+
end)
57+
end
58+
end
59+
return estado

init.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
dofile("main.lua")
2-
1+
dofile("main.lua")

main.lua

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
tmr.alarm(1, 20000, 2, function() arranca() end)
2+
3+
function arranca()
4+
5+
local gifi=require("gifi")
6+
7+
gifi.conf()
8+
9+
print(gifi.conf())
10+
11+
if(gifi.conf()==5)then
12+
13+
local configuracion=require("configuracion")
14+
local xxx=(configuracion.latch())
15+
local apid=string.sub(xxx,1,20)
16+
local secreto=string.sub(xxx,21,60)
17+
local token=string.sub(xxx,61,66)
18+
19+
20+
21+
if file.exists("ACOUNTS") then
22+
print ("consultando el estado de app: "..apid)
23+
print""
24+
25+
local estado=require("estado")
26+
estado.send()
27+
else
28+
print ("Intentando parear app: "..apid)
29+
print""
30+
print ("con token: "..token)
31+
local pareado=require("pareado")
32+
pareado.send(apid,secreto,token)
33+
34+
print" "
35+
36+
37+
end
38+
39+
end
40+
41+
end
42+
43+
44+
45+
46+

pareado.lua

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
pareado={}
2+
urlp="https://latch.elevenpaths.com/api/1.3/pair/"
3+
function pareado.send(apid,secreto,token)
4+
5+
tmr.alarm(1, 10000, 1, function() pareado.latch(apid,secreto,token) end)
6+
end
7+
8+
9+
function pareado.latch(apid,secretos,token)
10+
sntp.sync("150.214.94.5", function() -- success
11+
tm = rtctime.epoch2cal(rtctime.get()+3600)
12+
timestamp=(string.format("%04d-%02d-%02d %02d:%02d:%02d", tm["year"], tm["mon"], tm["day"], tm["hour"], tm["min"], tm["sec"]))
13+
--funcion
14+
end)
15+
if (timestamp==nil)then
16+
17+
print "esperando sincronia sntp"else
18+
---construir headers y firma
19+
local pareado="GET\n"..timestamp.."\n\n/api/1.3/pair/"..token
20+
firma=(crypto.hmac("sha1",pareado,secretos))
21+
local b64= crypto.toBase64(firma)
22+
local header_table={}
23+
table.insert(header_table, 'Authorization: 11PATHS '..apid..' '..b64..'\r\n')
24+
table.insert(header_table, 'X-11Paths-Date: '..timestamp..'\r\n')
25+
table.insert(header_table, 'Request Signature: '..b64..'\r\n')
26+
local header=table.concat(header_table)
27+
http.request(urlp..token,"GET",header,' ', function(code, data)
28+
print(code, data)
29+
if (code < 0) then
30+
print("HTTP request failed")
31+
else
32+
local ps=string.sub(data,0,107)
33+
local aco=string.match(ps,"accountId...................................................................")
34+
if (aco==nil)then print " noooooooooooo"
35+
else
36+
cuenta=string.sub(aco,13,76)
37+
--print (cuenta)
38+
--grabando datos persistentes account
39+
file.open("ACOUNTS", "w")
40+
-- print"Grabando ACOUNTS.file"
41+
file.write(cuenta)
42+
file.close()
43+
---grabando APPID
44+
file.open("APPID", "w")
45+
-- print"Grabando APPID"
46+
file.write(apid)
47+
file.close()
48+
---grabando SECRET
49+
file.open("SK", "w")
50+
file.write(secretos)
51+
file.close()
52+
53+
end
54+
end
55+
end)
56+
end
57+
58+
end
59+
60+
return pareado

tiempo.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
tiempo={}
2+
3+
return tiempo

0 commit comments

Comments
 (0)