--curl  http://domogeek.entropialux.com/vigilance/56/

require "scripts/lua/functions"

--recupere les minutes
time=os.time()
minutes=tonumber(os.date('%M',time))
heures=tonumber(os.date('%H',time))

commandArray = {}

-- Function to update a switch
function updateIP()
  local idx = 1144
  local cmd = "curl -k -y 1 https://api4.my-ip.io/ip"
  local vac = os.capture(cmd, true)

  print("IP actuelle"..vac)
  local level = 0

  local old_ip = otherdevices_svalues['Mon_IP']
  if old_ip ~= vac then
    commandArray['SendNotification']='Changement IP '..vac..'#Attention changement IP '..vac..' #0'
  
    print("Recherche ip publique " .. cmd .. ": "..vac)
    commandArray['UpdateDevice']=idx..'|'..level..'|'..vac
    commandArray['SendEmail']='Changement IP#'..ip..'#souti@free.fr'
    return vac
  else
    print("Pas de changement IP : "..vac..' '..old_ip)
    commandArray['UpdateDevice']=idx..'|'..level..'|'..vac
    return vac
  end
end


time = os.date("*t")

if heures%3 == 0 and minutes % 10 == 8 then
  local old_ip = otherdevices_svalues['Mon_IP']
  local cmd = "curl -s -m 5 \"http://".. old_ip ..":81/name.txt\""
  print("Cmd="..cmd)
  local ok = os.capture(cmd, true)
  print("Ok="..ok .." IP="..old_ip)
  if ok ~= "Akhenaton" then
    print("updateIP")
    ip = updateIP()
    --commandArray['SendEmail']='Changement IP#'..ip..'#souti@free.fr'
  end
end

return commandArray
