Page 1 sur 1

Sortir du script si...

Publié : 16 nov. 2016, 17:38
par cedric2975
Bonjour ou bonjoir

Je suis debutant en lua.
J'ai un script permettant de pinguer mes telephones et pour declencher des actions en fonction du resultat.

Code : Tout sélectionner

 if (ping_success_tel1 or ping_success_tel2)  and otherdevices['Presence']=='On'
    
    then
       print('Aucun changement')
    print('laissez moi sortir')
    end
     
Je souhaiterais que si cette condition est remplie, on sorte du script, car j'ai d'autre condition ensuite et je ne veux pas qu'elle soit lu

Re: Sortir du script si...

Publié : 16 nov. 2016, 18:52
par vil1driver
Salut,

Code : Tout sélectionner

If
Elseif
Elseif 
Elseif 
Else 
End
La première condition remplie empêche les suivantes

Sinon place quand tu veux (même plusieurs fois)

Code : Tout sélectionner

Return commandArray 
Dès que c'est lu le script est quitté

Re: Sortir du script si...

Publié : 16 nov. 2016, 18:54
par thier
Tu remplaces end par else. Comme ça si tes ping sont bons tu fais ton print sinon tu fais ce qui est après else

Re: Sortir du script si...

Publié : 16 nov. 2016, 21:18
par cedric2975
thier a écrit :Tu remplaces end par else. Comme ça si tes ping sont bons tu fais ton print sinon tu fais ce qui est après else
Ok merci.
Le problème est que si j'utilise un commandArray['device'] à la suite du else, a chaque echec du ping je vais ré écrire l'état du device, donc impossible d'utiliser un devicehanged par la suite.
Bref j'ai bidouillé et fini par trouver une solution qui fonctionne
Si une fonction existait pour sortir du script complètement, elle m'aurait simplifié la vie.

Re: Sortir du script si...

Publié : 16 nov. 2016, 21:21
par cedric2975
le script_time

Code : Tout sélectionner

commandArray = {}




function timedifference (s)
  year = string.sub(s, 1, 4)
  month = string.sub(s, 6, 7)
  day = string.sub(s, 9, 10)
  hour = string.sub(s, 12, 13)
  minutes = string.sub(s, 15, 16)
  seconds = string.sub(s, 18, 19)
  t1 = os.time()
  t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
  difference = os.difftime (t1, t2)

  return difference

end

time = os.date("*t")


if (otherdevices['Presence']=='Off'  or (otherdevices['Presence']=='On'and timedifference(otherdevices_lastupdate['Presence']) > 120) 
 then  
   ping_success_tel1=os.execute('ping -c1 192.168.1.1')
   ping_success_tel2=os.execute('ping -c1 192.168.1.24')
 --ping_success_ipad=os.execute('ping -c1 192.168.1.93')

    if ping_success_tel1 
      then
      commandArray['Cédric']='On'
      else
      commandArray['Cédric']='Off'
    end

 
    if ping_success_tel2  
     then
     commandArray['Cat']='On'
         if otherdevices['Presence']=='Off' 
          then 
          commandArray['OpenURL'] = 'https://smsapi.free-mobile.fr/sendmsg?user=xxxxxxxxxx&pass=xxxxxxxxxxxxxx&msg=Cat est rentrée.'
         end 
      else
          commandArray['Cat']='Off'
    end
end

return commandArray
Le script_device

Code : Tout sélectionner

commandArray = {}

time = os.date("*t")
t=tonumber(otherdevices_svalues['ConsigneT'])

-- quelqu'un arrive
  if (devicechanged['Cat'] =='On' or devicechanged['Cédric']=='On' ) and otherdevices['Presence']=='Off'      -- Si tel ok et qu'iln'y avas personne ,retour, roomba à la base et augmentation de la temprérature de 3 °c
    then
        commandArray['Presence']='On'
        commandArray['RoombaDock']='On'             
        t = t + 30
        commandArray['ConsigneT']='Set Level '..t
        commandArray['Variable:consigne']=otherdevices_svalues['ConsigneT']  --synchro
  end
-- tout le monde est parti
  if  ( (((devicechanged['Cat'] =='Off' and otherdevices['Cédric']=='Off' ) and otherdevices['Presence']=='On') or 
         ((devicechanged['Cédric'] =='Off' and otherdevices['cat']=='Off' ) and otherdevices['Presence']=='On') or 
         ((devicechanged['Cat'] =='Off'and devicechanged['Cédric']=='Off' ) and otherdevices['Presence']=='On')) ) --
  then
         commandArray['Presence']='Off'
          t = t - 30
         commandArray['ConsigneT']='Set Level '..t
         commandArray['Variable:consigne']=otherdevices_svalues['ConsigneT']
          if  (time.hour>8 and time.hour<19) then 
              commandArray['RoombaClean']='On'
              commandArray['OpenURL'] = 'https://smsapi.free-mobile.fr/sendmsg?user=xxxxxxxx&pass=xxxxxxxxxxxxx&msg=RoombaClean !!'
              print('Départ du Roomba.')
          end
  end
-- personne n'arrive,ni ne part...
    if (devicechanged['Cat'] =='Off' and devicechanged['Cédric']=='Off' )  and otherdevices['Presence']=='Off'
    
    then
       print('Aucun changement')
  end

return commandArray

Re: Sortir du script si...

Publié : 16 nov. 2016, 21:34
par vil1driver
Tu peux tres bien tester si ton device est bien a Off avant de le passer a On et uniquement dans ce cas ;)

Ps: J'ai repondu a ta question pour sortir du script

Re: Sortir du script si...

Publié : 16 nov. 2016, 21:44
par cedric2975
vil1driver a écrit :Tu peux tres bien tester si ton device est bien a Off avant de le passer a On et uniquement dans ce cas ;)

Ps: J'ai repondu a ta question pour sortir du script
J avais essayer avec un return commandArray , mais j'avais des soucis avec les end, j aurais du approfondir peut etre