/** * File: app/radio.js * Author: Gerrit Linnemann * * Where the magic happens. */ // load the things we need var spawn = require('child_process').spawn; var exec = require('child_process').exec; var fs = require("fs"); var http = require('http'); var wget = require('wget'); var grep = require('simple-grep'); var App = null; var Control = null; var Log = null; var Helper = null; var Conf = null; const isWin = process.platform === "win32"; const isMac = process.platform === "darwin"; exports.init = function(Express) { App = Express; Conf = App.get('Configuration'); Log = App.get('Log'); Helper = App.get('Helper'); Control = App.get('Control'); Log.debug('Radio: ' + exports.countChannels() + ' channels configured'); Helper.each(Conf.channels, function(channel, isLast) { //Log.inspect('Channel', channel); Log.log('Radio: Found channel ' + channel.title); }); if(!isMac) { Control.setValueOnStartup(); } return this; } exports.countChannels = function() { return Conf.channels.length; } exports.getChannels = function() { return Conf.channels; } exports.getChannel = function(idx) { if(Helper.isDefinedAndNotNull(Conf.channels[idx])) { return Conf.channels[idx]; } else { return null; } } exports.play = function(idx) { var channel = Conf.channels[idx]; if(channel !== undefined) { var stream2play = channel.stream; var m3u = '/tmp/kitchenradio.m3u'; var streamIsMp3 = stream2play.toLowerCase().endsWith('mp3'); Helper.shspawn(App.get('tools_root')+'action.sh stop'); // if strem is mp3, write url to m3u file if(streamIsMp3) { fs.writeFileSync(m3u, stream2play); Helper.shspawn(App.get('tools_root')+'action.sh play'); } else { try { fs.unlinkSync(m3u); } catch(e) { Log.error('Radio: Error deleting "' + m3u + '".' + e); } var download = wget.download(stream2play, m3u); download.on('end', function(output) { Log.inspect('Radio: Download completed', output); Helper.shspawn(App.get('tools_root')+'action.sh play'); }); download.on('error', function(err) { Log.error('Radio: Error downloading playlist. ' + err); }); } } else { Log.error('Radio: No channel defined!'); } } exports.stop = function() { Log.log('Radio: stop playing'); Helper.shspawn(App.get('tools_root')+'action.sh stop'); } exports.volumeUp = function() { Control.volumeUp() } exports.volumeDown = function() { Control.volumeDown() }