mirror of
https://github.com/Doodle3D/doodle3d-firmware.git
synced 2025-04-20 18:16:30 +02:00
First attempt to add a post response function queue
This commit is contained in:
parent
a732e4890f
commit
3c59322cf7
70
src/main.lua
70
src/main.lua
@ -20,20 +20,20 @@ local function setupAutoWifiMode()
|
|||||||
end
|
end
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
local wifiState = wifi.getDeviceState()
|
local wifiState = wifi.getDeviceState()
|
||||||
local netName, netMode = wifiState.ssid, wifiState.mode
|
local netName, netMode = wifiState.ssid, wifiState.mode
|
||||||
|
|
||||||
local apSsid = wifi.getSubstitutedSsid(settings.get('network.ap.ssid'))
|
local apSsid = wifi.getSubstitutedSsid(settings.get('network.ap.ssid'))
|
||||||
local apMode = (apSsid == netName) and netMode == 'ap'
|
local apMode = (apSsid == netName) and netMode == 'ap'
|
||||||
|
|
||||||
local scanList,msg = wifi.getScanInfo()
|
local scanList,msg = wifi.getScanInfo()
|
||||||
local knownSsids = wifi.getConfigs()
|
local knownSsids = wifi.getConfigs()
|
||||||
|
|
||||||
if not scanList then
|
if not scanList then
|
||||||
return nil, "autowifi: could not scan wifi networks (" .. msg .. ")"
|
return nil, "autowifi: could not scan wifi networks (" .. msg .. ")"
|
||||||
end
|
end
|
||||||
|
|
||||||
log:info("current wifi name/mode: " .. (netName or "<nil>") .. "/" .. netMode .. ", ssid of self: " .. apSsid)
|
log:info("current wifi name/mode: " .. (netName or "<nil>") .. "/" .. netMode .. ", ssid of self: " .. apSsid)
|
||||||
local visNet, knownNet = {}, {}
|
local visNet, knownNet = {}, {}
|
||||||
for _,sn in ipairs(scanList) do
|
for _,sn in ipairs(scanList) do
|
||||||
@ -44,12 +44,12 @@ local function setupAutoWifiMode()
|
|||||||
end
|
end
|
||||||
log:info("visible networks: " .. table.concat(visNet, ", "))
|
log:info("visible networks: " .. table.concat(visNet, ", "))
|
||||||
log:info("known networks: " .. table.concat(knownNet, ", "))
|
log:info("known networks: " .. table.concat(knownNet, ", "))
|
||||||
|
|
||||||
-- if the currently active network is client mode and is also visible, do nothing since it will connect automatically further along the boot process
|
-- if the currently active network is client mode and is also visible, do nothing since it will connect automatically further along the boot process
|
||||||
if netMode == 'sta' and netName ~= nil and netName ~= "" and findSsidInList(scanList, netName) then
|
if netMode == 'sta' and netName ~= nil and netName ~= "" and findSsidInList(scanList, netName) then
|
||||||
return true, "autowifi: no action - existing configuration found for currently wifi visible network (" .. netName .. ")"
|
return true, "autowifi: no action - existing configuration found for currently wifi visible network (" .. netName .. ")"
|
||||||
end
|
end
|
||||||
|
|
||||||
-- try to find a known network which is also visible (ordered by known network definitions)
|
-- try to find a known network which is also visible (ordered by known network definitions)
|
||||||
local connectWith = nil
|
local connectWith = nil
|
||||||
for _,kn in ipairs(knownSsids) do
|
for _,kn in ipairs(knownSsids) do
|
||||||
@ -58,7 +58,7 @@ local function setupAutoWifiMode()
|
|||||||
break
|
break
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if connectWith then
|
if connectWith then
|
||||||
local rv,msg = netconf.associateSsid(connectWith)
|
local rv,msg = netconf.associateSsid(connectWith)
|
||||||
if rv then
|
if rv then
|
||||||
@ -76,18 +76,18 @@ local function setupAutoWifiMode()
|
|||||||
else
|
else
|
||||||
return true, "autowifi: no action - no known networks found, already in access point mode"
|
return true, "autowifi: no action - no known networks found, already in access point mode"
|
||||||
end
|
end
|
||||||
|
|
||||||
return nil, "autowifi: uh oh - bad situation in autowifi function"
|
return nil, "autowifi: uh oh - bad situation in autowifi function"
|
||||||
end
|
end
|
||||||
|
|
||||||
local function setupLogger()
|
local function setupLogger()
|
||||||
local logStream = io.stderr -- use stderr as hard-coded default target
|
local logStream = io.stderr -- use stderr as hard-coded default target
|
||||||
local logLevel = log.LEVEL.debug -- use debug logging as hard-coded default level
|
local logLevel = log.LEVEL.debug -- use debug logging as hard-coded default level
|
||||||
|
|
||||||
local logTargetSetting = settings.getSystemKey('logfile')
|
local logTargetSetting = settings.getSystemKey('logfile')
|
||||||
local logLevelSetting = settings.getSystemKey('loglevel')
|
local logLevelSetting = settings.getSystemKey('loglevel')
|
||||||
local logTargetError, logLevelError = nil, nil
|
local logTargetError, logLevelError = nil, nil
|
||||||
|
|
||||||
if type(logTargetSetting) == 'string' then
|
if type(logTargetSetting) == 'string' then
|
||||||
local specialTarget = logTargetSetting:match('^<(.*)>$')
|
local specialTarget = logTargetSetting:match('^<(.*)>$')
|
||||||
if specialTarget then
|
if specialTarget then
|
||||||
@ -96,13 +96,13 @@ local function setupLogger()
|
|||||||
end
|
end
|
||||||
elseif logTargetSetting:sub(1, 1) == '/' then
|
elseif logTargetSetting:sub(1, 1) == '/' then
|
||||||
local f,msg = io.open(logTargetSetting, 'a+')
|
local f,msg = io.open(logTargetSetting, 'a+')
|
||||||
|
|
||||||
if f then logStream = f
|
if f then logStream = f
|
||||||
else logTargetError = msg
|
else logTargetError = msg
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if type(logLevelSetting) == 'string' and logLevelSetting:len() > 0 then
|
if type(logLevelSetting) == 'string' and logLevelSetting:len() > 0 then
|
||||||
local valid = false
|
local valid = false
|
||||||
for idx,lvl in ipairs(log.LEVEL) do
|
for idx,lvl in ipairs(log.LEVEL) do
|
||||||
@ -113,59 +113,59 @@ local function setupLogger()
|
|||||||
end
|
end
|
||||||
if not valid then logLevelError = true end
|
if not valid then logLevelError = true end
|
||||||
end
|
end
|
||||||
|
|
||||||
log:init(logLevel)
|
log:init(logLevel)
|
||||||
log:setStream(logStream)
|
log:setStream(logStream)
|
||||||
|
|
||||||
local rv = true
|
local rv = true
|
||||||
if logTargetError then
|
if logTargetError then
|
||||||
log:error("could not open logfile '" .. logTargetSetting .. "', using stderr as fallback (" .. logTargetError .. ")")
|
log:error("could not open logfile '" .. logTargetSetting .. "', using stderr as fallback (" .. logTargetError .. ")")
|
||||||
rv = false
|
rv = false
|
||||||
end
|
end
|
||||||
|
|
||||||
if logLevelError then
|
if logLevelError then
|
||||||
log:error("uci config specifies invalid log level '" .. logLevelSetting .. "', using debug level as fallback")
|
log:error("uci config specifies invalid log level '" .. logLevelSetting .. "', using debug level as fallback")
|
||||||
rv = false
|
rv = false
|
||||||
end
|
end
|
||||||
|
|
||||||
return rv
|
return rv
|
||||||
end
|
end
|
||||||
|
|
||||||
local function init(environment)
|
local function init(environment)
|
||||||
setupLogger()
|
setupLogger()
|
||||||
|
|
||||||
local dbgText = ""
|
local dbgText = ""
|
||||||
if confDefaults.DEBUG_API and confDefaults.DEBUG_PCALLS then dbgText = "pcall and api"
|
if confDefaults.DEBUG_API and confDefaults.DEBUG_PCALLS then dbgText = "pcall and api"
|
||||||
elseif confDefaults.DEBUG_API then dbgText = "api"
|
elseif confDefaults.DEBUG_API then dbgText = "api"
|
||||||
elseif confDefaults.DEBUG_PCALL then dbgText = "pcall"
|
elseif confDefaults.DEBUG_PCALL then dbgText = "pcall"
|
||||||
end
|
end
|
||||||
|
|
||||||
if dbgText ~= "" then dbgText = " (" .. dbgText .. " debugging enabled)" end
|
if dbgText ~= "" then dbgText = " (" .. dbgText .. " debugging enabled)" end
|
||||||
|
|
||||||
log:info("Wifibox CGI handler started" .. dbgText)
|
log:info("Wifibox CGI handler started" .. dbgText)
|
||||||
|
|
||||||
if (environment['REQUEST_METHOD'] == 'POST') then
|
if (environment['REQUEST_METHOD'] == 'POST') then
|
||||||
local n = tonumber(environment['CONTENT_LENGTH'])
|
local n = tonumber(environment['CONTENT_LENGTH'])
|
||||||
postData = io.read(n)
|
postData = io.read(n)
|
||||||
end
|
end
|
||||||
|
|
||||||
local s, msg
|
local s, msg
|
||||||
s, msg = wifi.init()
|
s, msg = wifi.init()
|
||||||
if not s then return s, msg end
|
if not s then return s, msg end
|
||||||
|
|
||||||
s, msg = netconf.init(wifi, true)
|
s, msg = netconf.init(wifi, true)
|
||||||
if not s then return s, msg end
|
if not s then return s, msg end
|
||||||
|
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
local function main(environment)
|
local function main(environment)
|
||||||
local rq = RequestClass.new(environment, postData, confDefaults.DEBUG_API)
|
local rq = RequestClass.new(environment, postData, confDefaults.DEBUG_API)
|
||||||
|
|
||||||
if rq:getRequestMethod() == 'CMDLINE' and rq:get('autowifi') ~= nil then
|
if rq:getRequestMethod() == 'CMDLINE' and rq:get('autowifi') ~= nil then
|
||||||
log:info("running in autowifi mode")
|
log:info("running in autowifi mode")
|
||||||
local rv,msg = setupAutoWifiMode()
|
local rv,msg = setupAutoWifiMode()
|
||||||
|
|
||||||
if rv then
|
if rv then
|
||||||
log:info("autowifi setup done (" .. msg .. ")")
|
log:info("autowifi setup done (" .. msg .. ")")
|
||||||
else
|
else
|
||||||
@ -180,11 +180,19 @@ end
|
|||||||
log:info("remote IP/port: " .. rq:getRemoteHost() .. "/" .. rq:getRemotePort())
|
log:info("remote IP/port: " .. rq:getRemoteHost() .. "/" .. rq:getRemotePort())
|
||||||
log:debug("user agent: " .. rq:getUserAgent())
|
log:debug("user agent: " .. rq:getUserAgent())
|
||||||
end
|
end
|
||||||
|
|
||||||
local response, err = rq:handle()
|
local response, err = rq:handle()
|
||||||
|
|
||||||
|
|
||||||
|
local utils = require('util.utils')
|
||||||
|
local log = require('util.logger')
|
||||||
|
log:info("Main (request handled")
|
||||||
|
log:info(" response.postResponseQueue: "..utils.dump(response.postResponseQueue))
|
||||||
|
|
||||||
if err ~= nil then log:error(err) end
|
if err ~= nil then log:error(err) end
|
||||||
response:send()
|
response:send()
|
||||||
|
|
||||||
|
response:executePostResponseQueue()
|
||||||
else
|
else
|
||||||
log:info("Nothing to do...bye.\n")
|
log:info("Nothing to do...bye.\n")
|
||||||
end
|
end
|
||||||
@ -199,15 +207,15 @@ end
|
|||||||
-- @treturn number A Z+ return value suitable to return from wrapper script. Note that this value is ignored by uhttpd-mod-lua.
|
-- @treturn number A Z+ return value suitable to return from wrapper script. Note that this value is ignored by uhttpd-mod-lua.
|
||||||
function handle_request(env)
|
function handle_request(env)
|
||||||
local s, msg = init(env)
|
local s, msg = init(env)
|
||||||
|
|
||||||
if s == false then
|
if s == false then
|
||||||
local resp = ResponseClass.new()
|
local resp = ResponseClass.new()
|
||||||
local errSuffix = msg and " (" .. msg .. ")" or ""
|
local errSuffix = msg and " (" .. msg .. ")" or ""
|
||||||
|
|
||||||
resp:setError("initialization failed" .. errSuffix)
|
resp:setError("initialization failed" .. errSuffix)
|
||||||
resp:send()
|
resp:send()
|
||||||
log:error("initialization failed" .. errSuffix) --NOTE: this assumes the logger has been initialized properly, despite init() having failed
|
log:error("initialization failed" .. errSuffix) --NOTE: this assumes the logger has been initialized properly, despite init() having failed
|
||||||
|
|
||||||
return 1
|
return 1
|
||||||
else
|
else
|
||||||
main(env)
|
main(env)
|
||||||
|
@ -43,7 +43,7 @@ end
|
|||||||
-- and additionally: wifiiface/add, network/reload
|
-- and additionally: wifiiface/add, network/reload
|
||||||
function M.switchConfiguration(components)
|
function M.switchConfiguration(components)
|
||||||
local dirtyList = {} -- laundry list, add config/script name as key with value c (commit), r (reload) or b (both)
|
local dirtyList = {} -- laundry list, add config/script name as key with value c (commit), r (reload) or b (both)
|
||||||
|
|
||||||
for k,v in pairs(components) do
|
for k,v in pairs(components) do
|
||||||
local fname = k .. '_' .. v
|
local fname = k .. '_' .. v
|
||||||
if type(reconf[fname]) == 'function' then
|
if type(reconf[fname]) == 'function' then
|
||||||
@ -53,7 +53,7 @@ function M.switchConfiguration(components)
|
|||||||
log:warn("unknown component or action '" .. fname .. "' skipped")
|
log:warn("unknown component or action '" .. fname .. "' skipped")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- first run all commits, then perform reloads
|
-- first run all commits, then perform reloads
|
||||||
for k,v in pairs(dirtyList) do
|
for k,v in pairs(dirtyList) do
|
||||||
if v == 'c' or v == 'b' then M.commitComponent(k) end
|
if v == 'c' or v == 'b' then M.commitComponent(k) end
|
||||||
@ -105,7 +105,7 @@ function reconf.apnet_add(dirtyList, noReload)
|
|||||||
if s.ssid == ourSsid then sname = s['.name']; return false end
|
if s.ssid == ourSsid then sname = s['.name']; return false end
|
||||||
end)
|
end)
|
||||||
if sname == nil then sname = uci:add('wireless', 'wifi-iface') end
|
if sname == nil then sname = uci:add('wireless', 'wifi-iface') end
|
||||||
|
|
||||||
M.uciTableSet('wireless', sname, {
|
M.uciTableSet('wireless', sname, {
|
||||||
network = wifi.NET,
|
network = wifi.NET,
|
||||||
ssid = ourSsid,
|
ssid = ourSsid,
|
||||||
@ -113,7 +113,7 @@ function reconf.apnet_add(dirtyList, noReload)
|
|||||||
device = 'radio0',
|
device = 'radio0',
|
||||||
mode = 'ap',
|
mode = 'ap',
|
||||||
})
|
})
|
||||||
|
|
||||||
commitBit(dirtyList, 'wireless')
|
commitBit(dirtyList, 'wireless')
|
||||||
if noReload == nil or noReload == false then reloadBit(dirtyList, 'network') end
|
if noReload == nil or noReload == false then reloadBit(dirtyList, 'network') end
|
||||||
end
|
end
|
||||||
@ -189,14 +189,14 @@ function reconf.dnsredir_add(dirtyList)
|
|||||||
local sname = utils.getUciSectionName('dhcp', 'dnsmasq')
|
local sname = utils.getUciSectionName('dhcp', 'dnsmasq')
|
||||||
if sname == nil then return log:error("dhcp config does not contain a dnsmasq section") end
|
if sname == nil then return log:error("dhcp config does not contain a dnsmasq section") end
|
||||||
if uci:get('dhcp', sname, 'address') ~= nil then return log:debug("DNS address redirection already in place, not re-adding", false) end
|
if uci:get('dhcp', sname, 'address') ~= nil then return log:debug("DNS address redirection already in place, not re-adding", false) end
|
||||||
|
|
||||||
uci:set('dhcp', sname, 'address', {redirText})
|
uci:set('dhcp', sname, 'address', {redirText})
|
||||||
commitBit(dirtyList, 'dhcp'); reloadBit(dirtyList, 'dnsmasq')
|
commitBit(dirtyList, 'dhcp'); reloadBit(dirtyList, 'dnsmasq')
|
||||||
end
|
end
|
||||||
function reconf.dnsredir_rm(dirtyList)
|
function reconf.dnsredir_rm(dirtyList)
|
||||||
local sname = utils.getUciSectionName('dhcp', 'dnsmasq')
|
local sname = utils.getUciSectionName('dhcp', 'dnsmasq')
|
||||||
if sname == nil then return log:error("dhcp config does not contain a dnsmasq section") end
|
if sname == nil then return log:error("dhcp config does not contain a dnsmasq section") end
|
||||||
|
|
||||||
uci:delete('dhcp', sname, 'address')
|
uci:delete('dhcp', sname, 'address')
|
||||||
commitBit(dirtyList, 'dhcp'); reloadBit(dirtyList, 'dnsmasq')
|
commitBit(dirtyList, 'dhcp'); reloadBit(dirtyList, 'dnsmasq')
|
||||||
end
|
end
|
||||||
@ -254,7 +254,7 @@ function M.setupAccessPoint(ssid)
|
|||||||
-- NOTE: dnsmasq must be reloaded after network or it will be unable to serve IP addresses
|
-- NOTE: dnsmasq must be reloaded after network or it will be unable to serve IP addresses
|
||||||
M.switchConfiguration{ wifiiface="add", network="reload", staticaddr="add", dhcppool="add_noreload", wwwredir="add", dnsredir="add" }
|
M.switchConfiguration{ wifiiface="add", network="reload", staticaddr="add", dhcppool="add_noreload", wwwredir="add", dnsredir="add" }
|
||||||
M.switchConfiguration{dhcp="reload"}
|
M.switchConfiguration{dhcp="reload"}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -274,7 +274,7 @@ function M.associateSsid(ssid, passphrase, recreate)
|
|||||||
break
|
break
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- if not, or if newly created configuration is requested, create a new configuration
|
-- if not, or if newly created configuration is requested, create a new configuration
|
||||||
if cfg == nil or recreate ~= nil then
|
if cfg == nil or recreate ~= nil then
|
||||||
local scanResult = wifi.getScanInfo(ssid)
|
local scanResult = wifi.getScanInfo(ssid)
|
||||||
@ -285,18 +285,18 @@ function M.associateSsid(ssid, passphrase, recreate)
|
|||||||
return nil,"no wireless network with requested SSID is available"
|
return nil,"no wireless network with requested SSID is available"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- try to associate with the network
|
-- try to associate with the network
|
||||||
wifi.activateConfig(ssid)
|
wifi.activateConfig(ssid)
|
||||||
--M.switchConfiguration{ wifiiface="add", apnet="rm", staticaddr="rm", dhcppool="rm", wwwredir="rm", dnsredir="rm", wwwcaptive="rm", wireless="reload" }
|
--M.switchConfiguration{ wifiiface="add", apnet="rm", staticaddr="rm", dhcppool="rm", wwwredir="rm", dnsredir="rm", wwwcaptive="rm", wireless="reload" }
|
||||||
M.switchConfiguration{ wifiiface="add", apnet="rm", staticaddr="rm", dhcppool="rm", wwwredir="rm", dnsredir="rm", wireless="reload" }
|
M.switchConfiguration{ wifiiface="add", apnet="rm", staticaddr="rm", dhcppool="rm", wwwredir="rm", dnsredir="rm", wireless="reload" }
|
||||||
|
|
||||||
-- check if we are actually associated
|
-- check if we are actually associated
|
||||||
local status = wifi.getDeviceState()
|
local status = wifi.getDeviceState()
|
||||||
if not status.ssid or status.ssid ~= ssid then
|
if not status.ssid or status.ssid ~= ssid then
|
||||||
return nil,"could not associate with network (incorrect passphrase?)"
|
return nil,"could not associate with network (incorrect passphrase?)"
|
||||||
end
|
end
|
||||||
|
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -20,14 +20,14 @@ function M.scan(request, response)
|
|||||||
local withRaw = utils.toboolean(request:get("with_raw"))
|
local withRaw = utils.toboolean(request:get("with_raw"))
|
||||||
local sr = wifi.getScanInfo()
|
local sr = wifi.getScanInfo()
|
||||||
local si, se
|
local si, se
|
||||||
|
|
||||||
if sr and #sr > 0 then
|
if sr and #sr > 0 then
|
||||||
response:setSuccess("")
|
response:setSuccess("")
|
||||||
local netInfoList = {}
|
local netInfoList = {}
|
||||||
for _, se in ipairs(sr) do
|
for _, se in ipairs(sr) do
|
||||||
if noFilter or se.mode ~= "ap" and se.ssid ~= wifi.getSubstitutedSsid(settings.get('network.ap.ssid')) then
|
if noFilter or se.mode ~= "ap" and se.ssid ~= wifi.getSubstitutedSsid(settings.get('network.ap.ssid')) then
|
||||||
local netInfo = {}
|
local netInfo = {}
|
||||||
|
|
||||||
netInfo["ssid"] = se.ssid
|
netInfo["ssid"] = se.ssid
|
||||||
netInfo["bssid"] = se.bssid
|
netInfo["bssid"] = se.bssid
|
||||||
netInfo["channel"] = se.channel
|
netInfo["channel"] = se.channel
|
||||||
@ -37,7 +37,7 @@ function M.scan(request, response)
|
|||||||
netInfo["quality"] = se.quality
|
netInfo["quality"] = se.quality
|
||||||
netInfo["quality_max"] = se.quality_max
|
netInfo["quality_max"] = se.quality_max
|
||||||
if withRaw then netInfo["_raw"] = utils.dump(se) end
|
if withRaw then netInfo["_raw"] = utils.dump(se) end
|
||||||
|
|
||||||
table.insert(netInfoList, netInfo)
|
table.insert(netInfoList, netInfo)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -53,7 +53,7 @@ end
|
|||||||
function M.known(request, response)
|
function M.known(request, response)
|
||||||
local noFilter = utils.toboolean(request:get("nofilter"))
|
local noFilter = utils.toboolean(request:get("nofilter"))
|
||||||
local withRaw = utils.toboolean(request:get("with_raw"))
|
local withRaw = utils.toboolean(request:get("with_raw"))
|
||||||
|
|
||||||
response:setSuccess()
|
response:setSuccess()
|
||||||
local netInfoList = {}
|
local netInfoList = {}
|
||||||
for _, net in ipairs(wifi.getConfigs()) do
|
for _, net in ipairs(wifi.getConfigs()) do
|
||||||
@ -75,7 +75,7 @@ end
|
|||||||
function M.status(request, response)
|
function M.status(request, response)
|
||||||
local withRaw = utils.toboolean(request:get("with_raw"))
|
local withRaw = utils.toboolean(request:get("with_raw"))
|
||||||
local ds = wifi.getDeviceState()
|
local ds = wifi.getDeviceState()
|
||||||
|
|
||||||
response:setSuccess()
|
response:setSuccess()
|
||||||
response:addData("ssid", ds.ssid or "")
|
response:addData("ssid", ds.ssid or "")
|
||||||
response:addData("bssid", ds.bssid or "")
|
response:addData("bssid", ds.bssid or "")
|
||||||
@ -92,23 +92,43 @@ end
|
|||||||
|
|
||||||
--requires ssid(string), accepts phrase(string), recreate(bool)
|
--requires ssid(string), accepts phrase(string), recreate(bool)
|
||||||
function M.associate_POST(request, response)
|
function M.associate_POST(request, response)
|
||||||
|
local utils = require('util.utils')
|
||||||
|
local log = require('util.logger')
|
||||||
|
log:info("API:Network:associate")
|
||||||
|
|
||||||
local argSsid = request:get("ssid")
|
local argSsid = request:get("ssid")
|
||||||
local argPhrase = request:get("phrase")
|
local argPhrase = request:get("phrase")
|
||||||
local argRecreate = request:get("recreate")
|
local argRecreate = request:get("recreate")
|
||||||
|
|
||||||
if argSsid == nil or argSsid == "" then
|
if argSsid == nil or argSsid == "" then
|
||||||
response:setError("missing ssid argument")
|
response:setError("missing ssid argument")
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local rv,msg = netconf.associateSsid(argSsid, argPhrase, argRecreate)
|
local associate = function()
|
||||||
|
local rv,msg = netconf.associateSsid(argSsid, argPhrase, argRecreate)
|
||||||
response:addData("ssid", argSsid)
|
end
|
||||||
|
--response:addPostResponseFunction(associate)
|
||||||
|
|
||||||
|
local helloA = function()
|
||||||
|
local log = require('util.logger')
|
||||||
|
log:info("HELLO A")
|
||||||
|
end
|
||||||
|
response:addPostResponseFunction(helloA)
|
||||||
|
|
||||||
|
local helloB = function()
|
||||||
|
local log = require('util.logger')
|
||||||
|
log:info("HELLO B")
|
||||||
|
end
|
||||||
|
response:addPostResponseFunction(helloB)
|
||||||
|
|
||||||
|
--[[response:addData("ssid", argSsid)
|
||||||
if rv then
|
if rv then
|
||||||
response:setSuccess("wlan associated")
|
response:setSuccess("wlan associated")
|
||||||
else
|
else
|
||||||
response:setFail(msg)
|
response:setFail(msg)
|
||||||
end
|
end]]--
|
||||||
|
response:setSuccess("wlan is trying to associate")
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.disassociate_POST(request, response)
|
function M.disassociate_POST(request, response)
|
||||||
@ -121,7 +141,7 @@ end
|
|||||||
function M.openap_POST(request, response)
|
function M.openap_POST(request, response)
|
||||||
local ssid = wifi.getSubstitutedSsid(settings.get('network.ap.ssid'))
|
local ssid = wifi.getSubstitutedSsid(settings.get('network.ap.ssid'))
|
||||||
local rv,msg = netconf.setupAccessPoint(ssid)
|
local rv,msg = netconf.setupAccessPoint(ssid)
|
||||||
|
|
||||||
response:addData("ssid", ssid)
|
response:addData("ssid", ssid)
|
||||||
if rv then
|
if rv then
|
||||||
response:setSuccess("switched to Access Point mode")
|
response:setSuccess("switched to Access Point mode")
|
||||||
@ -134,12 +154,12 @@ end
|
|||||||
--requires ssid(string)
|
--requires ssid(string)
|
||||||
function M.remove_POST(request, response)
|
function M.remove_POST(request, response)
|
||||||
local argSsid = request:get("ssid")
|
local argSsid = request:get("ssid")
|
||||||
|
|
||||||
if argSsid == nil or argSsid == "" then
|
if argSsid == nil or argSsid == "" then
|
||||||
response:setError("missing ssid argument")
|
response:setError("missing ssid argument")
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
if wifi.removeConfig(argSsid) then
|
if wifi.removeConfig(argSsid) then
|
||||||
response:setSuccess("removed wireless network with requested SSID")
|
response:setSuccess("removed wireless network with requested SSID")
|
||||||
response:addData("ssid", argSsid)
|
response:addData("ssid", argSsid)
|
||||||
|
@ -24,22 +24,25 @@ setmetatable(M, {
|
|||||||
--requestObject should always be passed (except on init failure, when it is not yet available)
|
--requestObject should always be passed (except on init failure, when it is not yet available)
|
||||||
function M.new(requestObject)
|
function M.new(requestObject)
|
||||||
local self = setmetatable({}, M)
|
local self = setmetatable({}, M)
|
||||||
|
|
||||||
self.body = { status = nil, data = {} }
|
self.body = { status = nil, data = {} }
|
||||||
self:setHttpStatus(200, 'OK')
|
self:setHttpStatus(200, 'OK')
|
||||||
self:setContentType('text/plain;charset=UTF-8')
|
self:setContentType('text/plain;charset=UTF-8')
|
||||||
--self:setContentType('application/json;charset=UTF-8')
|
--self:setContentType('application/json;charset=UTF-8')
|
||||||
|
|
||||||
|
-- a queue for functions to be executed when the response has bin given
|
||||||
|
-- needed for api calls like network/associate, which requires a restart of the webserver
|
||||||
|
self.postResponseQueue = {}
|
||||||
|
|
||||||
if requestObject ~= nil then
|
if requestObject ~= nil then
|
||||||
local rqId = requestObject:get(REQUEST_ID_ARGUMENT)
|
local rqId = requestObject:get(REQUEST_ID_ARGUMENT)
|
||||||
if rqId ~= nil then self.body[REQUEST_ID_ARGUMENT] = rqId end
|
if rqId ~= nil then self.body[REQUEST_ID_ARGUMENT] = rqId end
|
||||||
|
|
||||||
if settings.API_INCLUDE_ENDPOINT_INFO == true then
|
if settings.API_INCLUDE_ENDPOINT_INFO == true then
|
||||||
self.body['module'] = requestObject:getRequestedApiModule()
|
self.body['module'] = requestObject:getRequestedApiModule()
|
||||||
self.body['function'] = requestObject:getRealApiFunctionName() or ''
|
self.body['function'] = requestObject:getRealApiFunctionName() or ''
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -65,7 +68,7 @@ end
|
|||||||
function M:setError(msg)
|
function M:setError(msg)
|
||||||
self.body.status = 'error'
|
self.body.status = 'error'
|
||||||
if msg ~= '' then self.body.msg = msg end
|
if msg ~= '' then self.body.msg = msg end
|
||||||
|
|
||||||
self:addData('more_info', 'http://' .. defaults.API_BASE_URL_PATH .. '/wiki/wiki/communication-api')
|
self:addData('more_info', 'http://' .. defaults.API_BASE_URL_PATH .. '/wiki/wiki/communication-api')
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -76,6 +79,22 @@ function M:addData(k, v)
|
|||||||
self.binaryData = nil
|
self.binaryData = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function M:addPostResponseFunction(fn)
|
||||||
|
local utils = require('util.utils')
|
||||||
|
local log = require('util.logger')
|
||||||
|
log:info("Response:addPostResponseFunction: " .. utils.dump(fn))
|
||||||
|
table.insert(self.postResponseQueue, fn)
|
||||||
|
log:info(" self.postResponseQueue: " .. utils.dump(self.postResponseQueue))
|
||||||
|
end
|
||||||
|
|
||||||
|
function M:executePostResponseQueue()
|
||||||
|
local utils = require('util.utils')
|
||||||
|
local log = require('util.logger')
|
||||||
|
log:info("Response:executePostResponseQueue: " .. utils.dump(self.postResponseQueue))
|
||||||
|
|
||||||
|
for i,fn in ipairs(self.postResponseQueue) do fn() end
|
||||||
|
end
|
||||||
|
|
||||||
function M:apiURL(mod, func)
|
function M:apiURL(mod, func)
|
||||||
if not mod then return nil end
|
if not mod then return nil end
|
||||||
if func then func = '/' .. func else func = "" end
|
if func then func = '/' .. func else func = "" end
|
||||||
@ -103,17 +122,17 @@ end
|
|||||||
|
|
||||||
function M:setBinaryFileData(rFile, saveName, contentType)
|
function M:setBinaryFileData(rFile, saveName, contentType)
|
||||||
if type(rFile) ~= 'string' or rFile:len() == 0 then return false end
|
if type(rFile) ~= 'string' or rFile:len() == 0 then return false end
|
||||||
|
|
||||||
local f,msg = io.open(rFile, "rb")
|
local f,msg = io.open(rFile, "rb")
|
||||||
|
|
||||||
if not f then return nil,msg end
|
if not f then return nil,msg end
|
||||||
|
|
||||||
self.binaryData = f:read("*all")
|
self.binaryData = f:read("*all")
|
||||||
f:close()
|
f:close()
|
||||||
|
|
||||||
self.binarySavename = saveName
|
self.binarySavename = saveName
|
||||||
self:setContentType(contentType)
|
self:setContentType(contentType)
|
||||||
|
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user