diff --git a/kitchenradio/app/radio.js b/kitchenradio/app/radio.js index 45ccf78..7a93ec4 100644 --- a/kitchenradio/app/radio.js +++ b/kitchenradio/app/radio.js @@ -56,63 +56,64 @@ exports.play = function(idx) { var channel = Conf.channels[idx]; if(channel !== undefined) { var stream2play = channel.stream; - var m3u = '/tmp/kitchenradio.m3u'; + try { fs.unlinkSync(m3u); } catch(e) { Log.error('Radio: Error deleting "' + m3u + '".' + e); } - try { - wget.download(channel.stream, m3u); - } catch(e) { - Log.error('Radio: Error downloading "' + channel.stream + '".' + e); - } - if(Helper.isDefinedAndNotNull(Child)) { + var download = wget.download(channel.stream, m3u); + download.on('end', function(output) { + Log.inspect('Radio: Download completed', output); + + if(Helper.isDefinedAndNotNull(Child)) { + try { + Child.kill(); + } catch(err) { + Log.error('Radio: Error killing process'); + } + } + try { - Child.kill(); - } catch(err) { - Log.error('Radio: Error killing process'); + var parsers = require('playlist-parser'); + var M3U = parsers.M3U; + + var playlist = M3U.parse(fs.readFileSync(m3u, { encoding: 'utf8' })); + Log.inspect('Radio: playlist', playlist); + + if(Helper.isDefinedAndNotNull(playlist[0])) { + stream2play = playlist[0].file; + } + } catch(e) { + Log.error(e); } - } + + var Child = spawn( + 'mplayer', + [ + '-slave', + '-quiet', + stream2play + ] + ); + + // listen for any response from the child + Child.stdout.on('data', function(chunk) { + Log.log('Radio: ' + chunk); + }); + + // listen for any errors + Child.stderr.on('data', function (chunk) { + Log.error('Radio: ' + chunk); + }); + + /*Child.on('exit', function (code){ + Log.log('Radio: Child process exited with exit code ' + code); + });*/ - try { - var parsers = require('playlist-parser'); - var M3U = parsers.M3U; - - var playlist = M3U.parse(fs.readFileSync(m3u, { encoding: 'utf8' })); - Log.inspect('Radio: playlist', playlist); - - if(Helper.isDefinedAndNotNull(playlist[0])) { - stream2play = playlist[0].file; - } - } catch(e) { - Log.error(e); - } - - var Child = spawn( - 'mplayer', - [ - '-slave', - '-quiet', - stream2play - ] - ); - - // listen for any response from the child - Child.stdout.on('data', function(chunk) { - Log.log('Radio: ' + chunk); - }); - - // listen for any errors - Child.stderr.on('data', function (chunk) { - Log.error('Radio: ' + chunk); - }); - - /*Child.on('exit', function (code){ - Log.log('Radio: Child process exited with exit code ' + code); - });*/ + }); } else { Log.error('Radio: No channel defined!'); }