Backend optimization
This commit is contained in:
@@ -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);
|
||||
|
||||
+35
-37
@@ -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+'%');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user