require("luamods/command")
require("luamods/layout")
require("luamods/cgi")
require("config/rsm_conf")

header = layout.getHeader()
footer = layout.getFooter()

userLevel = db.getPriv(layout.username) or 1

if (userLevel == 1) then
    buttonControl = " disabled"
    inputControl = " readonly"
else
    buttonControl = ""
    inputControl = ""
end

-- Little helper to prevent restarts, ups, downs, etc. if MDD restarts
-- are locked.
local file = require('pl.file')
local function if_unlocked(action)
    local _, err = file.read(rsm_conf.mdd_restart_lock)
    if err then -- file's not there, we will allow the action.
        action()
    end
end

templatesFiles = {}
snmpFiles = {}
modbusFiles = {}
dlmsFiles = {}
tsdFiles = {}
flddFiles = {}
ckdFiles = {}
tk1dFiles = {}
naradadFiles = {}
camera_device_files = {}
camera_template_files = {}
camera_profile_file = {}

bareFiles={}

serialConfig = command.ex([[cat ]]..rsm_conf.serialconfig_path) or ""
snmpConfig = command.ex([[cat ]]..rsm_conf.sdd_xml_path) or ""
modbusConfig = command.ex([[cat ]]..rsm_conf.mdd_xml_path) or ""
dlmsConfig = command.ex([[cat ]]..rsm_conf.ddd_xml_path) or ""
tsdConfig = command.ex([[cat ]]..rsm_conf.tsd_xml_path) or ""
flddConfig = command.ex([[cat -E ]]..rsm_conf.fldd_conf_path) or ""
ckdConfig = command.ex([[cat ]]..rsm_conf.ckdd_conf_path) or ""
tk1dConfig = command.ex([[cat ]]..rsm_conf.tk1d_conf_path) or ""
naradadConfig = command.ex([[cat ]]..rsm_conf.naradad_conf_path) or ""
cameraConfig = command.ex([[cat ]]..rsm_conf.camera_xml_path) or ""

--Load all files for each device type
if (snmpConfig ~= "") then
    snmpPath = snmpConfig:match([[<path%s+name=%"devices%"%s->(.-)</path>]]) or ""
    templatesPath = snmpConfig:match([[<path%s+name=%"templates%"%s->(.-)</path>]]) or ""
    if (snmpPath ~= "") then
        snmpFiles = {}
        snmpFiles = command.getFilesInTable(snmpPath)
    end
    if (templatesPath ~= "") then
        templatesFiles = command.getFilesInTable(templatesPath)
    end
end

if (modbusConfig ~= "") then
    modbusPath = modbusConfig:match([[<path%s+name=%"devices%"%s->(.-)</path>]]) or ""
    modbus_templatesPath = modbusConfig:match([[<path%s+name=%"templates%"%s->(.-)</path>]]) or ""
    if (modbusPath ~= "") then
        modbusFiles = command.getFilesInTable(modbusPath)
    end
    if (modbus_templatesPath ~= "") then
        modbus_templateFiles = command.getFilesInTable(modbus_templatesPath)
    end
end

if (dlmsConfig ~= "") then
    dlmsPath = dlmsConfig:match([[<path%s+name=%"devices%"%s->(.-)</path>]]) or ""
    dlms_templatesPath = dlmsConfig:match([[<path%s+name=%"templates%"%s->(.-)</path>]]) or ""
    if (dlmsPath ~= "") then
        dlmsFiles = command.getFilesInTable(dlmsPath)
    end
    if (dlms_templatesPath ~= "") then
        dlms_templateFiles = command.getFilesInTable(dlms_templatesPath)
    end
end

if (tsdConfig ~= "") then
    tsdPath = tsdConfig:match([[<path%s+name=%"devices%"%s->(.-)</path>]]) or ""
    if (tsdPath ~= "") then
        tsdFiles = command.getFilesInTable(tsdPath)
    end
end

if (flddConfig ~= "") then
    flddPath = flddConfig:match([[fldd.device.dir=(.-)%$]]) or ""
    if (flddPath ~= "") then
        flddFiles = command.getFilesInTable(flddPath)
    end
end

if (ckdConfig ~= "") then
    ckdPath = ckdConfig:match([[ckdd.device.dir=(.-)%s]]) or ""

    if (ckdPath ~= "") then
        ckdFiles = command.getFilesInTable(ckdPath)
    end
end

if (tk1dConfig ~= "") then
    tk1dPath = tk1dConfig:match([[tk1d.device.dir=(.-)%s]]) or ""
    if (tk1dPath ~= "") then
        tk1dFiles =command.getFilesInTable(tk1dPath)
    end
end

if (naradadConfig ~= "") then
    naradadPath = naradadConfig:match([[naradad.device.dir=(.-)%s]]) or ""

    if (naradadPath ~= "") then
        naradadFiles =command.getFilesInTable(naradadPath)
    end
end

--Load all device and template files for CDD
if (cameraConfig ~= "") then

    camera_device_path = cameraConfig:match([[<path%s+name=%"devices%"%s->(.-)</path>]]) or ""
    camera_template_path = cameraConfig:match([[<path%s+name=%"templates%"%s->(.-)</path>]]) or ""
    camera_profile_path = cameraConfig:match([[<path%s+name=%"profiles%"%s->(.-)</path>]]) or ""

    if (camera_device_path ~= "") then
        camera_device_files = command.getFilesInTable(camera_device_path)
    end
    if (camera_template_path ~= "") then
        camera_template_files = command.getFilesInTable(camera_template_path)
    end
    if (camera_profile_path ~= "") then
        camera_profile_file = command.ex([[cat ]]..camera_profile_path) or ""
    end

end


bareFiles = command.getFilesInTable(rsm_conf.bdd_device_path)

--End load all files for each device type


-- SNMP 
if (templatesFiles ~= nil) then
    templates = {}
    for i,f in pairs(templatesFiles) do
        templateXML = command.ex("cat "..templatesPath..[[/]]..f)
        template = {}
        template.make = templateXML:match("<make>%s-(.-)%s-</make>") or "?"
        template.model = templateXML:match("<model>%s-(.-)%s-</model>") or "?"
        template.mType = templateXML:match("<type>%s-(.-)%s-</type>") or "Unknown"
        template.version = templateXML:match("<version>%s-(.-)%s-</version>") or ""
        if (template.version ~= "") then template.version = "(" .. template.version .. ")" end
        templates[f] = template
    end
end

content = ""

