<?php
date_default_timezone_set("Europe/Athens");
function check_device($device_id,$password)
{
$msg=0;
db_open();
$sql="SELECT * FROM devices WHERE device_id='$device_id' AND password='$password'";
@ $result=mysql_query($sql);
if ($result!=0)
	{
	$msg=1;
	}
else
	{
	$msg=0;
	}
db_close();
return $msg;
}

function db_open() {
@ $db = mysql_connect('localhost', 'gnnpls', '49150zn@') or
die('Connection could not be established.');
@ mysql_select_db('SmartGreenhouse') or die('There is no valid database.');
}


function db_close() {
@ mysql_close($db);
}

function estimate_sum($div,$temp_sum,$hum_sum,$inli_sum,$somo_sum,$fan_sum,$watering_sum,$lighting_sum)
{
if (is_numeric($div))
{
$data=array();
$divv=$div/2;
	$data[0]=$temp_sum/$div;
	$data[1]=$hum_sum/$div;
	$data[2]=$inli_sum/$div;
	$somo_check=$somo_sum/$divv;
	$fan_check=$fan_sum/$divv;
	$watering_check=$watering_sum/$divv;
	$lighting_check=$lighting_sum/$divv;
	if ($somo_check>1)
	{
		$somo_sum=1;
	}
	else
	{
		$somo_sum=0;
	}
	$data[3]=$somo_sum;
	if ($fan_check>1)
	{
		$fan_sum=1;
	}
	else
	{
		$fan_sum=0;
	}
	$data[4]=$fan_sum;
	if ($watering_check>1)
	{
		$watering_sum=1;
	}
	else
	{
		$watering_sum=0;
	}	
	$data[5]=$watering_sum;
	if ($lighting_check>1)
	{
		$lighting_sum=1;
	}
	else
	{
		$lighting_sum=0;
	}
	$data[6]=$lighting_sum;
	return $data;
	}
}
?>