From adf7df795239ac452204e451107129a6d8f87157 Mon Sep 17 00:00:00 2001 From: Gerrit Linnemann Date: Tue, 4 May 2021 21:11:48 +0200 Subject: [PATCH] Backend optimization --- kitchenradio/app.js | 12 ++++- kitchenradio/app/control.js | 89 +++++++++++++++++++++++++++++++++ kitchenradio/app/helper.js | 9 ++-- kitchenradio/app/radio.js | 72 +++++++++++++------------- kitchenradio/bin/start_radio.sh | 2 +- kitchenradio/bin/stop_radio.sh | 2 +- kitchenradio/config.json | 18 +++---- kitchenradio/views/layout.jade | 2 +- 8 files changed, 151 insertions(+), 55 deletions(-) create mode 100644 kitchenradio/app/control.js diff --git a/kitchenradio/app.js b/kitchenradio/app.js index e783233..249a10d 100644 --- a/kitchenradio/app.js +++ b/kitchenradio/app.js @@ -9,7 +9,8 @@ var Helper = require('./app/helper') , Log = require('./app/logging.js') , Version = require('./package.json') , Conf = require('./config.json') -, Radio = require('./app/radio'); +, Radio = require('./app/radio') +, Control = require('./app/control'); var app = express(); @@ -32,6 +33,7 @@ app.set('tools_root', __dirname + '/bin/'); Log.init(app); app.set('Log', Log); +app.set('Configuration', Conf); Log.log("Kitchen Radio " + Version.version + " is waking up ...", true); Log.debug('Kitchen Radio root: ' + app.get('tools_root')); @@ -42,10 +44,16 @@ Log.debug('Kitchen Radio root: ' + app.get('tools_root')); Helper.init(app); app.set('Helper', Helper); +/** + * Init Control. + */ +Control.init(app); +app.set('Control', Control); + /** * Init Radio. */ -var radioInstance = Radio.init(app, Conf); +var radioInstance = Radio.init(app); app.set('Radio', radioInstance); radioInstance.play(); diff --git a/kitchenradio/app/control.js b/kitchenradio/app/control.js new file mode 100644 index 0000000..481c1e7 --- /dev/null +++ b/kitchenradio/app/control.js @@ -0,0 +1,89 @@ +/** + * File: app/control.js + * Author: Gerrit Linnemann + * + * Control device. + */ + + +// load the things we need +var os = require('os') +, Log = require('./logging') +, childProcess = require('child_process'); + + +var App = null; +var Log = null +var Helper = null; +var Conf = null; + +const isWin = process.platform === "win32"; +const isMac = process.platform === "darwin"; +const volumeStep = 10 +var volume = isMac ? 10 : 30; // starting value in percent + + +exports.init = function(Express) { + App = Express; + Conf = App.get('Configuration'); + Log = App.get('Log'); + Helper = App.get('Helper'); + + getMacCurrentVolumeInPercent((level) => { + volume = level + }) + + return this; +} + +exports.setValueOnStartup = function() { + if(isWin) { + Log.log('Control: Are you kidding? Windows?'); + } else { + setVolumeTo(volume); + } +} + +exports.volumeUp = function() { + volume = parseInt((volume + volumeStep)) + setVolumeTo(volume) +} + +exports.volumeDown = function() { + volume = parseInt((volume - volumeStep)) + setVolumeTo(volume) +} + +exports.volume = function(newValue) { + setVolumeTo(newValue) +} + +function getMacCurrentVolumeInPercent(callback) { + //osascript -e 'get volume settings' + //osascript -e 'set ovol to output volume of (get volume settings)' + + const cmd = "osascript -e 'set ovol to output volume of (get volume settings)'" + childProcess.exec(cmd, [], (error, stdout, stderr) => { + if (error) { + Log.error('Control: ' + stderr); + throw error; + } + + Log.log('Control: Sound level ' + stdout); + callback(stdout); + }); +} + +function setVolumeTo(newValue) { + if(volume > 100) { volume = 100; } + if(volume < 0) { volume = 0; } + + if(isMac) { + var tick = Helper.translatePercentVolumeValueToMacScala(volume); + Log.log('Control: volume to ' + tick); + Helper.shspawn('osascript -e "set volume '+tick+'"'); + } else { + Log.log('Control: volume to ' + newValue); + Helper.shspawn('amixer sset \'PCM\' '+newValue+'%'); + } +} \ No newline at end of file diff --git a/kitchenradio/app/helper.js b/kitchenradio/app/helper.js index 949e382..67adfab 100644 --- a/kitchenradio/app/helper.js +++ b/kitchenradio/app/helper.js @@ -16,14 +16,11 @@ var os = require('os') var app = null; -var ConfReader = null; exports.init = function(express) { app = express; - ConfReader = app.get('ConfReader'); - return this; } @@ -123,11 +120,15 @@ exports.simpleHash = function(foo) { return hash; } +exports.translatePercentVolumeValueToMacScala = function(volumeInPercent) { + return parseInt((volumeInPercent / 10)); +} + /* private */ function doValueEqualityCheck(foo, bar) { var key; for(key in foo) { - Log.debug( "Compare \"" + foo[key] + "\" with \"" + bar[key] + "\" for key \"" + key + "\"" ); + Log.debug( "Helper: Compare \"" + foo[key] + "\" with \"" + bar[key] + "\" for key \"" + key + "\"" ); if(typeof foo[key] === 'object' && typeof bar[key] === 'object') { var isEquals = doValueEqualityCheck(foo, bar); diff --git a/kitchenradio/app/radio.js b/kitchenradio/app/radio.js index fdea410..f0e3741 100644 --- a/kitchenradio/app/radio.js +++ b/kitchenradio/app/radio.js @@ -16,18 +16,21 @@ var grep = require('simple-grep'); var App = null; -var Log = null +var Control = null; +var Log = null; var Helper = null; var Conf = null; -var volume = '40'; +const isWin = process.platform === "win32"; +const isMac = process.platform === "darwin"; -exports.init = function(Express, Configuration) { +exports.init = function(Express) { App = Express; - Conf = Configuration; + 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) { @@ -35,9 +38,9 @@ exports.init = function(Express, Configuration) { Log.log('Radio: Found channel ' + channel.title); }); - // set default volume level - Helper.shspawn('amixer scontrols'); - Helper.shspawn('amixer sset \'PCM\' '+volume+'%'); + if(!isMac) { + Control.setValueOnStartup(); + } return this; } @@ -63,24 +66,32 @@ exports.play = function(idx) { if(channel !== undefined) { var stream2play = channel.stream; var m3u = '/tmp/kitchenradio.m3u'; + var streamIsMp3 = stream2play.toLowerCase().endsWith('mp3'); - try { - fs.unlinkSync(m3u); - } catch(e) { - Log.error('Radio: Error deleting "' + m3u + '".' + e); - } + Helper.shspawn(App.get('tools_root')+'action.sh stop'); - var download = wget.download(channel.stream, m3u); - download.on('end', function(output) { - Log.inspect('Radio: Download completed', output); - - 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'); - }); - - download.on('error', function(err) { - Log.error('Radio: Error downloading playlist. ' + err); - }); + } 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!'); } @@ -92,22 +103,9 @@ exports.stop = function() { } exports.volumeUp = function() { - exports.volume(5) + Control.volumeUp() } exports.volumeDown = function() { - exports.volume(-5) + Control.volumeDown() } - -exports.volume = function(step) { - Log.log('Radio: volume ' + (step>0 ? 'up' : 'down')); - - if(step != 0) { - volume += step; - - if(volume > 100) { volume = 100; } - if(volume < 0) { volume = 0; } - - Helper.shspawn('amixer sset \'PCM\' '+volume+'%'); - } -} \ No newline at end of file diff --git a/kitchenradio/bin/start_radio.sh b/kitchenradio/bin/start_radio.sh index 9b4d264..a9551c9 100755 --- a/kitchenradio/bin/start_radio.sh +++ b/kitchenradio/bin/start_radio.sh @@ -19,4 +19,4 @@ echo echo "${BGre}Start playing $stream2play ...${RCol}" echo -mplayer -slave -quiet $stream2play \ No newline at end of file +mplayer -slave -quiet $stream2play diff --git a/kitchenradio/bin/stop_radio.sh b/kitchenradio/bin/stop_radio.sh index df26190..03f647e 100755 --- a/kitchenradio/bin/stop_radio.sh +++ b/kitchenradio/bin/stop_radio.sh @@ -14,4 +14,4 @@ echo echo "${BGre}Stop playing ...${RCol}" echo -pkill -f mplayer \ No newline at end of file +pkill -f mplayer diff --git a/kitchenradio/config.json b/kitchenradio/config.json index 59a7d7a..ebc4760 100644 --- a/kitchenradio/config.json +++ b/kitchenradio/config.json @@ -2,21 +2,21 @@ "channels": [ { "type": "radiostream", - "title": "SWR3", - "stream": "http://mp3-live.swr3.de/swr3_m.m3u", - "icon": "https://lh4.ggpht.com/TvZh5HdllZOnwlPLAn043r_MMwcOw73vC0esB8g4ytwG4buC57HMjy4VXlwpj7vS5ng=w300" + "title": "NDR2", + "stream": "http://www.ndr.de/resources/metadaten/audio/m3u/ndr2_hh.m3u", + "icon": "https://upload.wikimedia.org/wikipedia/commons/9/9e/NDR_2_Logo.svg" }, { "type": "radiostream", - "title": "WDR2", - "stream": "https://wdr-wdr2-ostwestfalenlippe.icecastssl.wdr.de/wdr/wdr2/ostwestfalenlippe/mp3/128/stream.mp3", - "icon": "http://static.radio.de/images/broadcasts/07/47/9771/c300.png" + "title": "WDR4", + "stream": "http://wdr-wdr4-live.icecast.wdr.de/wdr/wdr4/live/mp3/128/stream.mp3", + "icon": "https://upload.wikimedia.org/wikipedia/commons/6/60/WDR_4_logo_2012.svg" }, { "type": "radiostream", - "title": "1Live", - "stream": "https://wdr-1live-live.sslcast.addradio.de/wdr/1live/live/mp3/128/stream.mp3", - "icon": "http://static.radio.de/images/broadcasts/4e/0d/1382/1/c175.png" + "title": "WDR5", + "stream": "http://wdr-wdr5-live.icecast.wdr.de/wdr/wdr5/live/mp3/128/stream.mp3", + "icon": "https://upload.wikimedia.org/wikipedia/commons/d/da/WDR5_Logo_2012.svg" } ], "logging": { diff --git a/kitchenradio/views/layout.jade b/kitchenradio/views/layout.jade index 264f256..fe6448f 100644 --- a/kitchenradio/views/layout.jade +++ b/kitchenradio/views/layout.jade @@ -2,7 +2,7 @@ doctype html html head title= title - link(rel='stylesheet', href='https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css') + link(rel='stylesheet', href='https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css') link(rel='stylesheet', href='/stylesheets/style.css') script(src="/javascripts/vendor/jquery.min.js") script(src="/javascripts/radio.js")