Here's a way to make synchronous calls between QA - and it's pretty quick.
QA server. plugin.mainDeviceId = 240---------------------------------------------------------- -------- -------- --------
function foo (a, b) return a + b end --- fun test
-- call = {callerID = <id>, callVar = <string>, fun = <string>, args = <table>}
function QuickApp : RPC (call)
local res = _G [call.fun] and {pcall (_G [call.fun], table.unpack (call.args))} or { false , "No such function" }
fibaro.setGlobalVariable (call.var, json.encode (res))
end
function QuickApp : onInit ()
self: debug ( "RPC server" , plugin.mainDeviceId)
end
QA Client:
---------------------------------------------------------- -------- -------- -------------
local function rpc (id, timeout, fun, ...)
local v = "RPC" ..tostring ({}): sub ( 10 )
api.post ( "/ globalVariables" , {name = v})
fibaro.call (id, "RPC" , {id = plugin.mainDeviceId, var = v, fun = fun, args = {...}})
local t, res = os.time () + timeout, { false , "timeout" }
while os.time () <= t do
res = fibaro.getGlobalVariable (v)
print (type (res))
if res and res ~ = "" then res = json.decode (res) break end
end
api.delete ( "/ globalVariables /" ..v)
if res [ 1 ] then return select ( 2 , table.unpack (res)) else error (res [ 2 ]) end
end
local function rpcFun (id, timeout, fun) return function (...) return rpc (id, timeout, fun, ...) end end
function QuickApp : onInit ()
self: debug ( "onInit" , plugin.mainDeviceId)
local foo = rpcFun ( 240 , 5 , "foo" )
for i = 1 , 10 do print (foo ( 8 , i)) end
end
---------------------------------------------------------- -------- -------- -------- -------------