|
| 1 | +const _ = require("lodash"); |
| 2 | +const {discoveries} = require("./discovery"); |
| 3 | +const {getPlaylistFromDB} = require("../db/controllers/playlist"); |
| 4 | + |
| 5 | +const WebSocket = require('ws'); |
| 6 | +const http = require('http'); |
| 7 | +const { |
| 8 | + v4: uuidv4, |
| 9 | +} = require('uuid'); |
| 10 | + |
| 11 | +const playlist = {}; |
| 12 | +exports.playlist = playlist; |
| 13 | + |
| 14 | +let currentPlaylist = [] |
| 15 | +let currentPlaylistData = [] |
| 16 | +let pixelBlazeData = [] |
| 17 | +let pixelBlazeIds = [] |
| 18 | +let playlistTimeout = null |
| 19 | +let playlistLoopTimeout = null |
| 20 | +let initInterval = null |
| 21 | +init = () => { |
| 22 | + getPlaylistFromDB() |
| 23 | + .then((data) => { |
| 24 | + try { |
| 25 | + currentPlaylist = [] // resetting current play so it doesn't grow to infinity |
| 26 | + currentPlaylist.push(...data) // adding new playlist items to list |
| 27 | + if (JSON.stringify(currentPlaylist) !== JSON.stringify(currentPlaylistData)) { |
| 28 | + currentPlaylistData = [] |
| 29 | + currentPlaylistData.push(...data) |
| 30 | + } |
| 31 | + } catch (err) { |
| 32 | + console.warn(`Error: ${err}`) |
| 33 | + } |
| 34 | + }) |
| 35 | + .catch('there was an error gathering playlist details') |
| 36 | + |
| 37 | + // gather pixelblaze data |
| 38 | + pixelBlazeData = _.map(discoveries, function (v, k) { |
| 39 | + let res = _.pick(v, ['lastSeen', 'address']); |
| 40 | + _.assign(res, v.controller.props); |
| 41 | + return res; |
| 42 | + }) |
| 43 | + pixelBlazeIds = _.map(pixelBlazeData, 'id') |
| 44 | +} |
| 45 | + |
| 46 | +initInterval = setInterval(init ,100) |
| 47 | + |
| 48 | +const server = http.createServer(); |
| 49 | +const address = '0.0.0.0' |
| 50 | +const port = 1890; |
| 51 | +const playlistServer = new WebSocket.Server({host: address , port: port}); |
| 52 | +console.log(`Playlist server is running on ${address}:${port}`) |
| 53 | + |
| 54 | +const playlistClients = {}; |
| 55 | + |
| 56 | +playlistServer.on('connection', (connection) => { |
| 57 | + // Generate a unique code for every user |
| 58 | + const clientId = uuidv4() |
| 59 | + console.log(`Recieved a new connection.`); |
| 60 | + |
| 61 | + // Store the new connection and handle messages |
| 62 | + playlistClients[clientId] = connection; |
| 63 | + console.log(`${clientId} connected.`); |
| 64 | + connection.on('message', async (data) => { |
| 65 | + let message |
| 66 | + try { |
| 67 | + message = JSON.parse(data); |
| 68 | + } catch (err) { |
| 69 | + sendError(playlistServer, `Wrong format ${err}`) |
| 70 | + return |
| 71 | + } |
| 72 | + if (message.type === 'LAUNCH_PLAYLIST_NOW') { |
| 73 | + console.log('received launch playlist now message!') |
| 74 | + await runPlaylistLoopNow() |
| 75 | + } |
| 76 | + }) |
| 77 | +}); |
| 78 | + |
| 79 | +const sendError = (ws, message) => { |
| 80 | + const messageObject = { |
| 81 | + type: 'ERROR', |
| 82 | + payload: message, |
| 83 | + }; |
| 84 | + ws.send(JSON.stringify(messageObject)); |
| 85 | +}; |
| 86 | +const broadcastMessage = (json) => { |
| 87 | + const data = JSON.stringify(json); |
| 88 | + for(let userId in playlistClients) { |
| 89 | + let playlistClient = playlistClients[userId]; |
| 90 | + if(playlistClient.readyState === WebSocket.OPEN) { |
| 91 | + playlistClient.send(data); |
| 92 | + } |
| 93 | + }; |
| 94 | +}; |
| 95 | +const sendPattern = (pattern) => { |
| 96 | + const name = pattern.name |
| 97 | + _.each(pixelBlazeIds, async id => { |
| 98 | + id = String(id); |
| 99 | + let controller = discoveries[id] && discoveries[id].controller; |
| 100 | + if (controller) { |
| 101 | + const command = { |
| 102 | + programName: pattern.name |
| 103 | + } |
| 104 | + await controller.setCommand(command); |
| 105 | + } |
| 106 | + }) |
| 107 | + let message = { |
| 108 | + currentRunningPattern: name, |
| 109 | + currentPlaylist: currentPlaylist |
| 110 | + } |
| 111 | + broadcastMessage(message) |
| 112 | +} |
| 113 | + |
| 114 | +const delaySendPattern = (pattern) => { |
| 115 | + return new Promise((resolve) => { |
| 116 | + resolve(sendPattern(pattern)) |
| 117 | + }) |
| 118 | +} |
| 119 | +const iterateOnPlaylist = async () => { |
| 120 | + for (let index = 0; index < currentPlaylist.length; index++) { |
| 121 | + const pattern = currentPlaylist[index] |
| 122 | + await delaySendPattern(pattern) |
| 123 | + await new Promise(resolve => { |
| 124 | + playlistTimeout = setTimeout(resolve, pattern.duration * 1000) |
| 125 | + }); |
| 126 | + } |
| 127 | +} |
| 128 | +module.exports.playlistLoop = async () => { |
| 129 | + while (true) { |
| 130 | + await new Promise(resolve => { |
| 131 | + playlistLoopTimeout = setTimeout(resolve, 100) |
| 132 | + }); |
| 133 | + if(pixelBlazeIds.length) { |
| 134 | + await iterateOnPlaylist() |
| 135 | + } |
| 136 | + initInterval = null |
| 137 | + playlistTimeout = null |
| 138 | + playlistLoopTimeout = null |
| 139 | + } |
| 140 | +} |
| 141 | +const runPlaylistLoopNow = async () => { |
| 142 | + clearInterval(initInterval) |
| 143 | + clearInterval(playlistTimeout) |
| 144 | + clearInterval(playlistLoopTimeout) |
| 145 | + |
| 146 | + await this.playlistLoop() |
| 147 | +} |
| 148 | +this.playlistLoop() |
0 commit comments