1
0
moha/class/db.php
2022-09-19 23:40:29 +02:00

124 lines
3.7 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, $configDir;
require_once $configDir . "/dbConfig.php";
$flagError = false;
if ($testMode)
{
$mysqlServer = "192.168.1.253"; // Your test server
}
while ($this->connect($mysqlServer, $username, $passwd, $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__);
}
$this->result = new mysqli_result($this);
}
function protect($string)
{
return $this->real_escape_string($string);
}
function logProperty(&$device, $property, $value)
{
global $mohaDB, $properties2log, $testMode;
$precision = 0;
//echo "############## logProperty ################\nproperty => " . $propertyTree .EOL;
//echo "logging in database";
//var_dump($device);
$ieeeAddress = $device->ieeeAddress;
//print_r($ieeeAddress);
//echo $query . EOL;
if (is_numeric($value) and !empty($properties2log[$property]))
{
if (!array_key_exists("lastValueLogged", $device->properties[$property]))
{
$oldValue = -1;
}else
{
$oldValue = $device->properties[$property]["lastValueLogged"];
}
// calculate a min/max value for storing data
$r = $properties2log[$property];
if (is_callable($r))
{
$minMax = $r($value);
}else
{
$minMax = $r;
}
logger(DEBUG, _("minMax = " . $minMax), __FILE__ . ":" . __LINE__);
//var_dump($value) . EOL;
//var_dump($minMax) . EOL;
//var_dump($oldValue) . EOL;
//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 true;
}
}
if ($testMode)
{
logger(INFO, _("Test mode on: not storing in DB "), __FILE__ . ":" . __LINE__);
}else
{
$query = "INSERT INTO logs (device, property, value) VALUES('" . $this->protect($ieeeAddress) . "', '" . $this->protect($property) . "', '" . $this->protect(bool2string($value)) . "')";
if(!$this->result = $this->query($query))
{
logger(ERROR, _("mysql query errror: ") . $this->error, __FILE__ . ":" . __LINE__);
}
$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__);
}
}
function moyenne($deviceObject, $property, $time)
{
$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 ) . "'";
logger(DEBUG, _("query is: ") . $query, __FILE__ . ":" . __LINE__);
if(!$this->result = $this->query($query))
{
logger(ERROR, _("mysql query errror: ") . $this->error, __FILE__ . ":" . __LINE__);
return 1;
}else
{
$value = $this->result->fetch_array(MYSQLI_NUM);
//var_dump($value);
logger(DEBUG, _("result is: ") . print_r($value[0], true), __FILE__ . ":" . __LINE__);
return $value[0];
}
}
/*function lastTime($deviceObject, $property)
{
$query = "SELECT date FROM logs WHERE device='" . $deviceObject->ieeeAddress . "' AND property='" . $property . "' AND TIMEDIFF(NOW(), date) < '00:" . str_pad($time, 2, "0", STR_PAD_LEFT ) . "'";
}*/
}
$mohaDB = new db();
?>