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.

Logger All Datagrams KNX BUS to mysql
#5
This is my code, maybe not perfect but it works

Code:
--[[
* table:
[datalen]
  * number: 4
[datahex]
  * string: 453FE000
[data]
  * number: 3070
[srcraw]
  * number: 8448
[dstraw]
  * number: 17930
[type]
  * string: groupwrite
[src]
  * string: 2.1.0
[dst]
  * string: 8/6/10
--]]

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
      --log(cursor)
      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
    --log(event)
    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
    --log(event)
    writemysql(event.datalen,event.datahex,'',event.srcraw,event.dstraw,event.type,event.src,event.dst,name)
  end

  -- knx connection
  client = eibdgm:new({ timeout = 1 })
  client:sethandler('groupwrite', writehandler)
  client:sethandler('groupresponse', writehandler)
  client:sethandler('groupread', readhandler)
end

-- handle knx
client:step()
Reply


Messages In This Thread
RE: Logger All Datagrams KNX BUS to mysql - by gjniewenhuijse - 01.05.2020, 06:35

Forum Jump: