Aller au contenu
Bloug

Les Childs pour les Nuls

Recommended Posts

Bon là c'est d'un autre niveau ! En tout cas Bravo !

 

Il y a 15 heures, mprinfo a dit :

Dans sa librairie il y a un exemple
 

j'ai jamais trop cherché a regarder ... surement la peur de voir le loup ! :wacko: 

 

 

Partager ce message


Lien à poster
Partager sur d’autres sites

@jang 

 

I just did a test with your example library and .... 

You did it again ! :lol:

Capture.thumb.JPG.a42b48e25e9daf8ed30a917a230bbc8f.JPG

 

At the first recording of the main I have the creation of the children. At the next recording there is again the creation of 6 additional children . 

Is it still a problem with my Hc3 :huh:!?

 

Partager ce message


Lien à poster
Partager sur d’autres sites

Well, of course there was a small problem with my code.... Try this version

The load and creation of children is protected with a pcall.

do
  local childID = 'ChildID'
  local classID = 'ClassName'
  local defChildren

  local children = {}
  local undefinedChildren = {}
  local createChild = QuickApp.createChildDevice
  class 'QwikAppChild'(QuickAppChild)

  function QwikAppChild:__init(device) 
    QuickAppChild.__init(self, device)
    self:debug("Instantiating object ",device.name)
    local uid = self:getVariable(childID) or ""
    if defChildren[uid] then
      children[uid]=self               -- Keep table with all children indexed by uid. uid is unique.
    else                               -- If uid not in our children table, we will remove this child
      undefinedChildren[#undefinedChildren+1]=self.id 
    end
  end

  function QuickApp:createChildDevice(uid,props,interfaces,className)
    __assert_type(uid,'string')
    __assert_type(className,'string')
    props.initialProperties = props.initialProperties or {}
    local qas = {{name=childID,value=uid},{name=classID,value=className}}
    props.initialProperties.quickAppVariables = qas
    props.initialInterfaces = interfaces
    self:debug("Creating device ",props.name)
    return createChild(self,props,_G[className])
  end

  local function getVar(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:loadExistingChildren(chs)
    __assert_type(chs,'table')
    local stat,err = pcall(function()
        defChildren = chs
        self.children = children
        function self.initChildDevices() end
        local cdevs,n = api.get("/devices?parentId="..self.id) or {},0 -- Pick up all my children
        for _,child in ipairs(cdevs) do
          local uid = getVar(child,childID)
          local className = getVar(child,classID)
          local childObject = _G[className] and _G[className](child) or QuickAppChild(child)
          self.childDevices[child.id]=childObject
          childObject.parent = self
        end
      end)
    if not stat then self:error("loadExistingChildren:"..err) end
  end

  function QuickApp:createMissingChildren()
    local stat,err = pcall(function()
        for uid,data in pairs(defChildren) do
          if not self.children[uid] then
            local props = {
              name = data.name,
              type = data.type,
            }
            self:createChildDevice(uid,props,data.interfaces,data.className)           
          end
        end
      end)
    if not stat then self:error("createMissingChildren:"..err) end
  end

  function QuickApp:removeUndefinedChildren()
    for _,deviceId in ipairs(undefinedChildren) do -- Remove children not in children table
      self:removeChildDevice(deviceId)
    end
  end

end


-------------------------------
-- 1 - Definition of class
-------------------------------
class 'Class_Temp'(QwikAppChild) -- Class NOM_Temps pour "com.fibaro.temperatureSensor"

-------------------------------
-- 2 - Class constructor
-------------------------------
function Class_Temp:__init(device) 
  QwikAppChild.__init(self, device)
end

--------------------------------
-- 3 - Definition of children  
--------------------------------
local myChildren = {    -- Unique ID (uid) -> child initialization data, in our case the name
  ["Child_Temps1"] = {
    name="Température1", type="com.fibaro.temperatureSensor", className='Class_Temp',
    interfaces = {'battery'},
  },
  ["Child_Temps2"] = {
    name="Température2", type="com.fibaro.temperatureSensor", className='Class_Temp',
    interfaces = {'battery'},
  },
  ["Child_Temps3"] = {
    name="Température3", type="com.fibaro.temperatureSensor", className='Class_Temp',
    interfaces = {'battery'},
  },
  ["Child_Temps4"] = {
    name="Température4", type="com.fibaro.temperatureSensor", className='Class_Temp',
    interfaces = {'battery'},
  },
  ["Child_Temps5"] = {
    name="Température5", type="com.fibaro.temperatureSensor", className='Class_Temp',
    interfaces = {'battery'},
  },
  ["Child_Temps6"] = {
    name="Température6", type="com.fibaro.temperatureSensor", className='Class_Temp',
    interfaces = {'battery'},
  },
}

--------------------------------
-- 4 - Initialization of children  
--------------------------------
function QuickApp:onInit()

  self:loadExistingChildren(myChildren)

  self:createMissingChildren()

  self:removeUndefinedChildren()

  self:update_child("Child_Temps5")       -- example, update child with uid 'Child_Temps5'
--  for uid,_ in pairs(self.children) do  -- update all children
--    self:update_child(uid)
--  end
end

--------------------------------
-- 5 - Accessing children  
--------------------------------
function QuickApp:update_child(uid)
  local default_temp = 37               --- pour l'ex du tuto
  local default_log = " I'm bad "       --- pour l'ex du tuto

  self.children[uid]:updateProperty("value", default_temp)
  self.children[uid]:updateProperty("log", default_log)
end

 

Partager ce message


Lien à poster
Partager sur d’autres sites

Bonsoir à tous,

superbe tuto, un grand merci @Bloug.

Mais juste une question, à part le fait que ce soit un enfant, ça sert à quoi un "child"????

Partager ce message


Lien à poster
Partager sur d’autres sites

à payer les retraites des "old" :98:

 

C'est un QuickApp enfant, ça permet de puis un QuickApp parent de gérer plusieurs enfants, avec le même code LUA.

Exemples : Une station Netatmo : un module parent qui va chercher les infos sur le cloud Netatmo, et qui met à jour tous les enfants, avec un enfant pour chaque capteur : température, humidité, pression, etc

Partager ce message


Lien à poster
Partager sur d’autres sites

Hello, bravo pour le détail, j'ai une question naïve ... Comment trouver la liste des Types disponible pour les Childs ? :unsure:

Merci

 

Edit :

Je crois que j'ai trouvé : https://manuals.fibaro.com/document/hc3-quick-apps/

Modifié par TitiXsi

Partager ce message


Lien à poster
Partager sur d’autres sites

En fait la liste officielle est sur l'API de ta box :

/api/quickApp/availableTypes

En pratique, on a même constaté que tu peux utiliser certains types non listés dans l'API précédente.
C'est à dire qu'un QuickApp peut utiliser n'importe quel type disponible sur la box, incluant donc tous les types des différents modules Z-Waves, Zigbee, etc.

Voir :

/api/devices/hierarchy

En plus cette dernière liste a le bon gout d'être hiérarchisée, ce qui est intéressant pour découvrir les relations entre les modules de type proche.

  • Like 1

Partager ce message


Lien à poster
Partager sur d’autres sites

and if you run hierarchy through graphviz...

tree.thumb.png.31f7a4a6f9d9e79a460b7b863a8174f0.png

 

Modifié par jang
  • Like 2

Partager ce message


Lien à poster
Partager sur d’autres sites

×