86 lines
2.4 KiB
PHP
86 lines
2.4 KiB
PHP
<?php
|
|
logger(DEBUG, _("Including db.php"));
|
|
|
|
class db extends mysqli
|
|
{
|
|
public $mysqlServer = "127.0.0.1"; // Your production server
|
|
public $username = "moha";
|
|
public $passwd = "MysqlMoha";
|
|
public $database = "moha";
|
|
public $result;
|
|
|
|
function __construct()
|
|
{
|
|
global $testMode;
|
|
$flagError = false;
|
|
if ($testMode)
|
|
{
|
|
$this->mysqlServer = "192.168.1.253"; // Your test server
|
|
}
|
|
while ($this->connect($this->mysqlServer, $this->username, $this->passwd, $this->database) === false)
|
|
{
|
|
logger(ERROR,_("Connection to sql server error :") . $this->connect_error, __FILE__ . ":" . __LINE__);
|
|
sleep(5);
|
|
$flagError = true;
|
|
}
|
|
if ($flagError === true)
|
|
{
|
|
logger(ERROR, _("Connection to sql server ready"), __FILE__ . ":" . __LINE__);
|
|
}
|
|
$result = new mysqli_result($this);
|
|
}
|
|
|
|
function protect($string)
|
|
{
|
|
return $this->real_escape_string($string);
|
|
}
|
|
|
|
function logProperty($device, $propertyTree, $value, $oldValue = 0)
|
|
{
|
|
global $mohaDB, $properties2log, $testMode;
|
|
$precision = 0;
|
|
//echo "############## logProperty ################\nproperty => " . $propertyTree .EOL;
|
|
if (array_key_exists($propertyTree, $properties2log))
|
|
{
|
|
//echo "logging in database";
|
|
$ieeeAddress = $device->ieeeAddress;
|
|
//print_r($ieeeAddress);
|
|
$query = "INSERT INTO logs (device, property, value) VALUES('" . $this->protect($ieeeAddress) . "', '" . $this->protect($propertyTree) . "', '" . $this->protect($value) . "')";
|
|
if (is_numeric($value) and !empty($properties2log[$propertyTree]))
|
|
{
|
|
// calculate a min/max value for storing data
|
|
|
|
$minMax = $properties2log[$propertyTree];
|
|
if (is_callable($minMax))
|
|
{
|
|
$minMax = $minMax($value);
|
|
}
|
|
|
|
//echo "minMax = " .$minMax . EOL;
|
|
//echo "oldValue = " . $oldValue . EOL;
|
|
//echo "Value = " . $value . EOL;
|
|
if ($value >= $oldValue - $minMax and $value <= $oldValue + $minMax)
|
|
{
|
|
//echo "========>>>>>>>>>not changed" . EOL;
|
|
return 0;
|
|
}
|
|
}
|
|
if ($testMode)
|
|
{
|
|
logger(INFO, _("Test mode on: not storing in DB "), __FILE__ . ":" . __LINE__);
|
|
}else
|
|
{
|
|
if(!$this->result = $this->query($query))
|
|
{
|
|
logger(ERROR, _("mysql query errror: ") . $this->error, __FILE__ . ":" . __LINE__);
|
|
}
|
|
}
|
|
logger(INFO, sprintf(_("New value (%s) of property: '%s' of device: %s stored in database"), bool2string($value), $propertyTree, $device->friendlyName), __FILE__ . ":" . __LINE__);
|
|
}
|
|
}
|
|
}
|
|
|
|
$mohaDB = new db();
|
|
|
|
?>
|