NodeJS dbus不工作

时间:2014-09-28 13:23:04

标签: node.js bash shell raspberry-pi dbus

我正试图通过DBus从omxplayer获取NodeJS的状态,为此我只是尝试执行shell脚本:

    #!/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"
    ;;

var exec = require('child_process').exec;
exec('bash status.sh', function() {
    console.log(arguments);
})

但打印

    { '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' }

当我直接在控制台中执行该脚本时,它可以工作。 NodeJS正在Raspberry Pi上运行。

更新

我也尝试过“node-dbus”和“dbus-native”模块,但它们都没有为我工作,但也许我错误地使用了它们?执行

dbus-send --print-reply=literal --session --reply-timeout=500 --dest=org.mpris.MediaPlayer2.omxplayer /org/mpris/MediaPlayer2 org.freedesktop.DBus.Properties.Duration
我用过 的 DBUS天然

    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);
        });
    });

这两种方法都抛出了这个错误:

    events.js:72
            throw er; // Unhandled 'error' event
                  ^
    Error: write EPIPE
        at errnoException (net.js:904:11)
        at Object.afterWrite (net.js:720:19)

更新2

我现在正在使用“dbus-native”模块,但仍然会出现“EPIPE”错误。我检查了“Handshake.js”,一切都没问题,所以我抛弃了stdin和stdout消息:

    {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} - 标准输出消息行 {stdin} - stdin消息行

然后是“EPIPE”。

更新3

我发现第一个dbus“DATA”命令后立即抛出“EPIPE”错误,在这种情况下它是

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

我是通过dbus进行通信的新手,但根据DBus protocol,应该发送消息DATA <data in hex encoding>,但dbus-native发送的消息没有DATA命令名。

1 个答案:

答案 0 :(得分:0)

您正在尝试使用properties api读取dbus对象属性。请注意,dbus-send的最后一个参数是interface.member,因此您发送的消息将是

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);
});