<?php
///////////Configuracion/////////////////////

$realtimefile = '/var/www/html/CumulusMX/realtime.txt';
$realtimecachefile = '/var/www/html/CumulusMX/realtimecache.txt';
$umbraltormenta = 4; //umbral en mm sobre el cual se considera tormenta
$tiempocomprobacion = 10; //minutos para la comprobación de la tormenta
$tiemporeanudacion = 120; //minutos comprobacion de reanudacion de lluvia
$direccionaenviar = '';
$test = false; //true, probar correo. false, no probar

/////////////////////////////////////////////

if ($test) {mail($direccionaenviar, "Prueba", ''); exit;}

$mensaje = "";

$realtime = explode(' ', file_get_contents($realtimefile));
$fecha = explode('/', $realtime[0]);
$fechar = $fecha[0].'-'.$fecha[1].'-20'.$fecha[2].' '.$realtime[1];
$lluvia = $realtime[9];

if (!file_exists($realtimecachefile)) file_put_contents($realtimecachefile, $fechar.','.$lluvia);
if (date('H:i') == "00:00") file_put_contents($realtimecachefile, $fechar.',0.0');

$fecha = strtotime($fechar);
$realtimecache = explode(',', file_get_contents($realtimecachefile));
$fechacache = strtotime($realtimecache[0]);
$lluviacache = $realtimecache[1];
$diff = $fecha - $fechacache;

$difflluvia = $lluvia - $lluviacache;

if ($lluvia == 0 or $difflluvia == 0) exit;
if ($lluviacache == 0 and $lluvia > 0) $mensaje = utf8_decode("Primera lluvia del día ").$lluvia." mm";
if ($difflluvia > 0 and $lluviacache > 0 and $diff >= $tiemporeanudacion * 60) $mensaje = "Vuelve a llover ".$lluvia." mm";
if ($difflluvia >= $umbraltormenta and $diff >= $tiempocomprobacion * 60) $mensaje = "Tormenta ".$lluvia." mm";

file_put_contents($realtimecachefile, $fechar.','.$lluvia);

if ($mensaje) mail($direccionaenviar, $mensaje, implode(' ', $realtime)); 


?>
