2014-09-28 41 views
0

我试图通过的DBus获得与从的NodeJS状态omxplayer,这样做,我只是想execuce shell脚本:DBUS的NodeJS不工作

#!/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上运行。

UPDATE:

我也试过“节点DBUS”和“DBUS天然的”模块,但他们没有为我工作,但也许我用他们是否有误?要执行

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 

{}标准输出 - 标准输出消息行 {}标准输入 - 标准输入信息行

,然后“EPIPE”。

更新3

我已经发现了 “EPIPE” 错误倒掉之后第一DBUS “DATA” 命令,在这种情况下,它

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命令名称。

+0

你尝试过任何现有节点库(我是一个作者,DBUS母语)? – 2014-09-29 14:16:46

+0

谢谢您的回复,我更新了我的问题。 – Luke 2014-09-29 15:02:00

+0

看起来不能连接到该地址。尝试使用'echo -e“\ 0AUTH ANONYMOUS \ r \ n”|等socat来测试socat abstract-client:/ tmp/dbus-7BuZanKhmv -' – 2014-09-29 21:59:29

回答

0

您正在尝试使用properties api来读取dbus对象propery。注意DBUS,发送的是最后一个参数是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); 
}); 
+0

感谢您的解释,但在使用invoke方法时仍然会遇到'EPIPE'错误。错误来自'abstractsocket/node_modules/exec_stream/index.js child.stdin.on('error')'事件。在Handshake.js中,一切正常,它执行'AUTH EXTERNAL 30'并接收'OK'。我更新了我的问题,增加了更多细节(请参阅“更新2”)。 – Luke 2014-10-01 08:59:20

+0

在握手过程中看起来像dbus drop连接,不知道为什么。 – 2014-10-01 23:42:35

+0

当我发布消息发送方法时,连接永远保持活跃状态​​,没有错误。但是在发送第一条消息之后,它会抛出错误。 – Luke 2014-10-02 07:22:11