-- DLMS Template Files
if (dlms_templateFiles ~= nil) then
    dlms_templates = {}
    for i,f in pairs(dlms_templateFiles) do
        templateXML = command.ex("cat "..dlms_templatesPath..[[/]]..f)
        template = {}
        template.make = templateXML:match("<make>%s-(.-)%s-</make>") or "?"
        template.model = templateXML:match("<model>%s-(.-)%s-</model>") or "?"
        template.mType = templateXML:match("<type>%s-(.-)%s-</type>") or "Unknown"
        template.version = templateXML:match("<version>%s-(.-)%s-</version>") or ""
        if (template.version ~= "") then template.version = "(" .. template.version .. ")" end
        dlms_templates[f] = template
    end
end

-- Modbus Template Files
if (modbus_templateFiles ~= nil) then
    modbus_templates = {}
    for i,f in pairs(modbus_templateFiles) do
        templateXML = command.ex("cat "..modbus_templatesPath..[[/]]..f)
        template = {}
        template.make = templateXML:match("<make>%s-(.-)%s-</make>") or "?"
        template.mType = templateXML:match("<type>%s-(.-)%s-</type>") or "Unknown"
        template.version = templateXML:match("<version>%s-(.-)%s-</version>") or ""
        if (template.version ~= "") then template.version = "(" .. template.version .. ")" end
        modbus_templates[f] = template
    end
end

-- cameraTemplates will be {} if no template xml files are found.
-- cameraTemplates = command.getFilesInTable(rsm_conf.cdd_templates_path)

-- load CDD Template Files
if (camera_template_files ~= nil) then
    cameraTemplates = {}
    for i,f in pairs(camera_template_files) do
        templateXML = command.ex("cat "..camera_template_path..[[/]]..f)
        template = {}
        template.make = templateXML:match("<make>%s-(.-)%s-</make>") or "?"
        template.model = templateXML:match("<model>%s-(.-)%s-</model>") or "?"
        template.mType = templateXML:match("<type>%s-(.-)%s-</type>") or "Unknown"
        template.version = templateXML:match("<version>%s-(.-)%s-</version>") or ""
        if (template.version ~= "") then template.version = "(" .. template.version .. ")" end
        cameraTemplates[f] = template
    end
end

voip_config = false ;
bareDevice = {}
if (bareFiles ~= nil) then
     for i,t in pairs(bareFiles) do
        voip_config = true ;
         bdd_device = command.ex("cat "..rsm_conf.bdd_device_path..[[/]]..t)
         temp = {}
         temp.sip_contacts = {}
         temp.label = bdd_device:match("<label>%s-(.-)%s-</label>") or "?"
         temp.serial = bdd_device:match("<serial>%s-(.-)%s-</serial>") or "?"
         temp.file_path = rsm_conf.bdd_device_path..[[/]]..t 
         local contacts_array = bdd_device:match("<contacts>%s-(.-)%s-</contacts>") or "?"

         for contact_match in contacts_array:gmatch("<contact>(.-)</contact>") do
            local sip_contact = {}
            sip_contact.address  = contact_match:match([[<address>(.-)</address>]])
            sip_contact.label  = contact_match:match([[<label>(.-)</label>]])
            table.insert(temp.sip_contacts, sip_contact)
          end
         bareDevice[t] = temp
     end
 end

 -- Modbus TCP devices
local modbus_tcp_device_files = {}
if (modbusFiles) then
    -- We need to filter by tcpport entries in the files.
    for i,f in pairs(modbusFiles) do
        temp = command.ex([[cat ]]..modbusPath..[[/]]..f) or ""
        local tcpport = temp:match('<tcpport>%s-(.-)%s-</tcpport>')
        if tcpport then
            table.insert(modbus_tcp_device_files, [[Filename: ]]..f..[[\n]]..temp)
        end
    end
end

local modbus_tcp_devices = {}
for i,cfg in pairs(modbus_tcp_device_files) do
    d = {}
    d.filename = cfg:match([[Filename:%s+(.-)%s-\n]]) or "unknown"
    d.label = cfg:match([[<label>%s-(.-)%s-</label>]]) or "unknown"
    d.serial = cfg:match([[<serial>%s-(.-)%s-</serial>]]) or "unknown"
    d.template = cfg:match([[<template>%s-(.-)%s-</template>]]) or "unknown"
    d.host = cfg:match([[<host>%s-(.-)%s-</host>]]) or "unknown"
    d.port = cfg:match([[<port>%s-(.-)%s-</port>]]) or "unknown"
    d.slave = cfg:match([[<slave>%s-(.-)%s-</slave>]]) or "unknown"
    table.insert(modbus_tcp_devices, d)
end

deviceConfs = {}
if (snmpFiles) then
    for i,f in pairs(snmpFiles) do
        temp = command.ex([[cat ]]..snmpPath..[[/]]..f) or ""
        table.insert(deviceConfs, [[Filename: ]]..f..[[\n]]..temp)
    end
end

-- SNMP devices
devices = {}
for i,cfg in pairs(deviceConfs) do
    d = {}
    d.filename = cfg:match([[Filename:%s+(.-)%s-\n]]) or "unknown"
    d.label = cfg:match([[<label>%s-(.-)%s-</label>]]) or "unknown"
    d.serial = cfg:match([[<serial>%s-(.-)%s-</serial>]]) or "unknown"
    d.template = cfg:match([[<template>%s-(.-)%s-</template>]]) or "unknown"
    d.host = cfg:match([[<host>%s-(.-)%s-</host>]]) or "unknown"
    table.insert(devices,d)
end

-- DLMS TCP/IP devices
dlms_deviceConfs = {}
if (dlmsFiles) then
    for i,f in pairs(dlmsFiles) do
        temp = command.ex([[cat ]]..dlmsPath..[[/]]..f) or ""
        table.insert(dlms_deviceConfs, [[Filename: ]]..f..[[\n]]..temp)
    end
end

-- DLMS TCP/IP devices
dlms_devices = {}
for i,cfg in pairs(dlms_deviceConfs) do
    d = {}
    d.filename = cfg:match([[Filename:%s+(.-)%s-\n]]) or "unknown"
    d.label = cfg:match([[<label>%s-(.-)%s-</label>]]) or "unknown"
    d.serial = cfg:match([[<serial>%s-(.-)%s-</serial>]]) or "unknown"
    d.template = cfg:match([[<template>%s-(.-)%s-</template>]]) or "unknown"
    d.ip = cfg:match([[<ip>%s-(.-)%s-</ip>]]) or ""
    if (d.ip ~= "") then
        table.insert(dlms_devices,d)
    end
