1
0
This commit is contained in:
2022-05-29 01:55:22 +02:00
parent 68fb0579ce
commit cde4d728cf
7 changed files with 79 additions and 63 deletions

View File

@ -40,60 +40,68 @@ class db extends mysqli
global $mohaDB, $properties2log, $testMode;
$precision = 0;
//echo "############## logProperty ################\nproperty => " . $propertyTree .EOL;
if (array_key_exists($property, $properties2log))
//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]))
{
//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]))
// calculate a min/max value for storing data
$r = $properties2log[$property];
if (is_callable($r))
{
// calculate a min/max value for storing data
$minMax = $properties2log[$property];
if (is_callable($minMax))
{
$minMax = $minMax($value);
}
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)
{
//echo "========>>>>>>>>>not changed" . EOL;
return true;
}
}
if ($testMode)
{
logger(INFO, _("Test mode on: not storing in DB "), __FILE__ . ":" . __LINE__);
$minMax = $r($value);
}else
{
if(!$this->result = $this->query($query))
{
logger(ERROR, _("mysql query errror: ") . $this->error, __FILE__ . ":" . __LINE__);
}
$minMax = $r;
}
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)
{
//echo "========>>>>>>>>>not changed" . EOL;
return true;
}
logger(INFO, sprintf(_("New value (%s) of property: '%s' of device: %s stored in database"), bool2string($value), $property, $device->friendlyName), __FILE__ . ":" . __LINE__);
}
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), $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:" . $time . "'";
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, true), __FILE__ . ":" . __LINE__);
return $value[0];
}
$value = $this->result->fetch_array(MYSQLI_NUM);
//var_dump($value);
return $value[0];
}
}

View File

@ -2,7 +2,7 @@
class hook
{
public $hookName = "";
public $active = true;
public $active;
public $initialized = false;
protected $devicelist;
protected $propertyInitialized;