I'm trying to get status from omxplayer with NodeJS via DBus, to do that i'm just trying to execuce shell script:
#!/bin/bash
#set -x
OMXPLAYER_DBUS_ADDR="/tmp/omxplayerdbus"
OMXPLAYER_DBUS_PID="/tmp/omxplayerdbus.pid"
export DBUS_SESSION_BUS_ADDRESS=`cat $OMXPLAYER_DBUS_ADDR`
export DBUS_SESSION_BUS_PID=`cat $OMXPLAYER_DBUS_PID`
[ -z "$DBUS_SESSION_BUS_ADDRESS" ] && { echo "Must have DBUS_SESSION_BUS_ADDRESS" >&2; exit 1; }
duration=`dbus-send --print-reply=literal --session --reply-timeout=500 --dest=org.mpris.MediaPlayer2.omxplayer /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Duration`
[ $? -ne 0 ] && exit 1
duration="$(awk '{print $2}' <<< "$duration")"
position=`dbus-send --print-reply=literal --session --reply-timeout=500 --dest=org.mpris.MediaPlayer2.omxplayer /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Position`
[ $? -ne 0 ] && exit 1
position="$(awk '{print $2}' <<< "$position")"
playstatus=`dbus-send --print-reply=literal --session --reply-timeout=500 --dest=org.mpris.MediaPlayer2.omxplayer /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.PlaybackStatus`
[ $? -ne 0 ] && exit 1
playstatus="$(sed 's/^ *//;s/ *$//;' <<< "$playstatus")"
paused="true"
[ "$playstatus" == "Playing" ] && paused="false"
echo "Duration: $duration"
echo "Position: $position"
echo "Paused: $paused"
;;
with
var exec = require('child_process').exec;
exec('bash status.sh', function() {
console.log(arguments);
})
but it prints
{ '0':
{ [Error: Command failed: Failed to open connection to "session" message bus: Did not receive a reply. Possible causes include: the remote application did not send a reply, the message bus security policy blocked the reply, the reply timeout expired, or the network connection was broken.
] killed: false, code: 1, signal: null },
'1': '',
'2': 'Failed to open connection to "session" message bus: Did not receive a reply. Possible causes include: the remote application did not send a reply, the message bus security policy blocked the reply, the reply timeout expired, or the network connection was broken.\n' }
When I execute that script directly in console it works. NodeJS is running on Raspberry Pi.
UPDATE:
I have also tried "node-dbus" and "dbus-native" modules, but none of them worked for me, but maybe I used them incorrectly? To execute
dbus-send --print-reply=literal --session --reply-timeout=500 --dest=org.mpris.MediaPlayer2.omxplayer /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Duration
I used dbus-native
var exec = require('child_process').exec;
exec('cat /tmp/omxplayerdbus', function(error, data, stderr) {
data = data.replace("\n",'');
var dbus = require('dbus-native');
var bus = dbus.sessionBus({
busAddress: data //unix:abstract=/tmp/dbus-7BuZanKhmv,guid=7fafe7baa2d38357478f04ff5429712a
});
bus.invoke({
path: '/org/mpris/MediaPlayer2',
destination: 'org.mpris.MediaPlayer2.omxplayer',
'interface': 'org.freedesktop.DBus.Properties.Position'
}, function(err, res) {
console.log(arguments);
});
//And this
var conn = dbus({
busAddress: data
});
conn.message({
path:'/org/mpris/MediaPlayer2',
destination: 'org.mpris.MediaPlayer2.omxplayer',
type: dbus.messageType.methodCall
});
conn.on('message', function(msg) { console.log(msg); }).on('error', function() {
console.log(arguments);
}).on('connect', function() {
console.log(arguments);
});
});
both of these methods throws me this error:
events.js:72
throw er; // Unhandled 'error' event
^
Error: write EPIPE
at errnoException (net.js:904:11)
at Object.afterWrite (net.js:720:19)
UPDATE 2
I'm now using "dbus-native" module and still keep getting "EPIPE" error. I have checked "Handshake.js" and there is everything alright, so I dumped stdin and stdout messages:
{stdin}AUTH EXTERNAL 30
{stdout}OK df028c4a159a4db39ccc41c0542b9e3b
{stdin}BEGIN
{stdin}lmo/org/freedesktop/DBussorg.freedesktop.DBussHellosorg.freedesktop.DBus
PuTTY{stdin}l5�o/org/mpris/MediaPlayer2sorg.freedesktop.DBus.PropertiessGets org.mpris.MediaPlayer2.omxplayegss org.mpris.MediaPlayer2.omxplayePosition
{stdout} - stdout message line {stdin} - stdin message line
and then "EPIPE".
UPDATE 3
I have found out that "EPIPE" error is throwed right after first dbus "DATA" command, in this case it's
lmo/org/freedesktop/DBussorg.freedesktop.DBussHellosorg.freedesktop.DBus
PuTTY{stdin}l5�o/org/mpris/MediaPlayer2sorg.freedesktop.DBus.PropertiessGets org.mpris.MediaPlayer2.omxplayegss org.mpris.MediaPlayer2.omxplayePosition
i'm new in communications via dbus, but according to DBus protocol, messages should be sent DATA <data in hex encoding>, but dbus-native sends messages without DATA command name.
You are trying to read dbus object propery using properties api. Note that last parameter of dbus-send is interface.member, so the message you are sending would be
var bus = dbus.sessionBus({ busAddress: fs.readFileSync('/tmp/omxplayerdbus', 'ascii').trim()})
bus.invoke({
path: "/org/mpris/MediaPlayer2",
interface: "org.freedesktop.DBus.Properties",
member: "Get",
destination: "org.mpris.MediaPlayer2.omxplayer",
signature: "ss",
body: [
"org.mpris.MediaPlayer2.omxplayer",
"Position"
]
}, function(err, position) {
console.log(err, position);
});