diff --git a/modules/admin.js b/modules/admin.js index 18658f8..80fdbd4 100644 --- a/modules/admin.js +++ b/modules/admin.js @@ -22,13 +22,14 @@ class admin { * * @type {object} */ - let previewInstances = []; let io = sharedIO.of("/admin-" + displayId); this.io = io; this.displayId = displayId; this.dispatcher = dispatcher; - + this.screenView = screenView; this.bundleManager = bundleManager; + this.previewInstances = []; + let self = this; // helper callback for global announce dispatcher.on("announce", function (obj) { @@ -60,18 +61,18 @@ class admin { socket.on('error', function (error) { cli.error(error, "WS error on socket id:" + socket.id); - for (let i in previewInstances) { - if (previewInstances[i].adminId === socket.id || previewInstances[i].preview === socket) { - delete previewInstances[i]; + for (let i in self.previewInstances) { + if (self.previewInstances[i].adminId === socket.id || self.previewInstances[i].preview === socket) { + self.previewInstances.splice(i, 1); break; } } }); socket.on('disconnect', function (reason) { - for (let i in previewInstances) { - if (previewInstances[i].adminId === socket.id || previewInstances[i].preview === socket) { - delete previewInstances[i]; + for (let i in self.previewInstances) { + if (self.previewInstances[i].adminId === socket.id || self.previewInstances[i].preview === socket) { + self.previewInstances.splice(i, 1); break; } } @@ -79,9 +80,9 @@ class admin { }); socket.on('sync.preview', function (socketId) { - for (let i in previewInstances) { - if (previewInstances[i].adminId === decodeURIComponent(socketId)) { - previewInstances[i].preview = socket; + for (let i in self.previewInstances) { + if (self.previewInstances[i].adminId === decodeURIComponent(socketId)) { + self.previewInstances[i].preview = socket; } } }); @@ -91,32 +92,36 @@ class admin { }); // toggle time - socket.on('controls.time.toggle', function (data) { - let view = getView(); + socket.on('controls.time.toggle', function () { + let view = self.getView(); let bool = !view.serverOptions.displayTime; view.getBundle().bundleData.displayTime = bool; view.serverOptions.displayTime = bool; view.io.emit("callback.time", bool); - updateDashboard(io); + self.updateDashboard(io); }); - socket.on('controls.previous', function (data) { - let view = getView(); - view.serverOptions.loopIndex -= 2; - view.mainLoop(); + socket.on('controls.previous', function () { + self.controls('previous'); }); - socket.on('controls.play', function (data) { - getServerOptions().loop = true; - getView().mainLoop(); - updateDashboard(io); + socket.on('controls.play', function () { + self.controls('play'); }); + socket.on('controls.pause', function () { + self.controls('pause'); + }); + + socket.on('controls.next', function () { + self.controls('next'); + }); + socket.on('controls.startStream', function (data) { - let view = getView(); - let serverOptions = getServerOptions(); + let view = self.getView(); + let serverOptions = self.getServerOptions(); if (serverOptions.isStreaming === false) { serverOptions.loop = false; @@ -131,27 +136,16 @@ class admin { cli.success("stop stream"); } view.io.emit("callback.load", view.getSlideData()); - updateDashboard(io); + self.updateDashboard(io); }); - socket.on('controls.pause', function (data) { - let view = getView(); - view.clearTimers(); - getServerOptions().loop = false; - updateDashboard(io); - }); - - socket.on('controls.next', function (data) { - getView().mainLoop(); - }); - - socket.on('controls.blackout', function (data) { - let view = getView(); + socket.on('controls.blackout', function () { + let view = self.getView(); view.serverOptions.blackout = view.serverOptions.blackout === false; view.io.emit("callback.blackout", {serverOptions: view.serverOptions}); - updateDashboard(io); + self.updateDashboard(io); }); socket.on("controls.preview", function (data) { @@ -162,7 +156,7 @@ class admin { } try { - getPreview(socket).preview.emit("callback.preview", + self.getPreview(socket).preview.emit("callback.preview", { bundleData: bundle.getBundleData(), json: json, @@ -177,19 +171,19 @@ class admin { }); socket.on("controls.skipTo", function (data) { - let bundleSettings = getView().getBundle(); + let bundleSettings = self.getView().getBundle(); let slides = bundleSettings.enabledSlides; let idx = slides.indexOf(data.fileName); if (idx >= 0) { - getServerOptions().loopIndex = idx; + self.getServerOptions().loopIndex = idx; } - getView().mainLoop(); + self.getView().mainLoop(); }); socket.on('controls.toggle', function (data) { let fileName = data.fileName; - let bundleSettings = getView().getBundle(); + let bundleSettings = self.getView().getBundle(); let idx = bundleSettings.disabledSlides.indexOf(fileName); if (idx > -1) { @@ -198,7 +192,7 @@ class admin { bundleSettings.setSlideStatus(fileName, false); } - updateDashboard(socket); + self.updateDashboard(socket); }); // override @@ -210,7 +204,7 @@ class admin { } dispatcher.emit("all.override", data); } else { - let view = getView(); + let view = self.getView(); view.overrideSlide(data.json, data.png, data.duration, data.transition); } } catch (err) { @@ -219,23 +213,23 @@ class admin { }); socket.on('admin.setBundle', function (data) { - let view = getView(); + let view = self.getView(); view.changeBundle(data.bundle); view.displayCurrentSlide(); - updateDashboard(io); + self.updateDashboard(io); }); socket.on('admin.dashboard.sync', function () { - syncDashboard(socket); - updateDashboard(socket); + self.syncDashboard(socket); + self.updateDashboard(socket); }); - socket.on('admin.listSlides', function (data) { - updateDashboard(socket); + socket.on('admin.listSlides', function () { + self.updateDashboard(socket); }); - socket.on('admin.listBundles', function (data) { - updateDashboard(socket); + socket.on('admin.listBundles', function () { + self.updateDashboard(socket); }); @@ -292,10 +286,10 @@ class admin { fs.copyFileSync("templates/bundle/slides.json", "data/bundles/" + data.dir + "/slides.json"); bundleManager.getBundle(data.dir); - syncDashboard(io); + self.syncDashboard(io); } else { socket.emit("callback.error", "Error: bundle already exists."); - syncDashboard(io); + self.syncDashboard(io); } } catch (err) { cli.error(err, "Error while creating new bundle"); @@ -399,8 +393,8 @@ class admin { transition = null; } - getServerOptions().transition = transition; - updateDashboard(io); + self.getServerOptions().transition = transition; + self.updateDashboard(io); }); socket.on('edit.save', function (data) { @@ -499,89 +493,98 @@ class admin { }); }); // io - - - /** helper functions */ - - /** - * @param {object} socket - */ - function updateDashboard(socket) { - let view = getView(); - let serverOptions = view.serverOptions; - let bundleSettings = view.getBundle(); - - socket.emit("callback.dashboard.update", { - bundleSettings: bundleSettings, - allSlides: bundleSettings.allSlides, - serverOptions: serverOptions, - displayId: displayId - }); - } - - function updateSlides(socket, bundlename, bundle) { - socket.emit("callback.dashboard.updateSlides", { - bundleName: bundlename, - bundleSettings: bundle - }); - } - - function syncDashboard(socket) { - previewInstances.push({adminId: socket.id, preview: {}, currentView: {}}); - let serverOptions = getView().serverOptions; - let bundleDirs = self.bundleManager.getBundleInfos(); - - - socket.emit("callback.dashboard.sync", { - displayId: displayId, - bundleDirs: bundleDirs, - serverOptions: serverOptions, - displays: config.displays - }); - } - - function getDirectories(path) { - return fs.readdirSync(path).filter(function (file) { - return fs.statSync(path + '/' + file).isDirectory(); - }); - } - - function getPreview(socket) { - for (let i in previewInstances) { - if (previewInstances[i].adminId === socket.id) { - return previewInstances[i]; - } - } - - throw "error"; - } - - /** Generate an uuid - * @url https://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript#2117523 **/ - function uuidv4() { - return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { - let r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8); - return v.toString(16); - }); - } - - /** - * @return infoscreen3/display.serverOptions - */ - function getServerOptions() { - return getView().serverOptions; - - } - - /** - * @return display - */ - function getView() { - return screenView; - } } -} // admin + /** helper functions */ + + /** + * @param {object} socket + */ + updateDashboard(socket) { + let view = this.getView(); + let serverOptions = view.serverOptions; + let bundleSettings = view.getBundle(); + + socket.emit("callback.dashboard.update", { + bundleSettings: bundleSettings, + allSlides: bundleSettings.allSlides, + serverOptions: serverOptions, + displayId: this.displayId + }); + + this.dispatcher.emit("dashboard.update", {displayId: this.displayId, serverOptions: serverOptions}); + + } + + static updateSlides(socket, bundlename, bundle) { + socket.emit("callback.dashboard.updateSlides", { + bundleName: bundlename, + bundleSettings: bundle + }); + } + + syncDashboard(socket) { + this.previewInstances.push({adminId: socket.id, preview: {}, currentView: {}}); + let serverOptions = this.getView().serverOptions; + let bundleDirs = this.bundleManager.getBundleInfos(); + + + socket.emit("callback.dashboard.sync", { + displayId: displayId, + bundleDirs: bundleDirs, + serverOptions: serverOptions, + displays: config.displays + }); + } + + getPreview(socket) { + for (let i in this.previewInstances) { + if (this.previewInstances[i].adminId === socket.id) { + return this.previewInstances[i]; + } + } + throw "error"; + } + + /** + * @return infoscreen3/display.serverOptions + */ + getServerOptions() { + return this.getView().serverOptions; + } + + /** + * @return display + */ + getView() { + return this.screenView; + } + + + controls(action) { + switch (action) { + case "previous": + let view = this.getView(); + view.serverOptions.loopIndex -= 2; + view.mainLoop(); + break; + case "play": + this.getServerOptions().loop = true; + this.getView().mainLoop(); + this.updateDashboard(this.io); + break + case "pause": + this.getView().clearTimers(); + this.getServerOptions().loop = false; + this.updateDashboard(this.io); + break; + case "next": + this.getView().mainLoop(); + break; + } + + } // admin +} /** * @type {admin} diff --git a/modules/bundleManager.js b/modules/bundleManager.js index a767309..cfb379a 100644 --- a/modules/bundleManager.js +++ b/modules/bundleManager.js @@ -87,4 +87,7 @@ function getJson(file) { return JSON.parse(fs.readFileSync(file).toString()); } +/** + * @type {bundleManager} + */ module.exports = bundleManager; \ No newline at end of file diff --git a/modules/cli.js b/modules/cli.js index 66cd475..140a765 100644 --- a/modules/cli.js +++ b/modules/cli.js @@ -17,7 +17,6 @@ module.exports = { }, log: function (string) { - console.log(getDateTime() + " " + string); } }; diff --git a/modules/db.js b/modules/db.js index be8d84a..d8e54c7 100644 --- a/modules/db.js +++ b/modules/db.js @@ -1,9 +1,10 @@ -var records = require("../config.js").admins; +const records = require(`../config.js`).admins; + module.exports = { - findById: function(id, cb) { + findById: function (id, cb) { process.nextTick(function () { - var idx = id - 1; + let idx = id - 1; if (records[idx]) { cb(null, records[idx]); } else { @@ -12,10 +13,10 @@ module.exports = { }); }, - findByUsername: function(username, cb) { + findByUsername: function (username, cb) { process.nextTick(function () { - for (var i = 0, len = records.length; i < len; i++) { - var record = records[i]; + for (let i = 0, len = records.length; i < len; i++) { + let record = records[i]; if (record.username === username) { return cb(null, record); } diff --git a/modules/display.js b/modules/display.js index 10ef5be..b687ab0 100644 --- a/modules/display.js +++ b/modules/display.js @@ -1,5 +1,4 @@ let fs = require("fs"); -let path = require("path"); let cli = require(`./cli.js`); let bundleManager = require(`./bundleManager.js`); @@ -207,7 +206,7 @@ class display { let that = this; // override slide timeout if set by slide - var slideTimeout = this.serverOptions.slideDuration * 1000; + let slideTimeout = this.serverOptions.slideDuration * 1000; if (this.serverOptions.currentMeta.duration > 5) { slideTimeout = parseFloat(this.serverOptions.currentMeta.duration) * 1000; } @@ -229,7 +228,7 @@ class display { overrideSlide(json, pngData, duration, transition) { this.clearTimers(); - var _transition = this.serverOptions.transition; + let _transition = this.serverOptions.transition; this.serverOptions.transition = transition; this.serverOptions.loop = false; this.serverOptions.isAnnounce = true; diff --git a/modules/websocket.js b/modules/websocket.js index c86ce10..c38e7af 100644 --- a/modules/websocket.js +++ b/modules/websocket.js @@ -6,12 +6,7 @@ let _bundleManager = require(`./bundleManager.js`); let chalk = require('chalk'); let fs = require('fs'); -/** - * @type {display[]} - */ -let screenView = []; -/** @type {admin[]} */ -let adminView = []; + /** * @param server @@ -21,6 +16,14 @@ let adminView = []; * @return {{screenView: display[], adminView: admin[], bundleManager: bundleManager}} */ module.exports = function (server, app, io, dispatcher) { + /** + * @type {display[]} + */ + let screenView = []; + + /** @type {admin[]} */ + let adminView = []; + console.log(chalk.green(">> ") + "InfoScreen3" + chalk.green("<<")); cli.log("Checking for write permissions..."); @@ -59,6 +62,7 @@ module.exports = function (server, app, io, dispatcher) { let screenId = 0; + // create screens and admin socket interfaces for (let metadata of availableDisplays) { let view = new display(io, dispatcher, metadata, screenId, bundleManager); screenView.push(view); @@ -66,7 +70,48 @@ module.exports = function (server, app, io, dispatcher) { screenId += 1; } + // create overview admin lobby + io.of("/admin").on("connection", function (socket) { + let arr = []; + for (let display of screenView) { + arr.push({displayId: display.serverOptions.displayId, serverOptions: display.serverOptions}); + } + socket.emit("callback.serverOptions", arr); + + + // handle other events + cli.info("WS/" + socket.conn.remoteAddress + " connect"); + socket.on('error', function (error) { + cli.error(error, "WS/ error"); + }); + socket.on('disconnect', function (reason) { + cli.info("WS/ " + reason + " " + socket.conn.remoteAddress); + }); + + socket.on('controls.previous', function (data) { + getAdminView(data.displayId).controls("previous"); + }); + + socket.on('controls.play', function (data) { + getAdminView(data.displayId).controls("play"); + }); + + socket.on('controls.next', function (data) { + getAdminView(data.displayId).controls("next"); + }); + + socket.on('controls.pause', function (data) { + getAdminView(data.displayId).controls("pause"); + }); + + dispatcher.on("dashboard.update", function (data) { + socket.emit("callback.serverOptions", [data]); + }); + + }); + + // create lobby io.of("/lobby").on("connection", function (socket) { cli.info("WS/" + socket.conn.remoteAddress + " connect"); @@ -91,5 +136,17 @@ module.exports = function (server, app, io, dispatcher) { }); }); + /** + * @return admin + */ + function getAdminView(index) { + try { + return adminView[index]; + } catch (err) { + cli.log(err); + } + } + return {screenView: screenView, adminView: adminView, bundleManager: bundleManager}; }; + diff --git a/views/admin/overview.twig b/views/admin/overview.twig index ad87f36..119dbc2 100644 --- a/views/admin/overview.twig +++ b/views/admin/overview.twig @@ -24,7 +24,8 @@

{{ display.name }}

- +