summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTed Trask <ttrask01@yahoo.com>2013-10-27 02:21:41 +0000
committerTed Trask <ttrask01@yahoo.com>2013-10-27 02:21:41 +0000
commit47a20ee51ea30a1b797642262b1ed1561f80e72c (patch)
tree5759f65e8cb24a735bb3b35ac14a20bb9628c711
parent89f1e294752c899ebdba06a6fbcfbcc390ecc807 (diff)
downloadacf-provisioning-47a20ee51ea30a1b797642262b1ed1561f80e72c.tar.bz2
acf-provisioning-47a20ee51ea30a1b797642262b1ed1561f80e72c.tar.xz
Reorganized model to put all local functions at the beginning
-rw-r--r--provisioning-model.lua473
1 files changed, 237 insertions, 236 deletions
diff --git a/provisioning-model.lua b/provisioning-model.lua
index e730f4f..c45cdb7 100644
--- a/provisioning-model.lua
+++ b/provisioning-model.lua
@@ -28,6 +28,10 @@ provdb.table_creation_scripts = require("provisioning/provisioning-scripts")
local saved_devices = {}
local saved_device_params = {}
+-- These are the functions that may be called from within loaded Lua code
+-- The functions must be added after they're declared
+local functions
+
-- ################################################################################
-- LOCAL FUNCTIONS
local function assert (v, m)
@@ -88,10 +92,6 @@ local deletedatabase = function()
return table.concat(result, "\n")
end
--- Declare runsqlcommand first because it's recursive
--- we also have recursion when runsqlcommand calls runscript, so we have to be careful in creating table_creation_scripts
-local runsqlcommand
-
local databaseconnect = function()
local result = false
local res, err = pcall(function()
@@ -114,14 +114,15 @@ local databaseconnect = function()
return result
end
-runsqlcommand = function(sql, transaction)
-mymodule.logevent(sql)
+-- This function is used by scripts, do not change prototype
+local runsqlcommand = function(sql, transaction)
+ mymodule.logevent(sql)
return provdb.runsqlcommand(sql, transaction)
end
-local getselectresponse
-getselectresponse = function(sql, transaction)
-mymodule.logevent(sql)
+-- This function is used by scripts, do not change prototype
+local getselectresponse = function(sql, transaction)
+ mymodule.logevent(sql)
return provdb.getselectresponse(sql, transaction)
end
@@ -146,10 +147,6 @@ validateparam = function(p, allowdefault)
return true
end
--- These are the functions that may be called from within loaded Lua code
--- The actual functions are added at the end of the file, after they're declared
-local functions = {}
-
local validateparamcoded
validateparamcoded = function(p, top)
top = top or p
@@ -263,6 +260,233 @@ local checkgroupdefaultoverride = function(device_id)
return (#tmp ~= 0)
end
+-- This function is used by scripts, do not change prototype
+local get_device = function(device_id, create)
+ local retval = {}
+ retval.device_id = cfe({value=device_id or "", label="Device ID", seq=1})
+ retval.classes = cfe({type="group", value={}, label="Classes", seq=2})
+ local errtxt
+ local res, err = pcall(function()
+ local classes={}
+ local connected = databaseconnect()
+ if not create and device_id and device_id ~= "" then
+ -- Get the device-to-class mappings
+ local sql = "SELECT class_id FROM devices_to_classes WHERE device_id='"..provdb.escape(device_id).."'"
+ local tmp = getselectresponse(sql)
+ for i,g in ipairs(tmp) do
+ classes[g.class_id] = true
+ end
+ end
+ -- Finally, get the class options
+ sql = "SELECT class_id, g.name, g.label AS group, c.label, c.seq FROM provisioning_classes c JOIN provisioning_class_groups g USING(class_group_id) ORDER BY g.seq ASC, g.label ASC, c.seq ASC, c.label ASC"
+ tmp = getselectresponse(sql)
+ for i,c in ipairs(tmp) do
+ if not retval.classes.value[c.name] then
+ retval.classes.value[c.name] = cfe({type="select", label=c.group, option={{value="", label=""}}, seq=i})
+ end
+ local class = retval.classes.value[c.name]
+ class.option[#class.option + 1] = {value=c.class_id, label=c.label}
+ if classes[c.class_id] then
+ class.value = c.class_id
+ end
+ end
+ if connected then provdb.databasedisconnect() end
+ end)
+ if not res and err then
+ errtxt = err
+ end
+
+ -- Save the device for later use
+ local output = cfe({ type="group", value=retval, label="Provisioning Device", errtxt=errtxt })
+ if not create and device_id and device_id ~= "" then saved_devices[device_id] = duplicatestructure(output) end
+ return output
+end
+
+-- This function is used by scripts, do not change prototype
+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='"..provdb.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, g2p.value AS groupdefault "..
+ "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='"..provdb.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='"..provdb.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")
+ if p.groupdefault then
+ p.groupdefault = (p.groupdefault == "true")
+ end
+ 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
+ -- Even more finally, add in a flag to show if group defaults have been overridden
+ retval.groupdefaultoverride = cfe({ type="boolean", value=checkgroupdefaultoverride(device_id), label="Group defaults have been overridden", readonly=true })
+ end
+ if connected then provdb.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
+
+-- This function is used by scripts, do not change prototype
+local set_device_params = function(params, editable)
+ -- Validate the settings
+ local success = validateparam(params)
+ local errtxt
+ if success then
+ local res, err = pcall(function()
+ local connected = databaseconnect()
+ success = validateparamcoded(params)
+ local sql = "SELECT * FROM devices_to_classes WHERE device_id='"..provdb.escape(params.value.device_id.value).."' LIMIT 1"
+ local tmp = getselectresponse(sql)
+ if not tmp or #tmp == 0 then
+ success = false
+ errtxt = "Device does not exist"
+ end
+ if success then
+ if not saved_device_params[params.value.device_id.value] then get_device_params(params.value.device_id.value) end
+
+ local sql = "BEGIN TRANSACTION"
+ runsqlcommand(sql)
+ if not editable then
+ -- Delete all values for this device (can't do this if only updating editable)
+ sql = "DELETE FROM provisioning_values WHERE device_id='"..provdb.escape(params.value.device_id.value).."'"
+ runsqlcommand(sql, true)
+ end
+ -- Loop through the groups and params
+ for group,v in pairs(params.value) do
+ if v.type == "group" then
+ for name,param in pairs(v.value) do
+ if editable then
+ sql = "DELETE FROM provisioning_values WHERE device_id='"..provdb.escape(params.value.device_id.value).."' AND group_name='"..provdb.escape(group).."' AND param_id='"..provdb.escape(param.param_id).."'"
+ runsqlcommand(sql, true)
+ end
+ if param.value ~= param.default then
+ sql = "INSERT INTO provisioning_values VALUES('"..provdb.escape(params.value.device_id.value).."', '"..provdb.escape(group).."', '"..provdb.escape(param.param_id).."', '"..provdb.escape(tostring(param.value)).."')"
+ runsqlcommand(sql, true)
+ end
+ end
+ end
+ end
+
+ sql = "COMMIT"
+ runsqlcommand(sql)
+
+ local tmp = saved_device_params[params.value.device_id.value]
+ local p = params
+ if not editable then
+ saved_device_params[params.value.device_id.value] = params
+ else
+ p = get_device_params(params.value.device_id.value)
+ end
+ callscript(updatedeviceparamsscriptfile, p, tmp)
+ end
+ if connected then provdb.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
+ params.errtxt = errtxt or "Failed to save device parameters"
+ end
+ return params
+end
+
+local get_device_values = function(device_id)
+ local retval = {}
+ local errtxt
+ if device_id and device_id ~= "" then
+ local res, err = pcall(function()
+ local connected = databaseconnect()
+ local sql = "SELECT g.name AS group, p.name, p.type, 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 "..
+ "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='"..provdb.escape(device_id).."'"
+ local tmp = getselectresponse(sql)
+ -- Loop through the params and put them into the groups
+ for i,p in ipairs(tmp) do
+ if p.type == "boolean" then
+ p.value = (p.value == "true")
+ end
+ if not retval[p.group] then
+ retval[p.group] = {}
+ end
+ retval[p.group][p.name] = p.value
+ end
+ if connected then provdb.databasedisconnect() end
+ end)
+ if not res and err then
+ errtxt = err
+ end
+ else
+ errtxt = "Invalid device id"
+ end
+
+ return cfe({type="structure", value=retval, label="Parameter Values", errtxt=errtxt})
+end
+
+-- These are the functions that may be called from within loaded Lua code
+-- The functions must be added after they're declared
+functions = {
+ getselectresponse=getselectresponse,
+ runsqlcommand=runsqlcommand,
+ get_device=get_device,
+ get_device_params=get_device_params,
+ set_device_params=set_device_params,
+}
+
-- ################################################################################
-- PUBLIC FUNCTIONS
@@ -1248,47 +1472,6 @@ mymodule.get_new_device = function(self, clientdata)
return get_device(clientdata.device_id, true)
end
-get_device = function(device_id, create)
- local retval = {}
- retval.device_id = cfe({value=device_id or "", label="Device ID", seq=1})
- retval.classes = cfe({type="group", value={}, label="Classes", seq=2})
- local errtxt
- local res, err = pcall(function()
- local classes={}
- local connected = databaseconnect()
- if not create and device_id and device_id ~= "" then
- -- Get the device-to-class mappings
- local sql = "SELECT class_id FROM devices_to_classes WHERE device_id='"..provdb.escape(device_id).."'"
- local tmp = getselectresponse(sql)
- for i,g in ipairs(tmp) do
- classes[g.class_id] = true
- end
- end
- -- Finally, get the class options
- sql = "SELECT class_id, g.name, g.label AS group, c.label, c.seq FROM provisioning_classes c JOIN provisioning_class_groups g USING(class_group_id) ORDER BY g.seq ASC, g.label ASC, c.seq ASC, c.label ASC"
- tmp = getselectresponse(sql)
- for i,c in ipairs(tmp) do
- if not retval.classes.value[c.name] then
- retval.classes.value[c.name] = cfe({type="select", label=c.group, option={{value="", label=""}}, seq=i})
- end
- local class = retval.classes.value[c.name]
- class.option[#class.option + 1] = {value=c.class_id, label=c.label}
- if classes[c.class_id] then
- class.value = c.class_id
- end
- end
- if connected then provdb.databasedisconnect() end
- end)
- if not res and err then
- errtxt = err
- end
-
- -- Save the device for later use
- local output = cfe({ type="group", value=retval, label="Provisioning Device", errtxt=errtxt })
- if not create and device_id and device_id ~= "" then saved_devices[device_id] = duplicatestructure(output) end
- return output
-end
-
mymodule.create_device = function(self, device, action)
return mymodule.update_device(self, device, action, true)
end
@@ -1418,80 +1601,6 @@ mymodule.get_all_device_params = function(self, clientdata, action)
return get_device_params(clientdata.device_id, false)
end
-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='"..provdb.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, g2p.value AS groupdefault "..
- "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='"..provdb.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='"..provdb.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")
- if p.groupdefault then
- p.groupdefault = (p.groupdefault == "true")
- end
- 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
- -- Even more finally, add in a flag to show if group defaults have been overridden
- retval.groupdefaultoverride = cfe({ type="boolean", value=checkgroupdefaultoverride(device_id), label="Group defaults have been overridden", readonly=true })
- end
- if connected then provdb.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
-
mymodule.get_class_options = function(self, clientdata)
clientdata = clientdata or {}
local class_id = clientdata.class_id
@@ -1551,72 +1660,6 @@ mymodule.set_all_device_params = function(self, params)
return set_device_params(params, false)
end
-set_device_params = function(params, editable)
- -- Validate the settings
- local success = validateparam(params)
- local errtxt
- if success then
- local res, err = pcall(function()
- local connected = databaseconnect()
- success = validateparamcoded(params)
- local sql = "SELECT * FROM devices_to_classes WHERE device_id='"..provdb.escape(params.value.device_id.value).."' LIMIT 1"
- local tmp = getselectresponse(sql)
- if not tmp or #tmp == 0 then
- success = false
- errtxt = "Device does not exist"
- end
- if success then
- if not saved_device_params[params.value.device_id.value] then get_device_params(params.value.device_id.value) end
-
- local sql = "BEGIN TRANSACTION"
- runsqlcommand(sql)
- if not editable then
- -- Delete all values for this device (can't do this if only updating editable)
- sql = "DELETE FROM provisioning_values WHERE device_id='"..provdb.escape(params.value.device_id.value).."'"
- runsqlcommand(sql, true)
- end
- -- Loop through the groups and params
- for group,v in pairs(params.value) do
- if v.type == "group" then
- for name,param in pairs(v.value) do
- if editable then
- sql = "DELETE FROM provisioning_values WHERE device_id='"..provdb.escape(params.value.device_id.value).."' AND group_name='"..provdb.escape(group).."' AND param_id='"..provdb.escape(param.param_id).."'"
- runsqlcommand(sql, true)
- end
- if param.value ~= param.default then
- sql = "INSERT INTO provisioning_values VALUES('"..provdb.escape(params.value.device_id.value).."', '"..provdb.escape(group).."', '"..provdb.escape(param.param_id).."', '"..provdb.escape(tostring(param.value)).."')"
- runsqlcommand(sql, true)
- end
- end
- end
- end
-
- sql = "COMMIT"
- runsqlcommand(sql)
-
- local tmp = saved_device_params[params.value.device_id.value]
- local p = params
- if not editable then
- saved_device_params[params.value.device_id.value] = params
- else
- p = get_device_params(params.value.device_id.value)
- end
- callscript(updatedeviceparamsscriptfile, p, tmp)
- end
- if connected then provdb.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
- params.errtxt = errtxt or "Failed to save device parameters"
- end
- return params
-end
-
mymodule.fetch_device_values = function(self, search)
local res, err = pcall(function()
local connected = databaseconnect()
@@ -1641,39 +1684,6 @@ mymodule.fetch_device_values = function(self, search)
return search
end
-get_device_values = function(device_id)
- local retval = {}
- local errtxt
- if device_id and device_id ~= "" then
- local res, err = pcall(function()
- local connected = databaseconnect()
- local sql = "SELECT g.name AS group, p.name, p.type, 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 "..
- "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='"..provdb.escape(device_id).."'"
- local tmp = getselectresponse(sql)
- -- Loop through the params and put them into the groups
- for i,p in ipairs(tmp) do
- if p.type == "boolean" then
- p.value = (p.value == "true")
- end
- if not retval[p.group] then
- retval[p.group] = {}
- end
- retval[p.group][p.name] = p.value
- end
- if connected then provdb.databasedisconnect() end
- end)
- if not res and err then
- errtxt = err
- end
- else
- errtxt = "Invalid device id"
- end
-
- return cfe({type="structure", value=retval, label="Parameter Values", errtxt=errtxt})
-end
-
mymodule.get_search_options = function()
local errtxt
retval = {}
@@ -2024,15 +2034,6 @@ function mymodule.put_file(self, clientdata)
return retval
end
--- The functions must be added after they're declared
-functions = {
- getselectresponse=getselectresponse,
- runsqlcommand=runsqlcommand,
- get_device=get_device,
- get_device_params=get_device_params,
- set_device_params=set_device_params,
-}
-
mymodule.list_requests = function()
local retval = {}
local errtxt