-
Notifications
You must be signed in to change notification settings - Fork 1
/
Netatmo.lua
220 lines (212 loc) · 6.71 KB
/
Netatmo.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
--[[
Netatmo SDK
@author ikubicki
]]
class 'Netatmo'
function Netatmo:new(config)
self.config = config
self.user = config:getUsername()
self.pass = config:getPassword()
self.client_id = config:getClientID()
self.client_secret = config:getClientSecret()
self.device_id = config:getDeviceID()
self.access_token = config:getAccessToken()
self.token = Globals:get('netatmo_atoken', '')
self.refresh_token = config:getRefreshToken()
self.http = HTTPClient:new({})
return self
end
function Netatmo:searchDevices(callback)
local buildModule = function(module)
return {
id = module._id,
name = module.module_name,
type = module.type,
data_type = module.data_type,
}
end
local buildStation = function(data)
local station = {
id = data._id,
home_id = data.home_id,
name = data.station_name,
modules = {},
}
table.insert(station.modules, buildModule(data))
return station
end
local getStationsDataCallback = function(devices)
local stations = {}
for _, device in ipairs(devices) do
local station = buildStation(device)
for _, module in ipairs(device.modules) do
table.insert(station.modules, buildModule(module))
end
table.insert(stations, station)
end
if callback ~= nil then
callback(stations)
end
end
local authCallback = function(response)
if response.error ~= nil then
callback(response)
return
end
self:getStationsData(getStationsDataCallback)
end
self:auth(authCallback)
end
function Netatmo:getWeatherData(callback)
local getStationsDataCallback = function(devices)
local device = devices[1]
local weatherData = {
_id = device._id,
temp = tonumber(device.dashboard_data["Temperature"]),
humi = tonumber(device.dashboard_data["Humidity"]),
rain = 0,
wind = 0,
}
for _, module in pairs(device.modules) do
if module.type == "NAModule1" then
weatherData.temp = tonumber(module.dashboard_data.Temperature)
weatherData.humi = tonumber(module.dashboard_data.Humidity)
end
if module.type == "NAModule2" then
weatherData.wind = tonumber(module.dashboard_data.WindStrength)
end
if module.type == "NAModule3" then
weatherData.rain = tonumber(module.dashboard_data.Rain)
end
end
if callback ~= nil then
callback(weatherData)
end
end
local authCallback = function(response)
if response.error ~= nil then
callback(response)
return
end
self:getStationsData(getStationsDataCallback)
end
self:auth(authCallback)
end
function Netatmo:getStationsData(callback, attempt)
if attempt == nil then
attempt = 0
end
local fail = function(response)
QuickApp:error('Unable to pull devices')
QuickApp:debug(json.encode(response.data))
Netatmo:setToken('')
if attempt < 3 then
attempt = attempt + 1
fibaro.setTimeout(3000, function()
QuickApp:debug('Netatmo:getStationData - Retry attempt #' .. attempt)
local authCallback = function(response)
self:getStationsData(callback, attempt)
end
Netatmo:auth(authCallback)
end)
end
end
local success = function(response)
if response.status > 299 then
fail(response)
return
end
local data = json.decode(response.data)
if callback ~= nil then
callback(data.body.devices)
end
end
local url = 'https://api.netatmo.com/api/getstationsdata'
if string.len(self.device_id) > 1 then
url = url .. '?device_id=' .. self.device_id
end
local headers = {
Authorization = "Bearer " .. self:getToken()
}
self.http:get(url, success, fail, headers)
end
function Netatmo:auth(callback)
if string.len(self:getToken()) > 10 then
-- QuickApp:debug('Already authenticated')
if callback ~= nil then
callback({})
end
return
end
if string.len(self.access_token) > 10 then
if callback ~= nil then
Netatmo:setToken(self.access_token)
callback({})
end
return
end
local data = {
["grant_type"] = 'refresh_token',
["refresh_token"] = self.refresh_token,
["client_id"] = self.client_id,
["client_secret"] = self.client_secret,
}
local fail = function(response)
QuickApp:error('Unable to authenticate')
if self.access_token == self.token then
QuickApp:error('Removing configured AccessToken')
self.config:setAccessToken('')
end
Netatmo:setToken('')
if callback ~= nil then
callback(response)
end
end
if string.len(self.refresh_token) < 10 then
QuickApp:error('No refresh token available. Cannot authenticate the device.')
fail({
error = "No refresh token available. Cannot authenticate the device."
})
return
end
local success = function(response)
-- QuickApp:debug('---')
-- QuickApp:debug(response.status)
-- QuickApp:debug(response.data)
if response.status > 299 or response.status < 200 then
fail({
error = "Unable to authenticate"
})
return
end
local data = json.decode(response.data)
Netatmo:setToken(data.access_token)
Netatmo:setRefreshToken(data.refresh_token)
if callback ~= nil then
callback({})
end
end
self.http:postForm('https://api.netatmo.net/oauth2/token', data, success, fail)
end
function Netatmo:setToken(token)
self.token = token
Globals:set('netatmo_atoken', token)
end
function Netatmo:setRefreshToken(refresh_token)
QuickApp:debug('setting new refresh token ')
QuickApp:debug(refresh_token)
self.refresh_token = refresh_token
Globals:set('netatmo_rtoken', refresh_token)
Config:setRefreshToken(refresh_token)
end
function Netatmo:getToken()
if not self.token and self.access_token ~= nil then
self.token = self.access_token
end
if string.len(self.token) > 10 then
return self.token
elseif string.len(Globals:get('netatmo_atoken', '')) > 10 then
return Globals:get('netatmo_atoken', '')
end
return ""
end