local analogIn = {}

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

function analogIn:updatePort(port,devId)
  if (port["pnum"].__value__ ~= nil) then
    local cmd = rsm_conf.rsmc_path .. " SET:"
    cmd = cmd .. [[ TAG_AI_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_GAIN=]]..port["gain"].__value__
    cmd = cmd .. [[ TAG_IO_OFFSET=]]..port["offset"].__value__
    cmd = cmd .. [[ TAG_AI_SAMPLE_RATE=]]..port["sampleRate"].__value__
    cmd = cmd .. [[ TAG_AI_REPORT_RATE=]]..port["reportRate"].__value__
    cmd = cmd .. [[ TAG_AI_ALARM_LOW_WARN=]]..port["lowWarn"].__value__
    cmd = cmd .. [[ TAG_AI_ALARM_HIGH_WARN=]]..port["highWarn"].__value__
    cmd = cmd .. [[ TAG_AI_ALARM_LOW_ERR=]]..port["lowErr"].__value__
    cmd = cmd .. [[ TAG_AI_ALARM_HIGH_ERR=]]..port["highErr"].__value__
    cmd = cmd .. [[ TAG_AI_DISPLAY_UNIT="]]..port["unit"].__value__..[["]]
    cmd = cmd .. [[ TAG_AI_ACCEPTABLE_NOISE=]]..port["acceptableNoise"].__value__

    command.ex(cmd)
  end
end


function analogIn:getUpdateCommand(port,devId)
  if (port["pnum"].__value__ ~= nil) then
    cmd = ""
    cmd = cmd .. [[ TAG_AI_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_GAIN=]]..port["gain"].__value__
    cmd = cmd .. [[ TAG_IO_OFFSET=]]..port["offset"].__value__
    cmd = cmd .. [[ TAG_AI_SAMPLE_RATE=]]..port["sampleRate"].__value__
    cmd = cmd .. [[ TAG_AI_REPORT_RATE=]]..port["reportRate"].__value__
    cmd = cmd .. [[ TAG_AI_ALARM_LOW_WARN=]]..port["lowWarn"].__value__
    cmd = cmd .. [[ TAG_AI_ALARM_HIGH_WARN=]]..port["highWarn"].__value__
    cmd = cmd .. [[ TAG_AI_ALARM_LOW_ERR=]]..port["lowErr"].__value__
    cmd = cmd .. [[ TAG_AI_ALARM_HIGH_ERR=]]..port["highErr"].__value__
    cmd = cmd .. [[ TAG_AI_DISPLAY_UNIT="]]..port["unit"].__value__..[["]]
    cmd = cmd .. [[ TAG_AI_ACCEPTABLE_NOISE=]]..port["acceptableNoise"].__value__

  end

  return cmd
end




