-
Compteur de contenus
209 -
Inscription
-
Dernière visite
-
Jours gagnés
42
Tout ce qui a été posté par jang
-
Should be - setTimeout (Main, 50) - not setTimeout (Main(), 50) Anyway, it looks ok. My point was that sometimes the success() will not be called until there are events available. e.g. > 50ms. You could think about how to do the filtering effective with some kind of dispatch table....
-
api.get("/refreshState returns when there are events available - or times out after ~30s with empty events. So you will not always poll every 50ms. I always do http:request("http://127.0.0.1:11111/api/refreshStates?last=" .. lastRefresh... these days so I will not block other timers. (Ex. QA_Toolbox) You could have a master QA that push events to your other QAs. Pseudo code: QA.id=42 { subscribers = {43,44} while(true) events = get refreshState if events then for _,id in ipairs(subscribers) do fibaro.call(id,"NEW_EVENTS",events) end end end } QA.id=43 { function QuickApp:NEW_EVENTS(events) ... end } QA.is=44 { function QuickApp:NEW_EVENTS(events) ... end } You could add additional filtering to only send relevant events to each QA.
-
Performance worries... function foo3(x,y) return (x or 5)+(y or 6) end function QuickApp:onInit() local function printf(...) self:debug(string.format(...)) end local n = 1000 local t0 = os.clock() for i=1,n do setTimeout(foo3,0) end local t1 = os.clock()-t0 printf("SetTimeout:%.3fms",1000*t1/n) t0 = os.clock() for i=1,n do call(foo3) end local t2 = os.clock()-t0 printf("Call:%.3fms",1000*t2/n) printf("Factor:%.2f",t2/t1) end [02.08.2020] [11:02:26] [DEBUG] [QUICKAPP1449]: SetTimeout:0.015ms [02.08.2020] [11:02:26] [DEBUG] [QUICKAPP1449]: Call:0.019ms [02.08.2020] [11:02:26] [DEBUG] [QUICKAPP1449]: Factor:1.28
-
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