1
0

finishing debugging presence detection

This commit is contained in:
Daniel Tartavel 2022-09-01 18:48:20 +02:00
parent 39a3b19bbf
commit 3950ab7d36
5 changed files with 169 additions and 15 deletions

View File

@ -139,10 +139,18 @@ function apiServer($read)
logger(DEBUG, $command . _(" reached"), __FILE__ . ":" . __LINE__); logger(DEBUG, $command . _(" reached"), __FILE__ . ":" . __LINE__);
htmlSend($spawn, PropertiesDashboard($argList)); htmlSend($spawn, PropertiesDashboard($argList));
break; break;
case "deleteDevice": case "deletedevice":
logger(DEBUG, $command . _(" reached"), __FILE__ . ":" . __LINE__); logger(DEBUG, $command . _(" reached"), __FILE__ . ":" . __LINE__);
htmlSend($spawn, deleteDevice($argList)); htmlSend($spawn, deleteDevice($argList));
break; 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": case "test":
logger(DEBUG, $command . _(" reached"), __FILE__ . ":" . __LINE__); logger(DEBUG, $command . _(" reached"), __FILE__ . ":" . __LINE__);
htmlSend($spawn, test($argList)); htmlSend($spawn, test($argList));

View File

@ -135,6 +135,7 @@ function displayMethod($device, $propertyName)
$html = '<input type="button" id="' . $device->topic ."/" . $device->friendlyName . "/" . $propertyName . "/" . '" method="' . AUTO . "\" onmouseup=\"setPropertyMethod('" . $device->topic . "', '" . $device->friendlyName . "', '" . AUTO . "', '" . $propertyName . "')\">"; $html = '<input type="button" id="' . $device->topic ."/" . $device->friendlyName . "/" . $propertyName . "/" . '" method="' . AUTO . "\" onmouseup=\"setPropertyMethod('" . $device->topic . "', '" . $device->friendlyName . "', '" . AUTO . "', '" . $propertyName . "')\">";
$html = '<input type="button" id="' . $device->topic ."/" . $device->friendlyName . "/" . $propertyName . "/" . '" method="' . MANUAL . "\" onmouseup=\"setPropertyMethod('" . $device->topic . "', '" . $device->friendlyName . "', '" . MANUAL . "', '" . $propertyName . "')\">"; $html = '<input type="button" id="' . $device->topic ."/" . $device->friendlyName . "/" . $propertyName . "/" . '" method="' . MANUAL . "\" onmouseup=\"setPropertyMethod('" . $device->topic . "', '" . $device->friendlyName . "', '" . MANUAL . "', '" . $propertyName . "')\">";
} }
function mkHTML($device, $propertyName, $choice) function mkHTML($device, $propertyName, $choice)
{ {
$html = ""; $html = "";
@ -265,6 +266,7 @@ function displayProperty($device, $key, $value, $response)
function iterateProperty($device, $property, $value, &$response, $tab="") function iterateProperty($device, $property, $value, &$response, $tab="")
{ {
$method = array(1 => "IDLE", "AUTO", "MANUEL");
$tab .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"; $tab .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
if (is_array($value) or is_object($value)) if (is_array($value) or is_object($value))
{ {
@ -284,7 +286,8 @@ function iterateProperty($device, $property, $value, &$response, $tab="")
} }
}else }else
{ {
$response .= bool2string($value) . "<br>\n";//. displayChoice($device, $property); //value($property, $value, ""); if ($property == "method") $value = $method[$value]; //display name of method rather than a number
$response .= bool2string($value) . "<br>\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() function test()
{ {
global $hooks; global $hooks;
@ -578,12 +609,13 @@ function test()
$portes = $hooks["alerte_intrusion"]->testPortes(); $portes = $hooks["alerte_intrusion"]->testPortes();
if (!empty($portes)) if (!empty($portes))
{ {
$msg = _("Opened doors:") . EOLH;
foreach($portes as $value) foreach($portes as $value)
{ {
$msg .= $value . _(" is open") . EOL; $msg .= $value . EOL;
} }
} }
return nl2br($msg); return $msg;
} }
?> ?>

100
daemons/presenceDaemon.php Executable file
View File

@ -0,0 +1,100 @@
#!/usr/bin/php
<?php
define("EOL", "\n");
$title = "PresenceDaemon";
cli_set_process_title($title);
file_put_contents("/proc/".getmypid()."/comm",$title);
declare(ticks = 1);
require "/etc/moha/liste_telephones.php";
$msg = "";
$result = array();
$presence = array();
$curlErr = 0;
$command = "sudo nmap -n -sP 192.168.1.*"; //need to configure /etc/sudo/sudoers to permit execution of nmap as superuser
echo "presenceDaemon is starting\n";
while (1)
{
//echo "presenceDaemon is testing\n";
$msg = "";
exec($command, $result);
foreach ($macAddresses as $name =>$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;
}
}
}
?>

View File

@ -9,35 +9,34 @@ function presence($argList)
$name = $argList["nom"]; $name = $argList["nom"];
}else }else
{ {
return "Le paramètre 'nom' est obligatoire\npresent&nom=<nom>&presence=<true/false>" . EOLH; return _("Le paramètre 'nom' est obligatoire<br>present&nom=<nom>&presence=<true/false>") . EOLH;
} }
if(array_key_exists("presence", $argList)) if(array_key_exists("presence", $argList))
{ {
$key= $argList["presence"]; $key = $argList["presence"];
}else }else
{ {
return "Le paramètre 'presence' est obligatoire\npresent&nom=<nom>&presence=<true/false>" . EOLH; return _("Le paramètre 'presence' est obligatoire<br>present&nom=<nom>&presence=<true/false>") . EOLH;
} }
$key = $argList["presence"];
if (!array_key_exists($name, $presence)) if (!array_key_exists($name, $presence))
{ {
$presence[] = $name; return sprintf(_("Ce nom (%s) n'existe pas dans la base."), $name) . EOLH;
$presence[$name] = false;
} }
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"; //echo $name . " est présent";
$presence[$name] = true; $presence[$name] = true;
} }
}elseif ($presence[$name] === true) }elseif ($presence[$name] == true)
{ {
$presence[$name] = false; $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");
} }

15
systemd/presenceD.service Normal file
View File

@ -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