diff --git a/apiserver/apiserver.php b/apiserver/apiserver.php
index bc32968..0ff5f48 100644
--- a/apiserver/apiserver.php
+++ b/apiserver/apiserver.php
@@ -139,10 +139,18 @@ function apiServer($read)
logger(DEBUG, $command . _(" reached"), __FILE__ . ":" . __LINE__);
htmlSend($spawn, PropertiesDashboard($argList));
break;
- case "deleteDevice":
+ case "deletedevice":
logger(DEBUG, $command . _(" reached"), __FILE__ . ":" . __LINE__);
htmlSend($spawn, deleteDevice($argList));
break;
+ case "present":
+ logger(DEBUG, $command . _(" reached"), __FILE__ . ":" . __LINE__);
+ htmlSend($spawn,presence($argList));
+ break;
+ case "ispresent":
+ logger(DEBUG, $command . _(" reached"), __FILE__ . ":" . __LINE__);
+ htmlSend($spawn, whoIsPresent($argList));
+ break;
case "test":
logger(DEBUG, $command . _(" reached"), __FILE__ . ":" . __LINE__);
htmlSend($spawn, test($argList));
diff --git a/apiserver/cmd_functions.php b/apiserver/cmd_functions.php
index 27ff412..e36e83c 100644
--- a/apiserver/cmd_functions.php
+++ b/apiserver/cmd_functions.php
@@ -135,6 +135,7 @@ function displayMethod($device, $propertyName)
$html = 'friendlyName . "/" . $propertyName . "/" . '" method="' . AUTO . "\" onmouseup=\"setPropertyMethod('" . $device->topic . "', '" . $device->friendlyName . "', '" . AUTO . "', '" . $propertyName . "')\">";
$html = 'friendlyName . "/" . $propertyName . "/" . '" method="' . MANUAL . "\" onmouseup=\"setPropertyMethod('" . $device->topic . "', '" . $device->friendlyName . "', '" . MANUAL . "', '" . $propertyName . "')\">";
}
+
function mkHTML($device, $propertyName, $choice)
{
$html = "";
@@ -265,6 +266,7 @@ function displayProperty($device, $key, $value, $response)
function iterateProperty($device, $property, $value, &$response, $tab="")
{
+ $method = array(1 => "IDLE", "AUTO", "MANUEL");
$tab .= " ";
if (is_array($value) or is_object($value))
{
@@ -284,7 +286,8 @@ function iterateProperty($device, $property, $value, &$response, $tab="")
}
}else
{
- $response .= bool2string($value) . "
\n";//. displayChoice($device, $property); //value($property, $value, "");
+ if ($property == "method") $value = $method[$value]; //display name of method rather than a number
+ $response .= bool2string($value) . "
\n"; //. displayChoice($device, $property); //value($property, $value, "");
}
}
@@ -567,6 +570,34 @@ function apiDisplayByType($argList)
}
*/
+function whoIsPresent($argList)
+{
+ if (array_key_exists("nom", $argList))
+ {
+ $nom = $argList["nom"];
+ $r = isPresent($nom);
+ if ($r)
+ {
+ $r = $nom . _(" est présent") . EOLH;
+ logger(DEBUG, _(" est présent") , __FILE__ . ":" . __LINE__);
+ }elseif(!$r)
+ {
+ $r = $nom . _(" est absent") . EOLH;
+ logger(DEBUG, _("Testing doors") , __FILE__ . ":" . __LINE__);
+ }else
+ {
+ $r = $nom . _(" n'existe pas dans la base de données") . EOLH;
+ logger(DEBUG, _("Testing doors") , __FILE__ . ":" . __LINE__);
+ }
+
+ }else
+ {
+ $r = isPresent(false, true);
+ $r = "Sont présent: " . EOLH . $r . EOLH;
+ }
+ return $r;
+}
+
function test()
{
global $hooks;
@@ -578,12 +609,13 @@ function test()
$portes = $hooks["alerte_intrusion"]->testPortes();
if (!empty($portes))
{
+ $msg = _("Opened doors:") . EOLH;
foreach($portes as $value)
{
- $msg .= $value . _(" is open") . EOL;
+ $msg .= $value . EOL;
}
}
- return nl2br($msg);
+ return $msg;
}
?>
diff --git a/daemons/presenceDaemon.php b/daemons/presenceDaemon.php
new file mode 100755
index 0000000..d4dabd3
--- /dev/null
+++ b/daemons/presenceDaemon.php
@@ -0,0 +1,100 @@
+#!/usr/bin/php
+$value)
+ {
+ if (!array_key_exists($name, $presence))
+ {
+ $presence[] = $name;
+ $presence[$name] = false;
+ }
+ $r = search($value, $result);
+
+ if ($r === true)
+ {
+ echo "présent\n";
+ if ($presence[$name] === false)
+ {
+ $presence[$name] = true;
+ $msg .= "nom=" . $name . _("&presence=true");
+ send($msg);
+ echo $name . " est mis présent dans tableau\n";
+ }
+ }elseif ($presence[$name] === true)
+ {
+ $presence[$name] = false;
+ echo $name . " est mis absent dans tableau\n";
+ $msg .= "nom=" . $name . _("&presence=false");
+ send($msg);
+ }
+ }
+
+ sleep(1);
+}
+
+function search($needle, $array)
+{
+ foreach ($array as $string)
+ {
+ if (str_contains($string, $needle))
+ {
+ echo "found\n";
+ return true;
+ }
+ }
+ return false;
+}
+
+function send($msg)
+{
+ global $curlErr;
+ $ch = curl_init();
+
+ $msg = "http://localhost:1025/present&" . $msg;
+ // set url
+ curl_setopt($ch, CURLOPT_URL, $msg );
+
+ // return the transfer as a string
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+ // $result contains the output string
+ echo _("Curl sending message -") . __FILE__ . ":" . __LINE__ . EOL;
+
+ if ($curlErr <= 5)
+ {
+ echo $msg . EOL;
+ curl_exec($ch);
+ if (curl_errno($ch) != 0)
+ {
+ $curlErr += 1;
+ echo sprintf( _("Sleeping 1 minute because Curl return error %d: %s when sending notification - "), curl_errno($ch), curl_error($ch)) . __FILE__ . ":" . __LINE__ . EOL;
+ sleep (60);
+ }else
+ {
+ echo _("Curl return no error - ") . __FILE__ . ":" . __LINE__ . EOL;
+ $curlErr = 0;
+ }
+ }
+}
+
+?>
diff --git a/presence.php b/presence.php
index 4069af3..1413766 100644
--- a/presence.php
+++ b/presence.php
@@ -9,35 +9,34 @@ function presence($argList)
$name = $argList["nom"];
}else
{
- return "Le paramètre 'nom' est obligatoire\npresent&nom=&presence=" . EOLH;
+ return _("Le paramètre 'nom' est obligatoire
present&nom=&presence=") . EOLH;
}
if(array_key_exists("presence", $argList))
{
- $key= $argList["presence"];
+ $key = $argList["presence"];
}else
{
- return "Le paramètre 'presence' est obligatoire\npresent&nom=&presence=" . EOLH;
+ return _("Le paramètre 'presence' est obligatoire
present&nom=&presence=") . EOLH;
}
- $key = $argList["presence"];
if (!array_key_exists($name, $presence))
{
- $presence[] = $name;
- $presence[$name] = false;
+ return sprintf(_("Ce nom (%s) n'existe pas dans la base."), $name) . EOLH;
}
- if ($key === true)
+ if ($key == "true")
{
- if ($presence[$name] === false)
+ if ($presence[$name] == false)
{
- logger(ALERT, $name . _(" est présent"), null, $device);
+ logger(ALERT, $name . _(" est présent"), __FILE__ . ":" . __LINE__ , $device);
//echo $name . " est présent";
$presence[$name] = true;
}
- }elseif ($presence[$name] === true)
+ }elseif ($presence[$name] == true)
{
$presence[$name] = false;
- logger(ALERT, $name . _(" est présent"), null, $device);
+ logger(ALERT, $name . _(" est présent"), __FILE__ . ":" . __LINE__, $device);
}
+ return _("demande prise en compte");
}
diff --git a/systemd/presenceD.service b/systemd/presenceD.service
new file mode 100644
index 0000000..4115324
--- /dev/null
+++ b/systemd/presenceD.service
@@ -0,0 +1,15 @@
+[Unit]
+Description=Presence daemon for MOHA
+After=moha.service
+
+[Service]
+ExecStart=php /usr/bin/presenceDaemon.php
+StandardOutput=inherit
+StandardError=inherit
+Restart=always
+User=domotique
+Group=domotique
+
+[Install]
+WantedBy=multi-user.target
+