server: refactor device handling

Split device handling logic into dedicated class.