0
0
mirror of https://github.com/Doodle3D/doodle3d-firmware.git synced 2024-12-22 19:13:49 +01:00

First attempt to add a post response function queue

This commit is contained in:
peteruithoven 2013-09-02 15:01:16 +02:00
parent a732e4890f
commit 3c59322cf7
4 changed files with 113 additions and 66 deletions

View File

@ -183,8 +183,16 @@ 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

View File

@ -292,7 +292,7 @@ function M.associateSsid(ssid, passphrase, recreate)
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

View File

@ -92,6 +92,10 @@ 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")
@ -101,14 +105,30 @@ function M.associate_POST(request, response)
return return
end end
local rv,msg = netconf.associateSsid(argSsid, argPhrase, argRecreate) local associate = function()
local rv,msg = netconf.associateSsid(argSsid, argPhrase, argRecreate)
end
--response:addPostResponseFunction(associate)
response:addData("ssid", argSsid) 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)

View File

@ -24,12 +24,15 @@ 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
@ -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