Good day!
Can you help me with such question:
I need to create regular log files *txt type. Each day it will be a new file with a name like date. It is necessary to save it on any shared local net folder
27.04.2017, 21:55 (This post was last modified: 27.04.2017, 22:13 by gtsamis.)
Here is a part of my daily report script. It runs in a scheduled script every day at 00:00 and creates a csv file in /tmp folder for object logs of previous day.
PS: Credit for parts of the script goes to Edgars
Code:
-- 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)
-- 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
-- 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
-- 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 ('/tmp/'..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
02.05.2017, 14:41 (This post was last modified: 02.05.2017, 14:42 by deimostier.)
(27.04.2017, 21:55)gtsamis Wrote: Here is a part of my daily report script. It runs in a scheduled script every day at 00:00 and creates a csv file in /tmp folder for object logs of previous day.
PS: Credit for parts of the script goes to Edgars
Code:
-- 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)
-- 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
-- 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
-- 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 ('/tmp/'..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
Sorry, but it is necessary to create only txt files
(27.04.2017, 21:55)gtsamis Wrote: Here is a part of my daily report script. It runs in a scheduled script every day at 00:00 and creates a csv file in /tmp folder for object logs of previous day.
PS: Credit for parts of the script goes to Edgars
Code:
-- 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)
-- 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
-- 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
-- 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 ('/tmp/'..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
Sorry, but it is necessary to create only txt files
You just need to change the extension to txt on logfile=string.format('DailyLog_%s.csv', os.date('%d-%m-%Y',log_start)) to logfile=string.format('DailyLog_%s.txt', os.date('%d-%m-%Y',log_start))
the structure although will be the same.
I "stole" this script (thank you). It runs perfect, but how can I access (download to PC) the CSV file produced?
I have tried Filezilla and can access the Wiser/SL successfully, but neither the user "ftp" nor the user "app" display anything that resembles "/tmp/......" which is the path used above in the following line: result, err = io.writefile ('/tmp/'..logfile, table.concat(buffer, '\r\n'))
(02.04.2020, 11:20)admin Wrote: FTP path is /home/ftp, just make sure that you have some algorithm to delete old files. Otherwise you can run out of free space at some point.
I'm still confused on this point. I used Filezilla to connect and placed a doorbell sound file (at what I'm taking from your post above is /home/ftp). My ftp login was user ftp (can choose from ftp or apps). But my Custom Javascript file appears not to see the copied file. For example, in the following file if I uncomment and use the mozilla file, the sound plays fine. But when written as shown here with the local file, the code does not work. Is it a linux permissions thing?
Code:
// the following function plays a sound when the doorbell is pressed (Object 0/250/72 is set true)
// FTP path is /home/ftp (use Filezilla to put sound file there)
$(function(){
grp.listen('0/250/72', function(object) {
if (object.value == true ) {
// var audio = new Audio('https://interactive-examples.mdn.mozilla.net/media/examples/t-rex-roar.mp3');
var audio = new Audio('/home/ftp/Doorbell.mp3');
audio.play();
}
});
});
(02.04.2020, 11:20)admin Wrote: FTP path is /home/ftp, just make sure that you have some algorithm to delete old files. Otherwise you can run out of free space at some point.
I am not able to find this file...
I log in with FileZilla with both "apps" and "ftp", but cannot find this folder. I have neihter the folder /home/ftp
In the script I log the err, result, and get true for result and nil for err.
There are 10 kinds of people in the world; those who can read binary and those who don't
The example script writes to /tmp/, which cannot be accessed via FTP. /home/ftp/ is the absolute path for writing from scripts. If you access FTP using ftp user then any file placed into /home/ftp/ will be visible in the base directory.
(02.04.2020, 11:20)admin Wrote: FTP path is /home/ftp, just make sure that you have some algorithm to delete old files. Otherwise you can run out of free space at some point.
How can old files be deleted?
For writing to new file one will use io.writefile, is there a similar for delete? I have tried to find it, but no luck...
There are 10 kinds of people in the world; those who can read binary and those who don't
18.06.2024, 08:03 (This post was last modified: 18.06.2024, 08:12 by phongvucba.)
"I am not able to find this file...
I log in with FileZilla with both "apps" and "ftp", but cannot find this folder. I have neihter the folder /home/ftp" . I changed
------------------
if #buffer > 1 then
result, err = io.writefile ('/home/ftp/'..logfile, table.concat(buffer, '\r\n'))
log('Created log file '..logfile)
end
--------------------
Can anyone see where that file is saved? I can't see where that file is
(27.04.2017, 21:55)gtsamis Wrote: Here is a part of my daily report script. It runs in a scheduled script every day at 00:00 and creates a csv file in /tmp folder for object logs of previous day.
PS: Credit for parts of the script goes to Edgars
Code:
-- 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)
-- 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
-- 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
-- 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 ('/tmp/'..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
Help me !! Can you explain more clearly how to do it, where to put this script, how to get the saved file? Because I don't see it
18.06.2024, 08:44 (This post was last modified: 18.06.2024, 08:45 by phongvucba.)
(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?
--------------------------------------------
-- 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)
-- 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
-- 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
-- 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
-------------------------------------------