From fd98747c53d476eb071613cb069cf3c8e2df1196 Mon Sep 17 00:00:00 2001 From: Erinome Date: Sat, 6 Feb 2021 17:00:53 +0300 Subject: [PATCH] mqtt: stop playing sounds without extra exec calls --- lib/MqttClient.js | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/lib/MqttClient.js b/lib/MqttClient.js index fd86597..66944ea 100644 --- a/lib/MqttClient.js +++ b/lib/MqttClient.js @@ -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"); @@ -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); @@ -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."); }