Updated the algorythm to detect and keep devices online:

  1. Insert new devices in the Database
  2. Update old devices that are in $devices - set their online_last to current time
  3. Delete devices that expired - their online_last is to old

To make this work properly a new field in who_online table was added - online_last. It keeps the last time the device was seen.