2022-01-10 00:12:30 +01:00
< ? php
2022-01-28 23:05:58 +01:00
logger ( DEBUG , _ ( " Including db.php " ));
2022-01-10 00:12:30 +01:00
class db extends mysqli
{
2022-01-23 09:46:06 +01:00
public $mysqlServer = " 127.0.0.1 " ; // Your production server
2022-01-10 00:12:30 +01:00
public $username = " moha " ;
public $passwd = " MysqlMoha " ;
public $database = " moha " ;
2022-01-17 00:18:50 +01:00
public $result ;
2022-01-10 00:12:30 +01:00
2022-01-17 00:18:50 +01:00
function __construct ()
2022-01-10 00:12:30 +01:00
{
2022-01-23 09:46:06 +01:00
global $testMode ;
$flagError = false ;
if ( $testMode )
2022-01-17 00:18:50 +01:00
{
2022-01-23 09:46:06 +01:00
$this -> mysqlServer = " 192.168.1.253 " ; // Your test server
}
while ( $this -> connect ( $this -> mysqlServer , $this -> username , $this -> passwd , $this -> database ) === false )
{
2022-01-28 23:05:58 +01:00
logger ( ERROR , _ ( " Connection to sql server error : " ) . $this -> connect_error , __FILE__ . " : " . __LINE__ );
2022-01-23 09:46:06 +01:00
sleep ( 5 );
$flagError = true ;
}
if ( $flagError === true )
{
2022-01-28 23:05:58 +01:00
logger ( ERROR , _ ( " Connection to sql server ready " ), __FILE__ . " : " . __LINE__ );
2022-01-17 00:18:50 +01:00
}
2022-05-05 21:28:26 +02:00
$this -> result = new mysqli_result ( $this );
2022-01-10 00:12:30 +01:00
}
function protect ( $string )
{
return $this -> real_escape_string ( $string );
}
2022-06-10 12:44:09 +02:00
function logProperty ( & $device , $property , $value )
2022-01-10 00:12:30 +01:00
{
2022-01-19 00:22:34 +01:00
global $mohaDB , $properties2log , $testMode ;
2022-01-17 00:18:50 +01:00
$precision = 0 ;
2022-06-10 12:44:09 +02:00
$oldValue = $device -> properties [ $property ][ " lastValueLogged " ];
2022-01-19 00:22:34 +01:00
//echo "############## logProperty ################\nproperty => " . $propertyTree .EOL;
2022-05-29 01:55:22 +02:00
//echo "logging in database";
//var_dump($device);
$ieeeAddress = $device -> ieeeAddress ;
//print_r($ieeeAddress);
$query = " INSERT INTO logs (device, property, value) VALUES(' " . $this -> protect ( $ieeeAddress ) . " ', ' " . $this -> protect ( $property ) . " ', ' " . $this -> protect ( bool2string ( $value )) . " ') " ;
//echo $query . EOL;
if ( is_numeric ( $value ) and ! empty ( $properties2log [ $property ]))
2022-01-17 00:18:50 +01:00
{
2022-05-29 01:55:22 +02:00
// calculate a min/max value for storing data
$r = $properties2log [ $property ];
if ( is_callable ( $r ))
2022-01-17 00:18:50 +01:00
{
2022-05-29 01:55:22 +02:00
$minMax = $r ( $value );
} else
{
$minMax = $r ;
2022-01-17 00:18:50 +01:00
}
2022-05-29 01:55:22 +02:00
logger ( DEBUG , _ ( " minMax = " . $minMax ), __FILE__ . " : " . __LINE__ );
var_dump ( $value ) . EOL ;
var_dump ( $minMax ) . EOL ;
var_dump ( $oldValue ) . EOL ;
if ( ! is_numeric ( $oldValue )) $oldValue = 0 ;
//echo "minMax = " .$minMax . EOL;
//echo "oldValue = " . $oldValue . EOL;
//echo "Value = " . $value . EOL;
if ( $value >= $oldValue - $minMax and $value <= $oldValue + $minMax )
2022-01-17 00:18:50 +01:00
{
2022-05-29 01:55:22 +02:00
//echo "========>>>>>>>>>not changed" . EOL;
return true ;
}
}
if ( $testMode )
{
logger ( INFO , _ ( " Test mode on: not storing in DB " ), __FILE__ . " : " . __LINE__ );
} else
{
if ( ! $this -> result = $this -> query ( $query ))
2022-01-19 00:22:34 +01:00
{
2022-05-29 01:55:22 +02:00
logger ( ERROR , _ ( " mysql query errror: " ) . $this -> error , __FILE__ . " : " . __LINE__ );
2022-01-17 00:18:50 +01:00
}
2022-06-10 12:44:09 +02:00
$device -> properties [ $property ][ " lastValueLogged " ] = $value ;
logger ( INFO , sprintf ( _ ( " New value (%s) of property: '%s' of device: %s stored in database " ), bool2string ( $value ), $property , $device -> friendlyName ), __FILE__ . " : " . __LINE__ );
2022-01-17 00:18:50 +01:00
}
2022-01-10 00:12:30 +01:00
}
2022-03-06 01:30:33 +01:00
function moyenne ( $deviceObject , $property , $time )
{
2022-06-10 12:44:09 +02:00
$query = " SELECT AVG(value) as value FROM logs WHERE device=' " . $deviceObject -> ieeeAddress . " ' AND property=' " . $property . " ' AND TIMEDIFF(NOW(), date) < '00: " . str_pad ( $time , 2 , " 0 " , STR_PAD_LEFT ) . " ' " ;
2022-05-29 01:55:22 +02:00
logger ( DEBUG , _ ( " query is: " ) . $query , __FILE__ . " : " . __LINE__ );
2022-03-06 01:30:33 +01:00
if ( ! $this -> result = $this -> query ( $query ))
{
logger ( ERROR , _ ( " mysql query errror: " ) . $this -> error , __FILE__ . " : " . __LINE__ );
2022-05-29 01:55:22 +02:00
return 1 ;
} else
{
$value = $this -> result -> fetch_array ( MYSQLI_NUM );
//var_dump($value);
logger ( DEBUG , _ ( " result is: " ) . print_r ( $value , true ), __FILE__ . " : " . __LINE__ );
return $value [ 0 ];
2022-03-06 01:30:33 +01:00
}
}
2022-01-10 00:12:30 +01:00
}
2022-01-17 00:18:50 +01:00
$mohaDB = new db ();
2022-01-10 00:12:30 +01:00
?>