local saver = {}


retTable = {}
--package.path = "/home/dusty/git_repos/templating/?.lua;"..package.path


package.path = "/usr/share/kses/templating/?.lua;"..package.path
--package.path = "/home/default/luatests/?.lua;"..package.path
package.path = "/srv/http/?.lua;"..package.path

local pretty = require("pl.pretty")
local xml = require("pl.xml")

function getRsmDPorts(deviceId)

  local retporttable = {}




  local analogInEditor= require("analogIn")
  local Analogs = analogInEditor:getAllAnalogInPorts(deviceId)
  if Analogs ~= nil then
    retporttable["Analog"] = Analogs
    --   table.insert(retTable,analogs)
  end

  local analogOutEditor= require("analogOut")
  local AnalogOuts = analogOutEditor:getAllAnalogOutPorts(deviceId)
  if AnalogOuts ~= nil then
    retporttable["AnalogOut"] = AnalogOuts
    --table.insert(retTable,analogOuts)
  end

  local InputsEditor= require("digitalIn")
  local Inputs = InputsEditor:getAllInPorts(deviceId)
  if Inputs ~= nil then
    retporttable["Input"] = Inputs
    --table.insert(retTable,Inputs)
  end


  local OutEditor= require("digitalOut")
  local Outputs = OutEditor:getAllOutPorts(deviceId)
  if Outputs ~= nil then
    retporttable["Output"] = Outputs
    --table.insert(retTable,Outs)
  end

  return retporttable

end


function getRsmDprofile()

  local  siteEditor = require ("site")
  local DevEditor = require("device")
  local site = siteEditor:getSite()
  local devs = DevEditor:getAllDevices()

  for key, dev in pairs(devs) do
    ports = getRsmDPorts(dev["Id"].__value__)
    dev["Ports"] = ports
  end

  site["device"] = devs
  retTable["site"] = site

  return retTable
end


function saver:saveConfigToFile()

  local returnTable = getRsmDprofile()

  local snmpEditor = require ("snmpEditor")
  local snmpStuff = snmpEditor:getXmlFiles()
  if snmpStuff ~= nil then
    returnTable["site"]["snmpSummary"] = snmpStuff
  end



  local mddEditor = require ("mddEditor")
  local mddStuff = mddEditor:getXmlFiles()
  if mddStuff ~= nil then
    returnTable["site"]["mddSummary"] = mddStuff
  end

  local dddEditor = require ("dddEditor")
  local dddStuff = dddEditor:getXmlFiles()
  if dddStuff ~= nil then
    returnTable["site"]["dddSummary"] = dddStuff
  end

  local flddEditor = require ("flddEditor")
  local flddStuff = flddEditor:getXmlFiles()
  if flddStuff ~= nil then
    returnTable["site"]["flddSummary"] = flddStuff
  end

  local cddEditor = require ("cddEditor")
  local cddStuff = cddEditor:getXmlFiles()
  if cddStuff ~= nil then
    returnTable["site"]["cddSummary"] = cddStuff
  end

  local gsmEditor = require ("gsmEditor")
  local gsmStuff = gsmEditor:getXmlFiles()
  if gsmStuff ~= nil then
    returnTable["site"]["gsmSummary"] = gsmStuff
  end


  local staticEditor = require ("staticEditor")
  local staticStuff = staticEditor:getXmlFiles()
  if staticStuff ~= nil then
    returnTable["site"]["staticSummary"] = staticStuff
  end

  return  returnTable

end


return saver
