local device = {}

local pretty = require("pl.pretty")
require("luamods/command")
require("config/rsm_conf")



function device:updateDevice(Dev)
  if (Dev["Id"].__value__ ~= nil) then
    local cmd = rsm_conf.rsmc_path .. " SET:"
    cmd = cmd .. [[ TAG_DEV_CFG ]]
    cmd = cmd .. [[ TAG_DEV_ID=]]..Dev["Id"].__value__
    cmd = cmd .. [[ TAG_DEV_LABEL=]]..Dev["Label"].__value__
    command.ex(cmd)
  end
end

function device:getUpdateCommand(Dev)
  if (Dev["Id"].__value__ ~= nil) then
     cmd = ""
    cmd = cmd .. [[ TAG_DEV_CFG ]]
    cmd = cmd .. [[ TAG_DEV_ID=]]..Dev["Id"].__value__
    cmd = cmd .. [[ TAG_DEV_LABEL=]]..Dev["Label"].__value__

  end

  return cmd
end



function  device:getDevice(DevId)

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

  if (devConfig:match("TAG_DEV_ID=(%x+)") ~= DevId) then devConfig = "" end

  dev = {}
  -- dev.Type = devConfig:match("TAG_DEV_TYPE=(%x+)") or "0A"
  -- dev.Status = devConfig:match("TAG_DEV_STATUS=(%x+)") or "02"
  -- dev.State = devConfig:match("TAG_DEV_STATE=(%x+)") or "03"
  -- dev.Label = devConfig:match([[TAG_DEV_LABEL=%"(.-)%"]]) or "unknown"
  -- dev.Version = devConfig:match([[TAG_VERSION=(%x+)]]) or ""
  -- dev.NAnalogIn = devConfig:match([[TAG_DEV_NANALOGINPUT=(%x+)]]) or ""
  -- dev.NAnalogOut = devConfig:match([[TAG_DEV_NANALOGOUTPUT=(%x+)]]) or ""
  -- dev.NDiscIn = devConfig:match([[TAG_DEV_NDISCINPUT=(%x+)]]) or ""
  -- dev.NDiscOut = devConfig:match([[TAG_DEV_NDISCOUTPUT=(%x+)]]) or ""


  local Id = {}
  Id.__value__ = devConfig:match("TAG_DEV_ID=(%x+)") or "0A"
  dev["Id"] = Id
  --dev.Id = devConfig:match("TAG_DEV_ID=(%x+)") or "0A"

  local Type = {}
  Type.__value__ = devConfig:match("TAG_DEV_TYPE=(%x+)") or "0A"
  dev["Type"] = Type
  --dev.Type = devConfig:match("TAG_DEV_TYPE=(%x+)") or "0A"

  local Status = {}
  Status.__value__ = devConfig:match("TAG_DEV_STATUS=(%x+)") or "02"
  dev["Status"] = Status
  --dev.Status = devConfig:match("TAG_DEV_STATUS=(%x+)") or "02"

  local State = {}
  State.__value__ = devConfig:match("TAG_DEV_STATE=(%x+)") or "03"
  dev["State"] = State
  --dev.State = devConfig:match("TAG_DEV_STATE=(%x+)") or "03"


  local Label = {}
  Label.__value__ = devConfig:match([[TAG_DEV_LABEL=%"(.-)%"]]) or "unknown"
  dev["Label"] = Label
  --dev.Label = devConfig:match([[TAG_DEV_LABEL=%"(.-)%"]]) or "unknown"

  local Version = {}
  Version.__value__ = devConfig:match([[TAG_VERSION=(%x+)]]) or ""
  dev["Version"] = Version
  --dev.Version = devConfig:match([[TAG_VERSION=(%x+)]]) or ""

  local NAnalogIn = {}
  NAnalogIn.__value__ = devConfig:match([[TAG_DEV_NANALOGINPUT=(%x+)]]) or ""
  dev["NAnalogIn"] = NAnalogIn
  --dev.NAnalogIn = devConfig:match([[TAG_DEV_NANALOGINPUT=(%x+)]]) or ""


  local NAnalogOut = {}
  NAnalogOut.__value__ = devConfig:match([[TAG_DEV_NANALOGOUTPUT=(%x+)]]) or ""
  dev["NAnalogOut"] = NAnalogOut
  --dev.NAnalogOut = devConfig:match([[TAG_DEV_NANALOGOUTPUT=(%x+)]]) or ""


  local NDiscIn = {}
  NDiscIn.__value__ = devConfig:match([[TAG_DEV_NDISCINPUT=(%x+)]]) or ""
  dev["NDiscIn"] = NDiscIn
  --dev.NDiscIn = devConfig:match([[TAG_DEV_NDISCINPUT=(%x+)]]) or ""

  local NDiscOut = {}
  NDiscOut.__value__ = devConfig:match([[TAG_DEV_NDISCOUTPUT=(%x+)]]) or ""
  dev["NDiscOut"] = NDiscOut
  --dev.NDiscOut = devConfig:match([[TAG_DEV_NDISCOUTPUT=(%x+)]]) or ""


