lua lgi订阅了DBus信号

问题描述 投票:1回答:1

我是第一次使用LUA,我正在尝试订阅一个回调,以便在收到DBus信号时执行。我找到了一些关于如何使用lgi发送DBus信号的例子,但我还没有找到任何关于如何收听信号的信息。那有什么例子吗?在lgi文档中我没有找到它。

谢谢

[编辑]

就这种情况而言,它对某人有用,我已设法使用dbus_proxy(https://github.com/stefano-m/lua-dbus_proxy)订阅信号。此示例代码订阅信号,然后发出一个信号以查看是否调用了回调:

local lgi = require('lgi')
local GLib = lgi.GLib 
local p = require('dbus_proxy')
local Bus = p.Bus
local Proxy = p.Proxy   

local proxy = Proxy:new(
  {
    bus = Bus.SESSION,
    name = "org.freedesktop.DBus",
    path= "/org/freedesktop/DBus",
    interface = "org.freedesktop.DBus"
  }
)

local called = false
local received_proxy
local received_params
local function callback(proxy_obj, ...)
  print('Hi!')
  called = true
  received_proxy = proxy_obj
  received_params = {...}
end
local signal_name = "NameOwnerChanged"
local sender_name = nil -- any sender
proxy:connect_signal(callback, signal_name, sender_name)

local bus_name = "com.example.Test2"
local DBUS_NAME_FLAG_REPLACE_EXISTING = 2
proxy:RequestName(bus_name, DBUS_NAME_FLAG_REPLACE_EXISTING)

main_loop = GLib.MainLoop()
main_loop:run()

我会继续研究它

另外,我试图弄清楚如何翻译这个python代码,使用lgi dbus将dbus信号发送到LUA:

class DBUSTestInterface(object):
    """
    Server_XML definition.
    Emit / Publish a signal that is a random integer every second 
    type='i' for integer. 
    """
    dbus = """
    <node>
        <interface name="com.test.device.aaa">
            <signal name="get">
                <arg type='s'/>
                <arg type='s'/>
                <arg type='s'/>
                <arg type='s'/>
                <arg type='s'/>
                <arg type='s'/>
                <arg type='s'/>
                <arg type='i'/>
            </signal>
        </interface>
    </node>
    """
    get = signal()

emit = DBUSTestInterface()
bus.publish("com.test.device.get", emit)

如果你能提供一些例子或指出我能找到它的地方,我将不胜感激!

谢谢!

lua lgi
1个回答
1
投票

这是一个代码示例,它为系统总线上的所有对象路径订阅bluez PropertiesChanged信号。您可以根据您的要求更改总线类型,信号,接口名称等,并进行尝试。

local lgi = require 'lgi'
local GLib, Gio = lgi.GLib, lgi.Gio
local main_loop = GLib.MainLoop()

--Get a system bus
local bus = Gio.bus_get_sync(Gio.BusType.SYSTEM)

--Create a user callback function that needs to operate when signal is received
function onDBusSignalCallback(conn, sender, object_path, interface_name, signal_name, user_data)
        local str = string.format("SIGNAL - object_path:%s, interface_name:%s, signal_name:%s", object_path, interface_name, signal_name)
        print(str)
end


--Subscribe to any signal.
local sub_id = bus:signal_subscribe('org.bluez', 'org.freedesktop.DBus.Properties',
                'PropertiesChanged', nil, nil, Gio.DBusSignalFlags.NONE, onDBusSignalCallback)
if sub_id then
        print("Subscription id", sub_id)
end

main_loop:run()
© www.soinside.com 2019 - 2024. All rights reserved.