1
0

- some debugging

- adding logs in mysql database
This commit is contained in:
daniel Tartavel 2022-01-10 00:12:30 +01:00
parent 7acec3a3c4
commit cd20e973cd
5 changed files with 55 additions and 4 deletions

33
class/db.php Normal file
View File

@ -0,0 +1,33 @@
<?php
class db extends mysqli
{
public $mysqlServer = "127.0.0.1";
public $username = "moha";
public $passwd = "MysqlMoha";
public $database = "moha";
function __construct($mysqlServer, $username, $passwd, $database)
{
return $this->connect($mysqlServer, $username, $passwd, $database);
}
function protect($string)
{
return $this->real_escape_string($string);
}
function logProperty($device, $property, $value)
{
$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
}
}
$mohaDB = new db($mysqlServer, $username, $passwd, $database);
if ($mohaDB->connect_error)
{
logger(ERROR, _("Mysql connection failed: ") . $db->connect_error);
}
?>

View File

@ -175,7 +175,8 @@ function iterateDevice($topic, $fn, &$device, $payloadArray)
$device->$key["value"] = $value; $device->$key["value"] = $value;
$changed[$fn]["key"] = $key; $changed[$fn]["key"] = $key;
$changed[$fn]["value"] = $value; $changed[$fn]["value"] = $value;
logger(INFO, sprintf(_("Device %s property %s, value changed to %s"), $device->friendlyName, $key, $value));
$mohaDB->logProperty($device, $key, $value);
//echo "oldvalue => " . print_r($oldValue, true) . EOL; //echo "oldvalue => " . print_r($oldValue, true) . EOL;
/*if (empty($oldValue)) /*if (empty($oldValue))
{ {
@ -204,7 +205,7 @@ function iterateDevice($topic, $fn, &$device, $payloadArray)
function getDevicesValues() function getDevicesValues()
{ {
//TODO
} }
?> ?>

13
devices_logs.php Normal file
View File

@ -0,0 +1,13 @@
<?php
function logDevice(& $device, $key, $value)
{
$db = new mysqli($mysqlServer, $username, $passwd, $database);
if ($conn->connect_error)
{
logger(ERROR, _("Mysql connection failed: ") . $db->connect_error);
}
}
?>

View File

@ -1,4 +1,7 @@
<?php <?php
$title = "moha";
cli_set_process_title($title);
file_put_contents("/proc/".getmypid()."/comm",$title);
//Constants //Constants
define( "EOL", "\n"); define( "EOL", "\n");
@ -35,7 +38,7 @@ $logFile = "/var/log/moha.log"; // Path of log file
$logFh = null; // filehandle of log file $logFh = null; // filehandle of log file
$curlErr = 0; // Number of errors returned by curl $curlErr = 0; // Number of errors returned by curl
$configDir = "./config"; // default config dir (production value is /etc/moha/) $configDir = "./config"; // default config dir (production value is /etc/moha/)
$properties2log = array("action", "state", "contact", "temperature", "state_l1", "state_l2", "humidity", "current_heating_setpoint", "position", "pressure", "occupancy", "tamper", "illuminance_lux","illuminance", "requested_brightness_level", );
if (!init()) exit(1); if (!init()) exit(1);
function logger($level, $log, $notif = true) function logger($level, $log, $notif = true)
@ -93,6 +96,7 @@ logger(DEBUG, _("requiring php modules"), false);
require "utils.php"; require "utils.php";
require "mqtt_functions.php"; require "mqtt_functions.php";
require "events.php"; require "events.php";
require "db.php";
require "db_functions.php"; require "db_functions.php";
// topics definition // topics definition

View File

@ -19,7 +19,7 @@ $topics["pws2mqtt"]->callback = function($topic, $message)
//addDevice($device[$fn], $fn, ); //addDevice($device[$fn], $fn, );
} }
$device = & $device[$fn]; $device = & $device[$fn];
print_r($device); //print_r($device);
} }
$payloadArray = json_decode($message->payload); $payloadArray = json_decode($message->payload);
changeDevice($topic[0], $topic[1], $device["device"], $payloadArray); changeDevice($topic[0], $topic[1], $device["device"], $payloadArray);