Device.onconnect

I saw an entry on this from back in april. I just tried to implement it, with the same result of no result.

device.onconnect is not firing after the device has started and connected.

i declare the callback at teh beginning fo teh agent script. then register the listener right after.

Can you show us some code? I tried with the following code, and it appears to be working (not that it won’t work the first time you run it since it needs to download the code first):

Agent code:
device.onconnect(function() { server.log("Device came online"); });

Logs:
Fri Jun 14 2013 09:42:08 GMT-0700 (PDT): Device came online
Fri Jun 14 2013 09:42:08 GMT-0700 (PDT): Device booting
Fri Jun 14 2013 09:42:09 GMT-0700 (PDT): Device configured to be “HTTP In Example”

I had

`device.onconnect(“deviceConnected”);

function deviceConnected()
{
server.log …
}`

But that doesn’t trigger in the agent script.

Try removing the quotes from the onconnect parameter:

`device.onconnect(deviceConnected);

function deviceConnected()
{
server.log …
}`

yep that did it.

And then it stopped working.

Any more details on “it stopped working”? You can register an ondisconnect handler too, to see both sides.

I don’t see any activity either, using the following code…

`device.onconnect(function() {
server.log(“Device came online”);
});

device.ondisconnect(function() {
server.log(“Device went offline”);
});`

My agent is apparently running 9ed008c - jenkins-ei-release-branch-675 - Fri Jun 21 10:25:11 2013

Toby

Update…

The “Device came online” and “Device went offline” messages do come through when inserting the Imp into its socket.

Oddly “Device went offline” doesn’t come through until re-inserting the Imp into the socket, at which point both messages come together.

Toby

If the imp has only been offline only a short time, the first the server hears about it might be when it sees the same imp reconnect – which means the first connection can’t any longer be valid.

When one end of a TCP connection just vanishes without sending a FIN (i.e., when the imp gets unplugged), and no data is flowing at the time, the peer TCP won’t notice anything until it next misses a keep-alive, which could be half an hour later. The imp server sends ping messages from time to time, which will detect a missing imp, but even that is only at 10-minute intervals.

Peter

Well all I want to establish is that the agent knows, when the device is actually online (for the first time) so that I can start the agent activities. In my case, the device has to wait till a sensor is ready. Unfortunately, if the sensor is ready before the network is there, the sensor ready call is missed by the agent, therefore I need to wait for the network to be established.

The recommended code above worked, then stopped working by the event callback not firing anymore.

Are you sure the event callback isn’t firing anymore?

What if you unplug the Imp from the socket and reinsert it?

Hitting ‘Run’ on the IDE does not appear to drop and re-establish a connection, the only way I could get the event to fire was by removing the Imp from its socket and reinserting it. I suppose a power cycle of the Imp carrier would achieve the same.

Toby

Yes, but i am not looking for a re-insert event, but for the connect event. If that doesn’t exist in that form, then I won’t have a solution.
Unless I do a by-foot handover setting via agent or so.

If you’re looking, at agent start, to determine whether an imp is already connected, the correct function to use is device.isconnected()

One way to test the firing of the onconnect and ondisconnect functions I’ve discovered is to call server.sleepfor(N).

Toby

@Hugo I believe there is a bug with device.isconnected(). It always returns false, regardless of the imp’s connectivity to the agent

I can replicate that. Seems to be a regression. Filed bug.

Any pointers on when this fix will be available?

Still seems to be giving the wrong connected/offline information in some situations, noticing especially after device starts after code change.

Toby

The fix is pushed - device.isconnected() should be working fine for you now.

Ahh, now I understand…