summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--provisioning-model.lua137
1 files changed, 69 insertions, 68 deletions
diff --git a/provisioning-model.lua b/provisioning-model.lua
index de2f748..2703596 100644
--- a/provisioning-model.lua
+++ b/provisioning-model.lua
@@ -322,6 +322,75 @@ duplicatestructure = function(value, saved)
end
end
+local get_device_params = function(device_id, editable)
+ local retval = {}
+ retval.device_id = cfe({value=device_id or "", label="Device ID", seq=0})
+ local errtxt = "Cannot find device"
+ if device_id and device_id ~= "" then
+ local res, err = pcall(function()
+ local connected = databaseconnect()
+ -- First, just check to see if device_id exists
+ local sql = "SELECT * FROM devices_to_classes WHERE device_id='"..escape(device_id).."' LIMIT 1"
+ local tmp = getselectresponse(sql)
+ if tmp and #tmp > 0 then
+ errtxt = nil
+ -- Next, get all of the param groups
+ sql = "SELECT * FROM provisioning_groups"
+ local tmp = getselectresponse(sql)
+ -- Loop through the groups and put them into the result
+ for i,g in ipairs(tmp) do
+ retval[g.name] = g
+ retval[g.name].label = g.name
+ retval[g.name].type="group"
+ end
+ -- Then, get all of the parameters for this device
+ sql = "SELECT g.name AS group, g.label AS grouplabel, p.param_id, p.name, p.type, p.label, p.descr, p.seq, p.regexp, p.validate, CASE WHEN v.value IS NOT NULL THEN v.value WHEN g2p.value IS NOT NULL THEN g2p.value ELSE p.value END AS value, CASE WHEN g2p.value IS NOT NULL THEN g2p.value ELSE p.value END AS default "..
+ "FROM (devices_to_classes d2t JOIN provisioning_classes t USING(class_id) JOIN classes_to_param_groups t2g USING (class_id) JOIN provisioning_groups g USING(group_id) "..
+ "JOIN param_groups_to_params g2p USING(group_id) JOIN provisioning_params p USING(param_id)) LEFT JOIN provisioning_values v ON(d2t.device_id=v.device_id AND p.param_id=v.param_id AND g.name=v.group_name ) "..
+ "WHERE d2t.device_id='"..escape(device_id).."'"
+ if editable then
+ sql = sql.." AND g2p.editable='t'"
+ end
+ local tmp = getselectresponse(sql)
+ -- Loop through the params to figure out options and put them into the groups
+ for i,p in ipairs(tmp) do
+ -- Options
+ if (p.type == "select") then
+ sql = "SELECT * FROM provisioning_options WHERE param_id='"..escape(p.param_id).."' ORDER BY seq ASC"
+ p.option = getselectresponse(sql) or {}
+ end
+ -- Groups
+ if not retval[p.group].value then
+ retval[p.group].value = {}
+ end
+ retval[p.group].label = p.grouplabel
+ local value = retval[p.group].value
+ if p.type == "boolean" then
+ p.value = (p.value == "true")
+ p.default = (p.default == "true")
+ end
+ value[p.name] = p
+ end
+ -- Finally, loop through the result and remove empty groups
+ for name,val in pairs(retval) do
+ if not val.value then
+ retval[name] = nil
+ end
+ end
+ end
+ if connected then databasedisconnect() end
+ end)
+ if not res and err then
+ errtxt = err
+ end
+ end
+
+ -- Save the device for later use
+ local output = cfe({ type="group", value=retval, label="Provisioning Device Parameters", errtxt=errtxt })
+ if device_id and device_id ~= "" and not editable then saved_device_params[device_id] = duplicatestructure(output) end
+ return output
+end
+
-- ################################################################################
-- PUBLIC FUNCTIONS
@@ -1367,74 +1436,6 @@ get_all_device_params = function(self, clientdata, action)
return get_device_params(clientdata.device_id, false)
end
-local get_device_params = function(device_id, editable)
- local retval = {}
- retval.device_id = cfe({value=device_id or "", label="Device ID", seq=0})
- local errtxt = "Cannot find device"
- if device_id and device_id ~= "" then
- local res, err = pcall(function()
- local connected = databaseconnect()
- -- First, just check to see if device_id exists
- local sql = "SELECT * FROM devices_to_classes WHERE device_id='"..escape(device_id).."' LIMIT 1"
- local tmp = getselectresponse(sql)
- if tmp and #tmp > 0 then
- errtxt = nil
- -- Next, get all of the param groups
- sql = "SELECT * FROM provisioning_groups"
- local tmp = getselectresponse(sql)
- -- Loop through the groups and put them into the result
- for i,g in ipairs(tmp) do
- retval[g.name] = g
- retval[g.name].label = g.name
- retval[g.name].type="group"
- end
- -- Then, get all of the parameters for this device
- sql = "SELECT g.name AS group, g.label AS grouplabel, p.param_id, p.name, p.type, p.label, p.descr, p.seq, p.regexp, p.validate, CASE WHEN v.value IS NOT NULL THEN v.value WHEN g2p.value IS NOT NULL THEN g2p.value ELSE p.value END AS value, CASE WHEN g2p.value IS NOT NULL THEN g2p.value ELSE p.value END AS default "..
- "FROM (devices_to_classes d2t JOIN provisioning_classes t USING(class_id) JOIN classes_to_param_groups t2g USING (class_id) JOIN provisioning_groups g USING(group_id) "..
- "JOIN param_groups_to_params g2p USING(group_id) JOIN provisioning_params p USING(param_id)) LEFT JOIN provisioning_values v ON(d2t.device_id=v.device_id AND p.param_id=v.param_id AND g.name=v.group_name ) "..
- "WHERE d2t.device_id='"..escape(device_id).."'"
- if editable then
- sql = sql.." AND g2p.editable='t'"
- end
- local tmp = getselectresponse(sql)
- -- Loop through the params to figure out options and put them into the groups
- for i,p in ipairs(tmp) do
- -- Options
- if (p.type == "select") then
- sql = "SELECT * FROM provisioning_options WHERE param_id='"..escape(p.param_id).."' ORDER BY seq ASC"
- p.option = getselectresponse(sql) or {}
- end
- -- Groups
- if not retval[p.group].value then
- retval[p.group].value = {}
- end
- retval[p.group].label = p.grouplabel
- local value = retval[p.group].value
- if p.type == "boolean" then
- p.value = (p.value == "true")
- p.default = (p.default == "true")
- end
- value[p.name] = p
- end
- -- Finally, loop through the result and remove empty groups
- for name,val in pairs(retval) do
- if not val.value then
- retval[name] = nil
- end
- end
- end
- if connected then databasedisconnect() end
- end)
- if not res and err then
- errtxt = err
- end
- end
-
- -- Save the device for later use
- local output = cfe({ type="group", value=retval, label="Provisioning Device Parameters", errtxt=errtxt })
- if device_id and device_id ~= "" and not editable then saved_device_params[device_id] = duplicatestructure(output) end
- return output
-end
get_class_options = function(self, clientdata)
local class_id = clientdata.classs_id