From 665a734bce62fceaeca2665a21d5262f7070a98a Mon Sep 17 00:00:00 2001 From: Ted Trask Date: Wed, 10 Nov 2010 09:04:44 +0000 Subject: Added searchdevices action and fixed getdeviceparams to be proper structure, not array --- provisioning-model.lua | 60 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 56 insertions(+), 4 deletions(-) (limited to 'provisioning-model.lua') diff --git a/provisioning-model.lua b/provisioning-model.lua index fa2ddad..71e9434 100644 --- a/provisioning-model.lua +++ b/provisioning-model.lua @@ -1120,7 +1120,6 @@ get_device_params = function(device_id, editable) for i,g in ipairs(tmp) do retval[g.name] = g retval[g.name].type="group" - retval[g.name].value={} end -- Then, get all of the parameters for this device sql = "SELECT g.name AS group, p.param_id, p.name, p.type, p.label, p.descr, p.seq, 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, g2p.value AS default ".. @@ -1133,15 +1132,18 @@ get_device_params = function(device_id, editable) local tmp = getselectresponse(sql) -- Loop through the params and put them into the groups for i,p in ipairs(tmp) do + if not retval[p.group].value then + retval[p.group].value = {} + end local value = retval[p.group].value if p.type == "boolean" then p.value = (p.value == "true") end - value[#value+1] = p + value[p.name] = p end -- Finally, loop through the result and remove empty groups for name,val in pairs(retval) do - if #val.value == 0 then + if not val.value then retval[name] = nil end end @@ -1152,7 +1154,6 @@ get_device_params = function(device_id, editable) errtxt = err end end - return cfe({ type="group", value=retval, label="Provisioning Device Parameters", errtxt=errtxt }) end @@ -1267,3 +1268,54 @@ get_device_values = function(name) return cfe({ type="group", value=retval, label="Provisioning Device Parameter Values", errtxt=errtxt }) end + +search_device_values = function(parameter_id, parameter_value) + local errtxt + retval = {} + retval.id = cfe({type="select", label="Parameter", option={}, seq=1}) + retval.value = cfe({label="Parameter Value", seq=2}) + retval.result = cfe({type="structure", value={}, label="List of Devices" }) + local group, param = string.match(parameter_id or "", "([^%.]*)%.?(.*)") + local res, err = pcall(function() + local connected = databaseconnect() + -- Get the group/parameter options + local sql = "SELECT g.name AS group_name, p.name AS param_name FROM provisioning_groups g JOIN param_groups_to_params USING(group_id) JOIN provisioning_params p USING(param_id) GROUP BY g.name, p.name ORDER BY g.name ASC, p.name ASC" + local tmp = getselectresponse(sql) + local blankopt = {} + local blankexists = {} + for i,v in ipairs(tmp) do + retval.id.option[#retval.id.option + 1] = v.group_name.."."..v.param_name + if not blankexists[v.param_name] then + blankopt[#blankopt+1] = "."..v.param_name + blankexists[v.param_name] = true + end + end + table.sort(blankopt) + for i,o in ipairs(blankopt) do + retval.id.option[#retval.id.option + 1] = o + end + if #tmp>1 then + retval.id.value = retval.id.option[1] + end + -- Get the devices from the DB + if param and param ~= "" and parameter_value and parameter_value ~= "" then + retval.id.value = parameter_id + retval.value.value = parameter_value + sql = "SELECT * FROM provisioning_devices WHERE device_id = (".. + "SELECT d2t.device_id 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 ) " + if group and group ~= "" then + sql = sql.."WHERE g.name='"..escape(group).."' AND p.name='"..escape(param).."' AND v.value='"..escape(parameter_value).."'" + else + sql = sql.."WHERE p.name='"..escape(param).."' AND v.value='"..escape(parameter_value).."'" + end + sql = sql..") ORDER BY name ASC, label ASC" + retval.result.value = getselectresponse(sql) + end + if connected then databasedisconnect() end + end) + if not res and err then + errtxt = err + end + return cfe({type="group", value=retval, label="Device Search", errtxt=errtxt}) +end -- cgit v1.2.3