From 59ffdc052dfe2316169529809aee8cbdcc80cfbd Mon Sep 17 00:00:00 2001
From: Sami Spets <savasp@utu.fi>
Date: Tue, 22 Oct 2019 12:34:53 +0300
Subject: [PATCH] stream apps components redirect to stream

---
 web-service/server/public/css/index.css | 16 +++++++-
 web-service/server/public/js/index.js   | 52 ++++++++++++++++---------
 web-service/server/src/index.js         | 36 +++++++----------
 3 files changed, 61 insertions(+), 43 deletions(-)

diff --git a/web-service/server/public/css/index.css b/web-service/server/public/css/index.css
index 63c546a3a..fd1cbba53 100644
--- a/web-service/server/public/css/index.css
+++ b/web-service/server/public/css/index.css
@@ -35,6 +35,18 @@ body {
     display: inline-block;
    }
 
-  img {
-    padding: 25px;  
+  .ftlab-card-component {
+    width: 150px;
+    height: auto;
+    margin: auto;
+    text-align: center;
+    border: 1px solid black;
+  }
+
+  .thumbnail-img {
+    width: 150px;
+  }
+
+  #container {
+    display: flex;
   }
\ No newline at end of file
diff --git a/web-service/server/public/js/index.js b/web-service/server/public/js/index.js
index 77f275e42..12b529f17 100644
--- a/web-service/server/public/js/index.js
+++ b/web-service/server/public/js/index.js
@@ -1,24 +1,24 @@
 const checkIfLoggedIn = async () => {
-    const token = window.localStorage.getItem('token')
-    console.log(token)
-    if(!token){
-        console.log("You need to login")
-        renderLogin()
-    }else{
+//     const token = window.localStorage.getItem('token')
+//     console.log(token)
+//     if(!token){
+//         console.log("You need to login")
+//         renderLogin()
+//     }else{
 
-        //Check if the token is valid
-        const response = await fetch('http://localhost:8080/auth/validation', {
-            method: 'POST',
-            headers: {'Authorization': token}
-        })
-        console.log('RESPONSE', response)
+//         //Check if the token is valid
+//         const response = await fetch('http://localhost:8080/auth/validation', {
+//             method: 'POST',
+//             headers: {'Authorization': token}
+//         })
+//         console.log('RESPONSE', response)
         
-        //Token is valid, show available streams
-        if(response.status === 200){
-            console.log("SUCCESS")
+//         //Token is valid, show available streams
+//         if(response.status === 200){
+//             console.log("SUCCESS")
             renderThumbnails()
-        }
-    }
+    //     }
+    // }
 }
 
 //Redirects the user to google authentication
@@ -55,6 +55,7 @@ const renderThumbnails = async () => {
     console.log(containerDiv)
     for(var i=0; i<thumbnails.length; i++){
         const encodedURI = encodeURIComponent(thumbnails[i])
+        console.log("THUMBNAIL[i]", thumbnails[i])
         try{
             const someData = await fetch(`http://localhost:8080/stream/rgb?uri=${encodedURI}`)
             console.log('SOME DATA', someData)
@@ -64,10 +65,10 @@ const renderThumbnails = async () => {
             const myBlob = await someData.blob();
             console.log('BLOB', myBlob)
             const objectURL = URL.createObjectURL(myBlob);
-            containerDiv.innerHTML += `<img src='${objectURL}' alt="Hups" width='500px'></img>`
+            containerDiv.innerHTML += createCard(objectURL, i+4, encodedURI)
         }catch(err){
             console.log("Couldn't create thumbnail");
-            return 
+            console.log(err) 
         }
     }
 }
@@ -97,4 +98,17 @@ const renderLogin = () => {
                 </div>
             </a>
         </div>`
+}
+
+const createCard = (url, viewers, uri) => {
+    return `<div class='ftlab-card-component' >
+                <img src='${url}' class="thumbnail-img" alt="Hups" width='500px'></img>
+                <p>Viewers: ${viewers}</p>
+                <button onclick="window.location.href='/stream/${uri}'">button</button>
+            </div>`
+}
+
+const cardLogic = () => {
+    const cards = document.getElementsByClassName('ftlab-card-component');
+    console.log("CARDS", cards)
 }
\ No newline at end of file
diff --git a/web-service/server/src/index.js b/web-service/server/src/index.js
index 0c99b14e6..cf8c88e10 100644
--- a/web-service/server/src/index.js
+++ b/web-service/server/src/index.js
@@ -12,11 +12,13 @@ const User = require('./models/users')
 const Configs = require('./models/generic')
 const Disparity = require('./models/disparity')
 const bodyParser = require('body-parser')
+const cors = require('cors')
 
 // ---- INDEXES ----------------------------------------------------------------
 app.use(passport.initialize());
 app.use(express.static(__dirname + './../public'));
 app.use(bodyParser.json())
+//app.use(cors())
 
 
 passport.serializeUser((user, done) => {
@@ -27,13 +29,13 @@ passport.deserializeUser((userDataFromCookie, done) => {
     done(null, userDataFromCookie);
 })
 
-mongoose.connect(config.MONGODB_URI, { useNewUrlParser: true, useUnifiedTopology: true })
-	.then(() => {
-		console.log('Connected to MongoDB');
-	})
-	.catch((err) => {
-		console.log(err);
-	})
+// mongoose.connect(config.MONGODB_URI, { useNewUrlParser: true, useUnifiedTopology: true })
+// 	.then(() => {
+// 		console.log('Connected to MongoDB');
+// 	})
+// 	.catch((err) => {
+// 		console.log(err);
+// 	})
 
 let peer_by_id = {};
 //let uri_to_peer = {};
@@ -132,8 +134,8 @@ RGBDStream.prototype.subscribe = function() {
 RGBDStream.prototype.pushFrames = function(latency, spacket, packet) {
 	//Checks that the type is jpg
 	if (packet[0] === 0){
-		if (spacket[1] & 0x1) this.depth = packet[4];
-		else this.rgb = packet[4];
+		if (spacket[3] > 0) this.depth = packet[5];
+		else this.rgb = packet[5];
 	}
 
 	console.log("Frame = ", packet[0], packet[1]);
@@ -185,15 +187,15 @@ app.get('/streams', (req, res) => {
 /**
  * A list that has Object.keys(uri_data) values and also the image that is 
  * binded to that 
- * Joku lista missä on Object.keys(uri_datan) arvot ja niihin bindattu
- * se
  */
 app.get('/stream/rgb', (req, res) => {
 	let uri = req.query.uri;
+	console.log("URI", uri)
 	if (uri_data.hasOwnProperty(uri)) {
 		uri_data[uri].peer.send("get_stream", uri, 10, 9, [Peer.uuid], uri);
 		res.writeHead(200, {'Content-Type': 'image/jpeg'});
-    	res.end(uri_data[uri].rgb);
+		console.log("URIDATA", uri_data[uri].rgb);
+		res.end(uri_data[uri].rgb);
 	}
 	res.end();
 });
@@ -307,18 +309,8 @@ app.get('/stream/config', async(req, res) => {
 	// 	save the get the first depth
 	// 	save the value with default values into the first depth
 	// }
-	
-	console.log(depth)
-	console.log(wholeURI)
-	console.log(uri)
-	console.log(depth)
-
-	return res.send(200)
 })
 
-const uriParser = (uri) => {
-
-}
 
 app.get('/stream', (req, res) => {
 	let uri = req.query.uri;
-- 
GitLab