diff --git a/apiserver/cmd_functions.php b/apiserver/cmd_functions.php
index 109f583..42ecc48 100644
--- a/apiserver/cmd_functions.php
+++ b/apiserver/cmd_functions.php
@@ -5,7 +5,7 @@ function apiDashboard($socket, $n="Général")
{
global $dashboards, $indexDevices;
require_once "apiserver/javascript.php";
- logger(INFO, _("apiDashboard function"), __FILE__ . ":" . __LINE__);
+ logger(DEBUG, _("apiDashboard function"), __FILE__ . ":" . __LINE__);
$response = insertJavascript();
if(array_key_exists($n, $dashboards))
@@ -69,7 +69,7 @@ function apiDashboard($socket, $n="Général")
function displayChoice($device, $propertyName, $value)
{
- logger(INFO, _("function displayChoice"), __FILE__ . ":" . __LINE__);
+ logger(DEBUG, _("function displayChoice"), __FILE__ . ":" . __LINE__);
$propertyObject = $device->properties[$propertyName];
$unit = '';
@@ -130,7 +130,7 @@ function displayChoice($device, $propertyName, $value)
function mkHTML($device, $propertyName, $choice)
{
$html = "";
- logger(INFO, "function mkHTML", __FILE__ . ":" . __LINE__);
+ logger(DEBUG, "function mkHTML", __FILE__ . ":" . __LINE__);
foreach ($choice as $key => $value)
{
$html .= 'friendlyName . "/" . $propertyName . "/" . strtolower($value) . '" value="' . $value . "\" onmouseup=\"setPropertyValue('" . $device->topic . "', '" . $device->friendlyName . "', '" . $value . "', '" . $propertyName . "')\">";
@@ -402,7 +402,7 @@ function apiNotify($argList)
function apiVerbose($argList)
{
global $logLevel, $logLevels;
- logger(INFO, _("apiVerbose function"), __FILE__ . ":" . __LINE__);
+ logger(DEBUG, _("apiVerbose function"), __FILE__ . ":" . __LINE__);
$response = "";
//print_r($argList);
foreach ($logLevels as $value => $level)
@@ -430,7 +430,6 @@ function getFn($argList)
$jsonArray = array();
logger(INFO, _("getFn function"), __FILE__ . ":" . __LINE__);
-
if (array_key_exists("ieeeAddress", $argList))
{
logger(DEBUG, "ieeeAddress = " . $argList["ieeeAddress"], __FILE__ . ":" . __LINE__);
diff --git a/class/db.php b/class/db.php
index 9728805..512bf6d 100644
--- a/class/db.php
+++ b/class/db.php
@@ -27,7 +27,7 @@ class db extends mysqli
{
logger(ERROR, _("Connection to sql server ready"), __FILE__ . ":" . __LINE__);
}
- $result = new mysqli_result($this);
+ $this->result = new mysqli_result($this);
}
function protect($string)
@@ -47,7 +47,7 @@ class db extends mysqli
$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;
+ //echo $query . EOL;
if (is_numeric($value) and !empty($properties2log[$property]))
{
// calculate a min/max value for storing data
@@ -66,7 +66,7 @@ class db extends mysqli
if ($value >= $oldValue - $minMax and $value <= $oldValue + $minMax)
{
//echo "========>>>>>>>>>not changed" . EOL;
- return 0;
+ return true;
}
}
if ($testMode)
diff --git a/events.php b/events.php
index 39409f6..29f92a6 100644
--- a/events.php
+++ b/events.php
@@ -181,7 +181,7 @@ function setDelay(device &$deviceObject, float $delay, string $unit, string $pro
$logLevel = $oldLevel;
}
-function removeEvent(device $deviceObject, string $property , $value, int $method = null)
+function removeEvent(device $deviceObject, string $property , $value, int $method = IDLE)
{
global $events;
$eventKey = searchEvent($deviceObject, $property , $value);
diff --git a/hooks/scripts/rdc_sdb_eclairage.php b/hooks/scripts/rdc_sdb_eclairage.php
index 763c671..4c33fd7 100644
--- a/hooks/scripts/rdc_sdb_eclairage.php
+++ b/hooks/scripts/rdc_sdb_eclairage.php
@@ -46,14 +46,14 @@ class rdc_sdb_eclairage extends hook
logger(DEBUG, _("CASE: state_l1"), __FILE__ . ":" . __LINE__);
if ($value == "ON")
{
- if ($deviceTarget->properties["state_l1"]["method"] != MANUAL)
- {
- $delay = $this->delay;
- $deviceTarget->properties["state_l1"]["method"] = AUTO;
- }else
+ if ($deviceTarget->properties["state_l1"]["method"] != AUTO)
{
$delay = $this->delayManual;
$deviceTarget->properties["state_l1"]["method"] = MANUAL;
+ }else
+ {
+ $delay = $this->delay;
+ $deviceTarget->properties["state_l1"]["method"] = MANUAL;
}
setDelay($deviceTarget, $delay, $this->timeUnit, "state_l1", "OFF", true, IDLE);
}elseif ($value == "OFF")
diff --git a/hooks/scripts/rdc_store.php b/hooks/scripts/rdc_store.php
index 9b2c1e9..8935a4d 100644
--- a/hooks/scripts/rdc_store.php
+++ b/hooks/scripts/rdc_store.php
@@ -31,6 +31,7 @@ class rdc_store extends hook
logger(DEBUG, "Callback : RDC_STORE", __FILE__ . ":" . __LINE__);
$rain = 0;
+ static $rainS;
$exterieur_lux = 0;
$salon_lux = 0;
$rafale = 0;
@@ -70,7 +71,9 @@ class rdc_store extends hook
{
if (array_key_exists("rainin", $indexDevices[METEO]->properties))
{
- $rain = $indexDevices[METEO]->properties["rainin"]["value"];
+ $rainTmp = $indexDevices[METEO]->properties["rainin"]["value"];
+ $rain = $rainTmp - $rainS;
+ $rainS = $rainTmp;
logger(DEBUG, "rain=" . var_dump($rain), __FILE__ . ":" . __LINE__);
}
if (array_key_exists("windgustkmh", $indexDevices[METEO]->properties))
@@ -105,7 +108,7 @@ class rdc_store extends hook
//$store2level = $this->maxLevel;
switch ($property)
{
- case "illuminance_lux":
+ case "illuminance_lux":
logger(DEBUG, "CASE: illuminance_lux:" . $value, __FILE__ . ":" . __LINE__);
if ($exterieur_lux > 30000)
{
@@ -123,7 +126,7 @@ class rdc_store extends hook
$this->set($store2level);
}
}
- }elseif ($exterieur_lux < 15000 and $salon_lux < 300)
+ }elseif ($exterieur_lux < 15000 and $salon_lux < 110)
{
logger(DEBUG, "exterieur_lux < 15000", __FILE__ . ":" . __LINE__);
$this->close("Luminosité faible");
diff --git a/utils.php b/utils.php
index edf49fa..04a481a 100644
--- a/utils.php
+++ b/utils.php
@@ -1,6 +1,55 @@
" . print_r($json, true) . EOLH;
+
+ $jsonArray = array();
+ $json = trim($json, " {[]}");
+ $tmp = explode(",", $json);
+ foreach($tmp as $value)
+ {
+ $tmp2 = explode (":" , $value);
+ $jsonArray[trim($tmp2[0], '"')] = trim($tmp2[1], '"');
+ }
+ return $jsonArray;
+}
+
+function validateDate($date, $format = 'Y-m-d')
{
$d = DateTime::createFromFormat($format, $date);
return $d && $d->format($format) == $date;
diff --git a/webserver/class/db.class.php b/webserver/class/db.class.php
index 68a6af9..8c750bb 100644
--- a/webserver/class/db.class.php
+++ b/webserver/class/db.class.php
@@ -19,15 +19,15 @@ class db extends mysqli
}
while ($this->connect($this->mysqlServer, $this->username, $this->passwd, $this->database) === false)
{
- //logger(ERROR,_("Connection to sql server error :") . $this->connect_error, __FILE__ . ":" . __LINE__);
+ 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__);
+ logger(ERROR, _("Connection to sql server ready"), __FILE__ . ":" . __LINE__);
}
- $result = new mysqli_result($this);
+ $this->result = new mysqli_result($this);
}
function protect($string)
@@ -35,4 +35,6 @@ class db extends mysqli
return $this->real_escape_string($string);
}
}
+$mohaDB = new db();
+
?>
diff --git a/webserver/config.php b/webserver/config.php
index f499e55..28022b0 100644
--- a/webserver/config.php
+++ b/webserver/config.php
@@ -2,4 +2,14 @@
$varPath = "/usr/share/moha/";
$mohaPath = "/home/daniel/moha";
$configPath = "/etc/moha/";
+
+
+function logger($level, $log, $pos = "")
+{
+ global $logLevel;
+ if ($level & $logLevel)
+ {
+ print ("MOHA-" . $level . $log .$pos . EOL);
+ }
+}
?>
diff --git a/webserver/display_stats.php b/webserver/display_stats.php
index 4f938d4..90c78d3 100644
--- a/webserver/display_stats.php
+++ b/webserver/display_stats.php
@@ -5,42 +5,52 @@
/*need device ieeeAddress, property, startDate and endDate */
/* */
/****************************************************************/
-
require_once "config.php";
-require_once "utils.php";
+require_once $mohaPath . "/utils.php";
require_once "header.php";
+require_once $mohaPath . "/constants.php";
+require_once "class/db.class.php";
+//require_once $configPath . "properties2log.php";
+$logLevel = INFO | NOTICE | WARNING | ERROR | ALERT;
function displayStats($ieeeAddress, $fn, $property, $startDate, $endDate)
{
- global $indexFriendlyNames, $mohaDB;
+ global $mohaDB;
//logger(INFO, "Function displayStats", __FILE__ . ":" . __LINE__);
-
- // Validation of the dates
- if (validateDate($argList["startDate"]) && validateDate($argList["finalDate"]))
+ if ( ($indexFriendlyNames = htmlGetFriendlyNames($ieeeAddress)) !== false)
{
- if (!array_key_exists($argList["fn"], $indexFriendlyNames))
+ //print_r($indexFriendlyNames);
+ // Validation of the dates
+ if (validateDate($startDate) && validateDate($endDate))
{
- htmlSend($socket, $argList["fn"] . _(" not found: Verify the syntax"));
- }elseif(!array_key_exists($argList["property"], $indexFriendlyNames[$argList["fn"]]->properties))
- {
- htmlSend($socket, $argList["property"] . _(" not found : Verify the syntax"));
+ if (!in_array($fn, $indexFriendlyNames))
+ {
+ print $fn . _(" not found: Verify the syntax");
+ }else
+ {
+ $query = "SELECT date, value FROM logs WHERE device='" . $ieeeAddress . "' AND property='" . $property . "' AND '" . $startDate . " 00:00:00'<= date AND '" . $endDate . " 23:59:59' >= date ORDER BY date";
+ logger(DEBUG, _("query : ") . $query, __FILE__ . ":" . __LINE__);
+ if(!($mohaDB->result = $mohaDB->query($query)))
+ {
+ print _("mysql query error: ") . $mohaDB->error . __FILE__ . ":" . __LINE__;
+ }
+ $datas = $mohaDB->result->fetch_all(MYSQLI_ASSOC);
+ //print_r($mohaDB);
+ if ($mohaDB->affected_rows != 0)
+ {
+ print diagramDisplay($datas);
+ }else
+ {
+ print _("Sorry, no results");
+ }
+
+ }
}else
{
- $query = "SELECT date, value FROM logs WHERE device='" . $indexFriendlyNames[$argList["fn"]]->ieeeAddress . "' AND property='" . $argList["property"] . "' AND '" . $argList["startDate"] . "'>= date AND '" . $argList["finalDate"] . "' <= date ORDER BY date";
- //logger(DEBUG, _("query : ") . $query, __FILE__ . ":" . __LINE__);
- if(!($mohaDB->result = $mohaDB->query($query)))
- {
- //logger(ERROR, _("mysql query error: ") . $mohaDB->error, __FILE__ . ":" . __LINE__);
- }
- $datas = $mohaDB->result->fetch_all(MYSQLI_ASSOC);
- htmlSend($socket, diagramDisplay($datas));
+ print _("Dates need to be of the form: YYYY-MM-DD 00:00:00");
}
- }else
- {
- htmlSend($socket, _("Dates are not of the form: YYYY-MM-DD 00:00:00"));
}
-
}
/************************************************************/
@@ -56,18 +66,17 @@ function diagramDisplay($datas)
$ndata = count($datas);
$i = $ndata;
- $tmp = min($datas);
- $min = $tmp["value"];
- $tmp = max($datas);
- $max = $tmp["value"];
+ $min = min(array_column($datas, "value"));
+ $max = max(array_column($datas, "value"));
+ //print "min =>" . $min . "max =>" . $max . EOLH;
//Type mime de l'image
//Chemin vers le police à utiliser
$font_file = '/usr/share/fonts/TTF/dejavu/DejaVuSans.ttf';
//Adapter la largeur de l'image avec le nombre de donnée
- $largeur = $i * 50 + 90;
+ $largeur = ($ndata + 1) * 30 + 90;
$hauteur = 400;
//Hauteur de l'abscisse par rapport au bas de l'image
- $absis = 80;
+ $absis = 120;
//Création de l'image
$courbe = imagecreatetruecolor($largeur, $hauteur);
//Allouer les couleurs à utiliser
@@ -82,7 +91,7 @@ function diagramDisplay($datas)
imageline($courbe, 50, $hauteur - $absis, $largeur - 10, $hauteur - $absis, $noir);
//Tracer l'axe des ordonnées
imageline($courbe, 50, $hauteur - $absis, 50, 20, $noir);
- //Decaler 10px vers le haut le si le minimum est différent de 0
+ //Decaler 10px vers le haut si le minimum est différent de 0
$a = 0;
if($min != 0)
{
@@ -92,17 +101,17 @@ function diagramDisplay($datas)
//Nombres des grides verticales
$nbOrdonne = 10;
//Calcul de l'echelle des abscisses
- $echelleX = ($largeur - 100) / $i;
+ $echelleX = ($largeur - 130) / $ndata;
//Calcul de l'echelle des ordonnees
- $echelleY = ($hauteur - $absis - 20) / $nbOrdonne;
+ $echelleY = ($hauteur - $absis) / ($nbOrdonne + 1);
$i = $min;
//Calcul des ordonnees des grides
$py = ($max - $min) / $nbOrdonne;
$pasY = $absis;
- while( $pasY < ($hauteur - 19))
+ while($i <= ($max - $min))
{
//Affiche la valeur de l'ordonnee
- imagestring($courbe, 2, 10 , $hauteur - $pasY - 6, round($i), $noir);
+ imagestring($courbe, 2, 10 , $hauteur - $pasY - 6, round($i, 0, PHP_ROUND_HALF_DOWN), $noir);
//Trace la gride
imageline($courbe, 50, $hauteur - $pasY, $largeur - 20, $hauteur - $pasY, $ligne);
//Decaller vers le haut pour la prochaine gride
@@ -112,22 +121,22 @@ function diagramDisplay($datas)
}
$j = -1;
- $pasX = 90;
- //Parcourir le tableau pour le traçage de la diagramme
+ $pasX = 50;
+ //Parcourir le tableau pour le traçage du diagramme.
foreach ($datas as $row)
{
$date = $row["date"];
$quantite = $row["value"];
- //calculer la hateur du point par rapport à sa valeur
+ //calculer la hauteur du point par rapport à sa valeur
$y = ($hauteur) - (($quantite - $min) * ($echelleY / $py)) - $absis;
//dessiner le point
imagefilledellipse($courbe, $pasX, $y, 6, 6, $rouge);
//Afficher le mois en français avec une inclinaison de 315°
- printf("pasx = %d hauteur = %d absis = %d noir= %d", $pasX, $hauteur, $absis, $noir);
+ //printf("pasx = %d hauteur = %d absis = %d noir= %d", $pasX, $hauteur, $absis, $noir);
imagefttext($courbe, 10, 315, $pasX, $hauteur - $absis + 20, $noir, $font_file, $date);
- //Tacer une ligne veticale de l'axe de l'abscisse vers le point
+ //Tracer une ligne verticale de l'axe de l'abscisse vers le point
imageline($courbe, $pasX, $hauteur - $absis + $a, $pasX, $y, $noir);
- if($j!==-1)
+ if($j !== -1)
{
//liée le point actuel avec la précédente
imageline($courbe, ($pasX - $echelleX), $yprev, $pasX, $y, $noir);
@@ -137,21 +146,19 @@ function diagramDisplay($datas)
$j = $quantite;
//enregister la hauteur du point actuel pour la liaison avec la suivante
$yprev = $y;
- //Decaller l'abscisse suivante par rapport à son echelle
- $pasX +=$echelleX;
+ //Decaler l'abscisse suivante par rapport à son echelle
+ $pasX += $echelleX;
}
//Envoyer le flux de l'image
-
- //header('Content-Type: image/png');
- $file = tempnam("php://temp", "moha-");
+ $file = tempnam("./", "moha-");
imagepng($courbe,$file);
- return '';
- //Desallouer la mémoire utiliser par l'image
+ return '';
+ //Désallouer la mémoire utiliser par l'image
imagedestroy($courbe);
}
$tmp = explode("|", $_GET["device"]);
-displayStats($tmp[0], $tmp[1], $tmp[2]);
+displayStats($tmp[0], $tmp[1], $tmp[2], $_GET["startDate"], $_GET["endDate"]);
?>
diff --git a/webserver/index.php b/webserver/index.php
index 127ebbc..d2879c0 100644
--- a/webserver/index.php
+++ b/webserver/index.php
@@ -1,47 +1,11 @@
' . EOL;
}
- print "";
+ print "" . EOLH;
+ print '' . EOLH;
+ print '' . EOLH;
print '';
print "" . EOL;
}
diff --git a/webserver/utils.php b/webserver/utils.php
deleted file mode 100644
index b0f76e9..0000000
--- a/webserver/utils.php
+++ /dev/null
@@ -1,16 +0,0 @@
-