New backend code and server API

This pull request contains the refactoring I've been working on. No new features, but extensive refactoring of the database and server code, and a redesigned server API now that we know what we need to expose a little better.

The old demo (register a device, report it lost, see it being tracked) should work. Comments welcome.