local analogOut = {}


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



function analogOut:updatePort(analogOut,devID)
  if (analogOut["pnum"].__value__ ~= nil) then
    local cmd = rsm_conf.rsmc_path .. " SET:"
    cmd = cmd .. [[ TAG_AO_CFG ]]
    cmd = cmd .. [[ TAG_DEV_ID=]]..devID
    cmd = cmd .. [[ TAG_IO_PNUM=]]..analogOut["pnum"].__value__
    cmd = cmd .. [[ TAG_IO_LABEL="]]..analogOut["label"].__value__
    cmd = cmd .. [[" TAG_IO_STATUS=]]..analogOut["status"].__value__
    cmd = cmd .. [[ TAG_IO_GAIN=]]..analogOut["gain"].__value__
    cmd = cmd .. [[ TAG_IO_OFFSET=]]..analogOut["offset"].__value__
    cmd = cmd .. [[ TAG_AO_OUTPUT_VALUE=]]..analogOut["outputValue"].__value__
    cmd = cmd .. [[ TAG_AO_ALARM_LOW_WARN=]]..analogOut["lowWarn"].__value__
    cmd = cmd .. [[ TAG_AO_ALARM_HIGH_WARN=]]..analogOut["highWarn"].__value__
    cmd = cmd .. [[ TAG_AO_ALARM_LOW_ERR=]]..analogOut["lowErr"].__value__
    cmd = cmd .. [[ TAG_AO_ALARM_HIGH_ERR=]]..analogOut["highErr"].__value__
    cmd = cmd .. [[ TAG_AO_DISPLAY_UNIT="]]..analogOut["unit"].__value__..[["]]
    command.ex(cmd)
  end
end


function analogOut:getUpdateCommand(analogOut,devID)
  if (analogOut["pnum"].__value__ ~= nil) then
     cmd = ""
    cmd = cmd .. [[ TAG_AO_CFG ]]
    cmd = cmd .. [[ TAG_DEV_ID=]]..devID
    cmd = cmd .. [[ TAG_IO_PNUM=]]..analogOut["pnum"].__value__
    cmd = cmd .. [[ TAG_IO_LABEL="]]..analogOut["label"].__value__
    cmd = cmd .. [[" TAG_IO_STATUS=]]..analogOut["status"].__value__
    cmd = cmd .. [[ TAG_IO_GAIN=]]..analogOut["gain"].__value__
    cmd = cmd .. [[ TAG_IO_OFFSET=]]..analogOut["offset"].__value__
    cmd = cmd .. [[ TAG_AO_OUTPUT_VALUE=]]..analogOut["outputValue"].__value__
    cmd = cmd .. [[ TAG_AO_ALARM_LOW_WARN=]]..analogOut["lowWarn"].__value__
    cmd = cmd .. [[ TAG_AO_ALARM_HIGH_WARN=]]..analogOut["highWarn"].__value__
    cmd = cmd .. [[ TAG_AO_ALARM_LOW_ERR=]]..analogOut["lowErr"].__value__
    cmd = cmd .. [[ TAG_AO_ALARM_HIGH_ERR=]]..analogOut["highErr"].__value__
    cmd = cmd .. [[ TAG_AO_DISPLAY_UNIT="]]..analogOut["unit"].__value__..[["]]
  end

  return cmd
end






function analogOut:getAnalogOutPort(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"
  analogOutCfg = command.ex(rsm_conf.rsmc_path..[[ GET: TAG_AO_CFG TAG_DEV_ID=]]..devID..[[ TAG_IO_PNUM=]]..pnum) or ""

  if (devConfig:match("TAG_DEV_ID=(%x+)") ~= devID) then devConfig = "" end
  if (analogOutCfg:match("TAG_AO_CFG")) then
    analogOut = {}
    -- analogOut.type = "04" -- This is fixed
    -- analogOut.label = analogOutCfg:match([[TAG_IO_LABEL="(.-)"]]) or ""
    -- analogOut.pnum = analogOutCfg:match([[TAG_IO_PNUM=(%x+)]]) or ""
    -- analogOut.status = analogOutCfg:match([[TAG_IO_STATUS=(%x+)]]) or "04"
    -- analogOut.sampledValue = analogOutCfg:match([[TAG_IO_SAMPLED_VALUE=([^ ]+)]]) or "nan"
    -- analogOut.offset = analogOutCfg:match([[TAG_IO_OFFSET=([^ ]+)]]) or "nan"
    -- analogOut.gain = analogOutCfg:match([[TAG_IO_GAIN=([^ ]+)]]) or "nan"
    -- analogOut.outputValue = analogOutCfg:match([[TAG_AO_OUTPUT_VALUE=([^ ]+)]]) or "nan"
    -- analogOut.lowWarn = analogOutCfg:match([[TAG_AO_ALARM_LOW_WARN=([^ ]+)]]) or "nan"
    -- analogOut.lowErr = analogOutCfg:match([[TAG_AO_ALARM_LOW_ERR=([^ ]+)]]) or "nan"
    -- analogOut.highWarn = analogOutCfg:match([[TAG_AO_ALARM_HIGH_WARN=([^ ]+)]]) or "nan"
    -- analogOut.highErr = analogOutCfg:match([[TAG_AO_ALARM_HIGH_ERR=([^ ]+)]]) or "nan"
    -- analogOut.maxValue = analogOutCfg:match([[TAG_AO_MAXIMUM_VALUE=([^ ]+)]]) or "nan"
    -- analogOut.minValue = analogOutCfg:match([[TAG_AO_MINIMUM_VALUE=([^ ]+)]]) or "nan"
    -- analogOut.unit = analogOutCfg:match([[TAG_AO_DISPLAY_UNIT="(.-)"]]) or ""



    local type = {}
    type.__value__ = "04" -- This is fixed
    analogOut["type"] = type
    --analogOut.type = "04" -- This is fixed

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

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

    local status = {}
    status.__value__ = analogOutCfg:match([[TAG_IO_STATUS=(%x+)]]) or "04"
    analogOut["status"] = status
    --analogOut.status = analogOutCfg:match([[TAG_IO_STATUS=(%x+)]]) or "04"

    -- local sampledValue = {}
    -- sampledValue.__value__ = analogOutCfg:match([[TAG_IO_SAMPLED_VALUE=([^ ]+)]]) or "nan"
    -- analogOut["sampledValue"] = sampledValue
    -- --analogOut.sampledValue = analogOutCfg:match([[TAG_IO_SAMPLED_VALUE=([^ ]+)]]) or "nan"

    local offset = {}
    offset.__value__ = analogOutCfg:match([[TAG_IO_OFFSET=([^ ]+)]]) or "nan"
    analogOut["offset"] = offset
    --analogOut.offset = analogOutCfg:match([[TAG_IO_OFFSET=([^ ]+)]]) or "nan"

    local gain = {}
    gain.__value__ = analogOutCfg:match([[TAG_IO_GAIN=([^ ]+)]]) or "nan"
    analogOut["gain"] = gain
    --analogOut.gain = analogOutCfg:match([[TAG_IO_GAIN=([^ ]+)]]) or "nan"

    local outputValue = {}
    outputValue.__value__ = analogOutCfg:match([[TAG_AO_OUTPUT_VALUE=([^ ]+)]]) or "nan"
    analogOut["outputValue"] = outputValue
    --analogOut.outputValue = analogOutCfg:match([[TAG_AO_OUTPUT_VALUE=([^ ]+)]]) or "nan"

    local lowWarn = {}
    lowWarn.__value__ = analogOutCfg:match([[TAG_AO_ALARM_LOW_WARN=([^ ]+)]]) or "nan"
    analogOut["lowWarn"] = lowWarn
    --analogOut.lowWarn = analogOutCfg:match([[TAG_AO_ALARM_LOW_WARN=([^ ]+)]]) or "nan"

    local lowErr = {}
    lowErr.__value__ = analogOutCfg:match([[TAG_AO_ALARM_LOW_ERR=([^ ]+)]]) or "nan"
    analogOut["lowErr"] = lowErr
    --analogOut.lowErr = analogOutCfg:match([[TAG_AO_ALARM_LOW_ERR=([^ ]+)]]) or "nan"

    local highWarn = {}
    highWarn.__value__ = analogOutCfg:match([[TAG_AO_ALARM_HIGH_WARN=([^ ]+)]]) or "nan"
    analogOut["highWarn"] = highWarn
    --analogOut.highWarn = analogOutCfg:match([[TAG_AO_ALARM_HIGH_WARN=([^ ]+)]]) or "nan"

    local highErr = {}
    highErr.__value__ = analogOutCfg:match([[TAG_AO_ALARM_HIGH_ERR=([^ ]+)]]) or "nan"
    analogOut["highErr"] = highErr
    --analogOut.highErr = analogOutCfg:match([[TAG_AO_ALARM_HIGH_ERR=([^ ]+)]]) or "nan"

    local maxValue = {}
    maxValue.__value__ = analogOutCfg:match([[TAG_AO_MAXIMUM_VALUE=([^ ]+)]]) or "nan"
    analogOut["maxValue"] = maxValue
    --analogOut.maxValue = analogOutCfg:match([[TAG_AO_MAXIMUM_VALUE=([^ ]+)]]) or "nan"

    local minValue = {}
    minValue.__value__ = analogOutCfg:match([[TAG_AO_MINIMUM_VALUE=([^ ]+)]]) or "nan"
    analogOut["minValue"] = minValue
    --analogOut.minValue = analogOutCfg:match([[TAG_AO_MINIMUM_VALUE=([^ ]+)]]) or "nan"

    local unit = {}
    unit.__value__ = analogOutCfg:match([[TAG_AO_DISPLAY_UNIT="(.-)"]]) or ""
    analogOut["unit"] = unit
    --analogOut.unit = analogOutCfg:match([[TAG_AO_DISPLAY_UNIT="(.-)"]]) or ""


  end

  return analogOut
end



function analogOut:getAllAnalogOutPorts(DevId)

  analogOutsCfg = command.ex(rsm_conf.rsmc_path..[[ GET: TAG_AO_CFG TAG_DEV_ID=]]..DevId) or ""
  count = 0
  if (analogOutsCfg:match("TAG_AO_CFG")) then
    analogOuts = {}

    for analogOutCfg in analogsOutCfg:gmatch([[TAG_AO_CFG (.-) TAG_IO_TYPE]]) do
      analogOut = {}


      local type = {}
      type.__value__ = "04" -- This is fixed
      analogOut["type"] = type
      --analogOut.type = "04" -- This is fixed

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

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

      local status = {}
      status.__value__ = analogOutCfg:match([[TAG_IO_STATUS=(%x+)]]) or "04"
      analogOut["status"] = status
      --analogOut.status = analogOutCfg:match([[TAG_IO_STATUS=(%x+)]]) or "04"

      -- local sampledValue = {}
      -- sampledValue.__value__ = analogOutCfg:match([[TAG_IO_SAMPLED_VALUE=([^ ]+)]]) or "nan"
      -- analogOut["sampledValue"] = sampledValue
      -- --analogOut.sampledValue = analogOutCfg:match([[TAG_IO_SAMPLED_VALUE=([^ ]+)]]) or "nan"

      local offset = {}
      offset.__value__ = analogOutCfg:match([[TAG_IO_OFFSET=([^ ]+)]]) or "nan"
      analogOut["offset"] = offset
      --analogOut.offset = analogOutCfg:match([[TAG_IO_OFFSET=([^ ]+)]]) or "nan"

      local gain = {}
      gain.__value__ = analogOutCfg:match([[TAG_IO_GAIN=([^ ]+)]]) or "nan"
      analogOut["gain"] = gain
      --analogOut.gain = analogOutCfg:match([[TAG_IO_GAIN=([^ ]+)]]) or "nan"

      local outputValue = {}
      outputValue.__value__ = analogOutCfg:match([[TAG_AO_OUTPUT_VALUE=([^ ]+)]]) or "nan"
      analogOut["outputValue"] = outputValue
      --analogOut.outputValue = analogOutCfg:match([[TAG_AO_OUTPUT_VALUE=([^ ]+)]]) or "nan"

      local lowWarn = {}
      lowWarn.__value__ = analogOutCfg:match([[TAG_AO_ALARM_LOW_WARN=([^ ]+)]]) or "nan"
      analogOut["lowWarn"] = lowWarn
      --analogOut.lowWarn = analogOutCfg:match([[TAG_AO_ALARM_LOW_WARN=([^ ]+)]]) or "nan"

      local lowErr = {}
      lowErr.__value__ = analogOutCfg:match([[TAG_AO_ALARM_LOW_ERR=([^ ]+)]]) or "nan"
      analogOut["lowErr"] = lowErr
      --analogOut.lowErr = analogOutCfg:match([[TAG_AO_ALARM_LOW_ERR=([^ ]+)]]) or "nan"

      local highWarn = {}
      highWarn.__value__ = analogOutCfg:match([[TAG_AO_ALARM_HIGH_WARN=([^ ]+)]]) or "nan"
      analogOut["highWarn"] = highWarn
      --analogOut.highWarn = analogOutCfg:match([[TAG_AO_ALARM_HIGH_WARN=([^ ]+)]]) or "nan"

      local highErr = {}
      highErr.__value__ = analogOutCfg:match([[TAG_AO_ALARM_HIGH_ERR=([^ ]+)]]) or "nan"
      analogOut["highErr"] = highErr
      --analogOut.highErr = analogOutCfg:match([[TAG_AO_ALARM_HIGH_ERR=([^ ]+)]]) or "nan"

      local maxValue = {}
      maxValue.__value__ = analogOutCfg:match([[TAG_AO_MAXIMUM_VALUE=([^ ]+)]]) or "nan"
      analogOut["maxValue"] = maxValue
      --analogOut.maxValue = analogOutCfg:match([[TAG_AO_MAXIMUM_VALUE=([^ ]+)]]) or "nan"

      local minValue = {}
      minValue.__value__ = analogOutCfg:match([[TAG_AO_MINIMUM_VALUE=([^ ]+)]]) or "nan"
      analogOut["minValue"] = minValue
      --analogOut.minValue = analogOutCfg:match([[TAG_AO_MINIMUM_VALUE=([^ ]+)]]) or "nan"

      local unit = {}
      unit.__value__ = analogOutCfg:match([[TAG_AO_DISPLAY_UNIT="(.-)"]]) or ""
      analogOut["unit"] = unit
      --analogOut.unit = analogOutCfg:match([[TAG_AO_DISPLAY_UNIT="(.-)"]]) or ""


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


  return analogOuts
end



return analogOut
