Aller au contenu

jang

Membres confirmés
  • Compteur de contenus

    209
  • Inscription

  • Dernière visite

  • Jours gagnés

    42

Tout ce qui a été posté par jang

  1. jang

    Valeur de Retour méthode QA

    Unfortunately, you were just lucky that the answer came during the 10ms of your setTimeout. When you enter the while-loop nothing else will run in the same QA. In fact, nothing happens in parallel in a QA, not call-backs, not calls to QuickApp: function etc. You need to constantly give time to other parts of your code with setTimeout. My example works because it's between two different QAs. If you use it within the same QA you need to use callbacks and setTimeout loop - not as elegant / simple. Ex. (Server same) Customer: local function rpc(callback,id,timeout,fun,...) local v,args = "RPC"..tostring({}):sub(10),{...} api.post("/globalVariables",{name=v}) setTimeout(function() fibaro.call(id,"RPC",{id=plugin.mainDeviceId,var=v,fun=fun,args=args}) end,0) local t,res=os.time()+timeout local function loop() if os.time()<=t then local res = fibaro.getGlobalVariable(v) if res and res~= "" then api.delete("/globalVariables/"..v) res = json.decode(res) if res[1] then callback(select(2,table.unpack(res))) else print("RPC error:"..res[2]) end else setTimeout(loop,10) end else api.delete("/globalVariables/"..v) print("RPC Timeout:"..fun) end end loop() end local function rpcFun(id,timeout,fun) return function(cb,...) return rpc(cb,id,timeout,fun,...) end end function QuickApp:onInit() self:debug("onInit ",plugin.mainDeviceId) local foo = rpcFun(240,5,"foo") for i=1,10 do foo(print,8,i) end end
  2. jang

    Valeur de Retour méthode QA

    I'm giving up. I can't paste code in this forum I think I succeeded.
  3. jang

    Valeur de Retour méthode QA

    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 ---------------------------------------------------------- -------- -------- -------- -------------
  4. Salut, je suis nouveau sur Fibaro et pas et ne parle pas français - complètement dépent sur ​​Google Translate
×
×
  • Créer...