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.

Creat logs in txt file
#20
(18.06.2024, 08:36)admin Wrote: Script from post #2 won't create a file if there are no logs in the given period.
what do you mean? I don't quite understand, do you mean this script? Sad
--------------------------------------------
-- Set logtime start-end and date to get past day data
date = os.date('*t')
date.hour=0
date.min=0
date.sec=0
log_end=os.time(date)-1
date.day = date.day - 1
log_start=os.time(date)


dates = {
['start'] = date,
['end'] = os.date('*t'),
}

-- Log filename
logfile=string.format('DailyLog_%s.csv', os.date('%d-%m-%Y',log_start))


-- list of objects by id
objects = {}

-- Select objects address and datatype
query = 'SELECT address, datatype, name FROM objects'
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'
query = 'SELECT * FROM objectlog WHERE logtime >='..log_start.. ' and logtime<=' .. log_end.. ' ORDER BY id'
for _, row in ipairs(db:getall(query)) 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('%d/%m/%Y %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

-- write file only when there's data in buffer
if #buffer > 1 then
result, err = io.writefile ('/home/ftp/'..logfile, table.concat(buffer, '\r\n'))
log('Created log file '..logfile)
end

-- error while writing
if err then
alert('File write error: %s', tostring(err))
end
-------------------------------------------
Reply


Messages In This Thread
Creat logs in txt file - by deimostier - 27.04.2017, 14:03
RE: Creat logs in txt file - by gtsamis - 27.04.2017, 21:55
RE: Creat logs in txt file - by deimostier - 02.05.2017, 14:41
RE: Creat logs in txt file - by gtsamis - 02.05.2017, 14:51
RE: Creat logs in txt file - by mjaanes - 02.04.2020, 10:11
RE: Creat logs in txt file - by admin - 02.04.2020, 11:20
RE: Creat logs in txt file - by tassiebean - 12.06.2020, 15:10
RE: Creat logs in txt file - by Trond Hoyem - 13.08.2020, 09:46
RE: Creat logs in txt file - by Trond Hoyem - 07.01.2021, 12:02
RE: Creat logs in txt file - by admin - 12.06.2020, 15:47
RE: Creat logs in txt file - by tassiebean - 13.06.2020, 03:20
RE: Creat logs in txt file - by admin - 13.08.2020, 11:12
RE: Creat logs in txt file - by admin - 07.01.2021, 12:14
RE: Creat logs in txt file - by phongvucba - 18.06.2024, 08:03
RE: Creat logs in txt file - by admin - 18.06.2024, 08:14
RE: Creat logs in txt file - by phongvucba - 18.06.2024, 08:27
RE: Creat logs in txt file - by Daniel - 18.06.2024, 08:33
RE: Creat logs in txt file - by phongvucba - 18.06.2024, 08:35
RE: Creat logs in txt file - by admin - 18.06.2024, 08:36
RE: Creat logs in txt file - by phongvucba - 18.06.2024, 08:44
RE: Creat logs in txt file - by Daniel - 18.06.2024, 08:48
RE: Creat logs in txt file - by phongvucba - 18.06.2024, 08:57
RE: Creat logs in txt file - by Daniel - 18.06.2024, 09:02
RE: Creat logs in txt file - by phongvucba - 18.06.2024, 09:12
RE: Creat logs in txt file - by Daniel - 18.06.2024, 09:12
RE: Creat logs in txt file - by admin - 18.06.2024, 09:15
RE: Creat logs in txt file - by phongvucba - 18.06.2024, 10:00

Forum Jump: