local rsm_serials = require("luamods/rsm_serials") local command = require("luamods/command") local layout = require("luamods/layout") local cgi = require("luamods/cgi") local rsm_conf = require("config/rsm_conf") local db = require("luamods/db") header = layout.getHeader() footer = layout.getFooter() userLevel = db.getPriv(layout.username) or 1 function log(m) command.ex([[logger -t webconfig ]]..[["]]..m..[["]]) end if (userLevel == 1) then buttonControl = " disabled" inputControl = " readonly" else buttonControl = "" inputControl = "" end serialConfig = command.ex([[cat ]]..rsm_conf.serialconfig_path) or "" modbusConfig = command.ex([[cat ]]..rsm_conf.mdd_xml_path) or "" templatesFiles = {} if (modbusConfig ~= "") then modbusPath = modbusConfig:match([[(.-)]]) or "" templatesPath = modbusConfig:match([[(.-)]]) or "" if (templatesPath ~= "") then templatesFiles = command.getFilesInTable(templatesPath) end end if (cgi.post.deleteButton == "Delete Device") and (userLevel ~= 1) then file = cgi.post.file or "" file = file:gsub("[^%w%.]+","") command.ex(rsm_conf.mdd_down) command.ex([[rm ]]..modbusPath..[[/]]..file) command.ex(rsm_conf.mdd_up) label = cgi.post.label or "unknown" bus = cgi.post.bus or "unknown" -- Stop here content = [[

Edit ModBus Device

Device Deleted

Device with filename "]]..file..[[" has been removed.

Back to ModBus
]] print(header..content..footer) os.exit() end if (cgi.post.saveButton == "Save and Apply") and (userLevel ~= 1) then newFile = cgi.post.file or "" newFile = newFile:gsub("[^%w%.]+","") if (newFile == "") then newSerial = rsm_serials.generateNewSerial() newFile = newSerial..".xml" createdNew = 1 else createdNew = 0 existingCfg = command.ex([[cat ]]..modbusPath..[[/]]..newFile) newSerial = existingCfg:match([[%s-(.-)%s-]]) if (newSerial == nil) then -- Bail on error content = [[

Edit ModBus Device

Filename / serial mismatch

The serial number of the device does not match the one in the file.
Back to Bus Configuration
]] print(header..content..footer) os.exit() end end newLabel = cgi.post.label or "unknown" newTemplate = cgi.post.template or "unknown" newConfig = cgi.post.config or rsm_conf.serialconfig_path newPort = cgi.post.bus or "unknown" newOwner = "mdd" newSlave = cgi.post.slave or "unknown" newMuxport = cgi.post.muxport or "unknown" newMuxslave = cgi.post.muxslave or "unknown" -- new -- newMuxbaud = cgi.post.muxbaud or "unknown" newMuxparity = cgi.post.muxparity or "unknown" newMuxdata = cgi.post.muxdata or "unknown" newMuxstop = cgi.post.muxstop or "unknown" newMuxtype = cgi.post.muxtype or "unknown" newXML = [[ ]]..newSerial..[[ ]]..newConfig..[[ ]]..newPort..[[ ]]..newOwner..[[ ]]..newSlave..[[ ]]..newMuxport..[[ ]]..newMuxslave..[[ ]]..newMuxbaud..[[ ]]..newMuxparity..[[ ]]..newMuxdata..[[ ]]..newMuxstop..[[ ]]..newMuxtype..[[ ]] command.ex(rsm_conf.mdd_down) command.writefile(modbusPath..[[/]]..newFile, newXML) command.ex(rsm_conf.mdd_up) filename = newFile saved = 1 mode = "edit" else filename = cgi.get.file or "" if (filename == "") then mode = "new" else filename = filename:gsub("[^%w%.]+","") mode = "edit" end end bus = cgi.get.bus or cgi.post.bus or "" b, bCfg = serialConfig:match([[(.-)]]) if (b == bus) and (bCfg ~= nil) then if bCfg:match("%s-mdd%s-") == nil then mismatch = 1 else mismatch = 0 end else mismatch = 1 end if (mode == "edit") then cfg = command.ex([[cat ]]..modbusPath..[[/]]..filename) or "" if (cfg:match("(.+)") == nil) then -- bail on error content = [[

Edit ModBus Device

Invalid file

The specified file does not appear to be a valid ModBus device file.

Back to bus configuration
]] print(header..content..footer) os.exit() else d = {} d.label = cfg:match([[]]) or "unknown" d.serial = cfg:match([[%s-(.-)%s-]]) or "unknown" d.template = cfg:match([[]]) or "unknown" d.config = cfg:match([[%s-(.-)%s-]]) or rsm_conf.serialconfig_path d.port = cfg:match([[%s-(.-)%s-]]) if (d.port ~= bus) then -- bail on error content = [[

Edit ModBus Device

Device / bus mismatch

The device being edited is not on the currently selected bus.

Back to bus configuration
]] print(header..content..footer) os.exit() end d.owner = "mdd" d.slave = cfg:match([[%s-(.-)%s-]]) or "unknown" d.muxport = cfg:match([[%s-(.-)%s-]]) or "unknown" d.muxslave = cfg:match([[%s-(.-)%s-]]) or "unknown" -- new -- d.muxbaud = cfg:match([[%s-(.-)%s-]]) or "unknown" d.muxparity = cfg:match([[%s-(.-)%s-]]) or "unknown" d.muxdata = cfg:match([[%s-(.-)%s-]]) or "unknown" d.muxstop = cfg:match([[%s-(.-)%s-]]) or "unknown" d.muxtype = cfg:match([[%s-(.-)%s-]]) or "unknown" end else d = {} d.label = "" d.serial = "(unassigned)" d.template = "" d.config = rsm_conf.serialconfig_path d.port = bus d.owner = "mdd" d.slave = "" d.muxport = "" d.muxslave = "" -- new -- d.muxbaud = "" d.muxparity = "" d.muxdata = "" d.muxstop = "" d.muxtype = "" end templatesOptions = [[ ]] foundTemplate = 0 if (#(templatesFiles) > 0) then for i,f in pairs(templatesFiles) do templateXML = command.ex("cat "..templatesPath..[[/]]..f) details = templateXML:match("
%s-(.-)%s-
") or nil make = templateXML:match("%s-(.-)%s-") or "" model = templateXML:match("%s-(.-)%s-") or "" mType = templateXML:match("%s-(.-)%s-") or "" version = templateXML:match("%s-(.-)%s-") or "" if (version ~= "") then version = "("..version..")" end templatesOptions = templatesOptions..[[ ]] end else templatesOptions = [[ ]] foundTemplate = 1 -- Yeah this looks like it doesn't make sense, but it does end if (foundTemplate == 0) and (mode ~= "new") then templatesOptions = templatesOptions .. [[ ]] baudChanged = 0 baudOptions, baudChanged = baudOptions:gsub([[()]], [[]]) if (baudChanged == 0) then baudOptions = baudOptions:gsub([[()]], [[]]) end parityOptions1 = "" parityOptions2 = "" parityOptions3 = "" parityOptions4 = "" parityOptions5 = "" if (d.muxparity == "E") then parityOptions2 = " selected" elseif (d.muxparity == "O") then parityOptions3 = " selected" elseif (d.muxparity == "M") then parityOptions4 = " selected" elseif (d.muxparity == "S") then parityOptions5 = " selected" else parityOptions1 = " selected" end parityOptions = [[ ]] dataOptions = [[ ]] dataChanged = 0 dataOptions, dataChanged = dataOptions:gsub([[()]], [[]]) if (dataChanged == 0) then dataOptions = dataOptions:gsub([[()]], [[]]) end stopOptions = [[ ]] stopChanged = 0 stopOptions, stopChanged = stopOptions:gsub([[()]], [[]]) if (stopChanged == 0) then stopOptions = stopOptions:gsub([[()]], [[]]) end typeOptions1 = "" typeOptions2 = "" typeOptions3 = "" if (d.muxtype == "rs485") then typeOptions2 = " selected" elseif (d.muxtype == "4wire") then typeOptions3 = " selected" else typeOptions1 = " selected" end typeOptions = [[ ]] content = content .. [[
Device Label:
Device Serial: ]]..d.serial..[[
ModBus: ]]..bus..[[
Template File:
Device Address:
Serial Config: ]]..d.config..[[
Gateway Address:
Gateway Port:
Mux Baud:
Mux Parity:
Mux Data:
Mux Stop:
Mux Type:



]] if (saved == 1) then if (createdNew == 1) then content = content .. [[ Saved new device with serial ]]..d.serial..[[

]] else content = content .. [[ Changes saved.

]] end end content = content .. [[ ]] if (mode == "edit") then content = content .. [[ ]] end content = content .. [[
]] if (mode == "edit") then cfg = cfg:gsub("<","<") cfg = cfg:gsub(">",">") content = content .. [[
View ]]..modbusPath..[[/]]..filename..[[
]] end print(header..content..footer)