-
Notifications
You must be signed in to change notification settings - Fork 0
/
webscript_callback.lua
129 lines (113 loc) · 3.53 KB
/
webscript_callback.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
126
127
128
129
local CONSUMERTOKEN = '---'
local CONSUMERSECRET = '---'
local ACCESSTOKEN = storage.accesstoken or nil
local TOKENSECRET = storage.tokensecret or nil
local threshold = 2000 --steps threshold
local debug = ""
local alerted = storage.alerted or "false"
local checked = storage.checked or "false"
local date = os.date("%Y").."-"..os.date("%m").."-"..os.date("%d")
local steps = {}
steps[1] = 0
function check_fitbit()
local response = http.request {
url='http://api.fitbit.com/1/user/23THB6/activities/date/'..date..'.json',
auth = { oauth = {
consumertoken = CONSUMERTOKEN,
consumersecret = CONSUMERSECRET,
accesstoken = ACCESSTOKEN,
tokensecret = TOKENSECRET
}}
}
return {json.parse(response.content).summary.steps}
end
function controlWemo(status, port)
local response = http.request {
url = 'http://83.212.96.61:'..tonumber(port)..'/upnp/control/basicevent1',
method = 'POST',
headers = {
charset='utf-8',
SOAPACTION = '"urn:Belkin:service:basicevent:1#SetBinaryState"'
},
data = '<?xml version="1.0" encoding="utf-8"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:SetBinaryState xmlns:u="urn:Belkin:service:basicevent:1"><BinaryState>'..status..'</BinaryState></u:SetBinaryState></s:Body></s:Envelope>'
}
return response
end
function talkPusher(status)
local pusher = require('pusher')
local response = pusher.send(35559, 'KEY', 'SECRET', 'fitbit', 'fitbit', status)
end
function docheck()
local time_zone = (tonumber(os.date("%H"))+2)
if time_zone < 18 then
storage.alerted = "false"
storage.checked = "false"
alerted = "false"
checked = "false"
debug="will check later today..."
end
if alerted=="true" then
debug = "wemo switch control"
steps = check_fitbit()
if tonumber(steps[1])<threshold then
--controlWemo(0, 5555)
debug="wemo must be off"
else
storage.alerted = "false"
storage.checked = "true"
checked = "true"
alerted = "false"
--enable switch
--controlWemo(1, 5555)
alert.email("Well done mate!")
debug = "switch enabled"
end
else
if time_zone > 18 then
debug = "must check"
if checked=="false" then
debug = "checking with fitbit"
steps = check_fitbit()
if tonumber(steps[1])<threshold then
storage.alerted = "true"
alert.email("You need to move your @$$ today!")
debug = "checked below threshold"
else
storage.alerted = "false"
storage.checked = "true"
alert.email("Well done! "..tonumber(steps[1]).." steps so far today!")
debug = "well done today!"
end
else
debug = "already checked for today"
end
end
end
steps = check_fitbit()
return isAlerted..":"..storage.checked..":"..steps[1]..":"..time_zone..":"..debug
end
--main script execution starts here
if ACCESSTOKEN==nil and TOKENSECRET==nil then
--get some credentials to work from fitbit
local response = http.request {
url='http://api.fitbit.com/oauth/access_token',
auth = { oauth = {
consumertoken = CONSUMERTOKEN,
consumersecret = CONSUMERSECRET,
accesstoken = request.query.oauth_token,
tokensecret = storage['secret:'..request.query.oauth_token],
verifier = request.query.oauth_verifier
}}
}
local ret = http.qsparse(response.content)
-- clean up
storage['secret:'..request.query.oauth_token] = nil
ACCESSTOKEN = ret.oauth_token
TOKENSECRET = ret.oauth_token_secret
storage.accesstoken = ACCESSTOKEN
storage.tokensecret = TOKENSECRET
return docheck()
else
--got oauth credentials, let's check fitbit stats:
return docheck()
end