Posts: 138
Threads: 19
Joined: Apr 2018
Reputation:
0
04.10.2023, 20:38
(This post was last modified: 04.10.2023, 20:38 by manos@dynamitec .)
Hello Admin,
I can understand that but we have a LM5Lp2 and using it on various sites as a logger when required. It is not that it is running for very long periods. I just want to overcome the 50k data points limit. In the projects we have (not logging everything of course), I would guess that 50K telegrams will be enough for one day. If the LM is logging for 7-15 days then this file will be 15 time written or so. When installed on a new site for example we can delete all files from the FTP server and start over with the logging.
Is this possible to do?
Regards,
Posts: 8162
Threads: 43
Joined: Jun 2015
Reputation:
473
See this example:
https://openrb.com/example-export-last-h...-from-lm2/
It has both local file and remote FTP options.
Posts: 138
Threads: 19
Joined: Apr 2018
Reputation:
0
16.01.2024, 11:59
(This post was last modified: 16.01.2024, 12:03 by manos@dynamitec .)
(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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
ftpfile =
string.format (
'/home/ftp/%s.csv' ,
os.date (
'%Y-%m-%d_%H-%M' ))
logtime =
os.time () -
12 *
60 *
60
objects = {}
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
buffer = {
'"date","address","name","value"' }
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 ) ]
if object and row.eventtype ==
'write' then
datatype =
object.datatype
if datatype then
data =
knxdatatype.decode (
row.datahex ,
datatype )
if datatype ==
dt.char or datatype ==
dt.string then
data =
data :
gsub (
'%z+' ,
'' )
elseif datatype ==
dt.date then
data =
string.format (
'%.2d.%.2d.%.2d' ,
data.day ,
data.month ,
data.year )
elseif datatype ==
dt.time then
data =
string.format (
'%.2d:%.2d:%.2d' ,
data.hour ,
data.minute ,
data.second )
end
else
data =
''
end
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 ))
table.insert (
buffer ,
csv )
end
end
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,
Posts: 8162
Threads: 43
Joined: Jun 2015
Reputation:
473
Posts: 265
Threads: 39
Joined: Feb 2016
Reputation:
1
(17.01.2022, 08:17) admin Wrote: Escaping of the "to" address was missing.
Here's a rewritten script with clearer structure. You only need to modify the to/settings/subject at the to top of the script. SSL settings do not matter anymore because newer SSL library automatically negotiates the highest supported SSL/TLS version between server/client.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
to =
'to@example.com'
settings = {
from =
'sender@example.com' ,
user =
'sender@example.com' ,
password =
'12345678' ,
server =
'smtp.gmail.com' ,
port =
465 ,
secure =
'sslv23' ,
}
subject =
'CSV logs'
logtime =
os.time () -
60 *
60
buffer = {
'"date","address","name","value"' }
query =
[[
SELECT o.id, o.datatype, o.name, ol.datahex, ol.logtime, ol.eventtype
FROM objectlog ol
JOIN objects o ON ol.address=o.id
WHERE ol.logtime >= ?
ORDER BY ol.id DESC
]]
for _ ,
row in ipairs (
db :
getall (
query ,
logtime ))
do
if row.datatype and row.eventtype ==
'write' then
data =
grp.decodevalue (
row.datahex ,
row.datatype )
logdate =
os.date (
'%Y.%m.%d %H:%M:%S' ,
row.logtime )
buffer [ #
buffer +
1 ] =
string.format (
'%q,%q,%q,%q' ,
logdate ,
knxlib.decodega (
row.id ),
row.name ,
tostring (
data ))
end
end
csv =
table.concat (
buffer ,
'\r\n' )
smtp =
require (
'socket.smtp' )
mime =
require (
'mime' )
ltn12 =
require (
'ltn12' )
function escape (
v )
return '<' ..
tostring (
v ) ..
'>'
end
to =
escape (
to )
msgt = {
headers = {
to =
to ,
[
'content-type' ] =
'text/csv' ,
[
'content-disposition' ] =
'attachment; filename="logs.csv"' ,
[
'content-transfer-encoding' ] =
'BASE64' ,
subject =
subject ,
},
body =
ltn12.source.chain (
ltn12.source.string (
csv ),
ltn12.filter.chain (
mime.encode (
'base64' ),
mime.wrap (
'base64' ))
)
}
settings.source =
smtp.message (
msgt )
settings.from =
escape (
settings.from )
settings.rcpt = {
to }
res ,
err =
smtp.send (
settings )
log (
res ,
err )
Hi, thanks for the script,
is there an easy way to separate in different csv file the log object and send them in one email?
-----------
FRANCE SMARTHOME & SMARTBUILDING INTEGRATION
SE ECO EXPERT
Posts: 265
Threads: 39
Joined: Feb 2016
Reputation:
1
20.02.2024, 11:49
(This post was last modified: 20.02.2024, 12:07 by domotiqa .)
I test it but I think the smtp output of my customer is blocked (result 1, nil) for the log. I will try on my test server
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
smtp =
require (
'socket.smtp' )
mime =
require (
'mime' )
ltn12 =
require (
'ltn12' )
to =
'xxx@gmail.com'
subject =
'CSV logs'
message =
'export log'
logtime =
os.time () -
60 *
60
query =
[[
SELECT ol2.name
FROM objects ol2
WHERE ol2.disablelog = 0
]]
csv ={}
nombre =
0
nom ={}
for _ ,
objectlog in ipairs (
db :
getall (
query ,
logtime ))
do
nombre =
nombre +
1
nom [
nombre ]=
objectlog.name
buffer = {
'"date","name","value"' }
query2 =
[[
SELECT o.id, o.datatype, o.name, ol.datahex, ol.logtime, ol.eventtype
FROM objectlog ol
JOIN objects o ON ol.address=o.id
WHERE (ol.logtime >= ?) ORDER BY ol.id DESC
]]
for _ ,
row in ipairs (
db :
getall (
query2 ,
logtime ))
do
if row.datatype and row.eventtype ==
'write' and row.name ==
nom [
nombre ]
then
data =
grp.decodevalue (
row.datahex ,
row.datatype )
logdate =
os.date (
'%Y.%m.%d %H:%M:%S' ,
row.logtime )
buffer [ #
buffer +
1 ] =
string.format (
'%q,%q,%q' ,
logdate ,
string.split (
row.name ,
'-' )[
5 ],
tostring (
data ))
end
end
csv [
nombre ] =
table.concat (
buffer ,
'\r\n' )
end
attachement = {}
nombre =
0
for _ ,
nomCpt in ipairs (
nom )
do
nombre =
nombre +
1
table.insert (
attachement , {
filename =
nomCpt ..
'.csv' ,
filedata =
csv [
nombre ],
mimetype =
'text/csv' })
end
mailattach (
to ,
subject ,
message ,
attachement )
and function in common
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
function mailattach (
to ,
subject ,
message ,
files )
local settings = {
from =
'notification@xxx.com' ,
user =
'notification@xxx.com' ,
password =
'xxx' ,
server =
'xxx.net' ,
port =
465 ,
secure =
'sslv23' ,
}
local smtp =
require (
'socket.smtp' )
if type (
to ) ~=
'table' then
to = {
to }
end
for index ,
email in ipairs (
to )
do
to [
index ] =
'<' ..
tostring (
email ) ..
'>'
end
for _ ,
object in ipairs (
table )
do
end
local from =
'<' ..
tostring (
settings.from ) ..
'>'
email = {
headers = {},
body = {}}
email.headers = {
to =
table.concat (
to ,
', ' ),
subject =
subject ,
[
'From' ] =
from ,
}
email.body [
1 ] = {
headers = { [
'Content-Type' ] =
'text/html; charset=utf-8' , },
body =
mime.eol (
0 ,
message ) }
for _ ,
object in ipairs (
files )
do
email.body [
_ +
1 ] = {
headers = { [
'Content-Type' ] =
files [
_ ].
mimetype or 'text/plain' , [
'Content-Disposition' ] =
'attachment; filename="' ..
files [
_ ].
filename ..
'"' , [
'Content-Transfer-Encoding' ] =
'BASE64' , },
body =
ltn12.source.chain (
ltn12.source.string (
files [
_ ].
filedata ),
ltn12.filter.chain (
mime.encode (
'base64' ),
mime.wrap () ) )
}
end
settings.source =
smtp.message (
email )
settings.from =
from
settings.rcpt =
to
return smtp.send (
settings )
end
-----------
FRANCE SMARTHOME & SMARTBUILDING INTEGRATION
SE ECO EXPERT