Skip to content

Commit

Permalink
mqtt: stop playing sounds without extra exec calls
Browse files Browse the repository at this point in the history
  • Loading branch information
rand256 committed Feb 6, 2021
1 parent ea44c05 commit fd98747
Showing 1 changed file with 29 additions and 14 deletions.
43 changes: 29 additions & 14 deletions lib/MqttClient.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const fs = require("fs");
const { exec } = require("child_process");
const { spawn } = require("child_process");
const mqtt = require("mqtt");
const Tools = require("./Tools");
const Vacuum = require("./miio/Vacuum");
Expand Down Expand Up @@ -131,6 +131,8 @@ const MqttClient = function (valetudo) {

this.statusDelayTimer = null;

this.soundProc = null;

this.events.on("valetudo.features_initialized", () => {
if (this.vacuum.features.v3) {
Object.assign(FAN_SPEEDS,FAN_SPEEDS_V3);
Expand Down Expand Up @@ -710,30 +712,43 @@ MqttClient.prototype.handleCustomCommand = function (message) {
* }
*/
case CUSTOM_COMMANDS.PLAY_SOUND:
let cmd = null, stop = false;
let cmd = null, args = [], stop = false;
if (fs.existsSync('/usr/bin/sox') && (fs.existsSync(msg.file) || msg.file === 'none' || msg.location)) {
if (msg.file === 'none' || msg.location === 'none') {
stop = true;
cmd = '/usr/bin/pkill -f "^/usr/bin/sox "';
} else {
cmd = '/usr/bin/sox ' + (msg.volume ? '-v ' + parseFloat(msg.volume) + ' ' : '') + '"' + String(msg.location || msg.file).replace(/[\|'"]/g,'') + '" -d';
cmd = '/usr/bin/sox';
if (msg.volume) {
args.push('-v', msg.volume);
}
args.push(msg.location || msg.file, '-d');
}
} else if (fs.existsSync('/usr/bin/uart_test') && msg.file && (fs.existsSync(msg.file) && msg.file.endsWith('.wav') || msg.file === 'none')) {
if (msg.file === 'none' || msg.location === 'none') {
} else if (fs.existsSync('/usr/bin/uart_test') && (fs.existsSync(msg.file) && msg.file.endsWith('.wav') || msg.file === 'none')) {
if (msg.file === 'none') {
stop = true;
cmd = '/usr/bin/pkill -f "^/usr/bin/uart_test -e "';
} else {
cmd = '/usr/bin/uart_test -e "' + msg.file + '"';
cmd = '/usr/bin/uart_test';
args.push('-e', msg.file);
}
}
if (stop || cmd && this.soundProc) {
this.soundProc.kill();
this.soundProc = null;
}
if (cmd) {
exec(cmd, (error, stdout, stderr) => {
if (!error || stop && error.code === 1) {
this.publishCommandStatus(msg.command,'ok',null);
} else {
this.publishCommandStatus(msg.command,null,error);
}
this.soundProc = spawn(cmd, args);
this.soundProc.on('error', (err) => {
this.publishCommandStatus(msg.command,null,"Sound player failed to start: " + err);
this.soundProc = null;
});
this.soundProc.on('close', (code) => {
this.soundProc = null;
});
if (typeof this.soundProc.pid === "number") {
this.publishCommandStatus(msg.command,'ok',null);
}
} else if (stop) {
this.publishCommandStatus(msg.command,'ok',null);
} else {
this.publishCommandStatus(msg.command,null,"Can't play specified file or location.");
}
Expand Down

0 comments on commit fd98747

Please sign in to comment.