Hppt.onrequest()

When using http.onrequest() inside my Agent I would like to get a value from my Device and use this value to return with res.send(200,"my value = " + value )

I have tried to pass res using device.send(“request”,res) but this never gets to the agent.on(“request”, res )

Is it possible to get a value from a Device inside an onrequest()?

I second this request. It would be nice to have a sendsync command to send to the device that would then return whatever value(s) you are looking for. You could also include a device timeout so that the requesting service itself doesn’t time out.

It is possible to get a value from the device in onrequest, but the approach we’d suggest would be to cache the values inside the agent, and use a device.on() listener in the agent, and a agent.send() in the device to send value changes to the agent to cache.

That way, the design is much simpler in the http.onrequest cycle. The request handler just returns the value it caches. And when the device detects the value changes, it will send it to the agent to keep everything up to date.

Let me know if that approach works for your situation, and/or if you want an example, or still want to explore using a per request approach.

@aloharich Thank you for the advice, I have done this and it works for the demo I am producing.

I believe being able to interrogate the IMP only when required would save on battery power?

Yeah, saving power would be ideal. Here is an example of how you would go about getting per request info from the device. The key consideration to take note of is, you might get more requests between when you ask the device for data and when it returns. So your agent code would look like:
`
responses <- [];
http.onrequest(function(req, res) {
responses.push(res);
device.send(“ping”, 1);
});

device.on(“pong”, function(data) {
foreach(res in responses) {
res.send(200, data);
}
responses = [];
});
--- device code ---
agent.on(“ping”,function(msg){
agent.send(“pong”, “hello”);
});
`