16.09.2022, 10:26
(15.09.2022, 12:55)admin Wrote: This example will push only objects that have influx tag attached.
Code:local http = require('socket.http')
http.TIMEOUT = 5
local objs = {}
local tagobjs = grp.tag('influx')
for _, obj in ipairs(tagobjs) do
objs[ obj.address ] = {
name = obj.name:gsub(' ', '\\ '),
datatype = obj.datatype,
}
end
function callback(event)
local addr = event.dst
local obj = objs[ addr ]
if obj then
local value = knxdatatype.decode(event.datahex, obj.datatype)
sendmetric('rawdata', obj.name, addr, value)
end
end
function sendmetric(table, name, addr, value)
local url = 'https://europe-west1-1.gcp.cloud2.influxdata.com/api/v2/write?bucket=XX&org=XXX'
local body
if type(value) == 'boolean' or type(value) == 'number' then
body = string.format('%s,name=%s,addr=%s state=%s', table, name, addr, value)
elseif type(value) == 'string' then
body = string.format('%s,name=%s,addr=%s value=%q', table, name, addr, value)
else
log('invalid data type', addr, type(value))
return
end
local res, code = http.request({
url = url,
method = 'POST',
body = body,
headers = {
Authorization = 'Token XXXXXXXXXXXXXXXXXXX'
}
})
if code ~= 204 then
log('error sending to influx', res, code, body)
end
end
local busclient = require('localbus').new()
busclient:sethandler('groupwrite', callback)
while true do
busclient:loop(1)
end
Everything is fine (although I still have to learn how to handle the data to create a decent report )
Now I have the final step: to connect influxdb to Grafana.
I don't know how to choose the right authentication metod to connect it. Where to put the token and org name??!