debugging
This commit is contained in:
51
class/db.php
51
class/db.php
@ -1,15 +1,22 @@
|
||||
<?php
|
||||
logger(DEBUG,"Including db.php");
|
||||
|
||||
class db extends mysqli
|
||||
{
|
||||
public $mysqlServer = "127.0.0.1";
|
||||
public $mysqlServer = "192.168.1.253"; // "127.0.0.1";
|
||||
public $username = "moha";
|
||||
public $passwd = "MysqlMoha";
|
||||
public $database = "moha";
|
||||
public $result;
|
||||
|
||||
function __construct($mysqlServer, $username, $passwd, $database)
|
||||
function __construct()
|
||||
{
|
||||
return $this->connect($mysqlServer, $username, $passwd, $database);
|
||||
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)
|
||||
@ -17,17 +24,39 @@ class db extends mysqli
|
||||
return $this->real_escape_string($string);
|
||||
}
|
||||
|
||||
function logProperty($device, $property, $value)
|
||||
function logProperty($device, $propertyTree, $value, $oldValue = 0)
|
||||
{
|
||||
$query = "SELECT * FROM logs WHERE device='" . $device->ieeeAddress . "' AND property='" .
|
||||
$query = "IF (EXISTS INSERT INTO logs(device, property, value) VALUES(" . $device->ieeeAddress . ", " . $property . ", " . $value . ") ON DUPLICATE KEY UPDATE value=" . $value
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$mohaDB = new db($mysqlServer, $username, $passwd, $database);
|
||||
if ($mohaDB->connect_error)
|
||||
{
|
||||
logger(ERROR, _("Mysql connection failed: ") . $db->connect_error);
|
||||
}
|
||||
$mohaDB = new db();
|
||||
|
||||
?>
|
||||
|
57
class/hook_class.php
Normal file
57
class/hook_class.php
Normal file
@ -0,0 +1,57 @@
|
||||
<?php
|
||||
class hook
|
||||
{
|
||||
public $hookName = "";
|
||||
public $active = true;
|
||||
public $initialized = false;
|
||||
protected $devicelist;
|
||||
|
||||
// list of devices we are listening to
|
||||
function __construct()
|
||||
{
|
||||
logger(DEBUG, _("Initializing hook: ") . $this->hookName);
|
||||
$this->installHooks();
|
||||
}
|
||||
|
||||
function installHooks()
|
||||
{
|
||||
global $indexDevices;
|
||||
$result = true;
|
||||
// assigne the function to the sensors devices
|
||||
if ($this->active === true)
|
||||
{
|
||||
foreach ($this->devicelist as $ieeeAddress => $property2change)
|
||||
{
|
||||
logger(DEBUG, _("Device: ") . $ieeeAddress);
|
||||
if ($property2change[1] === false)
|
||||
{
|
||||
logger(DEBUG, _("Trying to store callback"));
|
||||
if (isset($indexDevices[$ieeeAddress]))
|
||||
{
|
||||
$property = $property2change[0];
|
||||
$indexDevices[$ieeeAddress]->$property["functions"][] = array($this,"callback");
|
||||
$property2change[1] = true;
|
||||
logger(DEBUG, sprintf(_("Property '%s' is initialized with callback"), $property2change[0]));
|
||||
}else
|
||||
{
|
||||
logger(WARNING, sprintf(_("Hook %s can not initialize Property '%s' of device %s"), $this->hookName, $property2change[0], $ieeeAddress));
|
||||
$result = false;
|
||||
}
|
||||
}else
|
||||
{
|
||||
logger(DEBUG, _("Callback already installed"));
|
||||
}
|
||||
}
|
||||
echo "result => "; var_dump($result);
|
||||
if ($result === true)
|
||||
{
|
||||
$this->initialized = true;
|
||||
logger(DEBUG, $this->hookName . (" initialized"));
|
||||
//var_dump($this);
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -1,4 +1,6 @@
|
||||
<?php
|
||||
logger(DEBUG,"Including class main.php");
|
||||
|
||||
class Message
|
||||
{
|
||||
public $id;
|
||||
|
Reference in New Issue
Block a user