end




function device:getAllDevices()

  local  devsConfig = command.ex(rsm_conf.rsmc_path..[[ GET: TAG_DEV_CFG ]]) or ""


  --print(pretty.write(devsConfig))
  --if (devConfig:match("TAG_DEV_ID=(%x+)") ~= DevId) then devConfig = "" end
  --s = string.gsub("Lua is cute", "cute", "great")

  devsConfig = string.gsub(devsConfig,"TAG_DEV_CFG", "DEV_END_HACK TAG_DEV_CFG")
  devsConfig = string.gsub(devsConfig,"\n", "")
  devsConfig = devsConfig.."DEV_END_HACK"

  --print(pretty.write(devsConfig))

  count = 1


  if (devsConfig:match("TAG_DEV_CFG")) then
    devs = {}
    for devConfig in devsConfig:gmatch([[TAG_DEV_CFG (.-) DEV_END_HACK]]) do
      --for devConfig in devsConfig:gmatch([[TAG_DEV_CFG (.-) TAG_VERSION]]) do

      dev = {}

      local Id = {}
      Id.__value__ = devConfig:match("TAG_DEV_ID=(%x+)") or "0A"
      dev["Id"] = Id
      --dev.Id = devConfig:match("TAG_DEV_ID=(%x+)") or "0A"

      local Type = {}
      Type.__value__ = devConfig:match("TAG_DEV_TYPE=(%x+)") or "0A"
      dev["Type"] = Type
      --dev.Type = devConfig:match("TAG_DEV_TYPE=(%x+)") or "0A"

      local Status = {}
      Status.__value__ = devConfig:match("TAG_DEV_STATUS=(%x+)") or "02"
      dev["Status"] = Status
      --dev.Status = devConfig:match("TAG_DEV_STATUS=(%x+)") or "02"

      local State = {}
      State.__value__ = devConfig:match("TAG_DEV_STATE=(%x+)") or "03"
      dev["State"] = State
      --dev.State = devConfig:match("TAG_DEV_STATE=(%x+)") or "03"


      local Label = {}
      Label.__value__ = devConfig:match([[TAG_DEV_LABEL=%"(.-)%"]]) or "unknown"
      dev["Label"] = Label
      --dev.Label = devConfig:match([[TAG_DEV_LABEL=%"(.-)%"]]) or "unknown"

      local Version = {}
      Version.__value__ = devConfig:match([[TAG_VERSION=(%x+)]]) or ""
      dev["Version"] = Version
      --dev.Version = devConfig:match([[TAG_VERSION=(%x+)]]) or ""

      local NAnalogIn = {}
      NAnalogIn.__value__ = devConfig:match([[TAG_DEV_NANALOGINPUT=(%x+)]]) or ""
      dev["NAnalogIn"] = NAnalogIn
      --dev.NAnalogIn = devConfig:match([[TAG_DEV_NANALOGINPUT=(%x+)]]) or ""


      local NAnalogOut = {}
      NAnalogOut.__value__ = devConfig:match([[TAG_DEV_NANALOGOUTPUT=(%x+)]]) or ""
      dev["NAnalogOut"] = NAnalogOut
      --dev.NAnalogOut = devConfig:match([[TAG_DEV_NANALOGOUTPUT=(%x+)]]) or ""


      local NDiscIn = {}
      NDiscIn.__value__ = devConfig:match([[TAG_DEV_NDISCINPUT=(%x+)]]) or ""
      dev["NDiscIn"] = NDiscIn
      --dev.NDiscIn = devConfig:match([[TAG_DEV_NDISCINPUT=(%x+)]]) or ""

      local NDiscOut = {}
      NDiscOut.__value__ = devConfig:match([[TAG_DEV_NDISCOUTPUT=(%x+)]]) or ""
      dev["NDiscOut"] = NDiscOut
      --dev.NDiscOut = devConfig:match([[TAG_DEV_NDISCOUTPUT=(%x+)]]) or ""

      --print("adding dev:".. dev["Id"].__value__ .. "to count: "..count)
      devs[count] = dev
      count = count+1
    end

  end
  return devs

end

function  device:getDeviceIds()

  local ids = {}

  local devs = command.ex(rsm_conf.rsmc_path..[[ GET: TAG_DEV_CFG]]) or ""

  for dev in devs:gmatch("TAG_DEV_CFG (.-) TAG_VERSION") do
    local devID = dev:match("TAG_DEV_ID=(%x+)") or "unknown"
    table.insert(ids,devID)
  end

  return ids

end

return device
