18.10.2022, 06:36
Try this rewritten version:
Code:
local http = require('socket.http')
http.TIMEOUT = 5
local url = 'http://192.168.1.200:8086/api/v2/write?bucket=XXX&org=XXX'
local token = 'XXXXX'
local influxtable = 'rawdata'
local tagobjs = grp.tag('influxdb')
local function escape(value)
return tostring(value):gsub('[\\ ,=]', '\\%1')
end
local objects = {}
for _, obj in ipairs(tagobjs) do
objects[ obj.id ] = {
name = escape(obj.name),
datatype = obj.datatype,
}
end
local function send(name, addr, value)
local body
if type(value) == 'boolean' then
body = string.format('%s,name=%s,addr=%s state=%s', influxtable, name, addr, value)
else
body = string.format('%s,name=%s,addr=%s value=%s', influxtable, name, addr, escape(value))
end
local res, code = http.request({
url = url,
method = 'POST',
body = body,
headers = {
Authorization = 'Token ' .. token
}
})
if code ~= 204 then
log('error sending to influx', res, code, body)
end
end
local busclient = require('localbus').new(60)
busclient:sethandler('groupwrite', function(event)
local obj = objects[ event.dstraw ]
if obj then
local value = busdatatype.decode(event.datahex, obj.datatype)
if value ~= nil then
send(obj.name, event.dst, value)
end
end
end)
while true do
busclient:loop(60)
end