end

-- Camera devices
camera_deviceConfs = {}
if (camera_device_files) then
    for i,f in pairs(camera_device_files) do
        temp = command.ex([[cat ]]..camera_device_path..[[/]]..f) or ""
        table.insert(camera_deviceConfs, [[Filename: ]]..f..[[\n]]..temp)
    end
end

-- cameras
cameras = {}
for i,cfg in pairs(camera_deviceConfs) do
    c = {}
    c.filename = cfg:match([[Filename:%s+(.-)%s-\n]]) or ""
    c.cameraLabel = cfg:match([[<label>%s-(.-)%s-</label>]]) or ""
    c.cameraSerial = cfg:match([[<serial>%s-(.-)%s-</serial>]]) or ""
    c.cameraIP = cfg:match([[<ip>%s-(.-)%s-</ip>]]) or ""
    c.temp = cfg:match([[<template>%s-(.-)%s-</template>]]) or "unknown"
    c.camera_username = cfg:match([[<user_name>%s-(.-)%s-</user_name>]]) or ""
    c.camera_password = cfg:match([[<user_password>%s-(.-)%s-</user_password>]]) or ""
    table.insert(cameras,c)
end

if (cgi.post.saveButton == "Save and Apply") and (userLevel ~= 1) and (serialConfig ~= "") then
    newXML = [[
<!-- ]]..rsm_conf.serialconfig_path..[[ generated by webconfig -->
<config>
    <serialconfig>
]]
    cnt = 0
    for cnt = 1,10 do
        if (cgi.post["device"..cnt] ~= nil) then
            device = cgi.post["device"..cnt]
            owner = cgi.post["owner"..cnt] or "rdd"
            baud = cgi.post["baud"..cnt] or "115200"
            data = cgi.post["data"..cnt] or "8"
            stop = cgi.post["stop"..cnt] or "1"
            mode = cgi.post["mode"..cnt] or "rs485"
            nonblock = cgi.post["nonblock"..cnt] or "true"
            newXML = newXML .. [[
        <port device="]]..device..[[">
            <owner>]]..owner..[[</owner>
            <baud>]]..baud..[[</baud>
            <data>]]..data..[[</data>
            <stop>]]..stop..[[</stop>
            <nonblock>]]..nonblock..[[</nonblock>
            <mode>]]..mode..[[</mode>
        </port>
]]
        end--if
    end--for
newXML = newXML .. [[
    </serialconfig>
</config>
<!-- end of file -->
]]
    command.ex(rsm_conf.rdd_down)
    command.ex(rsm_conf.sdd_down)
    if_unlocked(function ()
        command.ex(rsm_conf.mdd_down)
    end)
    command.ex(rsm_conf.ddd_down)
    command.ex(rsm_conf.tsd_down)
    command.ex(rsm_conf.fldd_down)
    command.ex(rsm_conf.ckdd_down)
    command.ex(rsm_conf.tk1d_down)
    command.ex(rsm_conf.naradad_down)
    

    command.writefile(rsm_conf.serialconfig_path, newXML)

    
    command.ex(rsm_conf.rdd_up)
    command.ex(rsm_conf.sdd_up)
    if_unlocked(function ()
        command.ex(rsm_conf.mdd_up)
    end)
    command.ex(rsm_conf.ddd_up)
    command.ex(rsm_conf.tsd_down)
    command.ex(rsm_conf.fldd_up)
    command.ex(rsm_conf.ckdd_up)
    command.ex(rsm_conf.tk1d_up)
    command.ex(rsm_conf.naradad_up)
    

    serialConfig = command.ex([[cat ]]..rsm_conf.serialconfig_path)
    
    saved = 1
    
end--if


buses={}
if (serialConfig ~= "") then
    
    for device, busConfig in serialConfig:gmatch([[<port%s+device=%"(.-)%"%s->(.-)</port>]]) do
        
        bus = {}
        bus.device = device or "unknown"
        bus.type = busConfig:match("<owner>%s-(.-)%s-</owner>") or "" -- type is "owner" in xml
        bus.baud = busConfig:match("<baud>%s-(%d+)%s-</baud>") or "115200"
        bus.data = busConfig:match("<data>%s-(%d+)%s-</data>") or "8"
        bus.stop = busConfig:match("<stop>%s-(%d+)%s-</stop>") or "1"
        bus.mode = busConfig:match("<mode>%s-(.-)%s-</mode>") or "rs485"
        bus.nonblock = busConfig:match("<nonblock>%s-(.-)%s-</nonblock>") or "true"
        bus.deviceCount = 0
        
        for i,f in pairs(modbusFiles) do
            temp = command.ex("cat "..modbusPath.."/"..f)
            if temp:match("<port>%s-(.-)%s-</port>") == device then
                bus.deviceCount = bus.deviceCount + 1
            end--if
        end--for
        for i,f in pairs(dlmsFiles) do
            temp = command.ex("cat "..dlmsPath.."/"..f)
            if temp:match("<port>%s-(.-)%s-</port>") == device then
                bus.deviceCount = bus.deviceCount + 1
            end--if
        end--for
        for i,f in pairs(tsdFiles) do
            temp = command.ex("cat "..tsdPath.."/"..f)
            if temp:match("<port>%s-(.-)%s-</port>") == device then
                bus.deviceCount = bus.deviceCount + 1
            end--if
        end--for
        for i,f in pairs(flddFiles) do
            temp = command.ex("cat "..flddPath.."/"..f)
            if temp:match("<port>%s-(.-)%s-</port>") == device then
                bus.deviceCount = bus.deviceCount + 1
            end--if
        end--for
        for i,f in pairs(ckdFiles) do
            temp = command.ex("cat "..ckdPath.."/"..f)
            if temp:match("<port>%s-(.-)%s-</port>") == device then
                bus.deviceCount = bus.deviceCount + 1
            end--if
        end--for
        for i,f in pairs(tk1dFiles) do
            temp = command.ex("cat "..tk1dPath.."/"..f)
            if temp:match("<port>%s-(.-)%s-</port>") == device then
                bus.deviceCount = bus.deviceCount + 1
            end--if
        end--for
        for i,f in pairs(naradadFiles) do
            
            temp = command.ex("cat "..naradadPath.."/"..f)
            if temp:match("<port>%s-(.-)%s-</port>") == device then
                
                bus.deviceCount = bus.deviceCount + 1
            end--if
        end--for

        table.insert(buses, bus)
    end--for
