--[[
  init_device.lua
  This lua script is called on the asio thread in the Init() function of the DscKnxAppPrgNG
  after init_knxstack.lua.
  It initializes the DscKnxAppPrgNG and the object model according to the current download.
]]--

package.path = package.path .. ';/opt/gira/etc/dscknxappprgng/?.lua'

-- globals
deviceUrn_base = "ISSDAIKX03"
DeviceDefinitionFile = "/tmp/dynamicdevicedefinition/" .. deviceUrn_base .. ".xml"
DeviceDefinition:SetDescription("dynamic RAP definition")
device = DeviceDefinition:AddDevice(deviceUrn_base)
helper = require("helper")

Logger:Info("-- init_device.lua started --")
Logger:Info("-- force sync of existing recording data")
os.execute("killall -q -USR1 recordingapp")
-- give the recordingapp some time to write to the db before the code below modifies the db
os.execute("sleep 1") 

-- start, check PDB version
local downloadVersionMajor,downloadVersionMinor = helper.GetAppPrg1Version()
Logger:Info(string.format("Found application program 1 version %d.%d", downloadVersionMajor, downloadVersionMinor))

-- Handle the correct PDB version
local downloadHandler

if downloadVersionMajor >= 7 then
  Logger:Info("Load 7.0 handler for current application program 1 version.")
  downloadHandler = require("download_handler_7")
elseif downloadVersionMajor >= 5 then
  -- PDB version 5 and 6 are equal during download handling
  Logger:Info("Found supported old application program 1 version - load conversion handler.")
  downloadHandler = require("download_handler_5")
else
  error("Incompatible application program 1 version for this firmware found!")
end

--downloadHandler.Init()

Logger:Info("-- init_device.lua done --")
