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
0 commit comments