cinhcet/node-red-contrib-mpd

Wrong output

Closed this issue · 8 comments

Hi,

First of all thanks for this great nodes! When I send the listfiles command through the MPD out node it's wrong segmented.

Example:

{
    "payload":[
        {
            "directory":"test",
            "Last-Modified":"2018-04-23T20:38:20Z",
            "file":"Tom Odell - Another Love (Dimitri Vangelis & Wyman Remix).mp3",
            "size":"5048320"
        },
        {
            "Last-Modified":"2018-03-29T10:37:25Z"
        }
    ],
    "_msgid":"8ba33e03.ed188"
}

File and size must be in the second item of the array. It looks like node-red create a new array item when a key is found more than once. In this case Last-Modified.

Is it possible to fix this or is this node-red behaviour?

Thanks in advance!

this seems to be an issue of the underlying mpd library in this method
https://github.com/andrewrk/mpd.js/blob/master/index.js#L173

could you maybe output the raw message and post it here?
To do this, add console.log(msg); in line 112 of https://github.com/cinhcet/node-red-contrib-mpd/blob/master/mpd/mpd.js#L112

Thanks for your fast reply!
The raw message:

directory: test
Last-Modified: 2018-04-23T20:38:20Z
file: Tom Odell - Another Love (Dimitri Vangelis & Wyman Remix).mp3
size: 5048320
Last-Modified: 2018-03-29T10:37:25Z

Looking at https://github.com/andrewrk/mpd.js/blob/master/index.js#L173 seems that my assumption is correct. Shall I open an issue there then?

thank you for your fast response also :-)

looking at the mpd reference for this command, it says: " The response contains at least one line for each directory entry with the prefix "file: " or "directory: ", and may be followed by file attributes such as "Last-Modified" and "size". "

Therefore, I guess such commands need special parsing. I could add an option (which could be set via the node input and/or via the config of the node) that the raw message is returned, e.g. an array which contains an entry of key value pair for each line. Then one could parse this separately.

What do you think?

It would be great if it is possible to set an option via the node input and output a key value pair for each line. It won't harm the existing code base and I can construct the new array by myself through a function.

will do that. I am busy this week, hopefully I will find time at the weekend.

please test. Install by "npm install cinhcet/node-red-contrib-mpd"
I would also be happy if you could test whether the old functionality (playing, stop, whatever) will still work, since I do not have much time at the moment.
Just set "rawOutput = true" in the input message.

It works! Getting the data as key value pairs. play, stop, forward, etc works either.
Many thanks for fixing this issue!

For those who wanted a grouped array

var objects = [];

for(i = 0; i < msg.payload.length; i++){
    var key = Object.keys(msg.payload[i])[0];
    if(key == 'directory' || key == 'file'){
        object = {};
        object[key] = msg.payload[i][key];
        objects.push(object);
    }
    else{
        object[key] = msg.payload[i][key];
    }
}

return {
    payload: objects
}

Thanks for the function. I might add this to the readme at some time :-) The readme could be improved anyhow....

now on npm

closed by e1dae4e