12.08.2024, 07:42
(12.08.2024, 07:38)admin Wrote: Post your script.
Code:
local http = require('socket.http')
http.TIMEOUT = 5
local url = 'http://192.168.100.20:8086/api/v2/write?bucket=ets&org=Matrix'
local token = 'deleted'
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', table, name, addr, value)
elseif type(value) == 'number' then
body = string.format('%s,name=%s,addr=%s value=%s', table, name, addr, value)
elseif type(value) == 'string' then
body = string.format('%s,name=%s,addr=%s string=%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 ' .. 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