Hi,
Edit: this has been solve. I have to emit a clearQueue before each emiting. Can somebody tell me why?
I’m currently developping an other script, wich basically launch several playlists in a random order.
It is for my remote controller, as my previous topic.
I’m using the function ‘enqueue’ of the API, which works fine.
This way, I can add the playlists. However, I can’t know when a playlist has been successfully played, because I don’t receive any message back on socket.on.
Am I missing something?
Here’s the function where I add recursively the playlist, then wait for a pushState.
function addPlaylists(playlists, socket) {
playlist = playlists.pop();
console.log("Play playlists " + playlist.name)
socket.emit('enqueue', playlist);
socket.on('pushState', function() {
console.log("Push state received");
if (playlists.length == 0) {
socket.disconnect();
} else {
addPlaylists(playlists, socket);
}
});
}
I have also tried to do all the emit, and then wait for a pushState, but it does not work either.
Here’s the full code:
[code]
let exec = require(‘child_process’).exec;
let fs = require(‘fs’);
let io = require(‘socket.io-client’);
function shuffleArray(array) {
for (var i = array.length - 1; i > 0; i–) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
function getPlaylists(fileName) {
let fs = require(‘fs’);
let playlists = JSON.parse(fs.readFileSync(fileName, ‘utf8’));
playlists = playlists.playlists
shuffleArray(playlists)
return playlists
}
function addPlaylists(playlists, socket) {
playlist = playlists.pop();
console.log("Play playlists " + playlist.name)
socket.emit(‘enqueue’, playlist);
socket.on(‘pushState’, function() {
console.log(“Push state received”);
if (playlists.length == 0) {
socket.disconnect();
} else {
addPlaylists(playlists, socket);
}
});
}
function printResults(error, stdout, stderr) {
console.log('stdout: ’ + stdout);
console.log('stderr: ’ + stderr);
if (error !== null) {
console.log('exec error: ’ + error);
}
}
function parseArgs() {
if (process.argv.length < 3) {
console.log(“Need to passe the file name”)
}
}
parseArgs();
let playlists = getPlaylists(process.argv[2])
console.log(playlists)
let child = exec(“volumio clear”, function (error, stdout, stderr) {
printResults(error, stdout, stderr);
let socket = io.connect('http://localhost:3000');
addPlaylists(playlists, socket);
});[/code]
Thanks for your help,
Best,
Cosmita