Logger:Info("-- init_knxstack_7.lua start --")

-- defines
local ProductInterfaceObjectId = 60001
local NotificationInterfaceObjectId = 50001
local TimeServerInterfaceObjectId = 50002
local DataLoggerInterfaceObjectId = 50003
local RecordingInterfaceObjectId = 50004

-- register callbacks for dynamic creation during download

-- notifications are registered on demand
function CreateNotificationProperties(numberOfNotifications)
    Logger:Info(string.format("Create %d NotificationInterfaceObjects", numberOfNotifications))
    for i = 1, numberOfNotifications do
        local interfaceObject = PropertyService:GetInterfaceObjectByTypeAndInstance(NotificationInterfaceObjectId, i)
        if not interfaceObject then
            Logger:Debug(string.format("Create object instance %d", i))
            interfaceObject = PropertyService:CreateInterfaceObject(NotificationInterfaceObjectId)
        end
        interfaceObject:RegisterArrayProperty(51, 0x02, 128):SetPersistence(true)             -- Filter
        interfaceObject:RegisterSimpleProperty(52, 0x02):SetPersistence(true)                 -- OnlyOnChange
        interfaceObject:RegisterSimpleProperty(53, 0x09):SetPersistence(true)                 -- ThresholdValue
        interfaceObject:RegisterSimpleProperty(54, 0x02):SetPersistence(true)                 -- Priority
        interfaceObject:RegisterArrayProperty(55, 0x02, 64):SetPersistence(true)              -- Category
        interfaceObject:RegisterArrayProperty(56, 0x02, 128):SetPersistence(true)             -- Subject
        interfaceObject:RegisterArrayProperty(57, 0x02, 128):SetPersistence(true)             -- Text
        interfaceObject:RegisterSimpleProperty(58, 0x02):SetPersistence(true)                 -- AddAttachment
        interfaceObject:RegisterArrayProperty(59, 0x02, 128):SetPersistence(true)             -- AttachmentURL
        interfaceObject:RegisterSimpleProperty(60, 0x02):SetPersistence(true)                 -- DatapointType
        interfaceObject:RegisterArrayProperty(61, 0x02, 64):SetPersistence(true)              -- NotificationName
    end
end

function OnWriteNumOfNotifications(property, value)
  local numberOfNotifications = value:GetAsUInt8()
  Logger:Info(" init_knxstack_7.lua: OnWriteNumOfNotifications: " .. numberOfNotifications)
  if numberOfNotifications > 0 then
    CreateNotificationProperties(numberOfNotifications)
  end
end

-- recordings are registered on demand
function CreateRecordingProperties(numberOfRecordings)
    Logger:Info(string.format("Create %d RecordingInterfaceObjects", numberOfRecordings))
    for i = 1, numberOfRecordings do
        local interfaceObject = PropertyService:GetInterfaceObjectByTypeAndInstance(RecordingInterfaceObjectId, i)
        if not interfaceObject then
            Logger:Debug(string.format("Create object instance %d", i))
            interfaceObject = PropertyService:CreateInterfaceObject(RecordingInterfaceObjectId)
        end
        interfaceObject:RegisterArrayProperty(51, 0x02, 64):SetPersistence(true)  -- name, array of PDT_UNSIGNED_CHAR (8 Bit)
        interfaceObject:RegisterSimpleProperty(52, 0x02):SetPersistence(true)	    -- DPT main type, PDT_UNSIGNED_CHAR (8 Bit)
        interfaceObject:RegisterSimpleProperty(53, 0x04):SetPersistence(true)	    -- DPT sub type, PDT_UNSIGNED_INT (16 Bit)
        interfaceObject:RegisterSimpleProperty(54, 0x02):SetPersistence(true)	    -- aggregation function PDT_UNSIGNED_CHAR (8 Bit)
    end
end

function OnWriteNumOfRecordings(property, value)
  local numberOfRecordings =  value:GetAsUInt8()
  Logger:Info(" init_knxstack_7.lua: OnWriteNumOfRecordings: " .. numberOfRecordings)
  if numberOfRecordings > 0 then
    CreateRecordingProperties(numberOfRecordings)
  end
end

-- register device interface object properties (including sdamanager app properties)
Logger:Info("Create product InterfaceObject (" .. ProductInterfaceObjectId .. ")")
local ProductInterfaceObject = PropertyService:GetOrCreateInterfaceObject(ProductInterfaceObjectId)
ProductInterfaceObject:RegisterSimpleProperty(51, 0x02):SetPersistence(true) -- General portal access after restart
ProductInterfaceObject:RegisterSimpleProperty(52, 0x02):SetPersistence(true) -- Remote access for group "Residents" after restart
ProductInterfaceObject:RegisterSimpleProperty(53, 0x02):SetPersistence(true) -- Remote access for group "Installers" after restart
ProductInterfaceObject:RegisterSimpleProperty(54, 0x02, OnWriteNumOfNotifications):SetPersistence(true) -- Number of Notification objects
ProductInterfaceObject:RegisterSimpleProperty(55, 0x02):SetPersistence(true) -- Separator for floating point numbers
ProductInterfaceObject:RegisterSimpleProperty(56, 0x02, OnWriteNumOfRecordings):SetPersistence(true) -- Number of recordings
ProductInterfaceObject:RegisterArrayProperty(57, 0x04, 1)                    -- PDB time zone id, PDT_UNSIGNED_SHORT (16 Bit)

Logger:Info("Create TimeServer InterfaceObject")
local timeServerInterfaceObject = PropertyService:GetOrCreateInterfaceObject(TimeServerInterfaceObjectId)
timeServerInterfaceObject:RegisterSimpleProperty(51, 0x02):SetPersistence(true) -- timer server active, PDT_UNSIGNED_CHAR (8 Bit)
timeServerInterfaceObject:RegisterSimpleProperty(52, 0x02):SetPersistence(true) -- send time interval, PDT_UNSIGNED_CHAR (8 Bit)
timeServerInterfaceObject:RegisterSimpleProperty(53, 0x02):SetPersistence(true) -- send date interval, PDT_UNSIGNED_CHAR (8 Bit)

Logger:Info("Create Datalogger InterfaceObject")
local dataLoggerInterfaceObject = PropertyService:GetOrCreateInterfaceObject(DataLoggerInterfaceObjectId)
dataLoggerInterfaceObject:RegisterSimpleProperty(51, 0x02):SetPersistence(true) -- data logger format, PDT_UNSIGNED_CHAR (8 Bit)
dataLoggerInterfaceObject:RegisterSimpleProperty(52, 0x02):SetPersistence(true) -- logger type, PDT_UNSIGNED_CHAR (8 Bit)
dataLoggerInterfaceObject:RegisterSimpleProperty(53, 0x02):SetPersistence(true) -- data logger active, PDT_UNSIGNED_CHAR (8 Bit)


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