Wondering if there is a way to do this?
I need to get the current value of a device connected to my imp via a web request and send the value back to the web in response from original request.
I can’t seem to figure it out, so for now, I am mimicking some device states on the agent, so it can respond back
to the web in the response to the http.onrequest();
Web sends JSON to agent
agent needs to get values from device and report them back to web in response from original http request.
agent and device seem to communicate asynchronously via device.send() and agent.send() with handlers to get responses, so it’s not like I can call directly to the device and get a value response inline in agent code.
Only thing I can think of is like the following, or am I missing something?
Thanks,
Scott
`
AGENT
commStat <-0;
curTemp <- 0;
timeOut >- 0;
device.on(“TempSent”, function(v) {
comStat = 1;
curTemp = v;
});
http.onrequest(request,res)
comStat = 0;
timeOut = 0;
device.send(“getTemp”,http.jsondecode(request.body));
wait for (comStat ==1 || timeOut)
if (!timeOut) {
local data = {“curTemp”:curTemp}
local message = http.jsonencode(data);
res.send(200, message);
}
else {
res.send(500, “Device timeout”);
}
DEVICE
agent.on(“getTemp”, function(value)){
agent.send(“TempSent”, getTemp(););
}
`