首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用gjs接口DBus

用gjs接口DBus
EN

Ask Ubuntu用户
提问于 2019-07-25 11:00:30
回答 1查看 919关注 0票数 2

我对gjs非常陌生,我想要与dbus接口。我想创建一个服务并聆听它。

一个小的例子或指南将是有用的。谢谢

EN

回答 1

Ask Ubuntu用户

发布于 2019-07-25 16:45:46

我已经读过Gnome正在走Java (GJS)的路线,用于它的大部分桌面,并减少C++和Python的数量。所以今天我发现这个问题特别有趣。

这是一个示例Java脚本,我将亲自尝试。在本例中,您将学习创建连接到服务的D总线客户端,学习如何调用方法、连接到信号并从服务获取属性。该示例使用管理键盘背光的D总线服务:

代码语言:javascript
复制
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;

// This the D-Bus interface as XML
const KbdBacklightInterface = '<node>\
<interface name="org.freedesktop.UPower.KbdBacklight"> \
    <method name="SetBrightness"> \
        <arg name="value" type="i" direction="in"/> \
    </method> \
    <method name="GetBrightness"> \
        <arg name="value" type="i" direction="out"/> \
    </method> \
    <method name="GetMaxBrightness"> \
        <arg name="value" type="i" direction="out"/> \
    </method> \
    <signal name="BrightnessChanged"> \
        <arg type="i"/> \
    </signal> \
</interface> \
</node>';

// Declare the proxy class based on the interface
const KbdBacklightProxy = Gio.DBusProxy.makeProxyWrapper(KbdBacklightInterface);

// Get the /org/freedesktop/UPower/KbdBacklight instance from the bus
let kbdProxy = new KbdBacklightProxy(
    Gio.DBus.system,
    "org.freedesktop.UPower",
    "/org/freedesktop/UPower/KbdBacklight"
);

// You can use proxy.<method>Sync syntax to 
// call the D-Bus method in a Sync way
print("The max brightness of your keyboard is " + kbdProxy.GetMaxBrightnessSync());

// Or you can use the syntax proxy.<method>Remote
// to call the method in an Async way
kbdProxy.GetBrightnessRemote(function(currentBrightness) {
    print("The current keyboard brightness is " + currentBrightness);
});

// Connecting to a D-Bus signal
kbdProxy.connectSignal("BrightnessChanged", function(proxy) {
    let newBrightness = proxy.GetBrightnessSync();
    print("The keyboard brightness has been changed, new brightness is " + newBrightness);
});

// Also you can get properties value using this syntax
// let property = proxy.PropertyName;

// Or you can set a property value
// proxy.PropertyName = "new value";

let loop = new GLib.MainLoop(null, false);
loop.run();

如果尚未完成,请安装gjs:

代码语言:javascript
复制
sudo apt update && sudo apt install gjs

为Java脚本创建一个目录:

代码语言:javascript
复制
mkdir ~/javascript

使用gedit创建上面的示例脚本并保存它:

代码语言:javascript
复制
gedit ~/javascript/dbusclient.js

现在运行它:

代码语言:javascript
复制
cd ~/javascript
gjs dbusclient.js

报告了WIP错误(我将修复该问题,并返回到此问题和结果):

代码语言:javascript
复制
(gjs:10134): Gjs-WARNING **: JS ERROR: Gio.DBusError: GDBus.Error:org.freedesktop.DBus.Error.UnknownMethod: No such interface 'org.freedesktop.UPower.KbdBacklight' on object at path /org/freedesktop/UPower/KbdBacklight
_proxyInvoker@resource:///org/gnome/gjs/modules/overrides/Gio.js:98
_makeProxyMethod/<@resource:///org/gnome/gjs/modules/overrides/Gio.js:124
@dbusclient.js:36

JS_EvaluateScript() failed

无键盘灯

虽然膝上型电脑有键盘灯,无线键盘有3种灯光设置,但Gnome的UPOWER无法将它们视为shell命令:

代码语言:javascript
复制
$ dbus-send --print-reply \
            --system \
            --dest=org.freedesktop.UPower \
            /org/freedesktop/UPower \
            org.freedesktop.UPower.EnumerateDevices

返回这个:

代码语言:javascript
复制
method return time=1564075040.686545 sender=:1.49 -> destination=:1.145 serial=4392 reply_serial=2
   array [
      object path "/org/freedesktop/UPower/devices/line_power_ACAD"
      object path "/org/freedesktop/UPower/devices/battery_BAT1"
      object path "/org/freedesktop/UPower/devices/ups_hiddev2"
      object path "/org/freedesktop/UPower/devices/mouse_0003o046Do101Ax0017"
      object path "/org/freedesktop/UPower/devices/keyboard_0003o046Do2010x0018"
   ]

这可能是一个方便的脚本修改,以监测笔记本电脑电池充电百分比。

(待续.)

票数 3
EN
页面原文内容由Ask Ubuntu提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://askubuntu.com/questions/1160950

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档