Aller au contenu

jang

Membres confirmés
  • Compteur de contenus

    7
  • Inscription

  • Dernière visite

Réputation sur la communauté

4 Neutral

À propos de jang

  • Rang
    Nouveau

Profile Information

  • Sexe :
    Homme
  • Ville :
    Home
  • Box
    Home Center 2

Visiteurs récents du profil

248 visualisations du profil
  1. jang

    Besoin d'aide, code indigeste

    'post' takes a delay in millisecond as second argument (defaults to 0). This allows you to introduce delays between write/reads in the steps if necessary.
  2. jang

    Besoin d'aide, code indigeste

    You are a fast learner! 'self' is a local variable inside QuickApp: functions (it's part of the object oriented model) 'self' is not available inside ordinary functions like 'post' or the 'handlers' functions To have it accessible everywhere we assign it to a local called 'SELF' outside the functions.
  3. jang

    Besoin d'aide, code indigeste

    Like this (incomplete example...) local SELF,handlers = nil,nil local function post(ev,time) setTimeout(function() SELF:debug("STEP:",ev.type) handlers[ev.type](ev) end,time or 0) end handlers={ start = function(ev) SELF.soc.connect(self:getVariable("IP"),23 ,{ success = function(result) post({type='connected',res=result}) end, error(msg) SELF:debug("Error:",msg) end }) end, connected = function(ev) print("SUCCESS CONNECTION") post({type='read',tag="CONNECT RESULT:",next='login'}) end, login = function(ev) SELF.soc.write("admin" .. "\ n",{ success=function(result) post({type='read', tag='LOGIN1:',next='password'}) end, error=function() ... end }) end, password = function(ev) SELF.soc.write("xxxxx" .. "\ n",{ success=function(result) post({type='read', tag='PASSWORD RESULT',next='whatever'}) end, error=function() ... end }) end, read = function(ev) SELF.soc.read({ success=function(result) SELF:debug(ev.tag,tostring(result)) post(ev.next) end, error = function(msg) SELF:debug("Error:",msg) end }) end, } function QuickApp:SwitchOn() SELF=self post({type='start'}) end See also https://forum.fibaro.com/topic/49113-hc3-quickapps-coding-tips-and-tricks/?do=findComment&comment=201413
  4. 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
  5. jang

    Valeur de Retour méthode QA

    I'm giving up. I can't paste code in this forum I think I succeeded.
  6. 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 ---------------------------------------------------------- -------- -------- -------- -------------
×