<?php
ini_set('max_execution_time', 300); //300 seconds = 5 minutes
//this file will be executed every 00:05 every day...
include_once("embedded/functions/site_functions.php");
db_open();
//tsekarei tin vasi gia na krataei mono sigkekrimena dedomena pou tha xrhsimopoiountai sta diagrammata
$year=date("Y");
$month=date("m");
$day=date("d",strtotime("-1 day"));
$dayy=date("D",strtotime("+1 day"));
$date=date("Y-m-d",strtotime("-1 day"));
$datex=date("Y-m-d",strtotime("-1 month",strtotime($date)));
$m=date("F",strtotime($date));
$mm=date("m",strtotime($date));
$t=date("t",strtotime($date));
$fday=date("D",mktime(0,0,0,$mm,1,$year));
$sday=date("D",mktime(0,0,0,$mm,$t,$year));

$sql="SELECT * FROM users WHERE userlevel='1' AND active='1'";
@ $resul=mysql_query($sql);
if (mysql_numrows($resul)!=0)
	{
	while($row=mysql_fetch_array($resul))
		{
		$device_id=$row['device_id'];
$expression="";
$time2="";
$change="";		
for ($aaa=23;$aaa>=0;$aaa--)
{
$temp_sum=0;
$hum_sum=0;
$inli_sum=0;
$somo_sum=0;
$fan_sum=0;
$watering_sum=0;
$lighting_sum=0;
	if($aaa<=9)
	{
	$aaa='0'.$aaa;
	}							
	$aaaa=$aaa.":00:59";
	$sqla="SELECT * FROM data WHERE device_id='$device_id' AND dater='$date' AND timer ='$aaaa'";
	@ $resulta=mysql_query($sqla);
	echo $aaaa;
	if (mysql_numrows($resulta)==0)
	{
		$change=$change.'1';
		for($mi=0;$mi<=59;$mi++)
		{
		if ($mi<=9)
			{
			$mi='0'.$mi;
			}
			
		$timer=$aaa.":".$mi.":00";
		$sql1="SELECT * FROM data WHERE device_id='$device_id' AND dater='$date' AND timer ='$timer'";
		@ $resulta=mysql_query($sql1);
		if ($resulta!=0)
			{
			$row=mysql_fetch_array($resulta);
			$temp_sum=$temp_sum + $row['temp'];
			$hum_sum=$hum_sum + $row['hum'];
			$somo_sum=$somo_sum +$row['somo'];
			$inli_sum=$inli_sum +$row['inli'];
			$fan_sum=$fan_sum+$row['fan'];
			$watering_sum=$watering_sum+$row['watering'];
			$lighting_sum=$lighting_sum+$row['lighting'];
			$sql2="DELETE FROM data WHERE device_id='$device_id' AND dater='$date' AND timer ='$timer'";
			@ $result2=mysql_query($sql2);
			}
		else
			{
			$temp_sum=$temp_sum + 0;
			$hum_sum=$hum_sum +0;
			$somo_sum=$somo_sum +0;
			$inli_sum=$inli_sum +0;
			$fan_sum=$fan_sum+0;
			$watering_sum=$watering_sum+0;
			$lighting_sum=$lighting_sum+0;
			}
		}
	$time_sum=$aaa.":00:59";
	$a=estimate_sum($div=60,$temp_sum,$hum_sum,$inli_sum,$somo_sum,$fan_sum,$watering_sum,$lighting_sum);
	$sql3="INSERT INTO data(data_id,device_id,dater,timer,temp,hum,inli,somo,fan,watering,lighting) 
	VALUES(NULL,'1','$date','$time_sum','$a[0]','$a[1]','$a[2]','$a[3]','$a[4]','$a[5]','$a[6]')";//vazoume to meso oro wras
	@ $resulta = mysql_query($sql3);
	}
}
if ($change!="")
	{
	$sql4="SELECT * FROM data WHERE device_id='$device_id' AND dater='$date' AND timer='23:00:58'";
	@ $resultaa=mysql_query($sql4);
	if (mysql_numrows($resultaa)!=0)
		{
		$sql2="DELETE FROM data WHERE device_id='$device_id' AND dater='$date' AND timer='23:00:58'";
		@ $result2=mysql_query($sql2);
		}
	$temp_sum=0;
	$hum_sum=0;
	$inli_sum=0;
	$somo_sum=0;
	$fan_sum=0;
	$watering_sum=0;
	$lighting_sum=0;							
	for($i=0;$i<=23;$i++)
		{
		if ($i<=9)
			{
			$i='0'.$i;
			}
		$timer=$i.":00:59";//mesos oros wras
		$sql4="SELECT * FROM data WHERE device_id='$device_id' AND dater='$date' AND timer='$timer'";
		@ $resultaa=mysql_query($sql4);
		if ($resultaa!=0)
			{
			$row=mysql_fetch_array($resultaa);
			$temp_sum=$temp_sum + $row['temp'];
			$hum_sum=$hum_sum + $row['hum'];
			$somo_sum=$somo_sum +$row['somo'];
			$inli_sum=$inli_sum +$row['inli'];
			$fan_sum=$fan_sum+$row['fan'];
			$watering_sum=$watering_sum+$row['watering'];
			$lighting_sum=$lighting_sum+$row['lighting'];
			}
		else
			{
			$temp_sum=$temp_sum + 0;
			$hum_sum=$hum_sum +0;
			$somo_sum=$somo_sum +0;
			$inli_sum=$inli_sum +0;
			$fan_sum=$fan_sum+0;
			$watering_sum=$watering_sum+0;
			$lighting_sum=$lighting_sum+0;
			}								
		}
		$time_sum="23:00:58";//meso oro meras
		$a=estimate_sum($div=24,$temp_sum,$hum_sum,$inli_sum,$somo_sum,$fan_sum,$watering_sum,$lighting_sum);
		$sql3="INSERT INTO data(data_id,device_id,dater,timer,temp,hum,inli,somo,fan,watering,lighting) 
		VALUES(NULL,'1','$date','$time_sum','$a[0]','$a[1]','$a[2]','$a[3]','$a[4]','$a[5]','$a[6]')";
		@ $resulta = mysql_query($sql3);//vazoume meso oro 	meras
	}
//edw teleiwnei h ekkatharish..
//ekkatharisi skentioul senariwn 3 mhnes prin
if ($day==$t)
{
$e=$month-3;
	$k=days_in_month($e,date("Y"));
	for ($j=1;$j<=$k;$j++)
		{
		$date=$year."-".$e."-".$j;
		$sqld="DELETE FROM schedule_scenarios WHERE day='$date'";
		@ $result=mysql_query($sqld);
		}
}
}}
//edw kwdikas pou stelnei diagrammata
$sqll="SELECT * FROM users WHERE userlevel='1' AND active='1'";
@ $ap=mysql_query($sqll);
	if (mysql_numrows($ap)!=0)
		{
		while ($row=mysql_fetch_array($ap))
			{
			$device_overview=$row['device_overview'];
			$sensor_overview=$row['sensor_overview'];
			$e_mail_user=$row['email'];
			$device_id=$row['device_id'];
			$email=array();
			$subject_s="";
			$subject_d="";
			$user_id=$row['user_id'];
			$language=$row['language'];
			$l=0;
			if ($day==$t)
			{//ekkatharisi diagrammatwn proigoumenou mina..
			$sql="SELECT * FROM charts WHERE device_id='$device_id' AND dater BETWEEN '$datex' AND '$date'";
			@ $result=mysql_query($sql);
			if (mysql_numrows($result)!=0)
				{
				while($row=mysql_fetch_array($result))
					{
					$filename=$row['filename'];
					if ($row['folder']==1)//diagramata gia sensors
						{
						$filename='charts/sensors/'.$filename.'.png';
						}
					else//diagrammata gia devices
						{
						$filename='charts/devices/'.$filename.'.png';
						}
					unlink($filename);	
					}
				}
			}	
			if ($device_overview!=0)
				{
				$msg=array();
				if ($device_overview==1)//mera
					{
					//final msg
					$msg[0]="_".$m."_".$day."_".$year;
					$msg[1]="_".$m."_".$day."_".$year;
					$msg[2]="_".$m."_".$day."_".$year;
					//d_msg
					$msg[6]=$year.'-'.$mm.'-'.$day;		
					$msg[7]=$year.'-'.$mm.'-'.$day;
					$msg[8]=$year.'-'.$mm.'-'.$day;
					//period
					$msg[9]="day";
					$msg[10]="day";
					$msg[11]="day";		
					if($language!=1)
						{
						$subject_d="Daily overview over devices";
						}
					else
						{
						$subject_d="Καθημερινή Αναφορά χρήσης συσκευής";
						}
					}
				else if ($device_overview==2)//vdomada
					{
					if($dayy==$fday)//check an einai telos vdomadas
						{
						$date1=$year."-".$mm."-01";
						$dif=floor((strtotime($date) - strtotime($date1))/86400/7)+1;
						//final msg
						$msg[0]="_".$m."_Week_".$dif."_".$year;
						$msg[1]="_".$m."_Week_".$dif."_".$year;
						$msg[2]="_".$m."_Week_".$dif."_".$year;
						//d_msg
						$msg[6]=$year.'-'.$m.'-'.$dif;	
						$msg[7]=$year.'-'.$m.'-'.$dif;
						$msg[8]=$year.'-'.$m.'-'.$dif;
						//period
						$msg[9]="week";
						$msg[10]="week";
						$msg[11]="week";
						if ($language!=1)
							{
							$subject_d="Weekly overview over devices";
							}
						else
							{
							$subject_d="Εβδομαδιαία αναφορά χρήσης συσκευής";
							}
						}
					else
						{
						for($i=0;$i<=11;$i++)
							{
							$msg[$i]="";
							}
						}
					}
				else if ($device_overview==3)//mhnas
					{
					if ($day==$t)//check an einai telos mhna
						{
						$msg[0]="_".$m."_".$year;
						$msg[1]="_".$m."_".$year;
						$msg[2]="_".$m."_".$year;
						//d_msg
						$msg[6]=$year.'-'.$m;	
						$msg[7]=$year.'-'.$m;
						$msg[8]=$year.'-'.$m;
						//period
						$msg[9]="month";
						$msg[10]="month";
						$msg[11]="month";
						if ($language!=1)
							{
							$subject_d="Monthly overview over devices";
							}
						else
							{
							$subject_d="Μηνιαία αναφορά χρήσης συσκευής";
							}				
						}
					else
						{
						for($i=0;$i<=11;$i++)
							{
							$msg[$i]="";
							}
						}	
					}
				else
					{
					if ($date==$year.'-12-31')//xronos
						{
						$msg[0]='_'.$year;
						$msg[1]='_'.$year;
						$msg[2]='_'.$year;
						//d_msg
						$msg[6]=$year;	
						$msg[7]=$year;
						$msg[8]=$year;
						//period
						$msg[9]="year";
						$msg[10]="year";
						$msg[11]="year";	
						if ($language!=1)
							{
							$subject_d="Annualy overview over devices";
							}
						else
							{
							$subject_d="Ετήσια αναφορά χρήσης συσκευής";
							}
						}
					else
						{
						for($i=0;$i<=11;$i++)
							{
							$msg[$i]="";
							}
						}	
					}
				if (!empty($msg[1]))
					{
					$msg[3]="fan";
					$msg[4]="watering";
					$msg[5]="lighting";
					for($i=0;$i<=2;$i++)
						{
						$e=1;
						$aa=$i+9;
						$period=$msg[$aa];
						$final_msg=$msg[$i];
						$aaa=$i+6;
						$d_msg=$msg[$aaa];
						$x=$i+3;
						$device=$msg[$x];
						include("embedded/functions/graph_device.php");
						if($path!="" && $image!="")
							{
							$email[$l]=$path;//path ths eikonas
							$period=$device=$final_msg=$d_msg='';
							$l++;
							}
						
						}
					}
				}
			if ($sensor_overview!=0)//GIA AISHTITIRES
				{
				$msg=array();
				if ($sensor_overview==1)//mera
					{
					//final msg
					$msg[0]="_".$m."_".$day."_".$year;
					$msg[1]="_".$m."_".$day."_".$year;
					$msg[2]="_".$m."_".$day."_".$year;
					$msg[3]="_".$m."_".$day."_".$year;
					//d_msg
					$msg[8]=$year.'-'.$mm.'-'.$day;		
					$msg[9]=$year.'-'.$mm.'-'.$day;
					$msg[10]=$year.'-'.$mm.'-'.$day;
					$msg[11]=$year.'-'.$mm.'-'.$day;
					
					//period
					$msg[12]="day";
					$msg[13]="day";
					$msg[14]="day";
					$msg[15]="day";
					if ($language!=1)
						{
						$subject_s="Daily overview over sensors";
						}
					else
						{
						$subject_s="Καθημερινή αναφορά χρήσης αισθητήρων";
						}
										
					}
				else if ($sensor_overview==2)//vdomada
					{
					if($dayy==$fday)//check an einai telos vdomadas
						{
						$date1=$year."-".$mm."-01";
						$dif=floor((strtotime($date) - strtotime($date1))/86400/7)+1;
						//final msg
						$msg[0]="_".$m."_Week_".$dif."_".$year;
						$msg[1]="_".$m."_Week_".$dif."_".$year;
						$msg[2]="_".$m."_Week_".$dif."_".$year;
						$msg[3]="_".$m."_Week_".$dif."_".$year;
						
						//d_msg
						$msg[8]=$year.'-'.$m.'-'.$dif;	
						$msg[9]=$year.'-'.$m.'-'.$dif;
						$msg[10]=$year.'-'.$m.'-'.$dif;
						$msg[11]=$year.'-'.$m.'-'.$dif;
						//period
						$msg[12]="week";
						$msg[13]="week";
						$msg[14]="week";
						$msg[15]="week";
						if ($language!=1)
							{
							$subject_s="Weekly overview over sensors";
							}
						else
							{
							$subject_s="Εβδομαδιαία αναφορά χρήσης αισθητήρων";
							}						
						}
					else
						{
						for($i=0;$i<=15;$i++)
							{
							$msg[$i]="";
							}
						}
					}
				else if ($sensor_overview==3)//mhnas
					{
					if ($day==$t)//check an einai telos mhna
						{
						$msg[0]="_".$m."_".$year;
						$msg[1]="_".$m."_".$year;
						$msg[2]="_".$m."_".$year;
						$msg[3]="_".$m."_".$year;
						//d_msg
						$msg[8]=$year.'-'.$m;	
						$msg[9]=$year.'-'.$m;
						$msg[10]=$year.'-'.$m;
						$msg[11]=$year.'-'.$m;
						//period
						$msg[12]="month";
						$msg[13]="month";
						$msg[14]="month";
						$msg[15]="month";
						if ($language!=1)
							{
							$subject_s="Monthly overview over sensors";
							}
						else
							{
							$subject_s="Μηνιαία αναφορά χρήσης αισθητήρων";
							}					
						}
					else
						{
						for($i=0;$i<=15;$i++)
							{
							$msg[$i]="";
							}
						}	
					}
				else
					{
					if ($date==$year.'-12-31')//xronos
						{
						$msg[0]='_'.$year;
						$msg[1]='_'.$year;
						$msg[2]='_'.$year;
						$msg[3]='_'.$year;
						//d_msg
						$msg[8]=$year;	
						$msg[9]=$year;
						$msg[10]=$year;
						$msg[11]=$year;
						//period
						$msg[12]="year";
						$msg[13]="year";
						$msg[14]="year";
						$msg[15]="year";
						if ($language!=1)
							{
							$subject_s="Annual overview over sensors";
							}
						else
							{
							$subject_s="Ετήσια αναφορά χρήσης αισθητήρων";
							}								
						}
					else
						{
						for($i=0;$i<=15;$i++)
							{
							$msg[$i]="";
							}
						}	
					}
				if (!empty($msg[1]))
					{
					$msg[4]="temperature";
					$msg[5]="humidity";
					$msg[6]="indoorlighting";
					$msg[7]="soilmoisture";
					for($i=0;$i<=3;$i++)
						{
						$e=1;
						$aa=$i+12;
						$period=$msg[$aa];
						$final_msg=$msg[$i];
						$aaa=$i+8;
						$d_msg=$msg[$aaa];
						$x=$i+4;
						$sensor=$msg[$x];
						include("embedded/functions/graph_sensor.php");
						if($path!="" && $image!="")
							{
							$email[$l]=$path;//path ths eikonas
							$period=$sensor=$final_msg=$d_msg='';
							$l++;
							}
						}
					}
				}
			if (!empty($email))
				{
				$now_date=date("Y-m-d");
				$now_time=date("H:i:s");
				$subject="Smart Greenhouse - ";
				$subject=$subject."-".$subject_s."-".$subject_d;
				$headers = "From:smartgreenhouse";
				$message="";
				$a=mail_with_attachments($e_mail_user,$subject,$message,$headers,$email);
				if ($a!=1)
					{
					$sql="INSERT INTO troubleshoots (troubleshoot_id,user_id,dater,timer,problem) VALUES (null, '$user_id', '$now_date', '$now_time', '8')";
					@ $result=mysql_query($sql);
					}
				
				}
		
			}
		}
//telos kwdika diagrammatwn

db_close();
?>