ubus lua client method and event registration code demo/example

 ubus lua client method and event registration code demo example

#!/usr/bin/env lua

require "ubus"
require "uloop"

local ubscli_conn = ubus.connect()
if not ubscli_conn then
    error("Failed to connect to ubus")
end

local test_method = {
	test1 = {
		t1 = 1,
		t2 = {
			function(req)
				print("Call to function test1 t2")
			end, {id = "error" }
		},
	},
	test2 = {
		t1 = {
			function(req, msg)
				ubscli_conn:reply(req, {message="hello test2.t1"});
				print("Call to function 't1'")
				for k, v in pairs(msg) do
					print("key=" .. k .. " value=" .. tostring(v))
				end
			end, {id = ubus.INT32, msg = ubus.STRING }
		},
		t2 = {
			function(req)
				ubscli_conn:reply(req, {message="1 hello test2.t2"});
				ubscli_conn:reply(req, {message="2 hello test2.t2"});
				print("Call to function 't2'")
			end, {id = ubus.INT32, msg = ubus.STRING }
		}
	}
}

local test_event = {
	test1 = function(msg)
		print("Call to test1 event")
		for k, v in pairs(msg) do
			print("key=" .. k .. " value=" .. tostring(v))
		end
	end,
}

ubscli_conn:add(test_method)
ubscli_conn:listen(test_event)

uloop.run()

Comments

Popular posts from this blog

Python Receiving and parse JSON Data via UDP protocol