local digitalIn = {}


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





function digitalIn:updatePort(port,devID)
  if (port["pnum"].__value__ ~= nil) then
    local cmd = rsm_conf.rsmc_path .. " SET:"
    cmd = cmd .. [[ TAG_IP_CFG ]]
    cmd = cmd .. [[ TAG_DEV_ID=]]..devID
    cmd = cmd .. [[ TAG_IO_PNUM=]]..port["pnum"].__value__
    cmd = cmd .. [[ TAG_IO_LABEL="]]..port["label"].__value__
    cmd = cmd .. [[" TAG_IO_STATUS=]]..port["status"].__value__
    cmd = cmd .. [[ TAG_IO_DEFAULT_STATE=]]..port["defaultState"].__value__
    command.ex(cmd)
  end
end


function digitalIn:getUpdateCommand(port,devID)
  if (port["pnum"].__value__ ~= nil) then
   cmd = ""
    cmd = cmd .. [[ TAG_IP_CFG ]]
    cmd = cmd .. [[ TAG_DEV_ID=]]..devID
    cmd = cmd .. [[ TAG_IO_PNUM=]]..port["pnum"].__value__
    cmd = cmd .. [[ TAG_IO_LABEL="]]..port["label"].__value__
    cmd = cmd .. [[" TAG_IO_STATUS=]]..port["status"].__value__
    cmd = cmd .. [[ TAG_IO_DEFAULT_STATE=]]..port["defaultState"].__value__

  end

  return cmd
end





function digitalIn:getDigitalInPort(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"
  inputConfig = command.ex(rsm_conf.rsmc_path..[[ GET: TAG_IP_CFG TAG_DEV_ID=]]..DevId..[[ TAG_IO_PNUM=]]..pnum) or ""

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

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

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


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


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


--Port.state = inputCfg:match([[TAG_IO_STATE=(%x+)]]) or "02"

  end

  return Port
end

function digitalIn:getAllInPorts(DevId)

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

  if (devConfig:match("TAG_DEV_ID=(%x+)") ~= DevId) then devConfig = "" end
  count = 0
  if (inputsConfig:match("TAG_IP_CFG")) then
      DigiInputs = {}
      for inputCfg in inputsConfig:gmatch([[TAG_IP_CFG (.-) TAG_IO_TYPE]]) do
          local Port = {}

          local type = {}
          type.__value__ = "01" -- This is fixed
          Port["type"] = type
          --Port.type = "01" -- This is fixed

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

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


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


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


  --Port.state = inputCfg:match([[TAG_IO_STATE=(%x+)]]) or "02"

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

  return DigiInputs

end


return digitalIn
