From d7932fbde58932e68a798bf97d9e5766fc77f91c Mon Sep 17 00:00:00 2001
From: Sami Spets <savasp@utu.fi>
Date: Thu, 10 Oct 2019 13:18:13 +0300
Subject: [PATCH] started to modify GET /stream/config route

---
 web-service/server/public/js/index.js    | 64 ++++++++++++++----------
 web-service/server/src/index.js          | 62 ++++++++++++++++-------
 web-service/server/src/models/generic.js | 53 ++++++++++++++++++++
 3 files changed, 135 insertions(+), 44 deletions(-)
 create mode 100644 web-service/server/src/models/generic.js

diff --git a/web-service/server/public/js/index.js b/web-service/server/public/js/index.js
index f0d647bef..d9cc5f94b 100644
--- a/web-service/server/public/js/index.js
+++ b/web-service/server/public/js/index.js
@@ -3,28 +3,7 @@ const checkIfLoggedIn = async () => {
     console.log(token)
     if(!token){
         console.log("You need to login")
-        const containerDiv = document.getElementById('container');
-        containerDiv.innerHTML = 
-        `<div id='Login'>
-            <h2>Welcome to Future Technology Lab</h2>
-            <h3>Please login!</h3>
-            <a className="button" onClick="handleLogin()">
-                <div>
-                    <span class="svgIcon t-popup-svg">
-                        <svg class="svgIcon-use" width="25" height="37" viewBox="0 0 25 25">
-                            <g fill="none" fill-rule="evenodd">
-                            <path d="M20.66 12.693c0-.603-.054-1.182-.155-1.738H12.5v3.287h4.575a3.91 3.91 0 0 1-1.697 2.566v2.133h2.747c1.608-1.48 2.535-3.65 2.535-6.24z" fill="#4285F4"/>
-                            <path d="M12.5 21c2.295 0 4.22-.76 5.625-2.06l-2.747-2.132c-.76.51-1.734.81-2.878.81-2.214 0-4.088-1.494-4.756-3.503h-2.84v2.202A8.498 8.498 0 0 0 12.5 21z" fill="#34A853"/>
-                            <path d="M7.744 14.115c-.17-.51-.267-1.055-.267-1.615s.097-1.105.267-1.615V8.683h-2.84A8.488 8.488 0 0 0 4 12.5c0 1.372.328 2.67.904 3.817l2.84-2.202z" fill="#FBBC05"/>
-                            <path d="M12.5 7.38c1.248 0 2.368.43 3.25 1.272l2.437-2.438C16.715 4.842 14.79 4 12.5 4a8.497 8.497 0 0 0-7.596 4.683l2.84 2.202c.668-2.01 2.542-3.504 4.756-3.504z" fill="#EA4335"/>
-                            </g>
-                        </svg>
-                    </span>
-                    <span class="button-label">Sign in with Google</span>
-                </div>
-            </a>
-        </div>`
-    //User has a token saved in the browser
+        renderLogin()
     }else{
 
         //Check if the token is valid
@@ -34,12 +13,9 @@ const checkIfLoggedIn = async () => {
         })
         console.log('RESPONSE', response)
         
-        //Token is valid, show the streams
+        //Token is valid, show available streams
         if(response.status === 200){
             console.log("SUCCESS")
-            /*
-            Most likely it will render a new HTML file
-            */
             renderThumbnails()
         }
     }
@@ -59,10 +35,17 @@ const getAvailableStreams = async () => {
     return streams;
 }
 
+const videoPlayer = () => {
+    const containerDiv = document.getElementById('container');
+    const asd = 'yeahboiii'
+    window.open(`http://localhost:8080/stream?uri=${asd}`)   
+}
 
 
 
-//Creates thumbnail (image) for all available streams and adds them to div class='container'
+/**
+ * Creates thumbnail (image) for all available streams and adds them to div class='container'
+ */
 const renderThumbnails = async () => {
     // const thumbnails = getAvailableStreams();
     // console.log('THUMBNAILS', thumbnails)
@@ -86,4 +69,31 @@ const renderThumbnails = async () => {
         //     return 
         // }
     }
+}
+
+/**
+ * Renders button that will redirect to google login
+ */
+const renderLogin = () => {
+    const containerDiv = document.getElementById('container');
+        containerDiv.innerHTML = 
+        `<div id='Login'>
+            <h2>Welcome to Future Technology Lab</h2>
+            <h3>Please login!</h3>
+            <a className="button" onClick="handleLogin()">
+                <div>
+                    <span class="svgIcon t-popup-svg">
+                        <svg class="svgIcon-use" width="25" height="37" viewBox="0 0 25 25">
+                            <g fill="none" fill-rule="evenodd">
+                            <path d="M20.66 12.693c0-.603-.054-1.182-.155-1.738H12.5v3.287h4.575a3.91 3.91 0 0 1-1.697 2.566v2.133h2.747c1.608-1.48 2.535-3.65 2.535-6.24z" fill="#4285F4"/>
+                            <path d="M12.5 21c2.295 0 4.22-.76 5.625-2.06l-2.747-2.132c-.76.51-1.734.81-2.878.81-2.214 0-4.088-1.494-4.756-3.503h-2.84v2.202A8.498 8.498 0 0 0 12.5 21z" fill="#34A853"/>
+                            <path d="M7.744 14.115c-.17-.51-.267-1.055-.267-1.615s.097-1.105.267-1.615V8.683h-2.84A8.488 8.488 0 0 0 4 12.5c0 1.372.328 2.67.904 3.817l2.84-2.202z" fill="#FBBC05"/>
+                            <path d="M12.5 7.38c1.248 0 2.368.43 3.25 1.272l2.437-2.438C16.715 4.842 14.79 4 12.5 4a8.497 8.497 0 0 0-7.596 4.683l2.84 2.202c.668-2.01 2.542-3.504 4.756-3.504z" fill="#EA4335"/>
+                            </g>
+                        </svg>
+                    </span>
+                    <span class="button-label">Sign in with Google</span>
+                </div>
+            </a>
+        </div>`
 }
