Aller au contenu
Franco268

tonumber et variable globale

Recommended Posts

Bonjour,

 

J'essaie de récupérer les id des utilisateurs que j'ai défini dans des variables globales et que j'utilise pour envoyer des notifications

Il y a la première méthode qui fonctionne et la seconde qui ne fonctionne pas. Savez-vous pourquoi? (la différence est dans la fonction majvariable)

 

Version ok:

function MajVariables()
	IdUtilisateurA = fibaro.getGlobalVariable("IdUtilisateurA");
	IdUtilisateurA = tonumber(IdUtilisateurA)
	IdUtilisateurB = fibaro.getGlobalVariable("IdUtilisateurB");
	IdUtilisateurB = tonumber(IdUtilisateurB)
end

function QuickApp:onInit()
    if debug then self:debug("onInit") end
	MajVariables()
    self:loop()
end
function QuickApp:btnActivation_onReleased(event)
	self:setVariable("NotifBAL", "Activé")
	self:updateView("lblNotif", "text", "Notification: " ..self:getVariable("NotifBAL"))
	
	fibaro.alert('push', {[1] = IdUtilisateurA, [2] = IdUtilisateurB, }, 'Les notifications push')
	fibaro.alert('email', {[1] = IdUtilisateurA, [2] = IdUtilisateurB, }, 'Les notifications mail')
	
end

function QuickApp:loop()
	self:main()
    fibaro.setTimeout(1*1000, function() --Fréquence de la loop
        self:loop()
    end)
end
function QuickApp:main()
end

Version NOK:

function MajVariables()
	IdUtilisateurA = tonumber(fibaro.getGlobalVariable("IdUtilisateurA"));
	IdUtilisateurB = tonumber(fibaro.getGlobalVariable("IdUtilisateurB"));
end

function QuickApp:onInit()
    if debug then self:debug("onInit") end
	MajVariables()
    self:loop()
end
function QuickApp:btnActivation_onReleased(event)
	self:setVariable("NotifBAL", "Activé")
	self:updateView("lblNotif", "text", "Notification: " ..self:getVariable("NotifBAL"))
	
	fibaro.alert('push', {[1] = IdUtilisateurA, [2] = IdUtilisateurB, }, 'Les notifications push')
	fibaro.alert('email', {[1] = IdUtilisateurA, [2] = IdUtilisateurB, }, 'Les notifications mail')
	
end

function QuickApp:loop()
	self:main()
    fibaro.setTimeout(1*1000, function() --Fréquence de la loop
        self:loop()
    end)
end
function QuickApp:main()
end

 

  • Thanks 1

Partager ce message


Lien à poster
Partager sur d’autres sites

fibaro.getGlobalVariable returns 2 values. String value of variable and time it was last changed.

Both these values are then passed to tonumber as arguments

The problem is that tonumber can take a second argument, the base for the string->number conversion.

In this case it will interpret the change time as the base which is probably out of range.

 

You can throw away all but the first returned value by putting the expression within parentheses.

UserIDA = tonumber (( fibaro.getGlobalVariable ( " UserIDA " )))

This will work...

 

Modifié par jang
  • Like 4

Partager ce message


Lien à poster
Partager sur d’autres sites

Big thx Jang,

 

Your explanation seems to me very clear AND it works!

 

But I tried the following, and it works as well, could explain the reason why?

Why 2 brackets front fibaro is needed in my tried and your solution?

IdUtilisateurA = tonumber((fibaro.getGlobalVariable("IdUtilisateurA")))

 

 

Partager ce message


Lien à poster
Partager sur d’autres sites

Sorry, 'time' was not necessary in the last example (cut&paste error...)

Another example is json.decode that takes a second optional value in QAs, not in Scenes! They have different implementations.

In QAs this will then give an error

a = json.decode(fibaro.getGlobalVariable("X"))

as json.decode will expect the second argument to be a table with parsing options, and in this case gets the modification time, and will throw an error.

Solution, like the last example is

a = json.decode((fibaro.getGlobalVariable("X")))

 

  • Like 2

Partager ce message


Lien à poster
Partager sur d’autres sites

×