| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- const SSH_CMD = "/usr/bin/ssh amixer@knacki.info"
- function sshCmd(cmd) {
- var cmdString = (SSH_CMD +' ' +cmd),
- cmd = cmdString.split(' ');
- return require('child_process').spawnSync(cmd[0], cmd.splice(1)).stdout.toString("utf-8");
- }
- function sshSetVolume(controlName, status) {
- sshCmd("mixSet " +status +" " +controlName);
- }
- function isChannelEnabled(line) {
- var percent = (/\[([0-9]+)%\]/).exec(line),
- state = (/\'([A-Za-z]+)\'/).exec(line);
- if (percent)
- return Number.parseInt(percent[1]) > 0;
- if (state)
- return state[1] === "Enabled";
- return false;
- }
- function sshGetVolume(controlName, channelList) {
- var output = sshCmd("mixGet " +controlName).split("\n");
- for (var i =0, len =output.length; i < len; ++i)
- for (var j =0, channelLen =channelList.length; j < channelLen; ++j) {
- var pos = output[i].indexOf(channelList[j] +":");
- if (pos >= 0 && isChannelEnabled(output[i].substr(pos +channelList[j].length +1)))
- return true;
- }
- return false;
- }
- function GenerateVolume(controlName) {
- return {
- on: function() { sshSetVolume(controlName, "100%") },
- off: function() { sshSetVolume(controlName, "0%") },
- get: function() { return sshGetVolume(controlName, [ "Front Left", "Front Right" ]); }
- };
- }
- function GenerateToggle(controlName) {
- return {
- on: function() { sshSetVolume(controlName, "Enabled") },
- off: function() { sshSetVolume(controlName, "Disabled") },
- get: function() { return sshGetVolume(controlName, [ "Item0" ]); }
- };
- }
- const inputs = {};
- (function() {
- var inputConfig = require('./config.js').ALSA_INPUT;
- for (var i in inputConfig.volume)
- inputs[i] = GenerateVolume(inputConfig.volume[i]);
- for (var i in inputConfig.toggle)
- inputs[i] = GenerateToggle(inputConfig.toggle[i]);
- })();
- module.exports.init = function() {
- require("./outputs.js").registerOutput("alsa", {
- name: "PC Speaker",
- getInputs: function() {
- var speakerInputs = {};
- for (var i in inputs)
- speakerInputs[i] = inputs[i].get();
- return speakerInputs;
- },
- setState: function(inputId, state) {
- var input = inputs[inputId];
- if (input === undefined)
- return false;
- state ? input.on() : input.off();
- return true;
- },
- volumeControl: false
- });
- }
|