alsaOutput.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. const SSH_CMD = "/usr/bin/ssh amixer@knacki.info"
  2. function sshCmd(cmd) {
  3. var cmdString = (SSH_CMD +' ' +cmd),
  4. cmd = cmdString.split(' ');
  5. return require('child_process').spawnSync(cmd[0], cmd.splice(1)).stdout.toString("utf-8");
  6. }
  7. function sshSetVolume(controlName, status) {
  8. sshCmd("mixSet " +status +" " +controlName);
  9. }
  10. function isChannelEnabled(line) {
  11. var percent = (/\[([0-9]+)%\]/).exec(line),
  12. state = (/\'([A-Za-z]+)\'/).exec(line);
  13. if (percent)
  14. return Number.parseInt(percent[1]) > 0;
  15. if (state)
  16. return state[1] === "Enabled";
  17. return false;
  18. }
  19. function sshGetVolume(controlName, channelList) {
  20. var output = sshCmd("mixGet " +controlName).split("\n");
  21. for (var i =0, len =output.length; i < len; ++i)
  22. for (var j =0, channelLen =channelList.length; j < channelLen; ++j) {
  23. var pos = output[i].indexOf(channelList[j] +":");
  24. if (pos >= 0 && isChannelEnabled(output[i].substr(pos +channelList[j].length +1)))
  25. return true;
  26. }
  27. return false;
  28. }
  29. function GenerateVolume(controlName) {
  30. return {
  31. on: function() { sshSetVolume(controlName, "100%") },
  32. off: function() { sshSetVolume(controlName, "0%") },
  33. get: function() { return sshGetVolume(controlName, [ "Front Left", "Front Right" ]); }
  34. };
  35. }
  36. function GenerateToggle(controlName) {
  37. return {
  38. on: function() { sshSetVolume(controlName, "Enabled") },
  39. off: function() { sshSetVolume(controlName, "Disabled") },
  40. get: function() { return sshGetVolume(controlName, [ "Item0" ]); }
  41. };
  42. }
  43. const inputs = {};
  44. (function() {
  45. var inputConfig = require('./config.js').ALSA_INPUT;
  46. for (var i in inputConfig.volume)
  47. inputs[i] = GenerateVolume(inputConfig.volume[i]);
  48. for (var i in inputConfig.toggle)
  49. inputs[i] = GenerateToggle(inputConfig.toggle[i]);
  50. })();
  51. module.exports.init = function() {
  52. require("./outputs.js").registerOutput("alsa", {
  53. name: "PC Speaker",
  54. getInputs: function() {
  55. var speakerInputs = {};
  56. for (var i in inputs)
  57. speakerInputs[i] = inputs[i].get();
  58. return speakerInputs;
  59. },
  60. setState: function(inputId, state) {
  61. var input = inputs[inputId];
  62. if (input === undefined)
  63. return false;
  64. state ? input.on() : input.off();
  65. return true;
  66. },
  67. volumeControl: false
  68. });
  69. }