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

devID = cgi.get.devID or "unknown"

content = ""

config = command.ex("/usr/bin/rsmc GET: TAG_PORT_CFG TAG_DEV_ID="..devID)
inputPorts = {}
outputPorts = {}
analogPorts = {}
analogOutPorts = {}

analogCount = 0
analogOutCount = 0
outCount = 0
inCount = 0

for portCfg, label in config:gmatch([[TAG_PORT_CFG (.-) TAG_IO_LABEL=%"(.-)%"]]) do
    port = {}
    port.label = label or "unknown"
    port.pnum = portCfg:match([[TAG_PORT_PNUM=(%x+)]]) or ""
    port.type = portCfg:match([[TAG_PORT_TYPE=(%x+)]]) or ""
    port.alarmStatus = portCfg:match([[TAG_PORT_ALARM_STATUS=(%x+)]]) or "02"
    port.state = portCfg:match([[TAG_PORT_STATE=(%x+)]]) or "03"
    if (port.type == "01") then
        idx = tonumber(port.pnum,16)
        port.pnum = idx
        inputPorts[idx] = port
        if idx > inCount then
          inCount = idx 
        end
    elseif (port.type == "02") then
        idx = tonumber(port.pnum,16)
        port.pnum = idx
        outputPorts[idx] = port
        if idx > outCount then
           outCount = idx
        end
    elseif (port.type == "03") then
         idx = tonumber(port.pnum,16)
          port.pnum = idx
         analogPorts[idx] = port
         if idx > analogCount then
           analogCount = idx
         end
    elseif (port.type == "04") then
         idx = tonumber(port.pnum,16)
         port.pnum = idx
         analogOutPorts[idx] = port
         if idx > analogOutCount then
            analogOutCount = idx 
         end
    end
end

content = ""

---test
--outCount = 0
--inCount = 0
--analogCount = 0

content = content .. [[
{
    "outputPorts" : 
    [
]]        

for idx = 1,outCount do
    if (outputPorts[idx] ~= nil) then
        content = content .. [[
        { "pnum" : "]]..outputPorts[idx].pnum..[[", "label" : "]]..outputPorts[idx].label..[[", "state" : "]]..outputPorts[idx].state..[[", "alarmStatus" : "]]..outputPorts[idx].alarmStatus..[[" }]]
        if (idx ~= outCount) then content = content .. [[,]] end
        content = content .. "\n"
    end
end

content = content .. [[
    ],
    "inputPorts" :
    [
]]

for idx = 1,inCount do
    if (inputPorts[idx] ~= nil) then
        content = content .. [[
        { "pnum" : "]]..inputPorts[idx].pnum..[[", "label" : "]]..inputPorts[idx].label..[[", "state" : "]]..inputPorts[idx].state..[[", "alarmStatus" : "]]..inputPorts[idx].alarmStatus..[[" }]]
        if (idx ~= inCount) then content = content .. [[,]] end
        content = content .. "\n"
    end
end

content = content .. [[
    ],
    "analogPorts" :
    [
]]

for idx = 1,analogCount do
    if (analogPorts[idx] ~= nil) then
        content = content .. [[
        { "pnum" : "]]..analogPorts[idx].pnum..[[", "label" : "]]..analogPorts[idx].label..[[", "state" : "]]..analogPorts[idx].state..[[", "alarmStatus" : "]]..analogPorts[idx].alarmStatus..[[" }]]
        if (idx ~= analogCount) then content = content .. [[,]] end
        content = content .. "\n"
    end
end

content = content .. [[
    ],
    "analogOutPorts" :
    [
]]

for idx = 1,analogOutCount do
    if (analogOutPorts[idx] ~= nil) then
        content = content .. [[
        { "pnum" : "]]..analogOutPorts[idx].pnum..[[", "label" : "]]..analogOutPorts[idx].label..[[", "state" : "]]..analogOutPorts[idx].state..[[", "alarmStatus" : "]]..analogOutPorts[idx].alarmStatus..[[" }]]
        if (idx ~= analogOutCount) then content = content .. [[,]] end
        content = content .. "\n"
    end
end


content = content .. [[
    ]
}
]]

print(content)