-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprovider_elastic.lua
58 lines (48 loc) · 1.42 KB
/
provider_elastic.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
local deviceId = 'esp8266-' .. wifi.sta.getmac():gsub(':', '')
function provider_submit_data(data, callback)
log('sending data to elastic')
local ts = rtctime.get()
local body = ''
foreach(data, function(sensorName, sensorData)
local indexLine = {
index = {
_index = cfg.elastic_index,
_type = deviceId .. '-' .. sensorName,
}
}
local ok, indexJson = pcall(sjson.encode, indexLine)
if not ok then
log('error encoding index json')
return
end
local dataLine = {
data = sensorData
}
local ok, dataJson = pcall(sjson.encode, dataLine)
if not ok then
log('error encoding data json')
return
end
body = body .. indexJson .. '\n' .. dataJson .. '\n'
end)
if body.len() == 0 then
callback()
return
end
http.post(
cfg.elastic_url .. '_bulk',
'Authorization: ' .. cfg.elastic_auth .. '\r\nContent-Type: application/x-ndjson',
body,
function(status_code, body, headers)
-- TODO: detect error, if any
if cfg.debug then
log(status_code, body)
if headers then
foreach(headers, log)
end
end
callback()
end
)
end
log('elastic provider module loaded');