diff --git a/modules/jukebox.js b/modules/jukebox.js index 6e908c1..95f8373 100644 --- a/modules/jukebox.js +++ b/modules/jukebox.js @@ -1,16 +1,15 @@ -// TODO: Properly integrate this -//https://gist.github.com/martinsik/2031681 - // Websocket Server const WebSocketServer = require('ws').Server; - // list of currently connected clients (users) var clients = { }; +// TODO: Any code in here will be limitted in functionality +var guests = { }; exports.setup = function(mstream, server, program){ + const wss = new WebSocketServer({ server: server }); // This callback function is called every time someone // tries to connect to the WebSocket server @@ -18,77 +17,98 @@ exports.setup = function(mstream, server, program){ // accept connection - you should check 'request.origin' to make sure that // client is connecting from your website - // var connection = request.accept(null, request.origin); console.log((new Date()) + ' Connection accepted.'); // Generate code and assure it doesn't exist - var code; - var n = 0; - while (true) { - code = Math.floor(Math.random()*90000) + 10000; - if(!(code in clients)){ - break; - } - if(n === 10){ - console.log('Failed to create ID for jukebox.'); - // FIXME: Close connection - return; - } - n++; + var code = createAccountNumber(10000); + var guestcode = createAccountNumber(10000); + + + // Handle code failures + if(code === false || guestcode === false){ + connection.send(JSON.stringify( { error: 'Failed To Create Instance'} )); + return; } - // Send Code - connection.send(JSON.stringify( { code: code} )); + + // Add code to clients object clients[code] = connection; + // Connect guest code to standard code + guests[guestcode] = code; + + + // Send Code + connection.send(JSON.stringify( { code: code, guestCode: guestcode} )); // user sent some message connection.on('message', function(message) { - if (message.type === 'utf8') { // accept only text - // Send client code back - connection.send(JSON.stringify( { code: code} )); - - // FIXME: Will need some work to add more commands - } + // Send client code back + connection.send(JSON.stringify( { code: code, guestCode: guestcode} )); }); // user disconnected connection.on('close', function(connection) { + // Remove client from array + delete guests[guestcode]; delete clients[code]; }); + }); + // Function for creating account numbers + function createAccountNumber(limit = 100000){ + // TODO: Check that limit is reasonably sized integer + + var n = 0; + while (true) { + code = Math.floor(Math.random() * (limit * 9)) + limit; + if(!(code in clients) && !(code in guests)){ + break; + } + if(n === 10){ + console.log('Failed to create ID for jukebox.'); + // FIXME: Try again with a larger number size + return false; + } + n++; + } + + return code; + } + // TODO: Get Album Art calls mstream.post( '/push-to-client', function(req, res){ // Get client id - console.log(req.body.json); const json = JSON.parse(req.body.json); - console.log(json); - console.log(json.code); - console.log(json.command); - // Check if client ID exists - const clientCode = json.code; + var clientCode = json.code; const command = json.command; - console.log(clientCode); - console.log(clientCode); - console.log(clientCode); - console.log(command); - if(!(clientCode in clients)){ + // + if(!(clientCode in clients) && !(clientCode in guests)){ res.status(500).json({ error: 'Client code not found' }); + return; } // TODO: Check if command logic makes sense + + if(clientCode in guests){ + // TODO: Check that command does not vioalt guest conditions + + clientCode = guests[clientCode]; + } + + // Push commands to client clients[clientCode].send(JSON.stringify({command:command})); diff --git a/public/img/x.svg b/public/img/x.svg new file mode 100644 index 0000000..29e90e9 --- /dev/null +++ b/public/img/x.svg @@ -0,0 +1,3 @@ + \ No newline at end of file diff --git a/public/js/aurora.js b/public/js/lib/aurora.js similarity index 100% rename from public/js/aurora.js rename to public/js/lib/aurora.js diff --git a/public/js/cookie.js b/public/js/lib/cookie.js old mode 100755 new mode 100644 similarity index 100% rename from public/js/cookie.js rename to public/js/lib/cookie.js diff --git a/public/js/flac.js b/public/js/lib/flac.js similarity index 100% rename from public/js/flac.js rename to public/js/lib/flac.js diff --git a/public/js/howler.core.min.js b/public/js/lib/howler.core.min.js similarity index 100% rename from public/js/howler.core.min.js rename to public/js/lib/howler.core.min.js diff --git a/public/js/vue-sortable.js b/public/js/lib/vue-sortable.js similarity index 100% rename from public/js/vue-sortable.js rename to public/js/lib/vue-sortable.js diff --git a/public/js/mstream.api.js b/public/js/mstream.api.js index 482dd69..434b74a 100644 --- a/public/js/mstream.api.js +++ b/public/js/mstream.api.js @@ -6,20 +6,226 @@ var MSTREAMAPI = (function () { mstreamModule.currentServer = { host:"", username:"", - password:"", + password:"", // TODO: Don't include this? + token: false, } + + + + var dataList = []; + + // dataItem = { + // type: '', + // data:'', + // + //} + + + + + + var fileExplorerArray = { + // This goes by the following pattern + // path-segemnt: scroll pos + + // music: 70 + // folder: 20 + // ACDC: 0 + // Greatest Hits: 0 + }; + + function getDirectoryContents(filepath){ + // Construct the directory string + var directoryString = ""; + for (var i = 0; i < fileExplorerArray.length; i++) { + directoryString += fileExplorerArray[i] + "/"; + } + + // If the scraper option is checked, then tell dirparer to use getID3 + $.post('dirparser', {dir: directoryString, filetypes: filetypes}, function(response) { + clearDatalist(); + + var parsedResponse = $.parseJSON(dir); + var path = parsedResponse.path; + + $.each(parsedResponse.contents, function() { + + dataList.push( + { + type: this.type, + path: path + this.name, + artist: false, // TODO: + title: false // TODO: + } + ); + + }); + }); + } + + + + + + // TODO Move this to a secondary module that's initiated when it's assured the MSTREAM module is looded + mstreamModule.savePlaylist = function(saveThis){ + // TODO: Verify all data in saveThis + + if(saveThis.length == 0){ + return; + } + + // Get playlist from MSTREAM + // var playlist = MSTREAM.whatever + + // Get user entered title + // var title = ''; + + + + // Check for special characters + if(/^[a-zA-Z0-9-_ ]*$/.test(title) == false) { + // TODO: Warn User + return false; + } + + // loop through array and add each file to the playlist + // $.each( playlistArray, function() { + // // TODO: + // }); + + + + $.ajax({ + type: "POST", + url: "saveplaylist", + data: { + title:title, + stuff:saveThis // TODO: Change this on server end + }, + }) + .done(function( msg ) { + + if(msg == 1){ + // ??? + } + if(msg == 0){ + // .. ??? + } + + }); + + // TODO: error handeling + } + + + + + mstreamModule.getAllPlaylists = function(){ + var request = $.ajax({ + url: "getallplaylists", + type: "GET" + }); + + request.done(function( msg ) { + clearDatalist(); + var parsedResponse = $.parseJSON(msg); + + //parse through the json array and make an array of corresponding divs + var playlists = []; + $.each(parsedResponse, function() { + dataList.push( + { + type: 'playlist', + name: this.name + } + ); + }); + }); + + request.fail(function( jqXHR, textStatus ) { + // TODO: + }); + } + + + // TODO: Can thie be cahnged to a reset of the variable + function clearDatalist(){ + while(dataList.length > 0){ + dataList.pop(); + } + } + + + + + + mstreamModule.deletePlaylist = function(playlistNameString){ + // Send to server + var request = $.ajax({ + url: "deleteplaylist", + type: "GET", + data: {playlistname: playlistNameString} + }); + + request.done(function( msg ) { + // TODO: Update datalist + }); + + request.fail(function( jqXHR, textStatus ) { + // TODO: + }); + } + + + + + + + mstreamModule.getPlaylistContents = function(playlistNameString){ + + // Make an AJAX call to get the contents of the playlist + $.ajax({ + type: "GET", + url: "loadplaylist", + data: {playlistname: playlistNameString}, + dataType: 'json', + }) + .done(function( msg ) { + // Add the playlist name to the modal + + // Clear the playlist + + // Append the playlist items to the playlist + $.each( msg, function(i ,item) { + + }); + }); + } + + + + + + + + + + + + + + + + + + mstreamModule.getSharedPlaylist = function(){ // Get the URL parameters console.log(window.location.pathname); - console.log(window.location.pathname); - console.log(window.location.pathname); - console.log(window.location.pathname); - console.log(window.location.pathname); - console.log(window.location.pathname); - console.log(window.location.pathname); - + // Call the api with the the short token @@ -27,6 +233,7 @@ var MSTREAMAPI = (function () { } + // Return an object that is assigned to Module return mstreamModule; }()); diff --git a/public/js/mstream.js b/public/js/mstream.js index 8cbfb43..7bb2e73 100755 --- a/public/js/mstream.js +++ b/public/js/mstream.js @@ -1,35 +1,123 @@ $(document).ready(function(){ + // jukebox global variable + var jukebox = { + connection: false, + live: false, + guestCode: false, + adminCode: false, + error: false, + accessAddress: false + }; + + + // The jukebox panel + $('#jukebox_mode').on('click', function(){ + // Hide the directory bar + $('.directoryTitle').hide(); + // Change the panel name + $('.panel_one_name').html('Jukebox Mode'); + // clear the list + $('#filelist').empty(); + $('#filelist').removeClass('scrollBoxHeight1'); + $('#filelist').removeClass('scrollBoxHeight2'); + $('#filelist').addClass('scrollBoxHeight2'); + + // TODO: Check if connection has been established + // setup correct html + var newHtml = ''; + if(jukebox.live !== false && jukebox.connection !== false){ + newHtml = createJukeboxPanel(); + + }else{ + newHtml = '\ +
\
+ Jukebox Mode will allow you to control this page of mStream remotely
\
+ Click the button to enable Jukebox Mode
\
+
'; + + if(jukebox.error !== false){ + // TODO: WARN THE USE + returnHtml = ''; + return returnHtml; + } + + if(jukebox.adminCode){ + returnHtml += '
Something went wrong
'); - }); - - }); - - -$("#filelist").on('click', '.deletePlaylist', function(){ - // Get Playlist ID - var playlistname = $(this).data('playlistname'); - - - // Send to server - var request = $.ajax({ - url: "deleteplaylist", - type: "GET", - data: {playlistname: playlistname} - }); - - request.done(function( msg ) { - - }); - - request.fail(function( jqXHR, textStatus ) { - // TODO: - }); - - $(this).parent().remove(); - -}); - - -// load up a playlist -$("#filelist").on('click', '.playlistz', function() { + }); 'click', '.playlistz', function() { var playlistname = $(this).data('playlistname'); var name = $(this).html(); @@ -770,9 +802,8 @@ $("#filelist").on('click', '.playlistz', function() { }); request.done(function( msg ) { - console.log(msg); + var parsedAlbums = $.parseJSON(msg); - // console.log(dirty); //clear the list $('#filelist').empty(); @@ -816,7 +847,7 @@ $("#filelist").on('click', '.playlistz', function() { //parse through the json array and make an array of corresponding divs var filelist = []; $.each(parsedMessage, function() { - console.log(this); + if(this.title==null){ filelist.push('Search Failed. Your database may not be setup
"); - }); } }); - }); diff --git a/public/mstream.html b/public/mstream.html index a675d36..31d4896 100755 --- a/public/mstream.html +++ b/public/mstream.html @@ -27,7 +27,7 @@ - + @@ -37,7 +37,7 @@ - + @@ -46,9 +46,9 @@ DO NOT Change to order these are loaded in You do not need to worry about how these work --> - - - + + + @@ -155,6 +155,7 @@ +