Backend optimization
This commit is contained in:
parent
39d27cec24
commit
adf7df7952
@ -9,7 +9,8 @@ var Helper = require('./app/helper')
|
|||||||
, Log = require('./app/logging.js')
|
, Log = require('./app/logging.js')
|
||||||
, Version = require('./package.json')
|
, Version = require('./package.json')
|
||||||
, Conf = require('./config.json')
|
, Conf = require('./config.json')
|
||||||
, Radio = require('./app/radio');
|
, Radio = require('./app/radio')
|
||||||
|
, Control = require('./app/control');
|
||||||
|
|
||||||
|
|
||||||
var app = express();
|
var app = express();
|
||||||
@ -32,6 +33,7 @@ app.set('tools_root', __dirname + '/bin/');
|
|||||||
|
|
||||||
Log.init(app);
|
Log.init(app);
|
||||||
app.set('Log', Log);
|
app.set('Log', Log);
|
||||||
|
app.set('Configuration', Conf);
|
||||||
Log.log("Kitchen Radio " + Version.version + " is waking up ...", true);
|
Log.log("Kitchen Radio " + Version.version + " is waking up ...", true);
|
||||||
Log.debug('Kitchen Radio root: ' + app.get('tools_root'));
|
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);
|
Helper.init(app);
|
||||||
app.set('Helper', Helper);
|
app.set('Helper', Helper);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Init Control.
|
||||||
|
*/
|
||||||
|
Control.init(app);
|
||||||
|
app.set('Control', Control);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Init Radio.
|
* Init Radio.
|
||||||
*/
|
*/
|
||||||
var radioInstance = Radio.init(app, Conf);
|
var radioInstance = Radio.init(app);
|
||||||
app.set('Radio', radioInstance);
|
app.set('Radio', radioInstance);
|
||||||
radioInstance.play();
|
radioInstance.play();
|
||||||
|
|
||||||
|
|||||||
89
kitchenradio/app/control.js
Normal file
89
kitchenradio/app/control.js
Normal file
@ -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+'%');
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -16,14 +16,11 @@ var os = require('os')
|
|||||||
|
|
||||||
|
|
||||||
var app = null;
|
var app = null;
|
||||||
var ConfReader = null;
|
|
||||||
|
|
||||||
|
|
||||||
exports.init = function(express) {
|
exports.init = function(express) {
|
||||||
app = express;
|
app = express;
|
||||||
|
|
||||||
ConfReader = app.get('ConfReader');
|
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -123,11 +120,15 @@ exports.simpleHash = function(foo) {
|
|||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
exports.translatePercentVolumeValueToMacScala = function(volumeInPercent) {
|
||||||
|
return parseInt((volumeInPercent / 10));
|
||||||
|
}
|
||||||
|
|
||||||
/* private */
|
/* private */
|
||||||
function doValueEqualityCheck(foo, bar) {
|
function doValueEqualityCheck(foo, bar) {
|
||||||
var key;
|
var key;
|
||||||
for(key in foo) {
|
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') {
|
if(typeof foo[key] === 'object' && typeof bar[key] === 'object') {
|
||||||
var isEquals = doValueEqualityCheck(foo, bar);
|
var isEquals = doValueEqualityCheck(foo, bar);
|
||||||
|
|||||||
@ -16,18 +16,21 @@ var grep = require('simple-grep');
|
|||||||
|
|
||||||
|
|
||||||
var App = null;
|
var App = null;
|
||||||
var Log = null
|
var Control = null;
|
||||||
|
var Log = null;
|
||||||
var Helper = null;
|
var Helper = null;
|
||||||
var Conf = 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;
|
App = Express;
|
||||||
Conf = Configuration;
|
Conf = App.get('Configuration');
|
||||||
Log = App.get('Log');
|
Log = App.get('Log');
|
||||||
Helper = App.get('Helper');
|
Helper = App.get('Helper');
|
||||||
|
Control = App.get('Control');
|
||||||
|
|
||||||
Log.debug('Radio: ' + exports.countChannels() + ' channels configured');
|
Log.debug('Radio: ' + exports.countChannels() + ' channels configured');
|
||||||
Helper.each(Conf.channels, function(channel, isLast) {
|
Helper.each(Conf.channels, function(channel, isLast) {
|
||||||
@ -35,9 +38,9 @@ exports.init = function(Express, Configuration) {
|
|||||||
Log.log('Radio: Found channel ' + channel.title);
|
Log.log('Radio: Found channel ' + channel.title);
|
||||||
});
|
});
|
||||||
|
|
||||||
// set default volume level
|
if(!isMac) {
|
||||||
Helper.shspawn('amixer scontrols');
|
Control.setValueOnStartup();
|
||||||
Helper.shspawn('amixer sset \'PCM\' '+volume+'%');
|
}
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@ -63,24 +66,32 @@ exports.play = function(idx) {
|
|||||||
if(channel !== undefined) {
|
if(channel !== undefined) {
|
||||||
var stream2play = channel.stream;
|
var stream2play = channel.stream;
|
||||||
var m3u = '/tmp/kitchenradio.m3u';
|
var m3u = '/tmp/kitchenradio.m3u';
|
||||||
|
var streamIsMp3 = stream2play.toLowerCase().endsWith('mp3');
|
||||||
|
|
||||||
try {
|
Helper.shspawn(App.get('tools_root')+'action.sh stop');
|
||||||
fs.unlinkSync(m3u);
|
|
||||||
} catch(e) {
|
|
||||||
Log.error('Radio: Error deleting "' + m3u + '".' + e);
|
|
||||||
}
|
|
||||||
|
|
||||||
var download = wget.download(channel.stream, m3u);
|
// if strem is mp3, write url to m3u file
|
||||||
download.on('end', function(output) {
|
if(streamIsMp3) {
|
||||||
Log.inspect('Radio: Download completed', output);
|
fs.writeFileSync(m3u, stream2play);
|
||||||
|
|
||||||
Helper.shspawn(App.get('tools_root')+'action.sh stop');
|
|
||||||
Helper.shspawn(App.get('tools_root')+'action.sh play');
|
Helper.shspawn(App.get('tools_root')+'action.sh play');
|
||||||
});
|
} else {
|
||||||
|
try {
|
||||||
download.on('error', function(err) {
|
fs.unlinkSync(m3u);
|
||||||
Log.error('Radio: Error downloading playlist. ' + err);
|
} 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 {
|
} else {
|
||||||
Log.error('Radio: No channel defined!');
|
Log.error('Radio: No channel defined!');
|
||||||
}
|
}
|
||||||
@ -92,22 +103,9 @@ exports.stop = function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
exports.volumeUp = function() {
|
exports.volumeUp = function() {
|
||||||
exports.volume(5)
|
Control.volumeUp()
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.volumeDown = function() {
|
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+'%');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -19,4 +19,4 @@ echo
|
|||||||
echo "${BGre}Start playing $stream2play ...${RCol}"
|
echo "${BGre}Start playing $stream2play ...${RCol}"
|
||||||
echo
|
echo
|
||||||
|
|
||||||
mplayer -slave -quiet $stream2play
|
mplayer -slave -quiet $stream2play
|
||||||
|
|||||||
@ -14,4 +14,4 @@ echo
|
|||||||
echo "${BGre}Stop playing ...${RCol}"
|
echo "${BGre}Stop playing ...${RCol}"
|
||||||
echo
|
echo
|
||||||
|
|
||||||
pkill -f mplayer
|
pkill -f mplayer
|
||||||
|
|||||||
@ -2,21 +2,21 @@
|
|||||||
"channels": [
|
"channels": [
|
||||||
{
|
{
|
||||||
"type": "radiostream",
|
"type": "radiostream",
|
||||||
"title": "SWR3",
|
"title": "NDR2",
|
||||||
"stream": "http://mp3-live.swr3.de/swr3_m.m3u",
|
"stream": "http://www.ndr.de/resources/metadaten/audio/m3u/ndr2_hh.m3u",
|
||||||
"icon": "https://lh4.ggpht.com/TvZh5HdllZOnwlPLAn043r_MMwcOw73vC0esB8g4ytwG4buC57HMjy4VXlwpj7vS5ng=w300"
|
"icon": "https://upload.wikimedia.org/wikipedia/commons/9/9e/NDR_2_Logo.svg"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"type": "radiostream",
|
"type": "radiostream",
|
||||||
"title": "WDR2",
|
"title": "WDR4",
|
||||||
"stream": "https://wdr-wdr2-ostwestfalenlippe.icecastssl.wdr.de/wdr/wdr2/ostwestfalenlippe/mp3/128/stream.mp3",
|
"stream": "http://wdr-wdr4-live.icecast.wdr.de/wdr/wdr4/live/mp3/128/stream.mp3",
|
||||||
"icon": "http://static.radio.de/images/broadcasts/07/47/9771/c300.png"
|
"icon": "https://upload.wikimedia.org/wikipedia/commons/6/60/WDR_4_logo_2012.svg"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"type": "radiostream",
|
"type": "radiostream",
|
||||||
"title": "1Live",
|
"title": "WDR5",
|
||||||
"stream": "https://wdr-1live-live.sslcast.addradio.de/wdr/1live/live/mp3/128/stream.mp3",
|
"stream": "http://wdr-wdr5-live.icecast.wdr.de/wdr/wdr5/live/mp3/128/stream.mp3",
|
||||||
"icon": "http://static.radio.de/images/broadcasts/4e/0d/1382/1/c175.png"
|
"icon": "https://upload.wikimedia.org/wikipedia/commons/d/da/WDR5_Logo_2012.svg"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"logging": {
|
"logging": {
|
||||||
|
|||||||
@ -2,7 +2,7 @@ doctype html
|
|||||||
html
|
html
|
||||||
head
|
head
|
||||||
title= title
|
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')
|
link(rel='stylesheet', href='/stylesheets/style.css')
|
||||||
script(src="/javascripts/vendor/jquery.min.js")
|
script(src="/javascripts/vendor/jquery.min.js")
|
||||||
script(src="/javascripts/radio.js")
|
script(src="/javascripts/radio.js")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user