end--if

-- content = ""

content = content .. [[
                    <div id="info">
                        <div style="background:url(images/busconfig.png); background-size:32px; background-repeat:no-repeat; background-position: 20px center;">
                            <center><h1>Bus Configuration</h1></center>
                        </div>
                    </div>
                    <script>
                        function toggleVis(target)
                        {
                            if (target.style.display == "none")
                            {
                                target.style.display = "inline-block";
                            }
                            else
                            {
                                target.style.display = "none";
                            }
                            return false;
                        }
                    </script>
]]

if (userLevel == 1) then
    content = content .. [[
                    <center><b>Note:</b> Editing this section requires administrator privileges.</center><br>
]]
end

--This is a warning message if, checks whether some of the files are not installed.
if (serialConfig == "") or (snmpConfig == "") or (modbusConfig == "") or (dlmsConfig == "") or (flddConfig == "") or (tsdConfig == "") or (cameraConfig == "") or (ckdConfig == "") or (tk1dConfig=="" or (naradadConfig==""))then
    -- Don't bail, just warn
    content = content .. [[
                    <div id="info">
                        <center>
                            <h2><img src="images/warning.png"/> Config file(s) missing</h2>
                            Cannot read the following file(s) required for bus configuration:<br><br>
                            <span style="display: inline-block; text-align: left; width: 400px;">
                                <ul>
    ]]
    if (serialConfig == "") then
        serialBroken = 1
        content = content .. [[
                                        <li>Serial Config - ]]..rsm_conf.serialconfig_path..[[</li>
    ]]
    end
    if (modbusConfig == "") then
        modbusBroken = 1
        content = content .. [[
                                        <li>ModBus Config - ]]..rsm_conf.mdd_xml_path..[[</li>
    ]]
    end

    if (dlmsConfig == "") then
        dlmsBroken = 1
        content = content .. [[
                                        <li>DLMS Config - ]]..rsm_conf.ddd_xml_path..[[</li>
    ]]
    end

    if (tsdConfig == "") then
        tsdBroken = 1
        content = content .. [[
                                        <li>TSD Config - ]]..rsm_conf.tsd_xml_path..[[</li>
    ]]

    end

    if (flddConfig == "") then
        flddBroken = 1
        content = content .. [[
                                        <li>FLDD Config - ]]..rsm_conf.fldd_conf_path..[[</li>
    ]]
    end

    if (snmpConfig == "") then
        snmpBroken = 1
        content = content .. [[
                                        <li>SNMP Config - ]]..rsm_conf.sdd_xml_path..[[</li>
    ]]
    end


   if (cameraConfig == "") then
        cameraBroken = 1
        content = content .. [[
                                        <li>Camera Config - ]]..rsm_conf.camera_xml_path..[[</li>
    ]]
    end

   if (ckdConfig == "") then
        ckdBroken = 1
        content = content .. [[
                                        <li>CKD Config -]]..rsm_conf.ckdd_conf_path..[[</li>

    ]]
    end

    if (tk1dConfig == "") then
        tk1dBroken = 1
        content = content .. [[
                                        <li>tk1d Config -]]..rsm_conf.tk1d_conf_path..[[</li>

    ]]
   end

    if (naradadConfig == "") then
        naradadBroken = 1
        content = content .. [[
                                        <li>naradad Config -]]..rsm_conf.naradad_conf_path..[[</li>

    ]]
    end

    if (voip_config == false) then
        content = content .. [[
                                        <li>RSM VOIP not configured - ]] .. rsm_conf.bdd_device_path .. [[ directory is empty</li>
    ]]
    end

    content = content .. [[
                                    </ul>
                                </span>
                            </center>
                        </div>
    ]]

end--if (serialConfig == "") or (snmpConfig == "") or (modbusConfig == "") or (dlmsConfig == "") or (cameraConfig == "") or (ckdConfig == "")
--End of warning msgs

