diff --git a/web-service/src/index.js b/web-service/src/index.js index 3db928b77ab05784ddb9d6af1b34deda39101d5e..6a67fce8bf570c90ccee5ac617d662e4a2eac6c3 100644 --- a/web-service/src/index.js +++ b/web-service/src/index.js @@ -6,15 +6,41 @@ const Peer = require('./peer.js'); // ---- INDEXES ---------------------------------------------------------------- let peer_by_id = {}; -let uri_to_peer = {}; +//let uri_to_peer = {}; let peer_uris = {}; +let uri_data = {}; + // ---- PROTOCOL --------------------------------------------------------------- app.get('/', (req, res) => { res.end(); }); +app.get('/streams', (req, res) => { + res.json(Object.keys(uri_data)); +}); + +app.get('/stream/rgb', (req, res) => { + let uri = req.query.uri; + if (uri_data.hasOwnProperty(uri)) { + res.writeHead(200, {'Content-Type': 'image/jpeg'}); + res.end(uri_data[uri].rgb); + } + res.end(); +}); + +app.get('/stream/depth', (req, res) => { + let uri = req.query.uri; + if (uri_data.hasOwnProperty(uri)) { + res.writeHead(200, {'Content-Type': 'image/png'}); + res.end(uri_data[uri].depth); + } + res.end(); +}); + +//app.get('/stream', (req, res)) + app.ws('/', (ws, req) => { console.log("New web socket request"); @@ -36,8 +62,16 @@ app.ws('/', (ws, req) => { peer.rpc("list_streams", (streams) => { console.log("STREAMS", streams); for (let i=0; i<streams.length; i++) { - uri_to_peer[streams[i]] = peer; + //uri_to_peer[streams[i]] = peer; peer_uris[peer.string_id].push(streams[i]); + + uri_data[streams[i]] = { + peer: peer, + title: "", + rgb: null, + depth: null, + pose: null + }; } }); } @@ -52,7 +86,8 @@ app.ws('/', (ws, req) => { if (puris) { for (let i=0; i<puris.length; i++) { console.log("Removing stream: ", puris[i]); - delete uri_to_peer[puris[i]]; + //delete uri_to_peer[puris[i]]; + delete uri_data[puris[i]]; } delete peer_uris[peer.string_id]; } @@ -60,11 +95,11 @@ app.ws('/', (ws, req) => { }); p.bind("list_streams", () => { - return Object.keys(uri_to_peer); + return Object.keys(uri_data); }); p.bind("find_stream", (uri) => { - if (uri_to_peer.hasOwnProperty(uri)) { + if (uri_data.hasOwnProperty(uri)) { return [Peer.uuid]; } else { return null; // or []?? @@ -72,7 +107,7 @@ app.ws('/', (ws, req) => { }); p.proxy("source_calibration", (cb, uri) => { - let peer = uri_to_peer[uri]; + let peer = uri_data[uri].peer; if (peer) { peer.rpc("source_calibration", cb, uri); } @@ -80,16 +115,19 @@ app.ws('/', (ws, req) => { p.bind("set_pose", (uri, vec) => { //console.log("SET POSE"); - let peer = uri_to_peer[uri]; + let peer = uri_data[uri].peer; if (peer) { + uri_data[uri].pose = vec; peer.send("set_pose", uri, vec); } }); p.bind("get_stream", (uri, N, rate, pid, dest) => { - let peer = uri_to_peer[uri]; + let peer = uri_data[uri].peer; if (peer) { peer.bind(uri, (rgb, depth) => { + uri_data[uri].rgb = rgb; + uri_data[uri].depth = depth; p.send(uri, rgb, depth); }); peer.send("get_stream", uri, N, rate, [Peer.uuid], dest);