local digitalOut = {}

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



function digitalOut:updateOutPort(output,devID)
  if (output["pnum"].__value__ ~= nil) then

    local cmd = rsm_conf.rsmc_path .. " SET:"
    cmd = cmd .. [[ TAG_OP_CFG ]]
    cmd = cmd .. [[ TAG_DEV_ID=]]..devID
    cmd = cmd .. [[ TAG_IO_PNUM=]]..output["pnum"].__value__
    cmd = cmd .. [[ TAG_IO_LABEL="]]..output["label"].__value__
    cmd = cmd .. [[" TAG_IO_STATUS=]]..output["status"].__value__
    cmd = cmd .. [[ TAG_IO_DEFAULT_STATE=]]..output["defaultState"].__value__
    command.ex(cmd)
  end
end


function digitalOut:getUpdateCommand(output,devID)

  --print("In digi out cmd....")
  if (output["pnum"].__value__ ~= nil) then

     cmd =  ""
    cmd = cmd .. [[ TAG_OP_CFG ]]
    cmd = cmd .. [[ TAG_DEV_ID=]]..devID
    cmd = cmd .. [[ TAG_IO_PNUM=]]..output["pnum"].__value__
    cmd = cmd .. [[ TAG_IO_LABEL="]]..output["label"].__value__
    cmd = cmd .. [[" TAG_IO_STATUS=]]..output["status"].__value__
    cmd = cmd .. [[ TAG_IO_DEFAULT_STATE=]]..output["defaultState"].__value__
  end

return cmd

end






function digitalOut:getDigitalOutPort(DevId,pnum)
  devConfig = command.ex(rsm_conf.rsmc_path..[[ GET: TAG_DEV_CFG TAG_DEV_ID=]]..DevId) or ""
  devLabel = devConfig:match([[TAG_DEV_LABEL="(.-)"]]) or "unknown"
  outputConfig = command.ex(rsm_conf.rsmc_path..[[ GET: TAG_OP_CFG TAG_DEV_ID=]]..DevId..[[ TAG_IO_PNUM=]]..pnum) or ""

  if (devConfig:match("TAG_DEV_ID=(%x+)") ~= DevId) then devConfig = "" end
  if (outputConfig:match("TAG_IP_CFG")) then
    output = {}
    local type = {}
    type.__value__ = "02" -- This is fixed
    output["type"] = type
    --output.type = "02" -- This is fixed

    local label = {}
    label.__value__ = outputCfg:match([[TAG_IO_LABEL="(.-)"]]) or ""
    output["label"] = label
    --output.label = outputCfg:match([[TAG_IO_LABEL="(.-)"]]) or ""


    local pnum = {}
    pnum.__value__ = outputCfg:match([[TAG_IO_PNUM=(%x+)]]) or ""
    output["pnum"] = pnum
    --output.pnum = outputCfg:match([[TAG_IO_PNUM=(%x+)]]) or ""

    local status = {}
    status.__value__ = outputCfg:match([[TAG_IO_STATUS=(%x+)]]) or "03"
    output["status"] = status
    --output.status = outputCfg:match([[TAG_IO_STATUS=(%x+)]]) or "03"

    -- local state = {}
    -- state.__value__ = outputCfg:match([[TAG_IO_STATE=(%x+)]]) or "02"
    -- output["state"] = state
    -- --output.state = outputCfg:match([[TAG_IO_STATE=(%x+)]]) or "02"

    local defaultState = {}
    defaultState.__value__ = outputCfg:match([[TAG_IO_DEFAULT_STATE=(%x+)]]) or "02"
    output["defaultState"] = defaultState
    --output.defaultState = outputCfg:match([[TAG_IO_DEFAULT_STATE=(%x+)]]) or "02"

    -- local toggled = {}
    -- toggled.__value__ = outputCfg:match([[TAG_PORT_LAST_TOGGLED=(%x+)]]) or "01"
    -- output["toggled"] = toggled
    -- --output.toggled = outputCfg:match([[TAG_PORT_LAST_TOGGLED=(%x+)]]) or "01"

  end

  return output
end




function digitalOut:getAllOutPorts(DevId)

  devConfig = command.ex(rsm_conf.rsmc_path..[[ GET: TAG_DEV_CFG TAG_DEV_ID=]]..DevId) or ""
  outputsConfig = command.ex(rsm_conf.rsmc_path..[[ GET: TAG_OP_CFG TAG_DEV_ID=]]..DevId) or ""

  if (devConfig:match("TAG_DEV_ID=(%x+)") ~= DevId) then devConfig = "" end
  count = 0
  if (outputsConfig:match("TAG_OP_CFG")) then
    outputs = {}
    for outputCfg in outputsConfig:gmatch([[TAG_OP_CFG (.-) TAG_IO_TYPE]]) do
      local output = {}

      local type = {}
      type.__value__ = "02" -- This is fixed
      output["type"] = type
      --output.type = "02" -- This is fixed

      local label = {}
      label.__value__ = outputCfg:match([[TAG_IO_LABEL="(.-)"]]) or ""
      output["label"] = label
      --output.label = outputCfg:match([[TAG_IO_LABEL="(.-)"]]) or ""


      local pnum = {}
      pnum.__value__ = outputCfg:match([[TAG_IO_PNUM=(%x+)]]) or ""
      output["pnum"] = pnum
      --output.pnum = outputCfg:match([[TAG_IO_PNUM=(%x+)]]) or ""

      local status = {}
      status.__value__ = outputCfg:match([[TAG_IO_STATUS=(%x+)]]) or "03"
      output["status"] = status
      --output.status = outputCfg:match([[TAG_IO_STATUS=(%x+)]]) or "03"

      -- local state = {}
      -- state.__value__ = outputCfg:match([[TAG_IO_STATE=(%x+)]]) or "02"
      -- output["state"] = state
      -- --output.state = outputCfg:match([[TAG_IO_STATE=(%x+)]]) or "02"

      local defaultState = {}
      defaultState.__value__ = outputCfg:match([[TAG_IO_DEFAULT_STATE=(%x+)]]) or "02"
      output["defaultState"] = defaultState
      --output.defaultState = outputCfg:match([[TAG_IO_DEFAULT_STATE=(%x+)]]) or "02"

      -- local toggled = {}
      -- toggled.__value__ = outputCfg:match([[TAG_PORT_LAST_TOGGLED=(%x+)]]) or "01"
      -- output["toggled"] = toggled
      -- --output.toggled = outputCfg:match([[TAG_PORT_LAST_TOGGLED=(%x+)]]) or "01"

      idx = tonumber(pnum.__value__ ,16)
      if idx > count then
        count = idx
      end
      outputs[idx] = output
    end

  end


  return outputs

end


return digitalOut