function analogIn:getAnalogInPort(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"
  analogCfg = command.ex(rsm_conf.rsmc_path..[[ GET: TAG_AI_CFG TAG_DEV_ID=]]..DevId..[[ TAG_IO_PNUM=]]..pnum) or ""

  if (devConfig:match("TAG_DEV_ID=(%x+)") ~= DevId) then devConfig = "" end
  if (analogCfg:match("TAG_AI_CFG")) then
    analog = {}
          local type = {}
          type.__value__ = "03" -- This is fixed
          analog["type"] = type
          --analog.type = "03" -- This is fixed

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

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

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

          -- local sampledValue = {}
          -- sampledValue.__value__ = analogCfg:match([[TAG_IO_SAMPLED_VALUE=([^ ]+)]]) or "0"
          -- analog["sampledValue"] = sampledValue
          --  --analog.sampledValue = analogCfg:match([[TAG_IO_SAMPLED_VALUE=([^ ]+)]]) or "0"

          local offset = {}
          offset.__value__ = analogCfg:match([[TAG_IO_OFFSET=([^ ]+)]]) or "0"
          analog["offset"] = offset
          --analog.offset = analogCfg:match([[TAG_IO_OFFSET=([^ ]+)]]) or "0"

          local gain = {}
          gain.__value__ = analogCfg:match([[TAG_IO_GAIN=([^ ]+)]]) or "0"
          analog["gain"] = gain
          --analog.gain = analogCfg:match([[TAG_IO_GAIN=([^ ]+)]]) or "0"

          local calculatedValue = {}
          calculatedValue.__value__ = analogCfg:match([[TAG_AI_CALCULATED_VALUE=([^ ]+)]]) or "0"
          analog["calculatedValue"] = calculatedValue
          --analog.calculatedValue = analogCfg:match([[TAG_AI_CALCULATED_VALUE=([^ ]+)]]) or "0"

          local lowWarn = {}
          lowWarn.__value__ = analogCfg:match([[TAG_AI_ALARM_LOW_WARN=([^ ]+)]]) or "0"
          analog["lowWarn"] = lowWarn
          --analog.lowWarn = analogCfg:match([[TAG_AI_ALARM_LOW_WARN=([^ ]+)]]) or "0"

          local lowErr = {}
          lowErr.__value__ = analogCfg:match([[TAG_AI_ALARM_LOW_ERR=([^ ]+)]]) or "0"
          analog["lowErr"] = lowErr
          --analog.lowErr = analogCfg:match([[TAG_AI_ALARM_LOW_ERR=([^ ]+)]]) or "0"


          local highWarn = {}
          highWarn.__value__ = analogCfg:match([[TAG_AI_ALARM_HIGH_WARN=([^ ]+)]]) or "0"
          analog["highWarn"] = highWarn
          --analog.highWarn = analogCfg:match([[TAG_AI_ALARM_HIGH_WARN=([^ ]+)]]) or "0"

          local highErr = {}
          highErr.__value__ = analogCfg:match([[TAG_AI_ALARM_HIGH_ERR=([^ ]+)]]) or "0"
          analog["highErr"] = highErr
          --analog.highErr = analogCfg:match([[TAG_AI_ALARM_HIGH_ERR=([^ ]+)]]) or "0"


          local sampleRate = {}
          sampleRate.__value__ = analogCfg:match([[TAG_AI_SAMPLE_RATE=(%x+)]]) or "0"
          analog["sampleRate"] = sampleRate
          --analog.sampleRate = analogCfg:match([[TAG_AI_SAMPLE_RATE=(%x+)]]) or "0"

          local reportRate = {}
          reportRate.__value__ = analogCfg:match([[TAG_AI_REPORT_RATE=(%x+)]]) or "0"
          analog["reportRate"] = reportRate
          --analog.reportRate = analogCfg:match([[TAG_AI_REPORT_RATE=(%x+)]]) or "0"

          local unit = {}
          unit.__value__ = analogCfg:match([[TAG_AI_DISPLAY_UNIT="(.-)"]]) or ""
          analog["unit"] = unit
          --analog.unit = analogCfg:match([[TAG_AI_DISPLAY_UNIT="(.-)"]]) or ""

          local acceptableNoise = {}
          acceptableNoise.__value__ = analogCfg:match([[TAG_AI_ACCEPTABLE_NOISE=([^ ]+)]]) or "0"
          analog["acceptableNoise"] = acceptableNoise
          --analog.acceptableNoise = analogCfg:match([[TAG_AI_ACCEPTABLE_NOISE=([^ ]+)]]) or "0"
  end

  return analog
end



-----------------------------------------------------------------


function analogIn:getAllAnalogInPorts(DevId)


  analogsCfg = command.ex(rsm_conf.rsmc_path..[[ GET: TAG_AI_CFG TAG_DEV_ID=]]..DevId) or ""

  count = 0
  if (analogsCfg:match("TAG_IA_CFG")) then
    analogs = {}

    for analogCfg in analogsCfg:gmatch([[TAG_AI_CFG (.-) TAG_IO_TYPE]]) do
      analog = {}

      local type = {}
      type.__value__ = "03" -- This is fixed
      analog["type"] = type
      --analog.type = "03" -- This is fixed

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

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

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

      -- local sampledValue = {}
      -- sampledValue.__value__ = analogCfg:match([[TAG_IO_SAMPLED_VALUE=([^ ]+)]]) or "0"
      -- analog["sampledValue"] = sampledValue
      -- --analog.sampledValue = analogCfg:match([[TAG_IO_SAMPLED_VALUE=([^ ]+)]]) or "0"

      local offset = {}
      offset.__value__ = analogCfg:match([[TAG_IO_OFFSET=([^ ]+)]]) or "0"
      analog["offset"] = offset
      --analog.offset = analogCfg:match([[TAG_IO_OFFSET=([^ ]+)]]) or "0"

      local gain = {}
      gain.__value__ = analogCfg:match([[TAG_IO_GAIN=([^ ]+)]]) or "0"
      analog["gain"] = gain
      --analog.gain = analogCfg:match([[TAG_IO_GAIN=([^ ]+)]]) or "0"

      local calculatedValue = {}
      calculatedValue.__value__ = analogCfg:match([[TAG_AI_CALCULATED_VALUE=([^ ]+)]]) or "0"
      analog["calculatedValue"] = calculatedValue
      --analog.calculatedValue = analogCfg:match([[TAG_AI_CALCULATED_VALUE=([^ ]+)]]) or "0"

      local lowWarn = {}
      lowWarn.__value__ = analogCfg:match([[TAG_AI_ALARM_LOW_WARN=([^ ]+)]]) or "0"
      analog["lowWarn"] = lowWarn
      --analog.lowWarn = analogCfg:match([[TAG_AI_ALARM_LOW_WARN=([^ ]+)]]) or "0"

      local lowErr = {}
      lowErr.__value__ = analogCfg:match([[TAG_AI_ALARM_LOW_ERR=([^ ]+)]]) or "0"
      analog["lowErr"] = lowErr
      --analog.lowErr = analogCfg:match([[TAG_AI_ALARM_LOW_ERR=([^ ]+)]]) or "0"


      local highWarn = {}
      highWarn.__value__ = analogCfg:match([[TAG_AI_ALARM_HIGH_WARN=([^ ]+)]]) or "0"
      analog["highWarn"] = highWarn
      --analog.highWarn = analogCfg:match([[TAG_AI_ALARM_HIGH_WARN=([^ ]+)]]) or "0"

      local highErr = {}
      highErr.__value__ = analogCfg:match([[TAG_AI_ALARM_HIGH_ERR=([^ ]+)]]) or "0"
      analog["highErr"] = highErr
      --analog.highErr = analogCfg:match([[TAG_AI_ALARM_HIGH_ERR=([^ ]+)]]) or "0"


      local sampleRate = {}
      sampleRate.__value__ = analogCfg:match([[TAG_AI_SAMPLE_RATE=(%x+)]]) or "0"
      analog["sampleRate"] = sampleRate
      --analog.sampleRate = analogCfg:match([[TAG_AI_SAMPLE_RATE=(%x+)]]) or "0"

      local reportRate = {}
      reportRate.__value__ = analogCfg:match([[TAG_AI_REPORT_RATE=(%x+)]]) or "0"
      analog["reportRate"] = reportRate
      --analog.reportRate = analogCfg:match([[TAG_AI_REPORT_RATE=(%x+)]]) or "0"

      local unit = {}
      unit.__value__ = analogCfg:match([[TAG_AI_DISPLAY_UNIT="(.-)"]]) or ""
      analog["unit"] = unit
      --analog.unit = analogCfg:match([[TAG_AI_DISPLAY_UNIT="(.-)"]]) or ""

      local acceptableNoise = {}
      acceptableNoise.__value__ = analogCfg:match([[TAG_AI_ACCEPTABLE_NOISE=([^ ]+)]]) or "0"
      analog["acceptableNoise"] = acceptableNoise
      --analog.acceptableNoise = analogCfg:match([[TAG_AI_ACCEPTABLE_NOISE=([^ ]+)]]) or "0"

      idx = tonumber( pnum.__value__,16)
      if idx > count then
        count = idx
      end
      analogs[idx] = analog
    end
  else
    return nil
  end


    return analogs
  end





  return analogIn
