-
Compteur de contenus
206 -
Inscription
-
Dernière visite
-
Jours gagnés
41
Tout ce qui a été posté par jang
-
Helper function? local function call(f,...) local args = {...} if type(f)=='number' then return setTimeout(function() args[1](select(2,unpack(args))) end,f) else return setTimeout(function() f(unpack(args)) end,0) end end function foo1(x) print(x) if x < 10 then call(foo1,x+1) -- 0s delay end end function foo2(x) print(x) if x < 10 then call(1000,foo2,x+1) -- 1s delay end end function QuickApp:onInit() foo1(0) foo2(0) end and function main() if ... then call(MySubFunction1,42) end if ... then call(MySubFunction2,17) end call(50,main) end function MySubFunction1(x) ... end function MySubFunction2(x) ... end
-
https://forum.fibaro.com/topic/49113-hc3-quickapps-coding-tips-and-tricks/?do=findComment&comment=206686
-
Yes, but potentially an interesting part of the problem Here are some add on to the QuickApp class to make child management easier. https://forum.fibaro.com/topic/49113-hc3-quickapps-coding-tips-and-tricks/?do=findComment&comment=209389 Reduces tasks to QuickApp:createChild{} and QuickApp:loadChildren() and fixes button interactions...
-
You could hijack the com.fibaro.sonosPlayer type class 'HEOSPlayer'(QuickAppChild) function HEOSPlayer:__init(dev) QuickAppChild.__init(self,dev) self.ip = dev.properties.ip self:trace("HEOS:",self.ip," initiated, deviceId:",self.id) end function HEOSPlayer:play_Button() self:trace("Play HEOS:",self.ip,", deviceId:",self.id) end function HEOSPlayer:pause_Button() self:trace("Pause HEOS:",self.ip,", deviceId:",self.id) end function HEOSPlayer:rewind_Button() self:trace("Rewind HEOS:",self.ip,", deviceId:",self.id) end function HEOSPlayer:forward_Button() self:trace("Forward HEOS:",self.ip,", deviceId:",self.id) end function HEOSPlayer:mute_on() self:trace("Mute HEOS:",self.ip,", deviceId:",self.id) end function HEOSPlayer:mute_off() self:trace("Unmute HEOS:",self.ip,", deviceId:",self.id) end function HEOSPlayer:volume_Slider(ev) self:trace("Volume HEOS:",self.ip,", deviceId:",self.id," Volume:",ev.values[1]) self:updateProperty("volume",ev.values[1]) self:updateProperty('ui.volume_Label.caption', tostring(ev.values[1])) end function HEOSPlayer:setInfo(label,title,creator) self:updateProperty('ui.position_Label.caption', "Label:"..label) self:updateProperty('ui.title_Label.caption', "Title:"..title) self:updateProperty('ui.creator_Label.caption', "Creator:"..creator) end local function getChildVariable(child,varName) for _,v in ipairs(child.properties.quickAppVariables or {}) do if v.name==varName then return v.value end end return "" end function QuickApp:UIHandler(event) local obj = self if self.id ~= event.deviceId then obj = self.childDevices[event.deviceId] end if not obj then return end local elm,etyp = event.elementName, event.eventType local cb = obj.uiCallbacks or {} if obj[elm] then return obj:callAction(elm, event) end if cb[elm] and cb[elm][etyp] then return obj:callAction(cb[elm][etyp], event) end if obj[elm.."Clicked"] then return obj:callAction(elm.."Clicked", table.unpack(event.values or {})) end self:warning("UI callback for element:", elm, " not found.") end function QuickApp:onInit() self:trace("Mother of HEOS, deviceId:",self.id) local cdevs = api.get("/devices?parentId="..self.id) or {} -- Pick up all my children function self:initChildDevices() end -- Null function, else Fibaro calls it after onInit()... if #cdevs ~= 2 then -- No children, create 2 local initChildData = { {className="HEOSPlayer", type="com.fibaro.sonosSpeaker", ip="1.2.3.4"}, {className="HEOSPlayer", type="com.fibaro.sonosSpeaker", ip="1.2.3.5"}, } for i,c in ipairs(initChildData) do local child = self:createChildDevice( {name = "HEOS "..i, type=c.type, initialProperties = { deviceIcon = 28, --icon = { path="plugins/com.fibaro.denonHeos/img/icon.png"}, ip = c.ip, }, -- Add properties if you need initialInterfaces = {}, -- Add interfaces if you need }, _G[c.className] -- Fetch class constructor from class name ) child:setVariable("className",c.className) -- Save class name so we know when we load it next time api.put("/devices/"..child.id,{properties={icon={path="plugins/com.fibaro.denonHeos/img/icon.png"}}}) end else for _,child in ipairs(cdevs) do local className = getChildVariable(child,"className") childObject = _G[className](child) self.childDevices[child.id]=childObject end end end
-
{ type = "date" , property = "cron" , operator = "match" , value = { "0/10" , "*" , "*" , "*" , "*" , "*" } } Toutes les 10 minutes
-
Almost... the parent asks the child to create himself, by calling the child's constructor. A child does not have a parent (is not aware of the parent) before being fully initialised (__init and _onInit are finished). When the child is fully initialised the parent sets the 'parent' field of the child to the parent. (in createChildDevice() and initChildDevices() )
-
=> You must make a call to setTimeout () to execute another function which will have the rights to access all objects setTimeout is the wrong weapon. child = self:createChildDevice(....) -- create new child, do minimal work here. child:Ask_State() -- child is completely setup - update state self:initChildDevices ({ [ "com.fibaro.binarySwitch" ] = IPX , }) -- create existing childs, , do minimal work here. for _,child in pairs(self.childDevices) do child:Ask_State() end -- childs are completely setup, update states
-
0 fonctionne également. Vous revenez de __init(). __init appelle :onInit() Mieux vaut mettre à jour les "child states" après "initChildDevices"
-
With my fibaroapiHC3.lua sdk I automatically crate a proxy on the HC3 for my QuickApp (running in ZeroBrane studio). The proxy QuickApp sends all actions/ui events back to the code in ZBS. A procy looks like: local function urlencode (str) return str and string.gsub(str ,"([^% w])",function(c) return string.format("%%% 02X",string.byte(c)) end) end local function POST2IDE(path,payload) url = "http://"..IP..path net.HTTPClient():request(url,{options={method='POST',data=json.encode(payload)}}) end local IGNORE={updateView=true,setVariable=true,updateProperty=true} -- Rewrite!!!! function QuickApp:actionHandler(action) if IGNORE[action.actionName] then return self:callAction(action.actionName, table.unpack(action.args)) end POST2IDE("/fibaroapiHC3/action/"..self.id,action) end function QuickApp:UIHandler(UIEvent) POST2IDE("/fibaroapiHC3/ui/"..self.id,UIEvent) end function QuickApp:CREATECHILD(id) self.childDevices[id]=QuickAppChild({id=id}) end function QuickApp:onInit() self:debug('Proxy Holidays',' deviceId:',self.id) IP = self:getVariable('PROXYIP') function QuickApp:initChildDevices() end end QuickApp:actionHandler / QuickApp:UIHandler
-
Je l'ai déjà résolu
-
self.parent est attribué une fois que __init est terminé.
-
Oui, ils diminuent l'intensité en abaissant les valeurs RGB ...
- 6 réponses
-
QuickApp function: turnOn () self: debug ("color controller turned on") self: updateProperty ("value", 99) end QuickApp function: turnOff () self: debug ("color controller turned off") self: updateProperty ("value", 0) end - Value is of integer (0-99) QuickApp function: setValue (value) self: debug ("color controller value set to:" .. tostring (value)) self: updateProperty ("value", value) end - Color is type of format r, g, b, w. - Eg. relaxing forest green, would look like this: 34,139,34,150 QuickApp function: setColor (r, g, b, w) local color = table.concat ({r, g, b, w}, ",") self: debug ("color controller color set to:", color) self: updateProperty ("color", color) end QuickApp function: onInit () self: debug ("onInit", self.name) self: turnOn () end
- 6 réponses
-
- 2
-
-
local function make_p() local x=0 return {variables = function() x=x+1; return x end} end local p = make_p() print(p.variables()) print(p.variables()) ----------------------- do local x=0 function p() x=x+1; return x end end print(p()) print(p()) --------------------- p = (function() local x = 0; return function() x=x+1; return x end end)() print(p()) print(p()) ---------------------
-
https://forum.fibaro.com/topic/49113-hc3-quickapps-coding-tips-and-tricks/?do=findComment&comment=206686
-
5.030.45 fibaro.debug("MyScene",sceneId)
-
Ça dépend. Vous devez décrire ces cas très spécifiques ...
-
Retour des fonctions QA https://forum.fibaro.com/topic/49113-hc3-quickapps-coding-tips-and-tricks/?do=findComment&comment=201165
-
for _,s in ipairs(api.get("/scenes")) do if s.content:match("hviojapivkveuhveuivhejnv") then print(s.id) end end :-)
-
'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.
-
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.
-
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
-
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
-
I'm giving up. I can't paste code in this forum I think I succeeded.
-
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 ---------------------------------------------------------- -------- -------- -------- -------------