I register for device.ondisconnect but it only seems to be called once the imp is powered back on. So i have my imp connected powered via USB port and everything works well. I then remove the power cable but i don’t see the event being called on the agent. When i power the board back on i see the disconnect event followed by the power one events.
When you pull the power cable, the imp doesn’t get a chance to tell the server that it’s going away. So the server may take a little while to notice that the imp isn’t there any more (it thinks the imp just hasn’t got anything to say to it). Once the server has noticed, which may take up to ten minutes, device.ondisconnect will get called.
The only way is by polling the device from the agent, and looking for a timely reply. The problem is that an idle TCP connection looks identical to an unplugged imp, that’s just the way TCP works.
Can you try powering the imp with a capacitor across the power leads (which will delay how long it takes to drain the power and shut down) and have a voltage compare watching for a drop in the power supply that will give time to send out your signal?