<?php
if ((isset($_POST['device_password']) && !empty($_POST['device_password'])) && (isset($_POST['device_id']) && !empty($_POST['device_id']) && is_numeric($_POST['device_id'])) &&(isset($_POST['option']) && ($_POST['option']==1 || $_POST['option']==2 || $_POST['option']==3)))
{

include("arduino_functions.php");
db_open();
$device_id=strip_tags(htmlspecialchars(mysql_real_escape_string(trim($_POST['device_id']))));
$device_password=strip_tags(htmlspecialchars(mysql_real_escape_string(trim($_POST['device_password']))));
$option=strip_tags(htmlspecialchars(mysql_real_escape_string(trim($_POST['option']))));
$a=check_device($device_id,$device_password);
if ($a==1)
	{
	
	$sql="SELECT * FROM users WHERE device_id='$device_id'";
	@ $result=mysql_query($sql);
	if ($result!=0)
		{
		$row=mysql_fetch_array($result);
		$user_id=$row['user_id'];
		$changes=$row['changes'];
		$troubleshoot=$row['troubleshoot'];
		$lost=$row['lost'];
		$e=$row['email'];
		$language=$row['language'];
		}
	if ($option==1)//pairnei ton pinaka tis katastaseis.
		{
		$date=date("Y-m-d");
		$h=date("H");
		$m=date("i");
		$time=$h.':'.$m.':00';		
		$sql1 = "SELECT * FROM schedule_scenarios WHERE device_id='$device_id' AND status='1' AND day='$date' AND (start_time<='$time' AND end_time>='$time') LIMIT 1";
		@ $res=mysql_query($sql1);
		if (mysql_numrows($res)==1)
				{
				$row=mysql_fetch_array($res);
				$scenario_id=$row['scenario_id'];
				$sqll="SELECT * FROM scenarios WHERE device_id='$device_id' AND scenario_id='$scenario_id'";
				@ $re=mysql_query($sqll);
				if ($re!=0)
					{
					$row=mysql_fetch_array($re);
					$data=array();
					//temperature sensor variables
					$data[0][5]=$row['temp_rule'];
					$data[0][1]=$row['temp_prior'];
					if ($row['temp1']>$row['temp2'])
						{
						$data[0][2]=$row['temp2'];
						$data[0][3]=$row['temp1'];
						}
					else
						{
						$data[0][2]=$row['temp1'];
						$data[0][3]=$row['temp2'];
						}
					if($data[0][2]<=9)
						{
						$data[0][3]="0".$data[0][2];
						}
					if ($data[0][2]<=9)
						{
						$data[0][3]="0".$data[0][3];
						}
					if ($data[0][2]<=99)
						{
						$data[0][2]="0".$data[0][2];
						}
					if ($data[0][3]<=99)
						{
						$data[0][3]="0".$data[0][3];
						}
					$data[0][4]=$row['temp_status'];
					$data[0][0]="1";//temp_rule
					//humidity sensor variables
					$data[1][5]=$row['hum_rule'];
					$data[1][1]=$row['hum_prior'];
					if ($row['hum1']>$row['hum2'])
						{
						$data[1][2]=$row['hum2'];
						$data[1][3]=$row['hum1'];
						}
					else
						{
						$data[1][2]=$row['hum1'];
						$data[1][3]=$row['hum2'];
						}
					if($data[1][2]<=9)
						{
						$data[1][2]="0".$data[1][2];
						}
					if ($data[1][3]<=9)
						{
						$data[1][3]="0".$data[1][3];
						}
					if ($data[1][2]<=99)
						{
						$data[1][2]="0".$data[1][2];
						}
					if ($data[1][3]<=99)
						{
						$data[1][3]="0".$data[1][3];
						}

					$data[1][4]=$row['hum_status'];
					$data[1][0]="2";//humidity rule
					
					//indoor lighting sensor variables
					$data[2][5]=$row['inli_rule'];
					$data[2][1]=$row['inli_prior'];
					if ($row['inli1']>$row['inli2'])
						{
						$data[2][2]=$row['inli2'];
						$data[2][3]=$row['inli1'];
						}
					else
						{
						$data[2][2]=$row['inli1'];
						$data[2][3]=$row['inli2'];
						}
					if($data[2][2]<=9)
						{
						$data[2][2]="0".$data[2][2];
						}
					if ($data[2][3]<=9)
						{
						$data[2][3]="0".$data[2][3];
						}
					if ($data[2][2]<=99)
						{
						$data[2][2]="0".$data[2][2];
						}
					if ($data[2][3]<=99)
						{
						$data[2][3]="0".$data[2][3];
						}
					$data[2][4]=$row['inli_status'];
					$data[2][0]="3";//indoor lighting rule
					
					//soil moisture variables
					$data[3][5]=$row['somo_rule'];
					$data[3][1]=$row['somo_prior'];
					$data[3][2]="00".$row['somo_limit'];
					$data[3][3]="000";
					$data[3][4]=$row['somo_status'];
					$data[3][0]="4";//indoor lighting rule

					//device variable output F V L
					$f=$row['fan'];
					$v=$row['watering'];
					$l=$row['lighting'];
					for($d=0;$d<=3;$d++)
							{
							$arr[$d]=$data[$d][1];
							}
						array_multisort($arr,$data);
					echo "*";	
					echo $f.$v.$l;		
					for($d=0;$d<=3;$d++)
							{
							for($i=0;$i<=5;$i++)
								{	if($data[$d][4]!=0)
									{
										if ($i!=4 && $i!=1)
										{
											echo $data[$d][$i];
										}
									}
								}
							}
					echo "@";
					}
				}
				else
					{
					echo '*000@';
					}
		}
	else if($option==2)//kai edw lamvanei tis metavlites..
		{
		$aa=explode("-",$_POST['date']);
		$bb=explode(":",$_POST['time']);
		if ((isset($_POST['temp']) && is_numeric($_POST['temp'])) && (isset($_POST['hum']) && is_numeric($_POST['hum'])) &&
			 (isset($_POST['somo']) && ($_POST['somo']==0 || $_POST['somo']==1 || $_POST['somo']==3)) && (isset($_POST['inli']) && is_numeric($_POST['inli']))
			 && (isset($_POST['f']) && ($_POST['f']==0 || $_POST['f']==1)) && (isset($_POST['v']) && ($_POST['v']==0 || $_POST['v']==1))
			 && (isset($_POST['l']) && ($_POST['l']==0 || $_POST['l']==1)) && (is_numeric($aa[0])&& $aa[1]>=1 && $aa[1]<=12 && $aa[2]<=31 && $aa[2]>=1 
			 && $bb[0]<=23 && $bb[0]>=0 && $bb[1]>=0 && $bb[1]<=59 && $bb[2]==00))
			{			
			$date11=date("Y-m-d",strtotime($_POST['date']));
			$time11=date("H:i:s",strtotime($_POST['time']));
			$msg="";
			//elegxh gia tin ekkatharisi..
			$m=$bb[1];
			$h=$bb[0];
				if($m==01)
						{
						$day=date("d",strtotime($date11));
						if ($h==00)
							{ 
							if ($day ==01)
								{
								$temp_sum=0;
								$hum_sum=0;
								$inli_sum=0;
								$somo_sum=0;
								$fan_sum=0;
								$watering_sum=0;
								$lighting_sum=0;
								$date1=date("m",strtotime("- 1 month",strtotime($date11)));
								$k=days_in_month($date1,date("Y",strtotime($date11)));
								//eksagetai o mesos oros mhna
								for ($i=1;$i<=$k;$i++)
									{
									if ($i<=9)
										{
										$i='0'.$i;
										}
									$timer="23:00:58";//mesos oros meras
									$date=date("Y",strtotime($date11))."-".$date1."-".$i;
									$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:57";//meso oro mhna
								$a=estimate_sum($div=$k,$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 mhna	
								$date2=$date;
								}
								else
								{
								$date2=date("Y-m-d",strtotime("-1 day",strtotime($date11)));
								}
							//eksagetai o mesos oros meras..
								$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='$date2' 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','$date2','$time_sum','$a[0]','$a[1]','$a[2]','$a[3]','$a[4]','$a[5]','$a[6]')";
							@ $resulta = mysql_query($sql3);//vazoume meso oro 	meras
							$h=23;
							$date111=date("Y-m-d",strtotime("-1 day",strtotime($date11)));
							}
						else
							{
							$h=$h-1;
							$date111=$date11;
							}
							//eksagetai o mesos oros wras
							for ($aaa=$h;$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='$date111' AND timer ='$aaaa'";
							@ $resulta=mysql_query($sqla);
							if (mysql_numrows($resulta)==0)
								{	
								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='$date111' 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='$date111' 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','$date111','$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);
								}
							}	
						}
				$temp=strip_tags(htmlspecialchars(mysql_real_escape_string(trim($_POST['temp']))));
				if ($temp==0)
					{
					$msg=$msg."1_";
					$sql="INSERT INTO troubleshoots (troubleshoot_id,user_id,dater,timer,problem) VALUES (null, '$user_id', '$date11', '$time11', '1')";
					@ $result=mysql_query($sql);
					
					}
				$hum=strip_tags(htmlspecialchars(mysql_real_escape_string(trim($_POST['hum']))));
				if ($hum==0)
					{
					$msg=$msg."2_";
					$sql="INSERT INTO troubleshoots (troubleshoot_id,user_id,dater,timer,problem) VALUES (null, '$user_id', '$date11', '$time11', '2')";
					@ $result=mysql_query($sql);
					
					}
				$somo=strip_tags(htmlspecialchars(mysql_real_escape_string(trim($_POST['somo'])))); 
				if ($somo==3)
					{
					$msg=$msg."3_";
					$sql="INSERT INTO troubleshoots (troubleshoot_id,user_id,dater,timer,problem) VALUES (null, '$user_id', '$date11', '$time11', '3')";
					@ $result=mysql_query($sql);
					
					}
				$inli=strip_tags(htmlspecialchars(mysql_real_escape_string(trim($_POST['inli']))));
				if ($inli<0)
					{
					$msg=$msg."4_";
					$sql="INSERT INTO troubleshoots (troubleshoot_id,user_id,dater,timer,problem) VALUES (null, '$user_id', '$date11', '$time11', '4')";
					@ $result=mysql_query($sql);
					
					}
				$f=strip_tags(htmlspecialchars(mysql_real_escape_string(trim($_POST['f']))));
				$v=strip_tags(htmlspecialchars(mysql_real_escape_string(trim($_POST['v']))));
				$l=strip_tags(htmlspecialchars(mysql_real_escape_string(trim($_POST['l']))));
				$sql1="INSERT INTO data(data_id,device_id,dater,timer,temp,hum,inli,somo,fan,watering,lighting) VALUES(NULL,'$device_id','$date11','$time11','$temp','$hum','$inli','$somo','$f','$v','$l')";
				@ $resulta = mysql_query($sql1);		
				//elegxos gia dedeomena apolliseis kai ta resta kathw kai gia tin epikoinwnia
				$exp="";
				for ($as=0;$as<=23;$as++)
					{
					$tt=$as.':00:59';
					if ($exp=="")
						{
						$exp="'".$tt."'";
						}
					else
						{
						$exp=$exp.",'".$tt."'";
						}
					}
				$sqll="SELECT * FROM data WHERE device_id='$device_id' AND timer NOT IN($exp) AND timer!='23:00:58' AND timer!='23:00:57' ORDER BY data_id DESC LIMIT 2";
				@ $rr=mysql_query($sqll);
				 if ($rr!=0)
					{
					if (mysql_numrows($rr)>1 && mysql_numrows($rr)<=2)
						{
						$i=1;
						while ($row=mysql_fetch_array($rr))
							{
							${'date'.$i}=$row['dater'];
							${'time'.$i}=$row['timer'];
							${'temp'.$i}=$row['temp'];
							${'hum'.$i}=$row['hum'];
							${'inli'.$i}=$row['inli'];
							${'somo'.$i}=$row['somo'];
							$i++;
							}
						if ($i==3)
							{
							$data_lost="";
							$sudden_change="";
							if(strtotime($date1)!=strtotime($date2))
								{
								if ($time2!="23:59:00" && $time1!="00:00:00")
									{
									$dif=abs(strtotime($date2) - strtotime($date1))/86400;
									$dif=$dif.'0';
									$msg=$msg.$dif."_";
									$sql="INSERT INTO troubleshoots (troubleshoot_id,user_id,dater,timer,problem) VALUES (null, '$user_id', '$date11', '$time11', '$dif')";
									@ $result=mysql_query($sql);
									}
								}
								else
								{
								$dif=abs(strtotime($time2) - strtotime($time1))/60;
								if ($dif>=3)
									{
									$dif=$dif.'9';
									$msg=$msg.$dif."_";
									$sql="INSERT INTO troubleshoots (troubleshoot_id,user_id,dater,timer,problem) VALUES (null, '$user_id', '$date11', '$time11', '$dif')";
									@ $result=mysql_query($sql);
									
									}								
								else
									{
									$t_dif=abs($temp2 - $temp1);
									$h_dif=abs($hum2 - $hum1);
									$i_dif=abs($inli2 - $inli1);
									if ($t_dif>=5)
										{
										$msg=$msg."11_";
										$sql="INSERT INTO troubleshoots (troubleshoot_id,user_id,dater,timer,problem) VALUES (null, '$user_id', '$date11', '$time11', '11')";
										@ $result=mysql_query($sql);
										
										}
									if ($h_dif>=5)
										{
										$msg=$msg."12_";
										$sql="INSERT INTO troubleshoots (troubleshoot_id,user_id,dater,timer,problem) VALUES (null, '$user_id', '$date11', '$time11', '12')";
										@ $result=mysql_query($sql);
										
										}
									if 	($i_dif>=800)
										{
										$msg=$msg."13_";
										$sql="INSERT INTO troubleshoots (troubleshoot_id,user_id,dater,timer,problem) VALUES (null, '$user_id', '$date11', '$time11', '13')";
										@ $result=mysql_query($sql);
										
										}										
									}
								}
							if ($msg!="")//an exei minimataki simeinei oti uparxei bag..tsekaroume ti thelei enhmerwseis kai ta resta kai tou stelnoume..eidalliws milagame..
								{
								$msg=explode("_",$msg);
								$subject="";
								$text="";
								$text1=$text2=$text3='';
								if($language==1)
									{
									$title="SmartGreenhouse -Ειδοποίηση:";
									}
								else
									{
									$title="SmartGreenhouse Notification:";
									}
								$title="SmartGreenhouse Notification:";
								for ($a=0;$a<count($msg)-1;$a++)
									{
									if ($changes==1)//diafora sta dedomena
										{
										if ($msg[$a]==11)
											{
											if($language==1)
												{
												$text1=$text1.'Σας πληροφορούμαι ότι διαγνώστηκε απότομη αλλαγή στην <b>Θερμοκρασία</b> στις <i>'.$time11.'</i> τις <i>'.$date11.'</i>. Αν το γεγονός αυτό συνεχίζει να συμβαίνει παρακαλώ επικοινωνήστε μαζί μας!<br />';
												}
											else
												{
												$text1=$text1.'We inform you that sudden change in <b>Temperature</b> sensor data occured at <i>'.$time11.'</i> on <i>'.$date11.'</i>.If this continue happening please contact us!<br />';
												}
											
											}
										else if ($msg[$a]==12)
											{
											if($language==1)
												{
												$text1=$text1.'Σας πληροφορούμαι ότι διαγνώστηκε απότομη αλλαγή στην <b>υγρασία</b> στις <i>'.$time11.'</i> τις <i>'.$date11.'</i>. Αν το γεγονός αυτό συνεχίζει να συμβαίνει παρακαλώ επικοινωνήστε μαζί μας!<br />';
												}
											else
												{
												$text1=$text1.'We inform you that sudden change in <b>Humidity</b> sensor data occured at <i>'.$time11.'</i> on <i>'.$date11.'</i>.If this continue happening please contact us!<br />';
												}
											
											}
										else if ($msg[$a]==11)
											{
											if($language==1)
												{
												$text1=$text1.'Σας πληροφορούμαι ότι διαγνώστηκε απότομη αλλαγή στον <b>φωτισμό</b> στις <i>'.$time11.'</i> τις <i>'.$date11.'</i>. Αν το γεγονός αυτό συνεχίζει να συμβαίνει παρακαλώ επικοινωνήστε μαζί μας!<br />';
												}
											else
												{
												$text1=$text1.'We inform you that sudden change in <b>Indoor Lighting</b> sensor data occured at <i>'.$time11.'</i> on <i>'.$date11.'</i>.If this continue happening please contact us!<br />';
												}
											
											}
										if ($text1!="")
											{
											if($language==1)
												{
												$subject1=" Ξαφνική ατμοσφαιρική αλλαγή";
												}
											else
												{
												$subject1=" Sudden Change in Sensors Data";
												}
											
											}
										}
									else
										{
										$subject1="";
										$text1=$text1.'';
										}
									if ($troubleshoot==1)
										{
										if ($msg[$a]==1)
											{
											if($language==1)
												{
												$text2=$text2.'Σας πληροφορούμαι ότι ο αισθητήρας <b>Θερμοκρασίας</b> είναι εκτός λειτουργίας στις <i>'.$time11.'</i> τις <i>'.$date11.'</i>.Αν το γεγονός αυτό συνεχίζει να συμβαίνει παρακαλώ επικοινωνήστε μαζί μας!<br />';
												}
											else
												{
												$text2=$text2.'We inform you that <b>Temperature</b> sensor is not working at <i>'.$time11.'</i> on <i>'.$date11.'</i>.If this continue happening please contact us!<br />';
												}
											
											}
										else if($msg[$a]==2)
											{
											if($language==1)
												{
												$text2=$text2.'Σας πληροφορούμαι ότι ο αισθητήρας <b>Υγρασίας</b> είναι εκτός λειτουργίας στις <i>'.$time11.'</i> τις <i>'.$date11.'</i>.Αν το γεγονός αυτό συνεχίζει να συμβαίνει παρακαλώ επικοινωνήστε μαζί μας!<br />';
												}
											else
												{
												$text2=$text2.'We inform you that <b>Humidity</b> sensor is not working at <i>'.$time11.'</i> on <i>'.$date11.'</i>.If this continue happening please contact us!<br />';
												}
											
											}
										else if($msg[$a]==3)
											{
											if($language==1)
												{
												$text2=$text2.'Σας πληροφορούμαι ότι ο αισθητήρας <b>Εδάφους</b> είναι εκτός λειτουργίας στις <i>'.$time11.'</i> τις <i>'.$date11.'</i>.Αν το γεγονός αυτό συνεχίζει να συμβαίνει παρακαλώ επικοινωνήστε μαζί μας!<br />';
												}
											else
												{
												$text2=$text2.'We inform you that <b>Soil Moisture</b> sensor is not working at <i>'.$time11.'</i> on <i>'.$date11.'</i>.If this continue happening please contact us!<br />';
												}
											
											}
										else if($msg[$a]==4)
											{
											if($language==1)
												{
												$text2=$text2.'Σας πληροφορούμαι ότι ο αισθητήρας <b>Φωτισμού</b> είναι εκτός λειτουργίας στις <i>'.$time11.'</i> τις <i>'.$date11.'</i>.Αν το γεγονός αυτό συνεχίζει να συμβαίνει παρακαλώ επικοινωνήστε μαζί μας!<br />';
												}
											else
												{
												$text2=$text2.'We inform you that <b>Indoor Lighting</b> sensor is not working at <i>'.$time11.'</i> on <i>'.$date11.'</i>.If this continue happening please contact us!<br />';
												}
											
											}
										if ($text2!="")
											{
											if($language==1)
												{
												$subject1="Αισθητήρας εκτός λειτουργίας";
												}
											else
												{
												$subject2=" Sensor Troubleshoot";
												}
											}
											
										}
									else
										{
										$subject2="";
										$text2=$text2.'';
										}
									if ($lost==1)
										{
										$text3=$text3.'';
										$l=strlen($msg[$a])-1;
										$number=$msg[$a];
										$duration="";
										if ($number[$l]==0)
											{											
											for ($i=0;$i<$l;$i++)
												{
												$duration=$duration.$number[$i];
												}
											if ($duration==1)
												{
												if($language==1)
													{
													$day='ημέρα';
													}
												else
													{
													$day='day';
													}
												}
											else
												{
												if($language==1)
													{
													$day='ημέρες';
													}
												else
													{
													$day='days';
													}
												
												}
												if($language==1)
													{
													$text3=$text3.'Σας πληροφορούμε ότι η σύνδεση χάθηκε για <b>'.$duration.' '.$day.'</b>! Παρακαλούμε ελέγξατε την σύνδεση σας!<br />';	
													}
												else
													{
													$text3=$text3.'We inform you that connection lost for <b>'.$duration.' '.$day.'</b>! Please check your machine connection.<br />';	
													}
											
											}
										else if ($number[$l]==9)
											{											
											for ($i=0;$i<$l;$i++)
												{
												$duration=$duration.$number[$i];
												}
											if ($duration==1)
												{
												if($language==1)
													{
													$day='λεπτό';
													}
												else
													{
													$day='minute';
													}
												}
											else
												{
												if($language==1)
													{
													$day='λεπτά';
													}
												else
													{
													$day='minutes';
													}
												}	
											if($language==1)
													{
													$text3=$text3.'Σας πληροφορούμε ότι η σύνδεση χάθηκε για <b>'.$duration.' '.$day.'</b>! Παρακαλούμε ελέγξατε την σύνδεση σας!<br />';	
													}
												else
													{
													$text3=$text3.'We inform you that connection lost for <b>'.$duration.' '.$day.'</b>! Please check your machine connection.<br />';	
													}
											}
										if ($text3!="")
											{
											if($language==1)
													{
													$subject3=" Απώλεια Σύνδεσης";
													}
												else
													{
													$subject3=" Connection Lost";
													}
											
											}	
											
										}
									else
										{
										$subject3="";
										$text3=$text3.'';
										}
									}
									$p=1;
								for($b=1;$b<=3;$b++)
									{
									if (isset(${'subject'.$b}) && !empty(${'subject'.$b}))
										{
										if ($subject=="")
											{
											$subject=${'subject'.$b};
											$text=$p.") ".${'text'.$b};
											}
										else
											{
											$subject=$subject."-".${"subject".$b};
											$text=$text.$p.") ".${"text".$b};
											}
										$p++;	
										}	
									}	
								if ($subject!="" && $text!="")
									{
									$title=$title.$subject;
									$headers = "From:smartgreenhouse \r\n";
									$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
									if($language==1)
										{
									$heading='<b>Ημερομηνία:</b>'.$date11.' <b>Ώρα:</b>'.$time11.'<br />';
									$footer="<br />Το μήνυμα αυτό στάλθηκε με βάση τις επιλογές σας στις <i>Ρυθμίσεις Συσκευής</i>.<br /><br />Eυχαριστούμε για την προτίμηση σας.
									<br /> <i>SmartGreenhouse team</i>";
										}
									else
										{
									$heading='<b>Date:</b>'.$date11.' <b>Time:</b>'.$time11.'<br />';
									$footer="<br />This message was sent to you by your own decision due to your preferences in <i>Device Info</i> page.<br /><br />Thank you for using our product.
									<br /> <i>SmartGreenhouse team</i>";

													}
									$text=$heading.$text.$footer;
									$to="$e";
									date_default_timezone_set("Europe/Athens");
									$mmm=mail($e,$title,$text,$headers);
									if ($mmm!=1)
										{
										$sql="INSERT INTO troubleshoots (troubleshoot_id,user_id,dater,timer,problem) VALUES (null, '$user_id', '$date11', '$time11', '7')";
										@ $result=mysql_query($sql);
										
										}
									}
								}								
							}
						}				
					}
			}
		
		}
	else//lamvanei thn wra.
		{		
		$year=date("Y");
		$month=date("m");
		$day=date("d");
		$hour=date("H");
		$minute=date("i");
		echo'*'.$year.$month.$day.$hour.$minute.'@';
		}
		
		
	}
db_close();
	}
?>
