-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.lua
125 lines (120 loc) · 3.28 KB
/
app.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
local config = require "config"
local req_timer = tmr.create()
-- Helper Functions
function createDevice()
post_url = config.server.url..'api/devices'
print(post_url)
http.post(post_url,
'Content-Type: application/x-www-form-urlencoded\r\n',
'name='..config.device.name..'&password='..config.device.key,
function(code, body, headers)
if (code < 0) then
print("err: HTTP request failed")
else
ok, t = pcall(sjson.decode,body)
if ok then
config.device.token = t["token"]
tmr.register(req_timer, 5000, tmr.ALARM_AUTO, updateDevice)
tmr.start(req_timer)
pcall(print,token)
else
print("err: failed to parse JSON response")
end
end
end)
end
function renewToken()
tmr.stop(req_timer)
post_url = config.server.url..'api/renew/'..config.device.name
print(post_url)
http.post(post_url,
'Content-Type: application/x-www-form-urlencoded\r\n',
'name='..config.device.name..'&password='..config.device.key,
function(code, body, headers)
if (code < 0) then
print("err: HTTP request failed")
else
print(body)
ok, t = pcall(sjson.decode,body)
if ok then
config.device.token = t["token"]
pcall(print,token)
tmr.start(req_timer)
else
renewToken()
print("err: failed to parse JSON response")
end
end
end)
end
function updateDevice()
get_url = config.server.url..'api/update/'..config.device.name..'?m=0'
print(get_url)
print("HTTP GET using token: "..config.device.token)
http.get(get_url,
'x-access-token:'..config.device.token..'\r\n',
function(code, body, headers)
if (code < 0) then
print("HTTP request failed")
else
ok, t = pcall(sjson.decode,body)
if ok then
if(t["auth"] ~= nil and t["auth"] == false) then
renewToken()
end
else
print("err: failed to parse JSON response")
end
end
end)
end
function motionDetected()
get_url = config.server.url..'api/update/'..config.device.name..'?m=1'
print(get_url)
print("HTTP GET using token: "..config.device.token)
http.get(get_url,
'x-access-token:'..config.device.token..'\r\n',
function(code, body, headers)
if (code < 0) then
print("HTTP request failed")
else
ok, t = pcall(sjson.decode,body)
if ok then
if(t["auth"] ~= nil and t["auth"] == false) then
renewToken()
end
else
print("err: failed to parse JSON response")
end
end
end)
end
function startDevice()
print("Device "..config.device.name.." bootstraped")
tmr.register(req_timer, 5000, tmr.ALARM_AUTO, updateDevice)
tmr.start(req_timer)
end
function blinkLED()
gpio.write(1, gpio.HIGH)
tmr.delay(1000 * 1000) --1sec
gpio.write(1, gpio.LOW)
end
--end of Helper Functions
last_state = 1
pin = 2 --GPIO4
gpio.mode(pin, gpio.INPUT)
gpio.mode(1, gpio.OUTPUT) --GPIO5
tmr.alarm(0,100, 1, function()
if gpio.read(pin) ~= last_state then
last_state = gpio.read(pin)
if last_state == 1 then
motionDetected()
blinkLED()
end
end
end)
if (config.device.token == "") then
createDevice()
else
startDevice()
end