15.09.2022, 12:55
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