content = content .. [[

                    <div class="formLayoutfull">

]]
if (serialBroken == 1) then
    content = content .. [[
                        <h3><img src="images/warning.png" alt="Serial config missing" title="Serial config missing"> Serial buses:</h3>
                        Cannot determine serial bus configuration.
]]
else
    content = content .. [[
                        <h3>Serial buses:</h3>
                        <form action="rsm_bus_config.lua" method="post">
                            <table class="onetwo" style="text-align: center;">
                                <tr>
                                    <th width="25%">Device Bus</th>
                                    <th width="15%">Type</th>
                                    <th width="10%">Baud</th>
                                    <th width="10%">Data</th>
                                    <th width="10%">Stop</th>
                                    <th width="15%">Non-block</th>
                                    <th width="15%">Mode</th>
                                </tr>
]]


    for bus = 1, #(buses) do
        content = content .. [[
                                <tr>
]]
        if (buses[bus].type == "mdd") and (modbusBroken ~= 1) then
            content = content .. [[
                                    <td style="text-align: left;"> <a title="Manage devices on this bus" href="rsm_modbus_config.lua?bus=]]..buses[bus].device..[[">]]..buses[bus].device..[[ []]..buses[bus].deviceCount..[[] <img src="images/spanner.png"/></a></td>
]]
        elseif (buses[bus].type == "ddd") and (dlmsBroken ~= 1) then
        content = content .. [[
                                    <td style="text-align: left;"> <a title="Manage devices on this bus" href="rsm_dlms_config.lua?bus=]]..buses[bus].device..[[">]]..buses[bus].device..[[ []]..buses[bus].deviceCount..[[] <img src="images/spanner.png"/></a></td>
]]
        elseif (buses[bus].type == "tsd") and (tsdBroken ~= 1) then
        content = content .. [[
                                    <td style="text-align: left;"> <a title="Manage devices on this bus" href="rsm_tsd_config.lua?bus=]]..buses[bus].device..[[">]]..buses[bus].device..[[ []]..buses[bus].deviceCount..[[] <img src="images/spanner.png"/></a></td>
]]
        elseif (buses[bus].type == "fldd") and (flddBroken ~= 1) then
          content = content .. [[
                                    <td style="text-align: left;"> <a title="Manage devices on this bus" href="rsm_fldd_edit.lua?bus=]]..buses[bus].device..[[">]]..buses[bus].device..[[ []]..buses[bus].deviceCount..[[] <img src="images/spanner.png"/></a></td>
]]
        elseif (buses[bus].type == "ckdd") and (ckdBroken ~= 1) then
          content = content .. [[
                                    <td style="text-align: left;"> <a title="Manage devices on this bus" href="rsm_ckd_config.lua?bus=]]..buses[bus].device..[[">]]..buses[bus].device..[[ []]..buses[bus].deviceCount..[[] <img src="images/spanner.png"/></a></td>
]]
        elseif (buses[bus].type == "Tk100") and (tk1dBroken ~= 1) then
            content = content .. [[
                <td style="text-align: left;"> <a title="Manage devices on this bus" href="rsm_tk1d_config.lua?bus=]]..buses[bus].device..[[">]]..buses[bus].device..[[ []]..buses[bus].deviceCount..[[] <img src="images/spanner.png"/></a></td>
]]
        elseif (buses[bus].type == "naradad") and (naradadBroken ~= 1) then
            content = content .. [[
                <td style="text-align: left;"> <a title="Manage devices on this bus" href="rsm_naradad_config.lua?bus=]]..buses[bus].device..[[">]]..buses[bus].device..[[ []]..buses[bus].deviceCount..[[] <img src="images/spanner.png"/></a></td>       
]]
        else
            content = content .. [[
                                    <td style="text-align: left;"> ]]..buses[bus].device..[[</td>
]]
        end --if,else if, else
        content = content .. [[
                                    <td>
                                        <select name="owner]]..bus..[["]]..buttonControl..[[>
                                            <option value="rdd"]] if buses[bus].type == "rdd" then content = content..[[ selected]] elseif (buses[bus].deviceCount > 0) then content = content .. [[ disabled]] end content = content .. [[>RSM Bus</option>
                                            <option value="mdd"]] if buses[bus].type == "mdd" then content = content..[[ selected]] elseif (buses[bus].deviceCount > 0) then content = content .. [[ disabled]] end content = content .. [[>ModBus</option>
                                            <option value="ddd"]] if buses[bus].type == "ddd" then content = content..[[ selected]] elseif (buses[bus].deviceCount > 0) then content = content .. [[ disabled]] end content = content .. [[>DLMS</option>
                                            <option value="fldd"]] if buses[bus].type == "fldd" then content = content..[[ selected]] elseif (buses[bus].deviceCount > 0) then content = content .. [[ disabled]] end content = content .. [[>Veeder Root</option>
                                            <option value="tsd"]] if buses[bus].type == "tsd" then content = content..[[ selected]] elseif (buses[bus].deviceCount > 0) then content = content .. [[ disabled]] end content = content .. [[>Tcp via Serial</option>
                                            <option value="ckdd"]] if buses[bus].type == "ckdd" then content = content..[[ selected]] elseif (buses[bus].deviceCount > 0) then content = content .. [[ disabled]] end content = content .. [[>CK100</option>
                                            <option value="Tk100"]] if buses[bus].type=="Tk100" then content = content..[[ selected]] elseif (buses[bus].deviceCount > 0) then content = content .. [[ disabled]] end content = content .. [[>Tk100</option>
                                            <option value="naradad"]] if buses[bus].type=="naradad" then content = content..[[ selected]] elseif (buses[bus].deviceCount > 0) then content = content .. [[ disabled]] end content = content .. [[>Narada</option>
                                        </select>
                                    </td>
                                    <td>
                                        <input type="hidden" name="device]]..bus..[[" value="]]..buses[bus].device..[["/>
                                        <select name="baud]]..bus..[["]]..buttonControl..[[>
                                            <option value="9600"]] if buses[bus].baud == "9600" then content = content..[[ selected]] end content = content .. [[>9600</option>
                                            <option value="14400"]] if buses[bus].baud == "14400" then content = content..[[ selected]] end content = content .. [[>14400</option>
                                            <option value="19200"]] if buses[bus].baud == "19200" then content = content..[[ selected]] end content = content .. [[>19200</option>
                                            <option value="28800"]] if buses[bus].baud == "28800" then content = content..[[ selected]] end content = content .. [[>28800</option>
                                            <option value="38400"]] if buses[bus].baud == "38400" then content = content..[[ selected]] end content = content .. [[>38400</option>
                                            <option value="56000"]] if buses[bus].baud == "56000" then content = content..[[ selected]] end content = content .. [[>56000</option>
                                            <option value="115200"]] if buses[bus].baud == "115200" then content = content..[[ selected]] end content = content .. [[>115200</option>
                                        </select>
                                    </td>
                                    <td>
                                        <select name="data]]..bus..[["]]..buttonControl..[[>
                                            <option value="5"]] if buses[bus].data == "5" then content = content..[[ selected]] end content = content .. [[>5</option>
                                            <option value="6"]] if buses[bus].data == "6" then content = content..[[ selected]] end content = content .. [[>6</option>
                                            <option value="7"]] if buses[bus].data == "7" then content = content..[[ selected]] end content = content .. [[>7</option>
                                            <option value="8"]] if buses[bus].data == "8" then content = content..[[ selected]] end content = content .. [[>8</option>
                                            <option value="9"]] if buses[bus].data == "9" then content = content..[[ selected]] end content = content .. [[>9</option>
                                        </select>
                                    </td>
                                    <td>
                                        <select name="stop]]..bus..[["]]..buttonControl..[[>
                                            <option value="1"]] if buses[bus].stop == "1" then content = content..[[ selected]] end content = content .. [[>1</option>
                                            <option value="2"]] if buses[bus].stop == "2" then content = content..[[ selected]] end content = content .. [[>2</option>
                                        </select>
                                    </td>
                                    <td>
                                        <select name="nonblock]]..bus..[["]]..buttonControl..[[>
                                            <option value="true"]] if buses[bus].nonblock == "true" then content = content..[[ selected]] end content = content .. [[>True</option>
                                            <option value="false"]] if buses[bus].nonblock == "false" then content = content..[[ selected]] end content = content .. [[>False</option>
                                        </select>
                                    </td>
                                    <td>
                                        <select name="mode]]..bus..[["]]..buttonControl..[[>
                                            <option value="rs232"]] if buses[bus].mode == "rs232" then content = content..[[ selected]] end content = content .. [[>RS-232</option>
                                            <option value="rs485"]] if buses[bus].mode == "rs485" then content = content..[[ selected]] end content = content .. [[>RS-485</option>
                                        </select>
                                    </td>
                                </tr>
]]
    end--for

    content = content .. [[

                            </table>
]]
    if (modbusBroken == 1 ) then
        content = content .. [[
                            <br><img src="images/warning.png" alt="ModBus config missing" title="ModBus config missing"/> Cannot determine path to ModBus devices.
]]
    else
        content = content .. [[
                            <br><b>Note:</b>
                            <ul>
                                <li>Click on the buses (marked with a <img src="images/spanner.png"/> ) to manage their devices.</li>
                                <li>ModBus,DLMS and Veeder Root device count is indicated in square brackets.</li>
                                <li>A bus with configured ModBus,DLMS or Veeder Root devices cannot be changed before all its devices are deleted.</li>
                            </ul>
]]
    end


    if (dlmsBroken == 1 ) then
    content = content .. [[
                        <br><img src="images/warning.png" alt="DLMS config missing" title="DLMS config missing"/> Cannot determine path to DLMS devices.
]]
    end

    if (tsdBroken == 1 ) then
    content = content .. [[
                        <br><img src="images/warning.png" alt="TSD config missing" title="TSD config missing"/> Cannot determine path to TSD devices.
]]
    end

    if (flddBroken == 1 ) then
        content = content .. [[
                        <br><img src="images/warning.png" alt="fldd config missing" title="fldd config missing"/> Cannot determine path to Veeder Root devices.
]]
    end

    if (ckdBroken == 1 ) then
        content = content .. [[
                        <br><img src="images/warning.png" alt="ckd config missing" title="ckd config missing"/> Cannot determine path to ckd devices.
]]
    end
    if (naradadBroken == 1 ) then
        content = content .. [[
                        <br><img src="images/warning.png" alt="naradad config missing" title="naradad config missing"/> Cannot determine path to Narada devices.

]]
    end
    content = content .. [[
                            <br><hr><br>
]]
    if (saved == 1) then
        content = content .. [[
                            <b><img src="images/ok.png"/> Changes saved.</b><br><br>
]]
    end
    content = content .. [[
                            <input type="submit" value="Save and Apply" name="saveButton"]]..buttonControl..[[/>
                        </form>

]]
end--if(serialBroken == 1)

