1
0
moha/class/db.php

63 lines
1.8 KiB
PHP
Raw Normal View History

<?php
2022-01-17 00:18:50 +01:00
logger(DEBUG,"Including db.php");
class db extends mysqli
{
2022-01-17 00:18:50 +01:00
public $mysqlServer = "192.168.1.253"; // "127.0.0.1";
public $username = "moha";
public $passwd = "MysqlMoha";
public $database = "moha";
2022-01-17 00:18:50 +01:00
public $result;
2022-01-17 00:18:50 +01:00
function __construct()
{
2022-01-17 00:18:50 +01:00
if ($this->connect($this->mysqlServer, $this->username, $this->passwd, $this->database) === false)
{
logger(ERROR,"Connection to sql server error :" . $this->connect_error);
return 2;
}
$result = new mysqli_result($this);
}
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-17 00:18:50 +01:00
global $mohaDB, $properties2log;
$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]))
{
$minMax = (float)$value * (float)$properties2log[$propertyTree] / 100;
//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(!$this->result = $this->query($query))
{
logger(ERROR, _("mysql query errror: ") . $this->error);
}
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
$mohaDB = new db();
?>