Best practice to report when device finishes work?

I’m following the example: https://electricimp.com/docs/gettingstarted/3-agents/

But, on the server, I want to report back to the original person who requested to change the LED when the work finishes. The flow is: user requests work from server/agent (?led=1), server/agent then requests device/imp to do some work, but then when device/imp finishes work I want confirmation to go back to the server/agent, then ultimately back to the original user. Mapping which users sent which requests may be more work than its worth right now, so for now I’m imagining just a single user.

// on device/imp function setLed(ledState) { led.write(ledState); server.log("Finished setting LED to: " + ledState); // server.onDeviceFinishedWork(ledState); }

We might be able to scope the problem a little more simply, but basically I’m just looking for an event/trigger on the agent side to do stuff once I know the device finished my work.

I’m looking through the APIs now but would love to hear how other people might have approached what would seem to be a very common problem.

Perhaps I’m misunderstanding or oversimplifying, but are you (at least first) simply looking for device --> agent communication? If so, you’d use the agent.send( ) / device.on( ) statements to handle this. (I.e. just the inverse of the device.send( ) / agent.on( ) pair you used to have the agent tell the device to control the LED). This is if you even wanted to have feedback from the device, which I wouldn’t think necessary.

I’m just talking about the device --> agent link, not the other item you mentioned of having feed back to the original web requesters.

The code in my oven project could be adapted to do this.

oven project documented in this thread

  1. Web app sends “get” request to agent.
  2. Agent handles this with http.onrequest and saves the response into a variable
    so that the onrequest function can complete and the response can be sent later.
  3. Agent sends request to Device ( at the end of onrequest function)
  4. Device responds to Agent.
  5. Agent uses the response saved previously to report back to the sender.

I started with code written by beardedinventor of electric imp.

<ahref=“http://forums.electricimp.com/discussion/comment/9022#Comment_9022”>WEB–>AGENT–>DEVICE–>AGENT–>WEB COMMUNICATION?

and went on from there. There is a lot to the code unfortunately even though the concept is not terribly complicated. The code presently does not have a way to track an individual user’s request and response but I think that could be added.