Posts: 88
Threads: 15
Joined: Sep 2019
Reputation:
1
Hello I have a project in which I need to save in a bbdd mysql all the states of all KNX groups in real time.
In other words, I need to listen to the KNX BUS and send all the datagrams that are written to the BUS to a mysql DB.
Can LM do this?
Can somebody help me?.
Thank you.
Posts: 8208
Threads: 43
Joined: Jun 2015
Reputation:
473
See this thread on a basic example of bus monitoring. It can be easily extended to send data to mysql or any other database:
https://forum.logicmachine.net/showthread.php?tid=273
Posts: 88
Threads: 15
Joined: Sep 2019
Reputation:
1
Maybe an idea would be, to mark all the objects with "LOG" and every hour, to synchronize the objects of the LM bbdd with the DB of mysql?
Posts: 8208
Threads: 43
Joined: Jun 2015
Reputation:
473
Posts: 461
Threads: 97
Joined: Jun 2015
Reputation:
6
This is my code, maybe not perfect but it works
Code: 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
if not client then
require( 'genohm-scada.eibdgm')
objects = grp.all()
datatypes = {}
for _, object in ipairs( objects) do
datatypes[ object.id ] = { object.name, object.datatype}
end
function writemysql( I_datalen, I_datahex, I_data, I_srcraw, I_dstraw, I_type, I_src, I_dst, I_name)
require( 'luasql.mysql')
env = luasql.mysql()
dbcon = env: connect( 'xxxxxxxxx', 'xxxxxxx', 'xxxxxxxx', '192.168.12.25', '3306')
if dbcon then
if I_type == 'groupread' then
cursor = dbcon: execute( 'insert into datalog (datetime,datalen,srcraw,dstraw,type,src,dst,dstname) values (now(),'.. I_datalen..','.. I_srcraw..','.. I_dstraw..',"'.. I_type..'","'.. I_src..'","'.. I_dst..'","'.. I_name..'") ' )
else
cursor = dbcon: execute( 'insert into datalog (datetime,datalen,datahex,data,srcraw,dstraw,type,src,dst,dstname) values (now(),'.. I_datalen..',"'.. I_datahex..'",'.. tostring( I_data).. ','.. I_srcraw..','.. I_dstraw..',"'.. I_type..'","'.. I_src..'","'.. I_dst..'","'.. I_name..'") ' )
end
dbcon: close()
env: close()
end
end
function writehandler( event)
if datatypes[ event.dstraw ] then
name = datatypes[ event.dstraw ][ 1]
dpt = datatypes[ event.dstraw ][ 2]
else
name = ''
end
if dpt then
event.data = knxdatatype.decode( event.datahex, dpt)
else
event.data = nil
end
writemysql( event.datalen, event.datahex, event.data, event.srcraw, event.dstraw, event.type, event.src, event.dst, name)
end
function readhandler( event)
if datatypes[ event.dstraw ] then
name = datatypes[ event.dstraw ][ 1]
else
name = ''
end
writemysql( event.datalen, event.datahex, '', event.srcraw, event.dstraw, event.type, event.src, event.dst, name)
end
client = eibdgm: new({ timeout = 1 })
client: sethandler( 'groupwrite', writehandler)
client: sethandler( 'groupresponse', writehandler)
client: sethandler( 'groupread', readhandler)
end
client: step()
|