---------------------------------
-- Script de détection de présence via livebox
-- Auteur : Sopalin
-- Date : 22 Juillet 2016
-- Nécessite un switch classique
-- source :

---------------------------------
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))


commandArray={}

function testActive(name, active)
  if active then
    --commandArray[name]='On'
    switchIfNeeded(name, 'On')
  else
    switchIfNeeded(name, 'Off')
    --commandArray[name]='Off'
  end
  debug("### Test active Name : "..name.." active "..tostring(active))
end

if heures == 8 then
  commandArray['Variable:Detection_Manon_20H'] = "0"
  commandArray['Variable:Detection_Theo_20H'] = "0"
  --uservariables['Detection_Manon_20H'] = 0
  --uservariables['Detection_Theo_20H'] = 0
  --print('Detection_Theo_20H='..string(uservariables['Detection_Theo_20H']))
end

--time = os.date("*t")
if (minutes % 5 == 4) then
--if (true) then

  --import des fontions pour lire le JSON
  json = (loadfile "/opt/domoticz/scripts/lua/JSON.lua")()

  --Récupération des informations sur la livebox
  --Utilisation d'un script VBS sous windows
  --Utilisation d'un script bash pour extraire les données
  ----------------------------------------------------------------
  --Lecture des données

  local f = assert(io.open("/tmp/Devices.txt","r"))
  local livebox = f:read('*all')
  f:close()
  --Décodage ud fichier
  local jsonLivebox = json:decode(livebox)
  if jsonLivebox == nil then
    return commandArray
  end

  --debug("DEBUG")
  --debug(timedifference(otherdevices_lastupdate['Presence Thomas']))

  commandArray['Domi']='Off'
  commandArray['Domi_Work']='Off'
  commandArray['Theo']='Off'
  commandArray['Moi']='Off'
  commandArray['Manon']='Off'
  commandArray['orangepizero']='Off'
  commandArray['jerome']='Off'
  commandArray['R53PP2003X0247']='Off'
  commandArray['R53PP2003X0247-1']='Off'

  devices = jsonLivebox['status']
  --Parcours du fichier pour vérifier la présence d'un téléphone
  for result,status in pairs(devices) do
    local name = status['Name']
      debug("Name : "..name.." Mac : "..status['Key'].." "..tostring(status['Active']))

    if (name == "Akhesa" or name == "Playstation3" or name == "DESKTOP-MUI1HPH" or name == "Theo") then
         -- testActive('Theo', status['Active'])
  	  if status['Active'] then
  	    commandArray['Theo']='On'
        if heures >= 19 then
          commandArray['Variable:Detection_Theo_20H'] = "1"
        end         
	    end
    end
    if (--name == "Nefertiti" or 
        string.find(name,"Jerome") or name == "Achille" or string.find(name, "Moi")
        or name == "iPhonedeJerome" or name == "iPhonedeJerome-1" or name == "jerome"
	or name == "Iphone-jerome"
      ) then
  	   if status['Active'] then
  	    commandArray['Moi']='On'
	     end
      end
      if (name == "DomiPro" or name == "Domi") then
          -- testActive('Domi', status['Active'])
  	  if status['Active'] then
  	    commandArray['Domi']='On'
	  end
      end
      if (name == "R53PP2003X0247-1" or name == "R53PP2003X0247") then
  	  if status['Active'] then
  	    commandArray['Domi_Work']='On'
	  end
      end
      if (name == "Akhenaton-3" or name == "Akhenaton") then
          testActive("Akhenaton", status['Active'])
      end
      if (name == "Manon" or name == "AcerManon" 
        or name == "DESKTOP-KH8V15B" or name == "iPhone-de-Manon" or name == "IPhonedeManon") 
      then
          -- testActive("Manon", status['Active'])
      	  if status['Active'] then
      	    commandArray['Manon']='On'
            if heures >= 20 then
              commandArray['Variable:Detection_Manon_20H'] = "1"
            end 
    	  end
      end

      if (name == "TeleChambre" or name == "AcerManon" 	or name == "Akhesa" or name == "AkhesaNew"
        or name == "Akhenaton" or name == "SoutiHP" or name == "Hackintosh"
	or name == "Octoprint" or name == "jerome"
        or name == "Volumio" or name == "LibreELEC" 
        or name == "Recovery") then
        testActive(name, status['Active'])
      end

      if (--name == "orangepizero" or name == "orangepizero-1" or 
        name == "orangepizero-2") then
        if status['Active'] then
          commandArray['orangepizero']='On'
        end
      end

      if (name == "RadiateurManon" or name == "RadiateurTheo" or name == "RadiateurBureau"
        or name == "RadiateurChambre") then
          if status['Active'] then
            -- nothing
          else
           -- commandArray['SendNotification']='Alerte radiateur '..name..'#Alerte radiateur ne repond pas au ping '..name
          end
      end
  end
end

return commandArray
