diff options
author | Ted Trask <ttrask01@yahoo.com> | 2013-09-23 18:13:03 +0000 |
---|---|---|
committer | Ted Trask <ttrask01@yahoo.com> | 2013-09-23 18:13:03 +0000 |
commit | fb00ff8f05dae3c110beceb929ea036e4cd14ae1 (patch) | |
tree | 51d124ce1c65e3a600a7da7b9dd57ffafd83f910 /provisioning-model.lua | |
parent | 60ef6e20019e046aa801325d29893ada12b5d5af (diff) | |
download | acf-provisioning-fb00ff8f05dae3c110beceb929ea036e4cd14ae1.tar.bz2 acf-provisioning-fb00ff8f05dae3c110beceb929ea036e4cd14ae1.tar.xz |
Change function prototypes to pass self and use self.model instead of functions table
Changed the order of the first two parameters passed to validate function in provisioning_params
Replaced all APP references with self, now that self is available
Still need to implement swapvalidateparams script
Diffstat (limited to 'provisioning-model.lua')
-rw-r--r-- | provisioning-model.lua | 88 |
1 files changed, 38 insertions, 50 deletions
diff --git a/provisioning-model.lua b/provisioning-model.lua index 6db5b98..fd20d7e 100644 --- a/provisioning-model.lua +++ b/provisioning-model.lua @@ -100,10 +100,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() if not con then -- create environment object @@ -146,6 +142,7 @@ local runscript = function(script, in_transaction) end end +-- This function is used by scripts, do not change prototype runsqlcommand = function(sql, in_transaction) logevent(sql) if in_transaction then assert(con:execute("SAVEPOINT before_command")) end @@ -166,7 +163,7 @@ logevent(err) end end -local getselectresponse +-- This function is used by scripts, do not change prototype getselectresponse = function(sql, in_transaction) local retval = {} if in_transaction then assert(con:execute("SAVEPOINT before_select")) end @@ -222,17 +219,13 @@ 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) +validateparamcoded = function(self, p, top) top = top or p local success = true if p.type == "group" then for n,p2 in pairs(p.value) do - success = validateparamcoded(p2, top) and success + success = validateparamcoded(self, p2, top) and success end return success end @@ -246,7 +239,7 @@ validateparamcoded = function(p, top) local f = loadstring(p.validate) if (f) then local res, err = pcall(function() - p.value, p.errtxt = f(p.value, functions, top) + p.value, p.errtxt = f(self.model, p.value, top) if p.errtxt then success = false end end) if not res and err then @@ -256,10 +249,11 @@ validateparamcoded = function(p, top) end setfenv (0, _G) end + --self.logevent(p.label.." validation "..tostring(success)) return success end -local function callscript(script, ...) +local function callscript(self, script, ...) local result={} local env = {} setmetatable (env, {__index = _G}) @@ -269,7 +263,7 @@ local function callscript(script, ...) local f = loadfile(script) if f then local res, err = pcall(function(...) - result = { f(functions, ...) } + result = { f(self.model, ...) } end, ...) if not res and err then assert(res, "Exception in "..script.." script\n"..err) @@ -1269,15 +1263,16 @@ end get_existing_device = function(self, clientdata) clientdata = clientdata or {} - return get_device(clientdata.device_id, false) + return get_device(self, clientdata.device_id, false) end get_new_device = function(self, clientdata) clientdata = clientdata or {} - return get_device(clientdata.device_id, true) + return get_device(self, clientdata.device_id, true) end -get_device = function(device_id, create) +-- This function is used by scripts, do not change prototype +get_device = function(self, 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}) @@ -1346,8 +1341,8 @@ update_device = function(self, device, action, create) end end if success then - if not saved_devices[device.value.device_id.value] then get_device(device.value.device_id.value) end - if not saved_device_params[device.value.device_id.value] then get_device_params(device.value.device_id.value) end + if not saved_devices[device.value.device_id.value] then get_device(self, device.value.device_id.value) end + if not saved_device_params[device.value.device_id.value] then get_device_params(self, device.value.device_id.value) end local sql = "BEGIN TRANSACTION" runsqlcommand(sql) @@ -1372,7 +1367,7 @@ update_device = function(self, device, action, create) runsqlcommand(sql) local s = saved_device_params[device.value.device_id.value] - callscript(updatedevicescriptfile, device, saved_devices[device.value.device_id.value], get_device_params(device.value.device_id.value), s) + callscript(self, updatedevicescriptfile, device, saved_devices[device.value.device_id.value], get_device_params(self, device.value.device_id.value), s) saved_devices[device.value.device_id.value] = device end if connected then databasedisconnect() end @@ -1410,8 +1405,8 @@ delete_device = function(self, delreq) if #tmp == 0 then delreq.value.device_id.errtxt = "Device does not exist" else - if not saved_device_params[device_id] then get_device_params(device_id) end - if not saved_devices[device_id] then get_device(device_id) end + if not saved_device_params[device_id] then get_device_params(self, device_id) end + if not saved_devices[device_id] then get_device(self, device_id) end sql = "BEGIN TRANSACTION" runsqlcommand(sql) @@ -1423,7 +1418,7 @@ delete_device = function(self, delreq) runsqlcommand(sql) delreq.errtxt = nil - callscript(deletedevicescriptfile, saved_devices[device_id], saved_device_params[device_id]) + callscript(self, deletedevicescriptfile, saved_devices[device_id], saved_device_params[device_id]) saved_devices[device_id] = nil saved_device_params[device_id] = nil end @@ -1439,15 +1434,16 @@ end get_editable_device_params = function(self, clientdata, action) clientdata = clientdata or {} - return get_device_params(clientdata.device_id, true) + return get_device_params(self, clientdata.device_id, true) end get_all_device_params = function(self, clientdata, action) clientdata = clientdata or {} - return get_device_params(clientdata.device_id, false) + return get_device_params(self, clientdata.device_id, false) end -get_device_params = function(device_id, editable) +-- This function is used by scripts, do not change prototype +get_device_params = function(self, device_id, editable) local retval = {} retval.device_id = cfe({value=device_id or "", label="Device ID", seq=0}) local errtxt = "Cannot find device" @@ -1568,21 +1564,22 @@ get_class_values = function(self, retval) end set_editable_device_params = function(self, params) - return set_device_params(params, true) + return set_device_params(self, params, true) end set_all_device_params = function(self, params) - return set_device_params(params, false) + return set_device_params(self, params, false) end -set_device_params = function(params, editable) +-- This function is used by scripts, do not change prototype +set_device_params = function(self, 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) + success = validateparamcoded(self, params) local sql = "SELECT * FROM devices_to_classes WHERE device_id='"..escape(params.value.device_id.value).."' LIMIT 1" local tmp = getselectresponse(sql) if not tmp or #tmp == 0 then @@ -1590,7 +1587,7 @@ set_device_params = function(params, editable) 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 + if not saved_device_params[params.value.device_id.value] then get_device_params(self, params.value.device_id.value) end local sql = "BEGIN TRANSACTION" runsqlcommand(sql) @@ -1623,9 +1620,9 @@ set_device_params = function(params, editable) if not editable then saved_device_params[params.value.device_id.value] = params else - p = get_device_params(params.value.device_id.value) + p = get_device_params(self, params.value.device_id.value) end - callscript(updatedeviceparamsscriptfile, p, tmp) + callscript(self, updatedeviceparamsscriptfile, p, tmp) end if connected then databasedisconnect() end end) @@ -1652,7 +1649,7 @@ fetch_device_values = function(self, search) elseif #search.value.result.value > 1 then search.errtxt = "Multiple devices found" else - search.value.values = get_device_values(search.value.result.value[1].device_id) + search.value.values = get_device_values(self, search.value.result.value[1].device_id) search.value.values.seq = 5 end if connected then databasedisconnect() end @@ -1665,7 +1662,7 @@ fetch_device_values = function(self, search) return search end -get_device_values = function(device_id) +get_device_values = function(self, device_id) local retval = {} local errtxt if device_id and device_id ~= "" then @@ -1989,7 +1986,7 @@ function get_file(self, clientdata) if #result.value.result.value == 0 then -- Determine which class to use (need a class that specifies a template) local c = list_classes() - local class = callscript(determineclassscriptfile, agent, c) + local class = callscript(self, determineclassscriptfile, agent, c) if class then local options = get_class_options(self, {}) @@ -2036,7 +2033,7 @@ function put_file(self, clientdata) elseif #search.value.result.value > 1 then retval.errtxt = "Multiple devices found" else - retval.value, retval.errtxt = callscript(processputscriptfile, mac, data, search.value.result.value[1].device_id) + retval.value, retval.errtxt = callscript(self, processputscriptfile, mac, data, search.value.result.value[1].device_id) -- If the script doesn't exist, allow the write retval.value = retval.value or data end @@ -2048,15 +2045,6 @@ function 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, -} - list_requests = function() local retval = {} local errtxt @@ -2134,10 +2122,10 @@ create_from_request = function(self, request) else -- Determine which class to use (need a class that specifies a template) local c = list_classes() - local class, group = callscript(determineclassscriptfile, tmp[1].agent, c) + local class, group = callscript(self, determineclassscriptfile, tmp[1].agent, c) -- Create the device - local device = get_device(nil, true) + local device = get_device(self, nil, true) if class and group and device.value.classes.value[group] then device.value.classes.value[group].value = class device = create_device(self, device) @@ -2150,11 +2138,11 @@ create_from_request = function(self, request) end request.errtxt = table.concat(request.errtxt, "\n") else - local params = get_device_params(device.value.device_id.value) + local params = get_device_params(self, device.value.device_id.value) -- Set the MAC Address if params.value.device and params.value.device.value.mac then params.value.device.value.mac.value = string.upper(request.value.mac.value) - params = set_device_params(params) + params = set_device_params(self, params) end if params.errtxt then request.errtxt = params.errtxt |