summaryrefslogtreecommitdiffstats
path: root/provisioning-model.lua
diff options
context:
space:
mode:
Diffstat (limited to 'provisioning-model.lua')
-rw-r--r--provisioning-model.lua121
1 files changed, 115 insertions, 6 deletions
diff --git a/provisioning-model.lua b/provisioning-model.lua
index 1c063fd..1791ffd 100644
--- a/provisioning-model.lua
+++ b/provisioning-model.lua
@@ -172,6 +172,7 @@ local runscript = function(script)
end
end
+local runsqlcommand
runsqlcommand = function(sql, in_transaction)
logevent(sql)
if in_transaction then assert(con:execute("SAVEPOINT before_command")) end
@@ -192,6 +193,7 @@ logevent(err)
end
end
+local getselectresponse
getselectresponse = function(sql, in_transaction)
local retval = {}
if in_transaction then assert(con:execute("SAVEPOINT before_select")) end
@@ -226,6 +228,20 @@ logevent(err)
return retval
end
+local validateparam
+validateparam = function(p)
+ if p.type == "group" then
+ local success = true
+ for n,p2 in pairs(p.value) do
+ success = validateparam(p2) and success
+ end
+ return success
+ elseif p.type == "select" then
+ return modelfunctions.validateselect(p)
+ end
+ return true
+end
+
-- ################################################################################
-- PUBLIC FUNCTIONS
@@ -590,6 +606,10 @@ get_group = function(group_id)
retval.params.option[#retval.params.option + 1] = {value=p.param_id, label=p.label}
retval.editable.option[#retval.editable.option + 1] = {value=p.param_id, label=p.label}
p.seq = i
+ 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
if p.type == "boolean" then
p.value = (p.value == "true")
end
@@ -654,6 +674,8 @@ update_group = function(group, create)
success = false
group.value.seq.errtxt = "Must be an integer"
end
+ -- Validate the param defaults
+ success = validateparam(group.value.defaults) and success
if success then
local res, err = pcall(function()
local connected = databaseconnect()
@@ -765,7 +787,7 @@ get_param = function(param_id)
local retval = {}
retval.param_id = cfe({value=param_id or "", label="Param ID", seq=1})
retval.name = cfe({label="Name", seq=2})
- retval.type = cfe({type="select", label="Type", option={"text", "boolean"}, seq=3})
+ retval.type = cfe({type="select", label="Type", option={"text", "boolean", "select"}, seq=3})
retval.label = cfe({label="Label", seq=4})
retval.descr = cfe({label="Description", seq=5})
retval.value = cfe({label="Default Value", seq=6})
@@ -1105,8 +1127,14 @@ get_device_params = function(device_id, editable)
sql = sql.." AND g2p.editable='t'"
end
local tmp = getselectresponse(sql)
- -- Loop through the params and put them into the groups
+ -- 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
@@ -1137,10 +1165,9 @@ set_editable_device_params = function(params)
end
set_device_params = function(params, editable)
- local success = true
- local errtxt
-- Validate the settings
--- FIXME
+ local success = validateparam(params)
+ local errtxt
if success then
local res, err = pcall(function()
local connected = databaseconnect()
@@ -1156,7 +1183,7 @@ set_device_params = function(params, editable)
local sql = "BEGIN TRANSACTION"
runsqlcommand(sql)
if not editable then
- -- Delete all values fro this device (can't do this if only updating editable)
+ -- Delete all values for this device (can't do this if only updating editable)
sql = "DELETE FROM provisioning_values WHERE device_id='"..escape(params.value.device_id.value).."'"
runsqlcommand(sql, true)
end
@@ -1327,3 +1354,85 @@ search_device_values = function(parameter_id, parameter_value, comparison)
end
return cfe({type="group", value=retval, label="Device Search", errtxt=errtxt})
end
+
+get_param_options = function(param_id)
+ local retval = {}
+ retval.param_id = cfe({value=param_id or "", label="Parameter ID", seq=0})
+ retval.name = cfe({label="Name", seq=1})
+ retval.label = cfe({label="Label", seq=2})
+ retval.options = cfe({type="list", value={}, label="Value Options", descr="Ordered list of options where each option is made up of 'value' or 'value,label'", seq=3})
+ local errtxt = "Cannot find parameter"
+ if param_id and param_id ~= "" then
+ local res, err = pcall(function()
+ local connected = databaseconnect()
+ -- First, just check to see if param_id exists
+ local sql = "SELECT * FROM provisioning_params WHERE param_id='"..escape(param_id).."'"
+ local tmp = getselectresponse(sql)
+ if tmp and #tmp > 0 then
+ errtxt = nil
+ retval.name.value = tmp[1].name
+ retval.label.value = tmp[1].label
+ -- Next, get all of the param options
+ sql = "SELECT * FROM provisioning_options WHERE param_id='"..escape(param_id).."' ORDER BY seq ASC"
+ local tmp = getselectresponse(sql) or {}
+ for i,t in ipairs(tmp) do
+ retval.options.value[#retval.options.value + 1] = t.value..","..t.label
+ end
+ end
+ if connected then databasedisconnect() end
+ end)
+ if not res and err then
+ errtxt = err
+ end
+ end
+ return cfe({ type="group", value=retval, label="Provisioning Parameter Options", errtxt=errtxt })
+end
+
+set_param_options = function(options)
+ local success = true
+ local errtxt
+ -- Validate the settings
+--FIXME
+ if success then
+ local res, err = pcall(function()
+ local connected = databaseconnect()
+ local sql = "SELECT * FROM provisioning_params WHERE param_id='"..escape(options.value.param_id.value).."'"
+ local tmp = getselectresponse(sql)
+ if not tmp or #tmp == 0 then
+ success = false
+ errtxt = "Parameter does not exist"
+ end
+ if success then
+ local sql = "BEGIN TRANSACTION"
+ runsqlcommand(sql)
+ -- Delete all options for this device
+ sql = "DELETE FROM provisioning_options WHERE param_id='"..escape(options.value.param_id.value).."'"
+ runsqlcommand(sql, true)
+ -- Loop through the options
+ for i,o in ipairs(options.value.options.value) do
+ local v,l = string.match(o, "^%s*([^,]+),%s*(.*%S)%s*$")
+ if v then
+ v = string.match(v, "^(.*%S)%s*$")
+ else
+ v = string.match(o, "^%s*(.*%S)%s*$")
+ l = v
+ end
+ sql = "INSERT INTO provisioning_options VALUES('"..escape(options.value.param_id.value).."', '"..escape(l).."', '"..escape(v).."', '"..i.."')"
+ runsqlcommand(sql, true)
+ end
+ sql = "COMMIT"
+ runsqlcommand(sql)
+ end
+ if connected then databasedisconnect() end
+ end)
+ if not res and err then
+ pcall(function() con:execute("ROLLBACK") end)
+ success = false
+ errtxt = err
+ end
+ end
+ if not success then
+ options.errtxt = errtxt or "Failed to save options"
+ end
+ return options
+end