Hello,
I am developing an app that requires multiple network requests. I will explain one portion of my code that I facing trouble with.
I send a network.request of type “post”, that sends a message to the gateway. This request utilizes a network listener.
It does return control to where the request was called from. But right after the network.request I should fire another network.download request of type “Get”, that downloads the response from the gateway. If I have these in two consecutive lines in code, the second request is fired before the first or they are fired simultaneously, which does not serve my purpose. So what I did is I have a network download request nested in network listener of my first network.request.
Also, the network download request has another network listener and Since I couldn’t find a way to control later events in the code I have most of my function calls from network listeners.
Is there a way to avoid this? Please let me know if you have any questions or are unclear about anything. Thank you.
example:
local function parseForTACK()
--I parse the file that was downloaded as a network response to the download request and see if I have the desired result or not.
–If i have the desired result i make the corresponding traceBackTime.rxed[n] = 1 and otherwise traceBackTime.rxed[n] = 0
–This will leave me a table that i will use in TACKNetworkListener() to determine which device needs a message to be resent through http request. – (basically means to repeat the entire process. I’m currently handling this by rewriting all the functions as i’m unable to reuse the code because of the code structure.)
end
local function TACKNetworkListener(event)
if(event.isError) then
print ("Network error: ", event.response)
elseif(event.phase == “began”) then
print(“Progress Phase: Began”)
elseif( event.phase == “ended”) then
print("length of traceBackTime.MacString: ",table.getn(traceBackTime.MacString))
timer.performWithDelay(200,parseForTACK())
TACKNL = 1
for n = 1, table.getn(traceBackTime.MacString) do
if(traceBackTime.rxed[n] == 1) then
elseif(traceBackTime.rxed[n] == 0) then
--ERRORHANDLE
print("Src Error: ", traceBackTime.MacString[n], "\n mac addr: ", MacAddr[n])
local headers = {}
headers[“Content-Type”] = “application/xml”
headers[“Accept-Language”] = “en-US”
local body = [[ <rci_request version=“1.1”>
<do_command target=“xbgw”>
<send_serial addr="]]…MacAddr[n]…[["
encoding=“utf-8”>T]]…timeValue…"$"…
[[</send_serial>
</do_command>
</rci_request>]]
local params = {}
params.headers = headers
params.body = body
network.request(“http://”…ipAddress…":8000", “POST”, timeNetworkListenerTwo, params)
end
end
end
end
function datastream(flag)
local headers = {}
headers[“Content-Type”] = “application/xml”
headers[“authorization”] = “Basic bWFpZWVlQHJpdC5lZHU6QWNjM3NzVDNjaCE=”
headers[“Accept-Language”] = “en-US”
if(flag ==2) then
network.download(“http://”…ipAddress…":8000", “GET”, TACKNetworkListener, headers, “datastreamtime.xml”, system.DocumentsDirectory)
end
end
function NetworkListener(event)
if(event.isError) then
print ("Network error: ", event.response)
elseif(event.phase == “began”) then
print(“timeNetworkListener: Began”)
elseif(event.phase == “ended”) then
print(“Time NetworkListener”)
TSent = TSent +1
print("TSent value: ", TSent, “len of MAc:”,table.getn(MacAddr) )
if(TSent == table.getn(MacAddr)) then
timer.performWithDelay(100, datastream(2))
end
end
end
function textListener(event)
if ( event.phase == “ended” or event.phase == “submitted” ) then
local params = {}
params.headers = headers
params.body = body
network.request(“http://”…ipAddress…":8000", “POST”, NetworkListener, params)
end
end
inputText:addEventListener( “userInput”, textListener )