local command = require("luamods/command")
local layout = require("luamods/layout")
local cgi = require("luamods/cgi")
local rsm_conf = require("config/rsm_conf")
local config = require("config/durabunny")
local json = require("json")



function getDeviceInfo()
    local devices = {}
    local response = command.ex(rsm_conf.rsmc_path.." GET: TAG_MSG_INFO TAG_MSG_INFO_IDX_ONLY TAG_DEV_CFG") or ""
    --command.log("db: "..response)
    for devid in response:gmatch("TAG_DEV_CFG%s+TAG_DEV_ID=(%x+)%s-") do
        --command.log("db: found device "..devid)
        device = {serial = devid}
        table.insert(devices,device)
    end
    for _,dev in ipairs(devices) do
        response  = command.ex(rsm_conf.rsmc_path.." GET: TAG_DEV_CFG TAG_DEV_ID="..dev.serial)
        --command.log("db: "..response)
        dev.name = response:match([[TAG_DEV_LABEL=%"(.-)%"]]) or "unknown"
        --command.log("db: found name "..dev.name)
        dev.inputs = {}
        dev.analogs = {}
        dev.outputs = {}
        response = command.ex(rsm_conf.rsmc_path.." GET: TAG_IP_CFG TAG_DEV_ID="..dev.serial)
        --command.log("db: "..response)
        for portCfg in response:gmatch("TAG_IP_CFG%s+(.-)%s+TAG_IO_TYPE=01") do
            local input = portCfg:match([[TAG_IO_LABEL=%"(.-)%"]]) or "unknown"
            --command.log("db: input "..input)
            table.insert(dev.inputs,input)
        end
        response = command.ex(rsm_conf.rsmc_path.." GET: TAG_OP_CFG TAG_DEV_ID="..dev.serial)
        --command.log("db: "..response)
        for portCfg in response:gmatch("TAG_OP_CFG%s+(.-)%s+TAG_IO_TYPE=02") do
            local output = portCfg:match([[TAG_IO_LABEL=%"(.-)%"]]) or "unknown"
            --command.log("db: output "..output)
            table.insert(dev.outputs,output)
        end
        response = command.ex(rsm_conf.rsmc_path.." GET: TAG_AI_CFG TAG_DEV_ID="..dev.serial)
        --command.log("db: "..response)
        for portCfg in response:gmatch("TAG_AI_CFG%s+(.-)%s+TAG_IO_TYPE=03") do
            local analog = portCfg:match([[TAG_IO_LABEL=%"(.-)%"]]) or "unknown"
            --command.log("db: analog "..analog)
            table.insert(dev.analogs,analog)
        end

    end
    return devices;
end

function isInArray(a,i)
    for k,v in ipairs(a) do
        if v == i then
            return true
        end
    end
    return false
end

local dura_bunny_cfg_str = command.ex("cat "..config.config_file)
local dura_bunny_cfg = json.decode(dura_bunny_cfg_str)
local datamodel = {}
datamodel.helpers = 
{
isInArray = isInArray
}
if(cgi.post.json ~= nil) then
    local cfg = cgi.post.json
   dura_bunny_cfg.cfg = cfg
   dura_bunny_cfg_str = json.encode(dura_bunny_cfg)
   command.log("db: cfg "..dura_bunny_cfg_str)
   command.writefile(config.config_file,dura_bunny_cfg_str)
   command.ex(config.restart)
end
datamodel.cfg = dura_bunny_cfg.cfg
datamodel.deviceinfo = getDeviceInfo()--testdata.deviceinfo

layout.renderLayout("templates/durabunny.html",datamodel)