if (serialBroken ~= 1) then
    serialConfig = serialConfig:gsub("<","&lt;")
    serialConfig = serialConfig:gsub(">","&gt;")
    content = content .. [[
                    <br><img src="images/magnify.png"/> <a href="javascript:void(0)" onclick="toggleVis(document.getElementById('serialconf'));">View ]]..rsm_conf.serialconfig_path..[[</a></a><br>

                    <div id="serialconf" style="display: none;">
                        <pre class="formLayoutfull">
]]..serialConfig..[[
                        </pre>
                    </div><br>
]]
end--if

content = content .. [[
                    </div>
]]

if (modbusBroken ~= 1) then
    modbusConfig = modbusConfig:gsub("<","&lt;")
    modbusConfig = modbusConfig:gsub(">","&gt;")
    content = content .. [[
                    <br><img src="images/magnify.png"/> <a href="javascript:void(0)" onclick="toggleVis(document.getElementById('modbusconfig'));">View ]]..rsm_conf.mdd_xml_path..[[</a></a><br>

                    <div id="modbusconfig" style="display: none;">
                        <pre class="formLayoutfull">
]]..modbusConfig..[[
                        </pre>
                    </div><br>
]]
end

--
-- Modbus TCP
--

if (cgi.post.modbusUnlockButton == "Unlock and Save") and (userLevel ~= 1) and (serialConfig ~= "") then
    local file = require('pl.file')
    file.delete(rsm_conf.mdd_restart_lock)

    command.ex(rsm_conf.mdd_down)

    command.ex(rsm_conf.mdd_up)
end

local modbus_unlock_button = [[
    <form style="margin: 5px;" action="rsm_bus_config.lua" method="post">
        <input type="submit" style="background-color: orangered; color: white; padding: 3px; border-radius: 3px;" 
           name="modbusUnlockButton" value="Unlock and Save"]]..buttonControl..[[/>
    </form>
]]
if_unlocked(function()
    modbus_unlock_button = [[
    ]]
end)

