使用 Gjs 监控电池百分比

使用 Gjs 监控电池百分比

我正在寻找答案来了解如何获取电池百分比的值。我使用修改了示例以下代码的文档:

const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;


// This the D-Bus interface as XML
const batInterface = '<node>\
<interface name="org.freedesktop.UPower.DisplayDevice"> \
    <method name="State"> \
        <arg name="State" type="u" direction="out"/> \
    </method> \
    <method name="Percentage"> \
        <arg name="Percentage" type="d" direction="out"/> \
    </method> \
</interface> \
</node>';

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

// Get the /org/freedesktop/UPower/DisplayDevice (the device being displayed I believe) instance from the bus
let batInstanceProxy = new batProxy(
    Gio.DBus.system,
    "org.freedesktop.UPower",
    "/org/freedesktop/UPower/devices/DisplayDevice"
);

// You can use proxy.<method>Sync syntax to 
// call the D-Bus method in a Sync way
print("The percentage is " + batInstanceProxy.PercentageSync());


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

但我收到了这个错误:


(gjs:25859): Gjs-WARNING **: 14:30:15.006: JS ERROR: Gio.DBusError: GDBus.Error:org.freedesktop.DBus.Error.AccessDenied: Rejected send message, 1 matched rules; type="method_call", sender=":1.282" (uid=1000 pid=25859 comm="gjs test.js " label="unconfined") interface="org.freedesktop.UPower.DisplayDevice" member="Percentage" error name="(unset)" requested_reply="0" destination=":1.40" (uid=0 pid=1226 comm="/usr/lib/upower/upowerd " label="unconfined")
_proxyInvoker@resource:///org/gnome/gjs/modules/core/overrides/Gio.js:139:46
_makeProxyMethod/<@resource:///org/gnome/gjs/modules/core/overrides/Gio.js:164:30
@test.js:29:47


(gjs:25859): Gjs-CRITICAL **: 14:30:15.006: Script test.js threw an exception

第一行显示member="Percentage" error name="(unset)"。这是否意味着该成员不存在,或者我只是因为权限问题而无法访问它?我怎么知道?该设备存在,我知道,因为它upower -i /org/freedesktop/UPower/devices/DisplayDevice成功返回了我的电池数据。也许我搞乱了 中的 XML batInterface

相关内容