require "scripts/lua/functions"

--recupere les minutes
time=os.time()
seconds=tonumber(os.date('%S',time))
minutes=tonumber(os.date('%M',time))
heures=tonumber(os.date('%H',time))
jour=tonumber(os.date('%w',time))
mois=tonumber(os.date('%M',time))


--Initialise la commande de retour finale
commandArray={}

-- -----------------------------------------------------------------------
--recupere les minutes
-- -----------------------------------------------------------------------
if (true or minutes % 10 == 5) then
   debug('####### Lancement du check Presence ' ..heures..'h'..minutes)
   if (otherdevices['Vacances'] == 'On') then
      switchIfNeeded('Presence','Off')
   else
    if otherdevices['Domi'] == 'On' 
      or otherdevices['Moi'] == 'On' 
      or otherdevices['Theo'] == 'On'
      or otherdevices['Manon'] == 'On' 
    then
        debug('       Presence détectée par téléphone à la maison')
        switchIfNeeded('Presence','On')
    elseif (true) then
        switchIfNeeded('Presence','Off')
    elseif (
      (josdJourChome() or otherdevices['PresenceForcee'] == 'On') 
      and otherdevices['AbsenceFamille'] == 'Off') 
    then
        debug('       Jour Chômé / Presence normale à la maison')
        switchIfNeeded('Presence','On')
    else
        local jourS=josdGetJourSemaine()
	      debug("jour "..jourS)
        if otherdevices['AbsenceFamille'] == 'Off' then
            if (
                --(jourS == "mercredi" and (heures >= 13 or (heures == 12 and minutes >= 30)))
                  (heures >= 18 or (heures == 17 and minutes >= 30)) 
		              or (heures < 8 or (heures == 8 and minutes <= 10))
                  or otherdevices["Zone B"] == 'On'
                ) then
              debug('       Jour non Chômé pas de vacances '..tostring(heures))
              switchIfNeeded('Presence','On')
            else
              switchIfNeeded('Presence','Off')
            end
        else
            if (
              (
               (heures == 8 and minutes <= 15) or heures < 8)
                or heures >= 20
                or otherdevices['Moi'] == 'On'
               -- or otherdevices['Akhenaton'] == 'On'
                or otherdevices['Philae'] == 'On'
            ) then
              switchIfNeeded('Presence','On')
            else
              switchIfNeeded('Presence','Off')
            end
        end
      end
   end
end


-- http://cellier.home:8081/0/detection/start

-- http://192.168.1.3:8080/0/detection/pause 

-- http://cellier.home:8081/0/detection/start

-- http://192.168.1.3:8080/0/detection/start

return commandArray
