const electron = require('electron'); const http = require('http'); const Actions = require('./actions.js'); const LOG = require('../logger/logger.js')('main'); let window = null; let port = 50233; electron.app.on('ready', (launchInfo) => { window = new electron.BrowserWindow({ width: 400, height: 200, webPreferences: { nodeIntegration: true } }); window.setMenuBarVisibility(false); LOG.debug('window being created...'); window.loadFile('index.html'); window.on('closed', () => { window = null }); LOG.debug('window created'); }); electron.app.on('window-all-closed', () => { electron.app.quit(); }); electron.ipcMain.on('restart-song', () => { LOG.debug('got restart song request'); }); electron.ipcMain.on('skip-song', () => { LOG.debug('got skip song request'); }); let wasAd = false; let curentSong = null; const RECLOG = require('../logger/logger.js')('rec'); const CVRTLOG = require('../logger/logger.js')('cvrt'); const METALOG = require('../logger/logger.js')('meta'); let recordingProcess = null; let durationInterval = null; async function stopRecordingStartConversion() { LOG.debug(`${currentSong.filename} stopping recording...`); await Actions.stopRecording(recordingProcess); let conversionSong = currentSong; LOG.debug(`${conversionSong.filename} starting conversion...`); let conversionProcess = Actions.convertToMp3(conversionSong); Actions.registerLogger(conversionProcess, CVRTLOG); conversionProcess.on('exit', (code, signal) => { LOG.info(`${conversionSong.filename} completed conversion`); LOG.debug(`${conversionSong.filename} starting adding metadata...`); let metadataProcess = Actions.addMetadata(conversionSong); Actions.registerLogger(metadataProcess, METALOG); metadataProcess.on('exit', (code, signal) => { LOG.info(`${conversionSong.filename} completed adding metadata`); Actions.removeIntermediate(conversionSong); }); LOG.info(`${conversionSong.filename} started adding metadata`); }); LOG.info(`${conversionSong.filename} started conversion`); } async function onNewSong(song) { wasAd = false; LOG.debug(`${song.filename} new song`); if (recordingProcess != null) { await stopRecordingStartConversion(); } LOG.debug(`${song.filename} starting recording...`); recordingProcess = Actions.startRecording(song); Actions.registerLogger(recordingProcess, RECLOG); recordingProcess.on('exit', (code, signal) => { LOG.info(`${song.filename} recording stopped`); recordingProcess = null; }); LOG.info(`${song.filename} started recording`); window.webContents.send('new-song', song); currentSong = song; let duration = 0; clearInterval(durationInterval); durationInterval = setInterval(() => { duration += 1; window.webContents.send('duration-tick', duration); if (duration >= 60 * 60) { LOG.debug('song lasted more than an hour, marking as an advertisement'); onAdvertisement(); } }, 1000); LOG.debug(`${song.filename} starting album art download`); await Actions.downloadAlbumArt(song); LOG.info(`${song.filename} album art downloaded`); } async function onUpdateSong(song) { LOG.debug(`${song.filename} update song`) window.webContents.send('update-song', song); LOG.debug(`${song.filename} starting full album art download`); await Actions.downloadAlbumArt(song); LOG.info(`${song.filename} full album art downloaded`); } async function onAdvertisement() { if (!wasAd) { LOG.debug(`advertisement`); } wasAd = true; clearInterval(durationInterval); if (recordingProcess != null) { await stopRecordingStartConversion(); } window.webContents.send('advertisement'); } let heartbeatTimeout = null; function onHeartbeat() { if (heartbeatTimeout == null) { LOG.debug('extension connected'); window.webContents.send('extension-connect'); } clearTimeout(heartbeatTimeout); heartbeatTimeout = setTimeout(() => { LOG.debug('extension disconnected'); window.webContents.send('extension-disconnect'); heartbeatTimeout = null; }, 8000); } http.createServer((req, res) => { let body = ''; req.on('data', (chunk) => { body += chunk; }); req.on('end', () => { let data = null; try { data = JSON.parse(body); } catch (e) { LOG.error('unable to parse request json', { body: body, error: e }); res.statusCode = 400; res.end(); return; } if (data.song) { data.song.filename = Actions.getSongFilename(data.song); } switch (data.type) { case 'new-song': onNewSong(data.song); break; case 'update-song': onUpdateSong(data.song); break; case 'advertisement': onAdvertisement(); case 'heartbeat': onHeartbeat(); break; default: res.statusCode = 400; res.end(); return; } res.statusCode = 200; res.end(); }); }).listen(port);