\ No newline at end of file
diff --git a/web-service/server/src/index.js b/web-service/server/src/index.js
index 2850542e0..e9dd0ccca 100644
--- a/web-service/server/src/index.js
+++ b/web-service/server/src/index.js
@@ -9,7 +9,7 @@ const keys = require('./passport/keys')
 const mongoose = require('mongoose')
 const config = require('./utils/config')
 const User = require('./models/users')
-const Config = require('./models/configs')
+const Configs = require('./models/generic')
 const Disparity = require('./models/disparity')
 const bodyParser = require('body-parser')
 
@@ -227,24 +227,49 @@ app.post('/stream/config', async (req, res) => {
 })
 
 app.get('/stream/config', async(req, res) => {
-	//example of uri ftl.utu.fi/stream/config?uri=ftl://utu.fi/stream/calibrations/
-	//example of uri /stream/config?uri=ftl://utu.fi/stream/calibrations/board_size/value=1
-	const wholeURI = req.query.urinote
-	const uri = wholeURI.substring(20)
+	//example of uri ftlab.utu.fi/stream/config?uri=ftl://utu.fi/stream/configurations/calibrations/
+	//example of uri ftlab.utu.fi/stream/config?uri=ftl://utu.fi/stream/configurations/calibrations/board_size/value=1
+	
+	const wholeURI = encodeURIComponent(req.query.uri)
+
+	// await new Configs({
+	// 	URI: wholeURI,
+	// 	data: {
+	// 		default: {
+	// 			board_size: [9,6],
+	// 			square_size: 1
+	// 		},
+	// 		perfect: {
+	// 			board_size: [4,2],
+	// 			square_size: 42
+	// 		}
+	// 	}
+	// }).save()
+
+	const response = await Configs.find({ URI : wholeURI});
+	if(response.length){
+		return res.status(200).json(response[0].data);
+	}
+
+	const uri = wholeURI.substring(35)
 	const depth = uri.split("/");
+	console.log(depth)
+	if(depth[depth.length-1].length == 0){
+		depth.pop;
+	}
+	
+
+
 	if(depth.length === 1){
-		let data
-		switch(depth[0]){
-			case 'calibrations':
-				data = await Config.find({});
-				return res.status(200).json(data);
-			case 'disparity':
-				data = await Disparity.find({});
-				return res.status(200).json(data);
-			default:
-				return res.status(500).json('Error');
-		}
+		let data = await Configs.find({ URI : wholeURI});
+		return res.status(200).json(data);
 	}else if(depth.length === 2){
+		const data = await Configs.find({ URI: wholeURI })
+		return res.status(200).json(data)
+	}else if(depth.length === 3){
+		return res.status(200).json('DEPTH 3')
+	}else{
+		return res.status(500).json('ERROR');
 	}
 
 	console.log(wholeURI)
@@ -269,7 +294,10 @@ app.get('/stream/config', async(req, res) => {
 	}
 })
 
-//app.get('/stream', (req, res))
+app.get('/stream', (req, res) => {
+	let uri = req.query.uri;
+	console.log(uri)
+})
 
 /*
  * Route for Google authentication API page
diff --git a/web-service/server/src/models/generic.js b/web-service/server/src/models/generic.js
new file mode 100644
index 000000000..efb4db9af
--- /dev/null
+++ b/web-service/server/src/models/generic.js
@@ -0,0 +1,53 @@
+/**
+ * This is the generic model for the MongoDB
+ * 
+ * Single collection contains the following values
+ * URI: {type: String, not null}
+ * data: Object
+ *      the actual data
+ * 
+ * 
+ * e.g
+ * 
+ * URI: 'ftl://utu.fi/stream/configurations/calibrations
+ * data: {
+ *      default: {
+ *              board_size: [9,7]
+ *              square_size: 1
+ *      }
+ * 
+ *      HD_quality: {
+ *              board_size: [5,2]
+ *              square_size: 5
+ *      }
+ * }
+ * 
+ * URI: 'ftl://utu.fi/stream/configurations/disparity/name/'
+ * data: {
+ *      default: {
+ *              name: 'default'
+ *      }
+ * 
+ *      HD_quality: {
+ *              name: 'HD_quality'
+ *      }
+ * 
+ * }
+ */
+
+
+const mongoose = require('mongoose')
+
+const configsSchema = mongoose.Schema({
+    URI: String,
+    data: Object
+  })
+
+  configsSchema.set('toJSON', {
+    transform: (document, returnedObject) => {
+      delete returnedObject._id
+      delete returnedObject.__v
+    }
+  })
+  
+module.exports = mongoose.model('configs', configsSchema)
\ No newline at end of file
-- 
GitLab