This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Automatic export all logs and send by mail
#23
(05.10.2023, 06:52)admin Wrote: See this example: https://openrb.com/example-export-last-h...-from-lm2/
It has both local file and remote FTP options.

Hello Admin,

I have applied the solution from the example on the link, although unfortunately I just noticed that not all fields are included on the csv file. I would like to export all data that can be seen on the object logs utility. For the moment only timestamp, address, name and value is included in the csv export...

I am using the following script:
Code:
   --ftp file
   ftpfile = string.format('/home/ftp/%s.csv', os.date('%Y-%m-%d_%H-%M'))
   -- get past 12 hours data (43200 seconds)
   logtime = os.time() - 12 * 60 * 60
    
    -- list of objects by id
    objects = {}
    
    -- objects with logging enabled
    query = 'SELECT address, datatype, name FROM objects WHERE disablelog=0'
    for _, object in ipairs(db:getall(query)) do
      objects[ tonumber(object.address) ] = {
        datatype = tonumber(object.datatype),
        name = tostring(object.name or ''),
      }
    end
    
    -- csv buffer
    buffer = { '"date","address","name","value"' }
    
    -- get object logs
    query = 'SELECT src, address, datahex, logtime, eventtype FROM objectlog WHERE logtime >= ? ORDER BY id DESC'
    for _, row in ipairs(db:getall(query, logtime)) do
      object = objects[ tonumber(row.address) ]
    
      -- found matching object and event type is group write
      if object and row.eventtype == 'write' then
        datatype = object.datatype
    
        -- check that object datatype is set
        if datatype then
          -- decode data
          data = knxdatatype.decode(row.datahex, datatype)
    
          -- remove null chars from char/string datatype
          if datatype == dt.char or datatype == dt.string then
            data = data:gsub('%z+', '')
          -- date to DD.MM.YYYY
          elseif datatype == dt.date then
            data = string.format('%.2d.%.2d.%.2d', data.day, data.month, data.year)
          -- time to HH:MM:SS
          elseif datatype == dt.time then
            data = string.format('%.2d:%.2d:%.2d', data.hour, data.minute, data.second)
          end
        else
          data = ''
        end
    
        -- format csv row
        logdate = os.date('%Y.%m.%d %H:%M:%S', row.logtime)
        csv = string.format('%q,%q,%q,%q', logdate, knxlib.decodega(row.address), object.name, tostring(data))
    
        -- add to buffer
        table.insert(buffer, csv)
      end
    end
    
        -- upload to ftp only when there's data in buffer
    if #buffer > 1 then
      data = table.concat(buffer, '\r\n')
      io.writefile(ftpfile, data)
    end

How can this be modified to include the type (read/write) and source address fields?

Kind Regards,
Reply


Messages In This Thread
RE: Automatic export all logs and send by mail - by manos@dynamitec - 16.01.2024, 11:59

Forum Jump: