2022-01-10 00:12:30 +01:00
|
|
|
<?php
|
2022-01-17 00:18:50 +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)
|
|
|
|
{
|
|
|
|
logger(ERROR,_("Connection to sql server error :") . $this->connect_error);
|
|
|
|
sleep(5);
|
|
|
|
$flagError = true;
|
|
|
|
}
|
|
|
|
if ($flagError === true)
|
|
|
|
{
|
|
|
|
logger(ERROR, _("Connection to sql server ready"));
|
2022-01-17 00:18:50 +01:00
|
|
|
}
|
|
|
|
$result = new mysqli_result($this);
|
2022-01-10 00:12:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function protect($string)
|
|
|
|
{
|
|
|
|
return $this->real_escape_string($string);
|
|
|
|
}
|
|
|
|
|
2022-01-17 00:18:50 +01:00
|
|
|
function logProperty($device, $propertyTree, $value, $oldValue = 0)
|
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-01-19 00:22:34 +01:00
|
|
|
//echo "############## logProperty ################\nproperty => " . $propertyTree .EOL;
|
2022-01-17 00:18:50 +01:00
|
|
|
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]))
|
|
|
|
{
|
2022-01-19 00:22:34 +01:00
|
|
|
// calculate a min/max value for storing data
|
2022-01-23 09:46:06 +01:00
|
|
|
|
2022-01-19 00:22:34 +01:00
|
|
|
$var = $properties2log[$propertyTree];
|
2022-01-23 09:46:06 +01:00
|
|
|
if (is_callable($var))
|
2022-01-19 00:22:34 +01:00
|
|
|
{
|
2022-01-23 09:46:06 +01:00
|
|
|
$var = $var($value);
|
2022-01-19 00:22:34 +01:00
|
|
|
}
|
2022-01-23 09:46:06 +01:00
|
|
|
//if (!is_numeric($var))
|
|
|
|
//{
|
|
|
|
$minMax = (float)$value + $var;
|
|
|
|
//}else
|
|
|
|
//{
|
|
|
|
// $minMax = $var;
|
|
|
|
//}
|
2022-01-19 00:22:34 +01:00
|
|
|
echo "minMax = " .$minMax . EOL;
|
2022-01-17 00:18:50 +01:00
|
|
|
//echo "oldValue = " . $oldValue . EOL;
|
|
|
|
//echo "Value = " . $value . EOL;
|
|
|
|
if ($value >= $oldValue - $minMax and $value <= $oldValue + $minMax)
|
|
|
|
{
|
|
|
|
//echo "========>>>>>>>>>not changed" . EOL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2022-01-19 00:22:34 +01:00
|
|
|
if ($testMode)
|
2022-01-17 00:18:50 +01:00
|
|
|
{
|
2022-01-19 00:22:34 +01:00
|
|
|
logger(INFO, _("Test mode on: not storing in DB "));
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
if(!$this->result = $this->query($query))
|
|
|
|
{
|
|
|
|
logger(ERROR, _("mysql query errror: ") . $this->error);
|
|
|
|
}
|
2022-01-17 00:18:50 +01:00
|
|
|
}
|
2022-01-19 00:22:34 +01:00
|
|
|
logger(INFO, sprintf(_("New value of property: '%s' of device: %s stored in database"), $propertyTree, $device->friendlyName, $value));
|
2022-01-17 00:18:50 +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
|
|
|
|
|
|
|
?>
|