Device.onconnect

Thankyou for adding the device.onconnect and device.ondisconnect functions.

I was expecting to see a device.onconnect when my new agent, or device code starts, but I don’t - Am I missing something here? Or is it assumed that when an agent and device start they are connected?

Toby

I’ve been playing around with device.onconnect as I need to send the host a message when a device comes online or off. These are my observations. Maybe I am not doing something correctly, here goes…

In the Agent:

device.onconnect(function() {
server.log(“Unknown imp just came online”); // this works fine …
server.log(format(“This imp just came online: %s”,hardware.getimpeeid()); // No joy as hardware object is not available to the agent.
device.send(“getid”,""); // No response as the device is probably not initialized yet.
});

So what I wound up doing is to add the following in the Device right after the imp.configure so it only gets fired at initialization: agent.send(“idup”,hardware.getimpeeid()); // Send my ID to the Agent

In the Agent, I added the following:

device.on(“idup”, function(deviceid) {
server.log(format(“This imp just came online: %s”, deviceid));
local d = date(time()-(86060));
server.log(format(“Online at: %s”, d.hour +":" +d.min + “:” + d.sec));
local myagenturl = http.agenturl();
local curtime = d.hour +":" +d.min + “:” + d.sec;
sendtohost(myagenturl,curtime,deviceid) // my function to send an HTTP.post
});

Ideally, the device.onconnect would pass the impeeid, but otherwise the function is of not much use as you can see from my results. Maybe I am missing something also.
Hope that helps.

Each impee gets its own instance of the agent. So if the agent stores the impeeid from the first idup message, all subsequent onconnect events to that agent instance will be from the same impee.

Peter

This is what I see:

When the device starts from hitting ‘Run’ in the IDE…

Sat Mar 23 2013 12:12:25 GMT+0000 (GMT): Power state: offline=>online
Sat Mar 23 2013 12:12:25 GMT+0000 (GMT): Downloading new code
Sat Mar 23 2013 12:12:25 GMT+0000 (GMT): Agent started
Sat Mar 23 2013 12:12:25 GMT+0000 (GMT): Agent’s URL is: http://agent.electricimp.com/qy9PhVuePe1Z
Sat Mar 23 2013 12:12:28 GMT+0000 (GMT): This Imp has the unique ID 0c2a690000bc
Sat Mar 23 2013 12:12:28 GMT+0000 (GMT): Connected to 001ff3c40db4
Sat Mar 23 2013 12:12:28 GMT+0000 (GMT): Free memory : 40908 bytes
Sat Mar 23 2013 12:12:28 GMT+0000 (GMT): Imp is idle, going to sleep!
Sat Mar 23 2013 12:12:28 GMT+0000 (GMT): sleeping until 1364040809000
Sat Mar 23 2013 12:12:28 GMT+0000 (GMT): Power state: online=>asleep
Sat Mar 23 2013 12:12:28 GMT+0000 (GMT): Imp disconnected

When the Imp wakes up…

Sat Mar 23 2013 12:13:35 GMT+0000 (GMT): Imp connected
Sat Mar 23 2013 12:13:35 GMT+0000 (GMT): Power state: asleep=>online
Sat Mar 23 2013 12:13:35 GMT+0000 (GMT): Device booting
Sat Mar 23 2013 12:13:36 GMT+0000 (GMT): This Imp has the unique ID 0c2a690000bc
Sat Mar 23 2013 12:13:36 GMT+0000 (GMT): Connected to 001ff3c40db4
Sat Mar 23 2013 12:13:36 GMT+0000 (GMT): Free memory : 39884 bytes
Sat Mar 23 2013 12:13:36 GMT+0000 (GMT): Imp is idle, going to sleep!
Sat Mar 23 2013 12:13:36 GMT+0000 (GMT): sleeping until 1364040879000
Sat Mar 23 2013 12:13:36 GMT+0000 (GMT): Power state: online=>asleep
Sat Mar 23 2013 12:13:36 GMT+0000 (GMT): Imp disconnected

So I correctly get the connected and disconnected calls when the Imp wakes and sleeps respectively, but not on the initial run. Is this the expected behaviour?

I’d like to be able to set a flag in my agent that lets me know when the Imp device is available, but unless I initialise it as true assuming that when the agent starts the device will also start I’m stuck.

Toby

When I unplug an SD Card imp from the socket, the device.ondisconnect() does not fire. After about 10 mins, I get Power state: online=>offline on the Planner, but still no device.ondisconnect(). Is this correct behavior?

I’ve filed a feature request to send the current connection state to the agent when it registers a handler for that event, so:

  • if imp is connected when agent registers an onconnected() handler, this will fire when events are next processed.
  • if imp is disconnected when agent registers an ondisconnect() handler, this will fire when events are next processed.

Does that work?

I think I understand what you are requesting, but let me rephrase it to make sure. Once an agent registers either of these handlers, then any future connections and disconnections will cause the handler to get called when any event occurs. If that is correct, then, yes that should work. In the case of the disconnection, that might not be right away, but when the event that displays Power state: online=>offline fires, the handler would get called -possibly 10 minutes later if no other events occur sooner? Would that be the worst case?

I’m saying that if you register onconnect, and the imp is connected, you would immediately get an onconnect callback (well, next time the event handlers run). This solves Toby’s issue I believe. Ditto for ondisconnect.

When you see an online->offline event, your ondisconnect handler would be called immediately if it was registered at that point.

Would it make more sense to have a device.isconnected() that returned a bool? That would let an agent, in its startup, distinguish an imp that’s still connected, from one that has just connected…

Peter

First of all, thank you for taking the time to respond to this.
As a work around (and maybe a permanent fix) to the pending issue, I implemented the solution that Hugo proposed on another thread where the Agent pings the imp every 5 seconds and clears a watchdog flag when I get a reply - meaning the imp is alive. I send our host an update with a time stamp every time I detect that the imp goes online and offline based on the ping so I can record outages and tell the host that it may not get scheduled data samples that the imp is collecting in the background because the imp is currently down.
My gut feeling at this point is that the ping approach is a simple and more reliable solution than the onconnect/disconnect and the price of reliability is the ping exchange overhead every 5 seconds. So from my perspective, I am not depending on a fix to these methods as a priority.
Thanks again for your suggestions and help.

Either Hugo’s or Peter’s suggestion provides what I need - Thanks guys.

Toby

Did either of these solutions end up getting implemented yet please?

device.isconnected() doesn’t seem to exist, but I’m not certain whether Hugo’s suggestion has been implemented or not.

Toby

I believe device.isconnected() is in there, it’s just the server has not had an update recently - so it’s coming.