content = content .. [[
    <div class="formLayoutfull">
    <h3>Modbus TCP:</h3>
]]
if (#(modbus_tcp_devices) > 0) then
    content = content .. [[
                    <table class="onetwo" style="text-align: center;">
                        <tr>
                            <th>Label:</th>
                            <th>Host:</th>
                            <th>Slave:</th>
                            <th>Template:</th>
                        </tr>
]]
    for i, dev in pairs(modbus_tcp_devices) do
        content = content .. [[
            <tr>
                <td><a href="rsm_modbus_tcp_edit.lua?file=]]..dev.filename..[["> ]]..dev.label..[[ <img src="images/spanner.png"/></a></td>
                <td>]]..dev.host..[[</td>
                <td>]]..dev.slave..[[</td>
]]
        if (modbus_templates[dev.template] ~= nil) then
            content = content .. [[
                <td>
                ]]..modbus_templates[dev.template].make .. " " .. modbus_templates[dev.template].mType .. " " .. modbus_templates[dev.template].version..[[
                </td>
            ]]
        else
            content = content  .. dev.template .. [[ <img src="images/warning.png" alt="Unknown template" title="Unknown template"/>]]
        end--if
        content = content .. [[
                                </tr>
        ]]
    end--for
    content = content .. [[
                            </table>
                            <br>
                            <b>Note:</b> Click on labels to manage TCP Modbus devices.<br>
]]
    else
        content = content .. [[ There are no configured TCP Modbus devices.<br>
]]
    end --if (#(devices) > 0, else
    content = content .. [[
                        <br>
                        <hr>
                        <br>
                        <div style="display: flex; flex-wrap: wrap; align-items: center;">
                            <form style="margin: 5px;" action="rsm_modbus_tcp_edit.lua" method="post">
                                <input type="submit" name="addButton" value="Add Modbus TCP Device"]]..buttonControl..[[/>
                            </form>
                            <form style="margin: 5px;" action="rsm_modbus_scan.lua" method="post">
                                <input type="submit" name="addButton" value="Scan Modbus Endpoint"]]..buttonControl..[[/>
                            </form>
                            ]] .. modbus_unlock_button .. [[
                        </div>
]]

content = content .. [[
    </div>
]]

--
-- end Modbus TCP
--

if (dlmsBroken ~= 1) then
    dlmsConfig = dlmsConfig:gsub("<","&lt;")
    dlmsConfig = dlmsConfig:gsub(">","&gt;")
    content = content .. [[
                    <br><img src="images/magnify.png"/> <a href="javascript:void(0)" onclick="toggleVis(document.getElementById('dlmsconfig'));">View ]]..rsm_conf.ddd_xml_path..[[</a></a><br>

                    <div id="dlmsconfig" style="display: none;">
                        <pre class="formLayoutfull">
]]..dlmsConfig..[[
                        </pre>
                    </div><br>
]]
end

if (tsdBroken ~= 1) then
    tsdConfig = tsdConfig:gsub("<","&lt;")
    tsdConfig = tsdConfig:gsub(">","&gt;")
    content = content .. [[
                    <br><img src="images/magnify.png"/> <a href="javascript:void(0)" onclick="toggleVis(document.getElementById('tsdconfig'));">View ]]..rsm_conf.tsd_xml_path..[[</a></a><br>

                    <div id="tsdconfig" style="display: none;">
                        <pre class="formLayoutfull">
]]..tsdConfig..[[
                        </pre>
                    </div><br>
]]
end

if (flddBroken ~= 1) then
    flddConfig = flddConfig:gsub("%$","")
    content = content .. [[
                    <br><img src="images/magnify.png"/> <a href="javascript:void(0)" onclick="toggleVis(document.getElementById('flddconfig'));">View ]]..rsm_conf.fldd_conf_path..[[</a></a><br>

                    <div id="flddconfig" style="display: none;">
                        <pre class="formLayoutfull">
]]..flddConfig..[[
                        </pre>

                    </div><br>
]]
end

if (ckdBroken ~= 1) then
    ckdConfig = ckdConfig:gsub("<","&lt;")
    ckdConfig = ckdConfig:gsub(">","&gt;")
    content = content .. [[
                    <br><img src="images/magnify.png"/> <a href="javascript:void(0)" onclick="toggleVis(document.getElementById('ckdconfig'));">View ]]..rsm_conf.ckdd_conf_path..[[</a></a><br>

                    <div id="ckdconfig" style="display: none;">
                        <pre class="formLayoutfull">
]]..ckdConfig..[[
                        </pre>
                    </div><br>
]]
end

if (tk1dBroken ~= 1) then
    tk1dConfig = tk1dConfig:gsub("<","&lt;")
    tk1dConfig = tk1dConfig:gsub(">","&gt;")
    content = content .. [[
                    <br><img src="images/magnify.png"/> <a href="javascript:void(0)" onclick="toggleVis(document.getElementById('tk1dconfig'));">View ]]..rsm_conf.tk1d_conf_path..[[</a></a><br>

                    <div id="tk1dconfig" style="display: none;">
                        <pre class="formLayoutfull">
]]..tk1dConfig..[[
                        </pre>
                    </div><br>
]]
end

if (naradadBroken ~= 1) then
    naradadConfig = naradadConfig:gsub("<","&lt;")
    naradadConfig = naradadConfig:gsub(">","&gt;")
    content = content .. [[
                    <br><img src="images/magnify.png"/> <a href="javascript:void(0)" onclick="toggleVis(document.getElementById('naradadconfig'));">View ]]..rsm_conf.naradad_conf_path..[[</a></a><br>

                    <div id="naradadconfig" style="display: none;">
                        <pre class="formLayoutfull">
]]..naradadConfig..[[
                        </pre>
                    </div><br>
]]
end


content = content .. [[
                    <div class="formLayoutfull">
]]
if (snmpBroken == 1) then
    content = content .. [[
                        <h3><img src="images/warning.png" alt="SNMP config missing" title="SNMP config missing"> SNMP buses:</h3>
                        Cannot determine path to SNMP devices.
]]
else
    content = content .. [[
                        <h3>SNMP bus:</h3>
]]
    if (#(devices) > 0) then
        content = content .. [[
                        <table class="onetwo" style="text-align: center;">
                            <tr>
                                <th>Label:</th>
                                <th>Device Serial:</th>
                                <th>Template:</th>
                            </tr>
]]
        for i, dev in pairs(devices) do
            content = content .. [[
                                <tr>
                                    <td><a href="rsm_snmp_edit.lua?file=]]..dev.filename..[["> ]]..dev.label..[[ <img src="images/spanner.png"/></a></td>
                                    <td>]]..dev.serial..[[</td>
                                    <td>
]]
            if (templates[dev.template] ~= nil) then
                content = content .. [[
                                            ]]..templates[dev.template].mType .. " " .. templates[dev.template].make .. " " .. templates[dev.template].model .. " " .. templates[dev.template].version..[[
]]
            else
                content = content .. dev.template .. [[ <img src="images/warning.png" alt="Unknown template" title="Unknown template"/>]]
            end--if
            content = content .. [[
                                        </td>
                                        </tr>
]]
        end--for
    content = content .. [[
                            </table>
                            <br>
                            <b>Note:</b> Click on labels to manage SNMP devices.<br>
]]
    else
        content = content ..  [[ There are no configured SNMP devices.<br>
]]
    end--if (#(devices) > 0, else
    content = content .. [[
                        <br>
                        <hr>
                        <br>
                        <form action="rsm_snmp_edit.lua" method="post">
                            <input type="submit" name="addButton" value="Add SNMP Device"]]..buttonControl..[[/>
                        </form>
]]
end--if snmpBroken, else

if (snmpBroken ~= 1) then
    snmpConfig = snmpConfig:gsub("<","&lt;")
    snmpConfig = snmpConfig:gsub(">","&gt;")
    content = content .. [[
                    <br><img src="images/magnify.png"/> <a href="javascript:void(0)" onclick="toggleVis(document.getElementById('snmpconfig'));">View ]]..rsm_conf.sdd_xml_path..[[</a></a><br>

                    <div id="snmpconfig" style="display: none;">
                        <pre class="formLayoutfull">
]]..snmpConfig..[[
                        </pre>
                    </div>
]]
end

content = content .. [[
                    </div>

                    <br>
                    <br>
                    <br>
                                        <div class="formLayoutfull">
]]

--New DLMS settings

if (dlmsBroken == 1) then
    content = content .. [[
                        <h3><img src="images/warning.png" alt="DLMS config missing" title="DLMS config missing">TCP/IP DLMS:</h3>
                        Cannot determine path to DLMS devices.
]]
else
    content = content .. [[
                        <h3>TCP/IP DLMS:</h3>
]]
    if (#(dlms_devices) > 0) then
        content = content .. [[
                        <table class="onetwo" style="text-align: center;">
                            <tr>
                                <th>Label:</th>
                                <th>Device Serial:</th>
                                <th>Device IP:</th>
                                <th>Template:</th>
                            </tr>
]]
        for i, dev in pairs(dlms_devices) do
            content = content .. [[
                                <tr>
                                    <td><a href="rsm_dlms_tcp_edit.lua?file=]]..dev.filename..[["> ]]..dev.label..[[ <img src="images/spanner.png"/></a></td>
                                    <td>]]..dev.serial..[[</td>
                                    <td>]]..dev.ip..[[</td>
                                    <td>
]]
            if (dlms_templates[dev.template] ~= nil) then
                content = content .. [[
                                            ]]..dlms_templates[dev.template].make .. " " .. dlms_templates[dev.template].model .. " " .. dlms_templates[dev.template].version..[[
]]
            else
                content = content  .. dev.template .. [[ <img src="images/warning.png" alt="Unknown template" title="Unknown template"/>]]
            end--if
            content = content .. [[
                                        </td>
                                        </tr>
]]
        end--for
    content = content .. [[
                            </table>
                            <br>
                            <b>Note:</b> Click on labels to manage TCP/IP DLMS devices.<br>
]]
    else
        content = content .. [[ There are no configured DLMS devices.<br>
]]
    end--if (#(devices) > 0, else
    content = content .. [[
                        <br>
                        <hr>
                        <br>
                        <form action="rsm_dlms_tcp_edit.lua" method="post">
                            <input type="submit" name="addButton" value="Add DLMS Device"]]..buttonControl..[[/>
                        </form>
]]
end--if dlmsBroken, else

content = content .. [[
                    </div>

                    <br>
                    <br>
                    <br>
                                        <div class="formLayoutfull">
]]

if (cameraBroken == 1) then
    content = content .. [[
                        <h3><img src="images/warning.png" alt="cam config missing" title="cam config missing"> Cameras:</h3>
                        Cannot determine path to cam.
]]
else
    content = content .. [[
                        <h3>Cameras:</h3>
]]


    if (#(cameras) > 0) then
        content = content .. [[
                        <table class="onetwo" style="text-align: center;">
                            <tr>
                                <th>Camera Label:</th>
                                <th>Camera Serial:</th>
                                <th>Camera IP:</th>
                                <th>Template:</th>
                            </tr>
]]
        for i, cam in pairs(cameras) do
            content = content .. [[
                <tr>
                <td><a href="rsm_cam_edit.lua?file=]]..cam.filename..[["> ]].. cam.cameraLabel ..[[ <img src="images/spanner.png"/></a></td>
                <td>]]..cam.cameraSerial..[[</td>
                <td>]]..cam.cameraIP..[[</td>
                <td>
                ]]



            if (cameraTemplates[cam.temp] ~= nil) then
                content = content .. [[
                                            ]].. " " .. cameraTemplates[cam.temp].make .. " " .. cameraTemplates[cam.temp].model .. " (" .. cameraTemplates[cam.temp].mType .. ") " .. cameraTemplates[cam.temp].version..[[
]]
            else
                content = content .. cam.temp .. [[ <img src="images/warning.png" alt="Unknown template" title="Unknown template"/>]]
            end--if
            content = content ..[[</td>


                                        </tr>
]]
        end--for
    content = content .. [[
                            </table>
                            <br>
                            <b>Note:</b> Click on labels to manage cameras<br>
]]
    else
        content = content .. [[ There are no configured cameras.<br>
]]
    end--if (#(cameras) > 0, else
    content = content .. [[
                        <br>
                        <hr>
                        <br>
                        <form action="rsm_cam_edit.lua" method="post">
                            <input type="submit" name="addButton" value="Add Camera"]]..buttonControl..[[/>
                        </form>
]]



if (cameraBroken ~= 1) then
    cameraConfig = cameraConfig:gsub("<","&lt;")
    cameraConfig = cameraConfig:gsub(">","&gt;")
    content = content .. [[
                    <br><img src="images/magnify.png"/> <a href="javascript:void(0)" onclick="toggleVis(document.getElementById('cameraconfig'));">View ]]..rsm_conf.camera_xml_path..[[</a></a><br>

                    <div id="cameraconfig" style="display: none;">
                        <pre class="formLayoutfull">
]]..cameraConfig..[[
                        </pre>
                    </div>
]]

end

end--if cameraBroken, else


content = content .. [[
    </div>

    <br>
    <br>
    <br>
                        <div class="formLayoutfull">
]]



    if (voip_config == false) then
        content = content .. [[
                            <h3><img src="images/warning.png" alt="VOIP config missing" title="voip device missing"> RSM VOIP:</h3>
                            No RSM VOIP devices configured.
    ]]

    content = content .. [[
        <br>
        <hr>
        <br>
        <form action="rsm_voip_edit.lua" method="post">
            <input type="submit" name="addButton" value="Configure RSM VOIP"]]..buttonControl..[[/>
        </form>
]]
    else
        content = content .. [[
                            <h3>RSM VOIP Device:</h3>
    ]]
        if (#(bareFiles) > 0) then
            content = content .. [[
                            <table class="onetwo" style="text-align: center;">
                                <tr>
                                    <th>Label:</th>
                                    <th>Serial:</th>
                                </tr>
    ]]
            for i, voip_device in pairs(bareDevice) do
                content = content .. [[
                                    <tr>
                                        <td><a href="rsm_voip_edit.lua?file=]]..voip_device.serial .. ".xml"..[["> ]]..voip_device.label..[[ <img src="images/spanner.png"/></a></td> 
                                      <td>]]..voip_device.serial..[[</td>
                                      </td>    
                                            </tr>
    ]]
            end--for
        content = content .. [[
                                </table>
                                <br>
                                <b>Note:</b> Click on label to manage VOIP device<br>
    ]]
    
    bdd_device = bdd_device:gsub("<","&lt;")
    bdd_device = bdd_device:gsub(">","&gt;")
    content = content .. [[
                    <br><img src="images/magnify.png"/> <a href="javascript:void(0)" onclick="toggleVis(document.getElementById('bdd_device'));">View VOIP device config file</a></a><br>

                    <div id="bdd_device" style="display: none;">
                        <pre class="formLayoutfull">
]]..bdd_device..[[
                        </pre>
                    </div>
]]

        else
            content = content .. [[ There are no configured VOIP devices.<br>
    ]]


        end--if VOIP is not configured
    end--if cameraBroken, else


content = content .. [[
                    </div>


]]

print(header..content..footer)
