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')
|
||||
, 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();
|
||||
|
||||
|
||||
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 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);
|
||||
|
||||
@ -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');
|
||||
|
||||
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(channel.stream, m3u);
|
||||
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 stop');
|
||||
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)
|
||||
}
|
||||
|
||||
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+'%');
|
||||
}
|
||||
Control.volumeDown()
|
||||
}
|
||||
@ -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": {
|
||||
|
||||
@ -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")
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user