89 lines
2.0 KiB
JavaScript
89 lines
2.0 KiB
JavaScript
/**
|
|
* 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+'%');
|
|
}
|
|
} |